InstanceIdentifierContext does not take generics
[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 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi;
9
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;
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     private final SchemaContextHandler schemaContextHandler;
39     private final DOMMountPointService mountPointService;
40
41     protected AbstractIdentifierAwareJaxRsProvider(final SchemaContextHandler schemaContextHandler,
42             final DOMMountPointService mountPointService) {
43         this.schemaContextHandler = schemaContextHandler;
44         this.mountPointService = mountPointService;
45     }
46
47     @Override
48     public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
49             final MediaType mediaType) {
50         return true;
51     }
52
53     @Override
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();
59
60         final PushbackInputStream pushbackInputStream = new PushbackInputStream(entityStream);
61
62         int firstByte = pushbackInputStream.read();
63         if (firstByte == -1) {
64             return emptyBody(path);
65         } else {
66             pushbackInputStream.unread(firstByte);
67             return readBody(path, pushbackInputStream);
68         }
69
70     }
71
72     /**
73      * Create a type corresponding to an empty body.
74      *
75      * @param path Request path
76      * @return empty body type
77      */
78     protected abstract T emptyBody(InstanceIdentifierContext path);
79
80     protected abstract T readBody(InstanceIdentifierContext path, InputStream entityStream)
81             throws WebApplicationException;
82
83     private String getIdentifier() {
84         return this.uriInfo.getPathParameters(false).getFirst("identifier");
85     }
86
87     private InstanceIdentifierContext getInstanceIdentifierContext() {
88         return ParserIdentifier.toInstanceIdentifier(getIdentifier(), getSchemaContext(),
89                 Optional.ofNullable(getMountPointService()));
90     }
91
92     protected UriInfo getUriInfo() {
93         return this.uriInfo;
94     }
95
96     protected EffectiveModelContext getSchemaContext() {
97         return schemaContextHandler.get();
98     }
99
100     protected DOMMountPointService getMountPointService() {
101         return mountPointService;
102     }
103
104     protected boolean isPost() {
105         return HttpMethod.POST.equals(this.request.getMethod());
106     }
107
108     public void setUriInfo(final UriInfo uriInfo) {
109         this.uriInfo = uriInfo;
110     }
111
112     public void setRequest(final Request request) {
113         this.request = request;
114     }
115 }