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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi;
11 import com.google.common.base.Optional;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.PushbackInputStream;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import javax.ws.rs.HttpMethod;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.Context;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.Request;
23 import javax.ws.rs.core.UriInfo;
24 import javax.ws.rs.ext.MessageBodyReader;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
28 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
29 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
30 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
36 private UriInfo uriInfo;
39 private Request request;
41 private final SchemaContextHandler schemaContextHandler;
42 private final DOMMountPointServiceHandler mountPointServiceHandler;
44 protected AbstractIdentifierAwareJaxRsProvider(SchemaContextHandler schemaContextHandler,
45 DOMMountPointServiceHandler mountPointServiceHandler) {
46 this.schemaContextHandler = schemaContextHandler;
47 this.mountPointServiceHandler = mountPointServiceHandler;
51 public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
52 final MediaType mediaType) {
57 public final T readFrom(final Class<T> type, final Type genericType,
58 final Annotation[] annotations, final MediaType mediaType,
59 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
60 WebApplicationException {
61 final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
63 final PushbackInputStream pushbackInputStream = new PushbackInputStream(entityStream);
65 int firstByte = pushbackInputStream.read();
66 if (firstByte == -1) {
67 return emptyBody(path);
69 pushbackInputStream.unread(firstByte);
70 return readBody(path, pushbackInputStream);
76 * Create a type corresponding to an empty body.
78 * @param path Request path
79 * @return empty body type
81 protected abstract T emptyBody(InstanceIdentifierContext<?> path);
83 protected abstract T readBody(InstanceIdentifierContext<?> path, InputStream entityStream)
84 throws WebApplicationException;
87 private String getIdentifier() {
88 return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER);
91 private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
92 return ParserIdentifier.toInstanceIdentifier(getIdentifier(), getSchemaContext(),
93 Optional.fromNullable(getMountPointService()));
96 protected UriInfo getUriInfo() {
100 protected SchemaContext getSchemaContext() {
101 return schemaContextHandler.get();
104 protected DOMMountPointService getMountPointService() {
105 return mountPointServiceHandler.get();
108 protected boolean isPost() {
109 return HttpMethod.POST.equals(this.request.getMethod());
112 public void setUriInfo(final UriInfo uriInfo) {
113 this.uriInfo = uriInfo;
116 public void setRequest(final Request request) {
117 this.request = request;