2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.PushbackInputStream;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import java.util.Optional;
16 import javax.ws.rs.HttpMethod;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.Context;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.Request;
22 import javax.ws.rs.core.UriInfo;
23 import javax.ws.rs.ext.MessageBodyReader;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
27 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
33 private UriInfo uriInfo;
36 private Request request;
38 private final SchemaContextHandler schemaContextHandler;
39 private final DOMMountPointService mountPointService;
41 protected AbstractIdentifierAwareJaxRsProvider(final SchemaContextHandler schemaContextHandler,
42 final DOMMountPointService mountPointService) {
43 this.schemaContextHandler = schemaContextHandler;
44 this.mountPointService = mountPointService;
48 public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
49 final MediaType mediaType) {
54 public final T readFrom(final Class<T> type, final Type genericType,
55 final Annotation[] annotations, final MediaType mediaType,
56 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
57 WebApplicationException {
58 final InstanceIdentifierContext path = getInstanceIdentifierContext();
60 final PushbackInputStream pushbackInputStream = new PushbackInputStream(entityStream);
62 int firstByte = pushbackInputStream.read();
63 if (firstByte == -1) {
64 return emptyBody(path);
66 pushbackInputStream.unread(firstByte);
67 return readBody(path, pushbackInputStream);
73 * Create a type corresponding to an empty body.
75 * @param path Request path
76 * @return empty body type
78 protected abstract T emptyBody(InstanceIdentifierContext path);
80 protected abstract T readBody(InstanceIdentifierContext path, InputStream entityStream)
81 throws WebApplicationException;
83 private String getIdentifier() {
84 return this.uriInfo.getPathParameters(false).getFirst("identifier");
87 private InstanceIdentifierContext getInstanceIdentifierContext() {
88 return ParserIdentifier.toInstanceIdentifier(getIdentifier(), getSchemaContext(),
89 Optional.ofNullable(getMountPointService()));
92 protected UriInfo getUriInfo() {
96 protected EffectiveModelContext getSchemaContext() {
97 return schemaContextHandler.get();
100 protected DOMMountPointService getMountPointService() {
101 return mountPointService;
104 protected boolean isPost() {
105 return HttpMethod.POST.equals(this.request.getMethod());
108 public void setUriInfo(final UriInfo uriInfo) {
109 this.uriInfo = uriInfo;
112 public void setRequest(final Request request) {
113 this.request = request;