9773c0afc06d65dc8a16d1f2c51fdf6bc9915ab0
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / 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.controller.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.yangtools.yang.model.export.YinExportUtils;
22
23 @Provider
24 @Produces(SchemaRetrievalService.YIN_MEDIA_TYPE)
25 public class SchemaExportContentYinBodyWriter implements MessageBodyWriter<SchemaExportContext> {
26
27     @Override
28     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
29             final MediaType mediaType) {
30         return type.equals(SchemaExportContext.class);
31     }
32
33     @Override
34     public long getSize(final SchemaExportContext t, final Class<?> type, final Type genericType,
35             final Annotation[] annotations, final MediaType mediaType) {
36         return -1;
37     }
38
39     @Override
40     public void writeTo(final SchemaExportContext t, final Class<?> type, final Type genericType,
41             final Annotation[] annotations, final MediaType mediaType,
42             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
43             WebApplicationException {
44         try {
45             YinExportUtils.writeModuleToOutputStream(t.getSchemaContext(), t.getModule(), entityStream);
46         } catch (final XMLStreamException e) {
47             throw new IllegalStateException(e);
48         }
49
50     }
51 }