Close input streams via try-with-resources 94/93394/3
authork.kazak <k.kazak@samsung.com>
Wed, 14 Aug 2019 08:18:31 +0000 (10:18 +0200)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Mon, 19 Aug 2019 10:52:19 +0000 (10:52 +0000)
DefaultDmaapPropertiesImpl: close FileInputStream after loading
ServicePluginFactory: close HttpClient if processing fails
close application.properties InputStream after loading
remove redundant catch block if behavior is the same
add logging of exception in finally block
BpmnInstaller: close csarFile if processing fails

Change-Id: Ic6f942c401277c5150c5cc1297aba27ccd40694c
Coverity-scan: CID-203903, CID-211476, CID-211615, CID-219308
Issue-ID: SO-2211
Signed-off-by: k.kazak <k.kazak@samsung.com>
asdc-controller/src/main/java/org/onap/so/asdc/installer/bpmn/BpmnInstaller.java
bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java
common/src/main/java/org/onap/so/client/defaultproperties/DefaultDmaapPropertiesImpl.java

index 095741c..195aa7e 100644 (file)
@@ -33,7 +33,6 @@ import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
-import org.onap.so.logger.LoggingAnchor;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpEntity;
@@ -49,6 +48,7 @@ import org.apache.http.entity.mime.content.StringBody;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.onap.so.asdc.client.ASDCConfiguration;
 import org.onap.so.logger.ErrorCode;
+import org.onap.so.logger.LoggingAnchor;
 import org.onap.so.logger.MessageEnum;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -71,9 +71,8 @@ public class BpmnInstaller {
 
     public void installBpmn(String csarFilePath) {
         logger.info("Deploying BPMN files from {}", csarFilePath);
-        try {
-            ZipInputStream csarFile =
-                    new ZipInputStream(new FileInputStream(Paths.get(csarFilePath).normalize().toString()));
+        try (ZipInputStream csarFile =
+                new ZipInputStream(new FileInputStream(Paths.get(csarFilePath).normalize().toString()))) {
             ZipEntry entry = csarFile.getNextEntry();
 
             while (entry != null) {
@@ -103,7 +102,6 @@ public class BpmnInstaller {
                 }
                 entry = csarFile.getNextEntry();
             }
-            csarFile.close();
         } catch (IOException ex) {
             logger.debug("Exception :", ex);
             logger.error(LoggingAnchor.FIVE, MessageEnum.ASDC_ARTIFACT_NOT_DEPLOYED_DETAIL.toString(), csarFilePath,
index 2f05eb5..29dca19 100644 (file)
@@ -50,6 +50,7 @@ import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.conn.ConnectTimeoutException;
 import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.util.EntityUtils;
 import org.camunda.bpm.engine.delegate.DelegateExecution;
@@ -98,8 +99,7 @@ public class ServicePluginFactory {
             new String[] {VS_MONITORED, VS_UNMONITORED, TS_MONITORED, TS_UNMONITORED};
 
     static {
-        try {
-            InputStream is = ClassLoader.class.getResourceAsStream("/application.properties");
+        try (InputStream is = ClassLoader.class.getResourceAsStream("/application.properties")) {
             if (null != is) {
                 Properties prop = new Properties();
                 prop.load(is);
@@ -855,14 +855,12 @@ public class ServicePluginFactory {
         HttpRequestBase method = null;
         HttpResponse httpResponse = null;
 
-        try {
+        try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
             int timeout = DEFAULT_TIME_OUT;
 
             RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
                     .setConnectionRequestTimeout(timeout).build();
 
-            HttpClient client = HttpClientBuilder.create().build();
-
             if ("POST".equalsIgnoreCase(methodType)) {
                 HttpPost httpPost = new HttpPost(msbUrl);
                 httpPost.setConfig(requestConfig);
@@ -902,9 +900,6 @@ public class ServicePluginFactory {
             method = null;
             return responseContent;
 
-        } catch (SocketTimeoutException | ConnectTimeoutException e) {
-            return null;
-
         } catch (Exception e) {
             return null;
 
@@ -913,13 +908,14 @@ public class ServicePluginFactory {
                 try {
                     EntityUtils.consume(httpResponse.getEntity());
                 } catch (Exception e) {
+                    logger.debug("Exception while executing finally block", e);
                 }
             }
             if (method != null) {
                 try {
                     method.reset();
                 } catch (Exception e) {
-
+                    logger.debug("Exception while executing finally block", e);
                 }
             }
         }
index 4ca5690..8ef0805 100644 (file)
@@ -35,11 +35,12 @@ public class DefaultDmaapPropertiesImpl implements DmaapProperties {
 
     public DefaultDmaapPropertiesImpl() throws IOException {
         File initialFile = new File("src/test/resources/dmaap.properties");
-        InputStream targetStream = new FileInputStream(initialFile);
         Properties properties = new Properties();
-        properties.load(targetStream);
-        this.properties = new HashMap<>();
-        properties.forEach((key, value) -> this.properties.put((String) key, (String) value));
+        try (InputStream targetStream = new FileInputStream(initialFile)) {
+            properties.load(targetStream);
+            this.properties = new HashMap<>();
+            properties.forEach((key, value) -> this.properties.put((String) key, (String) value));
+        }
     }
 
     @Override