9f4f8be89ce2b226e1304115e5c3080a02168b63
[netconf.git] / restconf / restconf-nb-bierman02 / 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.OutputStream;
11 import java.lang.annotation.Annotation;
12 import java.lang.reflect.Type;
13 import javax.ws.rs.Produces;
14 import javax.ws.rs.WebApplicationException;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.core.MultivaluedMap;
17 import javax.ws.rs.ext.MessageBodyWriter;
18 import javax.ws.rs.ext.Provider;
19 import javax.xml.stream.XMLStreamException;
20 import org.opendaylight.restconf.common.schema.SchemaExportContext;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
23
24 @Provider
25 @Produces({ YangConstants.RFC6020_YIN_MEDIA_TYPE })
26 public class SchemaExportContentYinBodyWriter implements MessageBodyWriter<SchemaExportContext> {
27
28     @Override
29     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
30             final MediaType mediaType) {
31         return type.equals(SchemaExportContext.class);
32     }
33
34     @Override
35     public long getSize(final SchemaExportContext context, final Class<?> type, final Type genericType,
36             final Annotation[] annotations, final MediaType mediaType) {
37         return -1;
38     }
39
40     @Override
41     public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType,
42             final Annotation[] annotations, final MediaType mediaType,
43             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws
44         WebApplicationException {
45         try {
46             YinExportUtils.writeModuleAsYinText(context.getModule(), entityStream);
47         } catch (final XMLStreamException e) {
48             throw new IllegalStateException(e);
49         }
50     }
51 }