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.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
28 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
29 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
31 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
34 private UriInfo uriInfo;
37 private Request request;
40 public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
41 final MediaType mediaType) {
46 public final T readFrom(final Class<T> type, final Type genericType,
47 final Annotation[] annotations, final MediaType mediaType,
48 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
49 WebApplicationException {
50 final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
52 final PushbackInputStream pushbackInputStream = new PushbackInputStream(entityStream);
54 int firstByte = pushbackInputStream.read();
55 if (firstByte == -1) {
56 return emptyBody(path);
58 pushbackInputStream.unread(firstByte);
59 return readBody(path, pushbackInputStream);
65 * Create a type corresponding to an empty body.
67 * @param path Request path
68 * @return empty body type
70 protected abstract T emptyBody(InstanceIdentifierContext<?> path);
72 protected abstract T readBody(InstanceIdentifierContext<?> path, InputStream entityStream)
73 throws IOException, WebApplicationException;
76 private String getIdentifier() {
77 return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER);
80 private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
81 return ParserIdentifier.toInstanceIdentifier(
83 SchemaContextHandler.getActualSchemaContext(),
84 Optional.of(RestConnectorProvider.getMountPointService()));
87 protected UriInfo getUriInfo() {
91 protected boolean isPost() {
92 return HttpMethod.POST.equals(this.request.getMethod());
95 public void setUriInfo(final UriInfo uriInfo) {
96 this.uriInfo = uriInfo;
99 public void setRequest(final Request request) {
100 this.request = request;