Correctly close NormalizedNodeStreamWriters 73/85573/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 5 Nov 2019 06:50:48 +0000 (07:50 +0100)
committerRobert Varga <nite@hq.sk>
Tue, 5 Nov 2019 07:06:20 +0000 (07:06 +0000)
This code ends up not not closing the stream writer correctly,
which means some state may end up being unfinished. Fix that up.

Change-Id: Icad45cd12d30e44dabe4f86e73a6790bd5137154
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/JsonStreamWriterWithDisabledValidation.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/errors/RestconfDocumentedExceptionMapper.java

index 4b000ddeb44d24fdce07dea91081477636cd76a5..a3b2b2fb0f3bdca99f3d8c66dbf2df49a4d7291b 100644 (file)
@@ -66,9 +66,4 @@ final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisab
     void endNodeWithDisabledValidation() {
         // nope
     }
-
-    @Override
-    public void close() throws IOException {
-        jsonWriter.close();
-    }
 }
\ No newline at end of file
index edc78bf73c9180409d4b7cadbe6ffb82f51b7a9f..f1f9fa017dc8889fd2db7151d24042970b95fc0c 100644 (file)
@@ -186,12 +186,10 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper<
     private String serializeErrorsContainerToJson(final ContainerNode errorsContainer) {
         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
              OutputStreamWriter streamStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
-             StreamWriterWithDisabledValidation jsonStreamWriter = new JsonStreamWriterWithDisabledValidation(
-                     RestconfModule.ERROR_INFO_QNAME, streamStreamWriter, ERRORS_GROUPING_PATH,
-                     RestconfModule.URI_MODULE, schemaContextHandler);
-             NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonStreamWriter)
         ) {
-            return writeNormalizedNode(errorsContainer, outputStream, nnWriter);
+            return writeNormalizedNode(errorsContainer, outputStream, new JsonStreamWriterWithDisabledValidation(
+                RestconfModule.ERROR_INFO_QNAME, streamStreamWriter, ERRORS_GROUPING_PATH,
+                RestconfModule.URI_MODULE, schemaContextHandler));
         } catch (IOException e) {
             throw new IllegalStateException("Cannot close some of the output JSON writers", e);
         }
@@ -204,22 +202,18 @@ public final class RestconfDocumentedExceptionMapper implements ExceptionMapper<
      * @return XML representation of the errors container.
      */
     private String serializeErrorsContainerToXml(final ContainerNode errorsContainer) {
-        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-             StreamWriterWithDisabledValidation streamWriter = new XmlStreamWriterWithDisabledValidation(
-                     RestconfModule.ERROR_INFO_QNAME, outputStream, ERRORS_GROUPING_PATH, schemaContextHandler);
-             NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(streamWriter)
-        ) {
-            return writeNormalizedNode(errorsContainer, outputStream, nnWriter);
+        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
+            return writeNormalizedNode(errorsContainer, outputStream, new XmlStreamWriterWithDisabledValidation(
+                RestconfModule.ERROR_INFO_QNAME, outputStream, ERRORS_GROUPING_PATH, schemaContextHandler));
         } catch (IOException e) {
             throw new IllegalStateException("Cannot close some of the output XML writers", e);
         }
     }
 
     private static String writeNormalizedNode(final NormalizedNode<?, ?> errorsContainer,
-            final ByteArrayOutputStream outputStream, final NormalizedNodeWriter nnWriter) {
-        try {
+            final ByteArrayOutputStream outputStream, final StreamWriterWithDisabledValidation streamWriter) {
+        try (NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(streamWriter)) {
             nnWriter.write(errorsContainer);
-            nnWriter.flush();
         } catch (IOException e) {
             throw new IllegalStateException("Cannot write error response body", e);
         }