Fix sonar bug in distribution 31/67031/3
authorramverma <ram.krishna.verma@ericsson.com>
Mon, 17 Sep 2018 14:55:06 +0000 (15:55 +0100)
committerramverma <ram.krishna.verma@ericsson.com>
Mon, 17 Sep 2018 16:23:33 +0000 (17:23 +0100)
Change-Id: I6bcb6f6af1020ceb22dd92d6633717d2a183d5b8
Issue-ID: POLICY-1035
Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
plugins/reception-plugins/src/main/java/org/onap/policy/distribution/reception/decoding/policy/file/PolicyDecoderFileInCsarToPolicy.java

index 80c5172..06a57a3 100644 (file)
@@ -70,46 +70,25 @@ public class PolicyDecoderFileInCsarToPolicy implements PolicyDecoder<Csar, Poli
     @Override
     public Collection<PolicyAsString> decode(final Csar csar) throws PolicyDecodingException {
         final Collection<PolicyAsString> policyList = new ArrayList<>();
-        ZipFile zipFile = null;
-        try {
-            zipFile = new ZipFile(csar.getCsarPath());
+
+        try (ZipFile zipFile = new ZipFile(csar.getCsarPath())) {
             final Enumeration<? extends ZipEntry> entries = zipFile.entries();
             while (entries.hasMoreElements()) {
                 final ZipEntry entry = entries.nextElement();
                 if (entry.getName().contains(decoderParameters.getPolicyFileName())) {
-                    final PolicyAsString poilcy = createPolicy(zipFile, entry);
-                    policyList.add(poilcy);
+                    final StringWriter writer = new StringWriter();
+                    IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8");
+                    final PolicyAsString policy = new PolicyAsString(decoderParameters.getPolicyFileName(),
+                            decoderParameters.getPolicyType(), writer.toString());
+                    policyList.add(policy);
                 }
             }
-        } catch (final IOException exp) {
+        } catch (final IOException exp) { // NOSONAR
             final String message = "Failed decoding the policy";
             LOGGER.error(message, exp);
             throw new PolicyDecodingException(message, exp);
-        } finally {
-            if (zipFile != null) {
-                try {
-                    zipFile.close();
-                } catch (final IOException exp) {
-                    LOGGER.error("Failed closing the zipFile", exp);
-                }
-            }
         }
-        return policyList;
-    }
 
-    /**
-     * Creates the policy from given input.
-     *
-     * @param zipFile the csar file
-     * @param entry an entry in the csar file
-     * @return the created policy
-     * @throws IOException if policy creation fails
-     */
-    private PolicyAsString createPolicy(final ZipFile zipFile, final ZipEntry entry) throws IOException {
-        final StringWriter writer = new StringWriter();
-        IOUtils.copy(zipFile.getInputStream(entry), writer, "UTF-8");
-        final PolicyAsString poilcy = new PolicyAsString(decoderParameters.getPolicyFileName(),
-                decoderParameters.getPolicyType(), writer.toString());
-        return poilcy;
+        return policyList;
     }
 }