Merge "Improve documentation of BindingAware{Provider,Consumer}"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / StructuredDataToJsonProvider.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.rest.impl;
9
10 import com.google.common.base.Charsets;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.Response;
22 import javax.ws.rs.ext.MessageBodyWriter;
23 import javax.ws.rs.ext.Provider;
24 import org.opendaylight.controller.sal.rest.api.Draft02;
25 import org.opendaylight.controller.sal.rest.api.RestconfService;
26 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
27 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
28 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30
31 @Provider
32 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
33     Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
34 public enum StructuredDataToJsonProvider implements MessageBodyWriter<StructuredData> {
35     INSTANCE;
36
37     @Override
38     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
39         return type.equals(StructuredData.class);
40     }
41
42     @Override
43     public long getSize(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
44         return -1;
45     }
46
47     @Override
48     public void writeTo(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations,
49             final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
50                     throws IOException, WebApplicationException {
51         CompositeNode data = t.getData();
52         if (data == null) {
53             throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
54         }
55
56         JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
57
58         if (t.isPrettyPrintMode()) {
59             writer.setIndent("    ");
60         } else {
61             writer.setIndent("");
62         }
63         JsonMapper jsonMapper = new JsonMapper(t.getMountPoint());
64         jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema());
65         writer.flush();
66     }
67 }