Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, InputStream> 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<String, InputStream> 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<String, InputStream> 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();
Expand Down
Loading