diff options
author | k.kazak <k.kazak@samsung.com> | 2019-08-14 10:18:31 +0200 |
---|---|---|
committer | Lukasz Muszkieta <lukasz.muszkieta@nokia.com> | 2019-08-19 10:52:19 +0000 |
commit | 81f26b4c1837c9f8ff49619782fe01d99f4f0683 (patch) | |
tree | d845ed5c21e2c55545320bd39b7157a27e60e241 | |
parent | 5b17afb416e3ab2d3f58694d275e67f20af5a1c6 (diff) |
Close input streams via try-with-resources
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>
3 files changed, 13 insertions, 18 deletions
diff --git a/asdc-controller/src/main/java/org/onap/so/asdc/installer/bpmn/BpmnInstaller.java b/asdc-controller/src/main/java/org/onap/so/asdc/installer/bpmn/BpmnInstaller.java index 095741c19b..195aa7e302 100644 --- a/asdc-controller/src/main/java/org/onap/so/asdc/installer/bpmn/BpmnInstaller.java +++ b/asdc-controller/src/main/java/org/onap/so/asdc/installer/bpmn/BpmnInstaller.java @@ -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, diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java index 2f05eb5f2d..29dca19820 100644 --- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java +++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java @@ -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); } } } diff --git a/common/src/main/java/org/onap/so/client/defaultproperties/DefaultDmaapPropertiesImpl.java b/common/src/main/java/org/onap/so/client/defaultproperties/DefaultDmaapPropertiesImpl.java index 4ca5690188..8ef08057e6 100644 --- a/common/src/main/java/org/onap/so/client/defaultproperties/DefaultDmaapPropertiesImpl.java +++ b/common/src/main/java/org/onap/so/client/defaultproperties/DefaultDmaapPropertiesImpl.java @@ -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 |