diff options
author | vasraz <vasyl.razinkov@est.tech> | 2023-01-30 19:26:02 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2023-02-02 10:48:57 +0000 |
commit | 036de7e2d91fb2aa121bde0300564b8650dd31a5 (patch) | |
tree | 8c44e291022bc4fe03fcd3deb4ad656cd395fbdb /catalog-be/src/main | |
parent | 41451b67d83608fbc4004b869c53d8d1d2d4d9e0 (diff) |
Fix Blocker Bug reported by Sonar
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Change-Id: I4eaa68dc8c5d6b0e2d09a2787da9bbbee1da123c
Issue-ID: SDC-4359
Diffstat (limited to 'catalog-be/src/main')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java index 4fcc3c0842..8974704199 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java @@ -231,7 +231,7 @@ public class TypesUploadServlet extends AbstractValidationsServlet { public Response uploadDataTypes(@Parameter(description = "FileInputStream") @FormDataParam("dataTypesZip") File file, @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, @Parameter(description = "model") @FormDataParam("model") String modelName, - @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { return uploadElementTypeServletLogic(this::createDataTypes, file, request, creator, NodeTypeEnum.DataType.getName(), modelName, includeToModelDefaultImports); } @@ -246,9 +246,9 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @ApiResponse(responseCode = "409", description = "Data types already exist")}) @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadDataTypesYaml(@Parameter(description = "FileInputStream") @FormDataParam("dataTypesYaml") File file, - @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, - @Parameter(description = "model") @FormDataParam("model") String modelName, - @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "model") @FormDataParam("model") String modelName, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { return uploadElementTypeServletLogicYaml(this::createDataTypes, file, request, creator, NodeTypeEnum.DataType.getName(), modelName, includeToModelDefaultImports); } @@ -344,22 +344,19 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } private Response uploadElementTypeServletLogicYaml(final ConsumerFourParam<Wrapper<Response>, String, String, Boolean> createElementsMethod, - final File file, final HttpServletRequest request, final String creator, - final String elementTypeName, final String modelName, final boolean includeToModelDefaultImports) { + final File file, final HttpServletRequest request, final String creator, + final String elementTypeName, final String modelName, + final boolean includeToModelDefaultImports) { init(); final String userId = initHeaderParam(creator, request, Constants.USER_ID_HEADER); try { - final Wrapper<String> yamlStringWrapper = new Wrapper<>(); final String url = request.getMethod() + " " + request.getRequestURI(); log.debug(START_HANDLE_REQUEST_OF, url); final Wrapper<Response> responseWrapper = doUploadTypeValidations(request, userId, file); if (responseWrapper.isEmpty()) { final String yamlAsString = getFileAsString(file); log.debug("received yaml: {}", yamlAsString); - yamlStringWrapper.setInnerElement(yamlAsString); - } - if (responseWrapper.isEmpty()) { - createElementsMethod.accept(responseWrapper, yamlStringWrapper.getInnerElement(), modelName, includeToModelDefaultImports); + createElementsMethod.accept(responseWrapper, yamlAsString, modelName, includeToModelDefaultImports); } return responseWrapper.getInnerElement(); } catch (final Exception e) { @@ -371,12 +368,15 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @NotNull private String getFileAsString(File file) throws IOException { - FileInputStream fl = new FileInputStream(file); - byte[] arr = new byte[(int) file.length()]; - fl.read(arr); - fl.close(); - final String yamlAsString = new String(arr, StandardCharsets.UTF_8); - return yamlAsString; + try (final FileInputStream fl = new FileInputStream(file)) { + byte[] arr = new byte[(int) file.length()]; + int read = fl.read(arr); + if (read > 0) { + return new String(arr, StandardCharsets.UTF_8); + } else { + throw new IOException(String.format("Failed to read bytes from '%s'", file.getName())); + } + } } private Wrapper<Response> doUploadTypeValidations(final HttpServletRequest request, String userId, File file) { |