Merge "Bug 9092: revert to org.json temporarily"
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / md / sal / rest / schema / SchemaExportContentYinBodyWriter.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.netconf.md.sal.rest.schema;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import javax.ws.rs.Produces;
15 import javax.ws.rs.WebApplicationException;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.MultivaluedMap;
18 import javax.ws.rs.ext.MessageBodyWriter;
19 import javax.ws.rs.ext.Provider;
20 import javax.xml.stream.XMLStreamException;
21 import org.opendaylight.restconf.Rfc8040;
22 import org.opendaylight.restconf.utils.RestconfConstants;
23 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
24
25 @Provider
26 @Produces({ SchemaRetrievalService.YIN_MEDIA_TYPE, Rfc8040.MediaTypes.YIN + RestconfConstants.XML })
27 public class SchemaExportContentYinBodyWriter implements MessageBodyWriter<SchemaExportContext> {
28
29     @Override
30     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
31             final MediaType mediaType) {
32         return type.equals(SchemaExportContext.class);
33     }
34
35     @Override
36     public long getSize(final SchemaExportContext context, final Class<?> type, final Type genericType,
37             final Annotation[] annotations, final MediaType mediaType) {
38         return -1;
39     }
40
41     @Override
42     public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType,
43             final Annotation[] annotations, final MediaType mediaType,
44             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
45             WebApplicationException {
46         try {
47             YinExportUtils.writeModuleToOutputStream(context.getSchemaContext(), context.getModule(), entityStream);
48         } catch (final XMLStreamException e) {
49             throw new IllegalStateException(e);
50         }
51
52     }
53 }