Skip to content

Commit a400165

Browse files
fix(license): Improve exception handling
Improve exception handling in uploadLicense method #3895 Co-authored-by: Gaurav Mishra <gmishx@gmail.com> Signed-off-by: keerthi-bl <121924406+keerthi-bl@users.noreply.github.com>
1 parent 44e7eea commit a400165

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/license/Sw360LicenseService.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,28 @@ private void copyDataStreamToResponse(HttpServletResponse response, ByteArrayInp
269269
FileCopyUtils.copy(buffer, response.getOutputStream());
270270
}
271271

272-
public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) throws IOException, TException {
273-
final HashMap<String, InputStream> inputMap = new HashMap<>();
272+
public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch)
273+
throws IOException, TException {
274+
275+
if (file == null || file.isEmpty()) {
276+
throw new BadRequestClientException("Unable to upload license file. File is null or empty.");
277+
}
274278

275279
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
276-
throw new BadRequestClientException("Unable to upload license file. User is not admin");
280+
throw new BadRequestClientException("Unable to upload license file. User is not admin.");
277281
}
282+
283+
final HashMap<String, InputStream> inputMap = new HashMap<>();
284+
Throwable primaryThrowable = null;
285+
278286
try (InputStream inputStream = file.getInputStream()) {
279287
ZipTools.extractZipToInputStreamMap(inputStream, inputMap);
280288
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
281289
final LicsImporter licsImporter = new LicsImporter(sw360LicenseClient, overwriteIfExternalIdMatches, overwriteIfIdMatchesEvenWithoutExternalIdMatch);
282290
licsImporter.importLics(sw360User, inputMap);
291+
} catch (Throwable t) {
292+
primaryThrowable = t;
293+
throw t;
283294
} finally {
284295
IOException closeFailure = null;
285296
for (InputStream in : inputMap.values()) {
@@ -293,11 +304,16 @@ public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteI
293304
}
294305
}
295306
}
307+
296308
if (closeFailure != null) {
297-
throw closeFailure;
309+
if (primaryThrowable != null) {
310+
primaryThrowable.addSuppressed(closeFailure);
311+
} else {
312+
throw closeFailure;
313+
}
298314
}
299315
}
300-
}
316+
}
301317

302318
public RequestSummary importOsadlInformation(User sw360User) throws TException {
303319
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();

0 commit comments

Comments
 (0)