diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java index 95c0355efd..34c717aa4f 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java @@ -271,34 +271,44 @@ private void copyDataStreamToResponse(HttpServletResponse response, ByteArrayInp } public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) throws IOException, TException { - final HashMap inputMap = new HashMap<>(); - + if (file == null || file.isEmpty()) { + throw new BadRequestClientException("License file is missing or empty"); + } if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { throw new BadRequestClientException("Unable to upload license file. User is not admin"); } + + final HashMap inputMap = new HashMap<>(); + Exception primaryException = null; + try (InputStream inputStream = file.getInputStream()) { ZipTools.extractZipToInputStreamMap(inputStream, inputMap); LicenseService.Iface sw360LicenseClient = getThriftLicenseClient(); final LicsImporter licsImporter = new LicsImporter(sw360LicenseClient, overwriteIfExternalIdMatches, overwriteIfIdMatchesEvenWithoutExternalIdMatch); licsImporter.importLics(sw360User, inputMap); + } catch (Exception e) { + primaryException = e; + throw e; } finally { - IOException closeFailure = null; - for (InputStream in : inputMap.values()) { - try { + closeInputMapStreams(inputMap, primaryException); + } + } + + private void closeInputMapStreams(Map inputMap, Exception primaryException) { + for (InputStream in : inputMap.values()) { + try { + if (in != null) { in.close(); - } catch (IOException e) { - if (closeFailure == null) { - closeFailure = e; - } else { - closeFailure.addSuppressed(e); - } } - } - if (closeFailure != null) { - throw closeFailure; + } catch (IOException e) { + if (primaryException != null) { + primaryException.addSuppressed(e); + } else { + throw new RuntimeException("Failed to close input stream", e); + } } } - } + } public RequestSummary importOsadlInformation(User sw360User) throws TException { LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();