Fix NPE when trying to download restconf provided yang files
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / md / sal / rest / schema / SchemaExportContentYangBodyWriter.java
index 182224fb05a4220f0fdc7f210600c7976579b1c4..4826405496ce17653e41e02f50b070e4adc177fb 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.netconf.md.sal.rest.schema;
 
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.PrintWriter;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import javax.ws.rs.Produces;
@@ -19,6 +18,9 @@ import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 import org.opendaylight.restconf.common.schema.SchemaExportContext;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 
 @Provider
 @Produces({ SchemaRetrievalService.YANG_MEDIA_TYPE })
@@ -41,8 +43,14 @@ public class SchemaExportContentYangBodyWriter implements MessageBodyWriter<Sche
             final Annotation[] annotations, final MediaType mediaType,
             final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException,
             WebApplicationException {
-        final PrintWriter writer = new PrintWriter(entityStream);
-        writer.write(context.getModule().getSource());
-
+        final RevisionSourceIdentifier sourceId = RevisionSourceIdentifier.create(context.getModule().getName(),
+                context.getModule().getQNameModule().getFormattedRevision());
+        final YangTextSchemaSource yangTextSchemaSource;
+        try {
+            yangTextSchemaSource = context.getSourceProvider().getSource(sourceId).checkedGet();
+        } catch (SchemaSourceException e) {
+            throw new WebApplicationException("Unable to retrieve source from SourceProvider.", e);
+        }
+        yangTextSchemaSource.copyTo(entityStream);
     }
 }