2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.impl;
10 import com.google.common.base.Charsets;
11 import com.google.gson.stream.JsonWriter;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.io.OutputStreamWriter;
16 import java.lang.annotation.Annotation;
17 import java.lang.reflect.Type;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.ext.MessageBodyWriter;
25 import javax.ws.rs.ext.Provider;
27 import org.opendaylight.controller.sal.rest.api.Draft02;
28 import org.opendaylight.controller.sal.rest.api.RestconfService;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.controller.sal.restconf.impl.StructuredData;
31 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
36 Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
37 public enum StructuredDataToJsonProvider implements MessageBodyWriter<StructuredData> {
41 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
42 return type.equals( StructuredData.class );
46 public long getSize(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
51 public void writeTo(final StructuredData t, final Class<?> type, final Type genericType, final Annotation[] annotations,
52 final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
53 throws IOException, WebApplicationException {
54 CompositeNode data = t.getData();
56 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
59 JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
60 writer.setIndent(" ");
61 JsonMapper jsonMapper = new JsonMapper(t.getMountPoint());
62 jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema());