Bug 2365: YIN Schema download support for Restconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / md / sal / rest / schema / SchemaExportContentYangBodyWriter.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.io.PrintWriter;
13 import java.lang.annotation.Annotation;
14 import java.lang.reflect.Type;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.WebApplicationException;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.ext.MessageBodyWriter;
20 import javax.ws.rs.ext.Provider;
21
22 @Provider
23 @Produces(SchemaRetrievalService.YANG_MEDIA_TYPE)
24 public class SchemaExportContentYangBodyWriter implements MessageBodyWriter<SchemaExportContext> {
25
26     @Override
27     public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
28             final MediaType mediaType) {
29         return type.equals(SchemaExportContext.class);
30     }
31
32     @Override
33     public long getSize(final SchemaExportContext t, final Class<?> type, final Type genericType,
34             final Annotation[] annotations, final MediaType mediaType) {
35         return -1;
36     }
37
38     @Override
39     public void writeTo(final SchemaExportContext t, final Class<?> type, final Type genericType,
40             final Annotation[] annotations, final MediaType mediaType,
41             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
42             WebApplicationException {
43         final PrintWriter writer = new PrintWriter(entityStream);
44         writer.write(t.getModule().getSource());
45
46     }
47 }