Migrate XML output to yang-data-codec-xml
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / jersey / providers / AbstractIdentifierAwareJaxRsProvider.java
index 69ebec275128f95f4eaaec1cf7fb6f0cbc96014b..8fa4c7e6021aea82908e6040b8f1ccead06cef0b 100644 (file)
@@ -9,18 +9,25 @@
 package org.opendaylight.restconf.jersey.providers;
 
 import com.google.common.base.Optional;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Request;
 import javax.ws.rs.core.UriInfo;
+import javax.ws.rs.ext.MessageBodyReader;
 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
 import org.opendaylight.restconf.RestConnectorProvider;
 import org.opendaylight.restconf.utils.parser.ParserIdentifier;
 
-public class AbstractIdentifierAwareJaxRsProvider {
-
-    private static final String POST = "POST";
+public abstract class AbstractIdentifierAwareJaxRsProvider<T> implements MessageBodyReader<T> {
 
     @Context
     private UriInfo uriInfo;
@@ -28,11 +35,42 @@ public class AbstractIdentifierAwareJaxRsProvider {
     @Context
     private Request request;
 
-    protected final String getIdentifier() {
+    @Override
+    public final boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
+            final MediaType mediaType) {
+        return true;
+    }
+
+    @Override
+    public final T readFrom(final Class<T> type, final Type genericType,
+            final Annotation[] annotations, final MediaType mediaType,
+            final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
+            WebApplicationException {
+        final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
+        if (entityStream.available() < 1) {
+            return emptyBody(path);
+        }
+
+        return readBody(path, entityStream);
+    }
+
+    /**
+     * Create a type corresponding to an empty body.
+     *
+     * @param path Request path
+     * @return empty body type
+     */
+    protected abstract T emptyBody(InstanceIdentifierContext<?> path);
+
+    protected abstract T readBody(InstanceIdentifierContext<?> path, InputStream entityStream)
+            throws IOException, WebApplicationException;
+
+
+    private String getIdentifier() {
         return this.uriInfo.getPathParameters(false).getFirst(RestconfConstants.IDENTIFIER);
     }
 
-    protected InstanceIdentifierContext<?> getInstanceIdentifierContext() {
+    private InstanceIdentifierContext<?> getInstanceIdentifierContext() {
         return ParserIdentifier.toInstanceIdentifier(
                 getIdentifier(),
                 ControllerContext.getInstance().getGlobalSchema(),
@@ -44,7 +82,7 @@ public class AbstractIdentifierAwareJaxRsProvider {
     }
 
     protected boolean isPost() {
-        return POST.equals(this.request.getMethod());
+        return HttpMethod.POST.equals(this.request.getMethod());
     }
 
     void setUriInfo(final UriInfo uriInfo) {