From 37d5b2c858f9f17bef12c170895e9ac03d50cf05 Mon Sep 17 00:00:00 2001 From: vempo Date: Mon, 23 Oct 2017 15:08:55 +0300 Subject: Finished fixing critical static analysis issues Release all resources such as InputStream in the onboarding code, along with automatic code cleanup and simpification. Change-Id: I0ff0bfe352ff63768d025fbd9dd85b53636e2d49 Issue-ID: SDC-291 Signed-off-by: vempo --- .../sdcrests/action/rest/services/ActionsImpl.java | 13 ++++----- .../OrchestrationTemplateCSARHandler.java | 34 +++++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) (limited to 'openecomp-be') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/action-library-rest/action-library-rest-services/src/main/java/org/openecomp/sdcrests/action/rest/services/ActionsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/action-library-rest/action-library-rest-services/src/main/java/org/openecomp/sdcrests/action/rest/services/ActionsImpl.java index 53ebf0baab..875b51548e 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/action-library-rest/action-library-rest-services/src/main/java/org/openecomp/sdcrests/action/rest/services/ActionsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/action-library-rest/action-library-rest-services/src/main/java/org/openecomp/sdcrests/action/rest/services/ActionsImpl.java @@ -557,15 +557,13 @@ public class ActionsImpl implements Actions { ACTION_REQUEST_MISSING_MANDATORY_PARAM + ARTIFACT_FILE); } - InputStream artifactInputStream = null; - try { - artifactInputStream = artifactToUpload.getDataHandler().getInputStream(); + try (InputStream artifactInputStream = artifactToUpload.getDataHandler().getInputStream()) { + payload = FileUtils.toByteArray(artifactInputStream); } catch (IOException exception) { LOGGER.error(ACTION_ARTIFACT_READ_FILE_ERROR, exception); throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE, ACTION_ARTIFACT_READ_FILE_ERROR); } - payload = FileUtils.toByteArray(artifactInputStream); //Validate Artifact size if (payload != null && payload.length > MAX_ACTION_ARTIFACT_SIZE) { throw new ActionException(ACTION_ARTIFACT_TOO_BIG_ERROR_CODE, ACTION_ARTIFACT_TOO_BIG_ERROR); @@ -761,15 +759,14 @@ public class ActionsImpl implements Actions { } if (artifactToUpdate != null) { - InputStream artifactInputStream = null; - try { - artifactInputStream = artifactToUpdate.getDataHandler().getInputStream(); + + try (InputStream artifactInputStream = artifactToUpdate.getDataHandler().getInputStream()) { + payload = FileUtils.toByteArray(artifactInputStream); } catch (IOException exception) { LOGGER.error(ACTION_ARTIFACT_READ_FILE_ERROR, exception); throw new ActionException(ACTION_INTERNAL_SERVER_ERR_CODE, ACTION_ARTIFACT_READ_FILE_ERROR); } - payload = FileUtils.toByteArray(artifactInputStream); //Validate Artifact size if (payload != null && payload.length > MAX_ACTION_ARTIFACT_SIZE) { throw new ActionException(ACTION_ARTIFACT_TOO_BIG_ERROR_CODE, diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java index 53d34749e7..db0851cd5f 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationTemplateCSARHandler.java @@ -22,10 +22,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; -import java.util.stream.Stream; import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters; -import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.ELIGBLE_FOLDERS; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.ELIGIBLE_FILES; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME; +import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_YAML_FILE_NAME; + public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateHandler implements OrchestrationTemplateFileHandler { @@ -59,14 +62,22 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH } private void validateManifest(UploadFileResponse uploadFileResponse, FileContentHandler contentMap) { + if (!validateFileExist(uploadFileResponse, contentMap, MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)){ return; } - InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME); - OnboardingManifest onboardingManifest = new OnboardingManifest(fileContent); - if (!onboardingManifest.isValid()){ - onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError( - SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error))); + + try (InputStream fileContent = contentMap.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME)) { + + OnboardingManifest onboardingManifest = new OnboardingManifest(fileContent); + if (!onboardingManifest.isValid()) { + onboardingManifest.getErrors().forEach(error -> uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, error))); + } + + } catch (IOException e) { + // convert to runtime to keep the throws unchanged + throw new RuntimeException("Failed to validate manifest", e); } } @@ -79,8 +90,6 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, getErrorWithParameters(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage(), unwantedFile)))); - - ; } } @@ -97,14 +106,11 @@ public class OrchestrationTemplateCSARHandler extends BaseOrchestrationTemplateH } private boolean filterFiles(String inFileName) { boolean valid = ELIGIBLE_FILES.stream().anyMatch(fileName -> fileName.equals(inFileName)); - if (valid){ - return !valid; - } - return filterFolders(inFileName); + return !valid && filterFolders(inFileName); } private boolean filterFolders(String fileName) { - return !ELIGBLE_FOLDERS.stream().anyMatch(dirName -> fileName.startsWith(dirName)); + return ELIGBLE_FOLDERS.stream().noneMatch(fileName::startsWith); } private boolean validateFileExist(UploadFileResponse uploadFileResponse, FileContentHandler contentMap, String fileName) { -- cgit 1.2.3-korg