Bug 8988 - Check for empty payload properly
[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.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;
30
31 public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
32
33     @Context
34     private UriInfo uriInfo;
35
36     @Context
37     private Request request;
38
39     @Override
40     public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
41             final MediaType mediaType) {
42         return true;
43     }
44
45     @Override
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();
51
52         final PushbackInputStream pushbackInputStream = new PushbackInputStream(entityStream);
53
54         int firstByte = pushbackInputStream.read();
55         if (firstByte == -1) {
56             return emptyBody(path);
57         } else {
58             pushbackInputStream.unread(firstByte);
59             return readBody(path, pushbackInputStream);
60         }
61
62     }
63
64     /**
65      * Create a type corresponding to an empty body.
66      *
67      * @param path Request path
68      * @return empty body type
69      */
70     protected abstract T emptyBody(InstanceIdentifierContext<?> path);
71
72     protected abstract T readBody(InstanceIdentifierContext<?> path, InputStream entityStream)
73             throws IOException, WebApplicationException;
74
75
76     private String getIdentifier() {
77         return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER);
78     }
79
80     private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
81         return ParserIdentifier.toInstanceIdentifier(
82                 getIdentifier(),
83                 SchemaContextHandler.getActualSchemaContext(),
84                 Optional.of(RestConnectorProvider.getMountPointService()));
85     }
86
87     protected UriInfo getUriInfo() {
88         return this.uriInfo;
89     }
90
91     protected boolean isPost() {
92         return HttpMethod.POST.equals(this.request.getMethod());
93     }
94
95     public void setUriInfo(final UriInfo uriInfo) {
96         this.uriInfo = uriInfo;
97     }
98
99     public void setRequest(final Request request) {
100         this.request = request;
101     }
102 }