1 package org.opendaylight.controller.sal.rest.impl;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.OutputStreamWriter;
6 import java.lang.annotation.Annotation;
7 import java.lang.reflect.Type;
9 import javax.ws.rs.Produces;
10 import javax.ws.rs.WebApplicationException;
11 import javax.ws.rs.core.MediaType;
12 import javax.ws.rs.core.MultivaluedMap;
13 import javax.ws.rs.core.Response;
14 import javax.ws.rs.ext.MessageBodyWriter;
15 import javax.ws.rs.ext.Provider;
17 import org.opendaylight.controller.sal.rest.api.Draft01;
18 import org.opendaylight.controller.sal.rest.api.Draft02;
19 import org.opendaylight.controller.sal.rest.api.RestconfService;
20 import org.opendaylight.controller.sal.restconf.impl.ResponseException;
21 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import com.google.gson.stream.JsonWriter;
28 @Produces({ Draft01.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
29 MediaType.APPLICATION_JSON })
30 public enum StructuredDataToJsonProvider implements MessageBodyWriter<StructuredData> {
34 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
39 public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
44 public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
45 MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
46 throws IOException, WebApplicationException {
47 CompositeNode data = t.getData();
49 throw new ResponseException(Response.Status.NOT_FOUND, "No data exists.");
52 JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, "UTF-8"));
53 writer.setIndent(" ");
54 JsonMapper jsonMapper = new JsonMapper();
55 jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema(), t.getMountPoint());