f516e5f1d45f2882e0f599a6d6f0bfe3bc453d0a
[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.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;
32
33 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
34
35     @Context
36     private UriInfo uriInfo;
37
38     @Context
39     private Request request;
40
41     private final SchemaContextHandler schemaContextHandler;
42     private final DOMMountPointServiceHandler mountPointServiceHandler;
43
44     protected AbstractIdentifierAwareJaxRsProvider(SchemaContextHandler schemaContextHandler,
45             DOMMountPointServiceHandler mountPointServiceHandler) {
46         this.schemaContextHandler = schemaContextHandler;
47         this.mountPointServiceHandler = mountPointServiceHandler;
48     }
49
50     @Override
51     public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
52             final MediaType mediaType) {
53         return true;
54     }
55
56     @Override
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();
62
63         final PushbackInputStream pushbackInputStream = new PushbackInputStream(entityStream);
64
65         int firstByte = pushbackInputStream.read();
66         if (firstByte == -1) {
67             return emptyBody(path);
68         } else {
69             pushbackInputStream.unread(firstByte);
70             return readBody(path, pushbackInputStream);
71         }
72
73     }
74
75     /**
76      * Create a type corresponding to an empty body.
77      *
78      * @param path Request path
79      * @return empty body type
80      */
81     protected abstract T emptyBody(InstanceIdentifierContext<?> path);
82
83     protected abstract T readBody(InstanceIdentifierContext<?> path, InputStream entityStream)
84             throws IOException, WebApplicationException;
85
86
87     private String getIdentifier() {
88         return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER);
89     }
90
91     private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
92         return ParserIdentifier.toInstanceIdentifier(getIdentifier(), getSchemaContext(),
93                 Optional.fromNullable(getMountPointService()));
94     }
95
96     protected UriInfo getUriInfo() {
97         return this.uriInfo;
98     }
99
100     protected SchemaContext getSchemaContext() {
101         return schemaContextHandler.get();
102     }
103
104     protected DOMMountPointService getMountPointService() {
105         return mountPointServiceHandler.get();
106     }
107
108     protected boolean isPost() {
109         return HttpMethod.POST.equals(this.request.getMethod());
110     }
111
112     public void setUriInfo(final UriInfo uriInfo) {
113         this.uriInfo = uriInfo;
114     }
115
116     public void setRequest(final Request request) {
117         this.request = request;
118     }
119 }