42c2d0bee7ca538378b3d09b67708fea5d83611f
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / spi / AbstractIdentifierAwareJaxRsProvider.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi;
10
11 import com.google.common.base.Optional;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
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.restconf.common.context.InstanceIdentifierContext;
25 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
27 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
28 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
29
30 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
31
32     @Context
33     private UriInfo uriInfo;
34
35     @Context
36     private Request request;
37
38     @Override
39     public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
40             final MediaType mediaType) {
41         return true;
42     }
43
44     @Override
45     public final T readFrom(final Class<T> type, final Type genericType,
46             final Annotation[] annotations, final MediaType mediaType,
47             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
48             WebApplicationException {
49         final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
50         if (entityStream.available() < 1) {
51             return emptyBody(path);
52         }
53
54         return readBody(path, entityStream);
55     }
56
57     /**
58      * Create a type corresponding to an empty body.
59      *
60      * @param path Request path
61      * @return empty body type
62      */
63     protected abstract T emptyBody(InstanceIdentifierContext<?> path);
64
65     protected abstract T readBody(InstanceIdentifierContext<?> path, InputStream entityStream)
66             throws IOException, WebApplicationException;
67
68
69     private String getIdentifier() {
70         return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER);
71     }
72
73     private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
74         return ParserIdentifier.toInstanceIdentifier(
75                 getIdentifier(),
76                 SchemaContextHandler.getActualSchemaContext(),
77                 Optional.of(RestConnectorProvider.getMountPointService()));
78     }
79
80     protected UriInfo getUriInfo() {
81         return this.uriInfo;
82     }
83
84     protected boolean isPost() {
85         return HttpMethod.POST.equals(this.request.getMethod());
86     }
87
88     void setUriInfo(final UriInfo uriInfo) {
89         this.uriInfo = uriInfo;
90     }
91
92     void setRequest(final Request request) {
93         this.request = request;
94     }
95 }