Merge "Increase timeout for waiting for broker service in sal-binding-it."
[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 java.io.IOException;
11 import java.io.OutputStream;
12 import java.io.OutputStreamWriter;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.ext.MessageBodyWriter;
22 import javax.ws.rs.ext.Provider;
23
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.ResponseException;
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 import com.google.gson.stream.JsonWriter;
32
33 @Provider
34 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
35         Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
36 public enum StructuredDataToJsonProvider implements MessageBodyWriter<StructuredData> {
37     INSTANCE;
38
39     @Override
40     public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
41         return true;
42     }
43
44     @Override
45     public long getSize(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
46         return -1;
47     }
48
49     @Override
50     public void writeTo(StructuredData t, Class<?> type, Type genericType, Annotation[] annotations,
51             MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
52             throws IOException, WebApplicationException {
53         CompositeNode data = t.getData();
54         if (data == null) {
55             throw new ResponseException(Response.Status.NOT_FOUND, "No data exists.");
56         }
57
58         JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, "UTF-8"));
59         writer.setIndent("    ");
60         JsonMapper jsonMapper = new JsonMapper();
61         jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema(), t.getMountPoint());
62         writer.flush();
63     }
64
65 }