2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.md.sal.rest.schema;
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import java.lang.annotation.Annotation;
13 import java.lang.reflect.Type;
14 import java.util.concurrent.ExecutionException;
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 import org.opendaylight.restconf.common.schema.SchemaExportContext;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 @Produces({ YangConstants.RFC6020_YANG_MEDIA_TYPE })
28 public class SchemaExportContentYangBodyWriter implements MessageBodyWriter<SchemaExportContext> {
31 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
32 final MediaType mediaType) {
33 return type.equals(SchemaExportContext.class);
37 public long getSize(final SchemaExportContext context, final Class<?> type, final Type genericType,
38 final Annotation[] annotations, final MediaType mediaType) {
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 final RevisionSourceIdentifier sourceId = RevisionSourceIdentifier.create(context.getModule().getName(),
48 context.getModule().getQNameModule().getRevision());
49 final YangTextSchemaSource yangTextSchemaSource;
51 yangTextSchemaSource = context.getSourceProvider().getSource(sourceId).get();
52 } catch (InterruptedException | ExecutionException e) {
53 throw new WebApplicationException("Unable to retrieve source from SourceProvider.", e);
55 yangTextSchemaSource.copyTo(entityStream);