0cd7e327b0147e430e35856e0aa763d6c41bd29d
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / 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.restconf.nb.rfc8040.jersey.providers.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.common.schema.SchemaExportContext;
22 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
23 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
24 import org.opendaylight.yangtools.yang.model.export.YinExportUtils;
25
26 @Provider
27 @Produces({ Rfc8040.MediaTypes.YIN + RestconfConstants.XML })
28 public class SchemaExportContentYinBodyWriter implements MessageBodyWriter<SchemaExportContext> {
29
30     @Override
31     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
32             final MediaType mediaType) {
33         return type.equals(SchemaExportContext.class);
34     }
35
36     @Override
37     public long getSize(final SchemaExportContext context, final Class<?> type, final Type genericType,
38             final Annotation[] annotations, final MediaType mediaType) {
39         return -1;
40     }
41
42     @Override
43     public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType,
44             final Annotation[] annotations, final MediaType mediaType,
45             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
46             WebApplicationException {
47         try {
48             YinExportUtils.writeModuleAsYinText(context.getModule(), entityStream);
49         } catch (final XMLStreamException e) {
50             throw new IllegalStateException(e);
51         }
52     }
53 }