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..f1aae48e2f 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 @@ -270,18 +270,33 @@ private void copyDataStreamToResponse(HttpServletResponse response, ByteArrayInp FileCopyUtils.copy(buffer, response.getOutputStream()); } - public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) throws IOException, TException { - final HashMap inputMap = new HashMap<>(); + public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) + throws IOException, TException { + + if (file == null || file.isEmpty()) { + throw new BadRequestClientException("Unable to upload license file. File is null or empty."); + } if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) { - throw new BadRequestClientException("Unable to upload license file. User is not admin"); + throw new BadRequestClientException("Unable to upload license file. User is not admin."); } + + final HashMap inputMap = new HashMap<>(); + Throwable primaryThrowable = 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); - } finally { + } + + catch (Throwable t) { + primaryThrowable = t; + throw t; + } + + finally { IOException closeFailure = null; for (InputStream in : inputMap.values()) { try { @@ -294,11 +309,16 @@ public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteI } } } + if (closeFailure != null) { - throw closeFailure; + if (primaryThrowable != null) { + primaryThrowable.addSuppressed(closeFailure); + } else { + throw closeFailure; + } } } - } + } public RequestSummary importOsadlInformation(User sw360User) throws TException { LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();