Modifying allowed Mime Types for RESTCONF

Change-Id: Idf96164a23aeaf81dedb55b1de198b2a06f33c15
diff --git a/apps/restconf/api/src/main/java/org/onosproject/restconf/api/MediaTypeRestconf.java b/apps/restconf/api/src/main/java/org/onosproject/restconf/api/MediaTypeRestconf.java
new file mode 100644
index 0000000..48ccf99
--- /dev/null
+++ b/apps/restconf/api/src/main/java/org/onosproject/restconf/api/MediaTypeRestconf.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.restconf.api;
+
+import javax.ws.rs.core.MediaType;
+
+/*
+ * Extension of the REST MediaType with 2 new types from the RESTCONF standard RFC 8040.
+ *
+ * Currently (1.14) the XML Media Type is not supported by the RESTCONF server in ONOS
+ */
+public class MediaTypeRestconf extends MediaType {
+    public static final String APPLICATION_YANG_DATA_XML = "application/yang-data+xml";
+    public static final MediaType APPLICATION_YANG_DATA_XML_TYPE = new MediaType("application", "yang-data+xml");
+    public static final String APPLICATION_YANG_DATA_JSON = "application/yang-data+json";
+    public static final MediaType APPLICATION_YANG_DATA_JSON_TYPE = new MediaType("application", "yang-data+json");
+
+}
diff --git a/apps/restconf/api/src/main/java/org/onosproject/restconf/api/RestconfJsonBodyWriter.java b/apps/restconf/api/src/main/java/org/onosproject/restconf/api/RestconfJsonBodyWriter.java
new file mode 100644
index 0000000..bf9b42c
--- /dev/null
+++ b/apps/restconf/api/src/main/java/org/onosproject/restconf/api/RestconfJsonBodyWriter.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.restconf.api;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+@Provider
+@Produces(MediaTypeRestconf.APPLICATION_YANG_DATA_JSON)
+public class RestconfJsonBodyWriter implements MessageBodyWriter<ObjectNode> {
+    private ObjectMapper mapper = new ObjectMapper();
+
+    @Override
+    public boolean isWriteable(Class<?> type, Type genericType,
+                               Annotation[] annotations, MediaType mediaType) {
+        return type == ObjectNode.class;
+    }
+
+    @Override
+    public void writeTo(ObjectNode node, Class<?> type, Type genericType,
+                        Annotation[] annotations, MediaType mediaType,
+                        MultivaluedMap<String, Object> httpHeaders,
+                        OutputStream entityStream) throws IOException {
+        mapper.writer().writeValue(entityStream, node);
+        entityStream.flush();
+    }
+}