Fix channel inactive handling for SSH transport
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / YangSchemaExportBodyWriter.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;
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 java.nio.charset.StandardCharsets;
15 import java.util.concurrent.ExecutionException;
16 import javax.ws.rs.Produces;
17 import javax.ws.rs.WebApplicationException;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.ext.Provider;
21 import org.opendaylight.restconf.nb.rfc8040.legacy.SchemaExportContext;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25
26 @Provider
27 @Produces(YangConstants.RFC6020_YANG_MEDIA_TYPE)
28 public class YangSchemaExportBodyWriter extends AbstractSchemaExportBodyWriter {
29     @Override
30     public void writeTo(final SchemaExportContext context, final Class<?> type, final Type genericType,
31             final Annotation[] annotations, final MediaType mediaType,
32             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException {
33         final var module = context.module();
34         final var sourceId = new SourceIdentifier(module.argument(),
35             module.localQNameModule().getRevision().orElse(null));
36         final YangTextSchemaSource yangTextSchemaSource;
37         try {
38             yangTextSchemaSource = context.sourceProvider().getSource(sourceId).get();
39         } catch (InterruptedException | ExecutionException e) {
40             throw new WebApplicationException("Unable to retrieve source from SourceProvider.", e);
41         }
42         yangTextSchemaSource.asByteSource(StandardCharsets.UTF_8).copyTo(entityStream);
43     }
44 }