use of org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException in project sw360portal by sw360.
the class LicenseInfoHandler method getLicenseInfoForAttachment.
@Override
public List<LicenseInfoParsingResult> getLicenseInfoForAttachment(Release release, String attachmentContentId, User user) throws TException {
if (release == null) {
return Collections.singletonList(noSourceParsingResult(MSG_NO_RELEASE_GIVEN));
}
List<LicenseInfoParsingResult> cachedResults = licenseInfoCache.getIfPresent(attachmentContentId);
if (cachedResults != null) {
return cachedResults;
}
Attachment attachment = nullToEmptySet(release.getAttachments()).stream().filter(a -> a.getAttachmentContentId().equals(attachmentContentId)).findFirst().orElseThrow(() -> {
String message = String.format("Attachment selected for license info generation is not found in release's attachments. Release id: %s. Attachment content id: %s", release.getId(), attachmentContentId);
return new IllegalStateException(message);
});
try {
List<LicenseInfoParser> applicableParsers = parsers.stream().filter(parser -> wrapTException(() -> parser.isApplicableTo(attachment, user, release))).collect(Collectors.toList());
if (applicableParsers.size() == 0) {
LOGGER.warn("No applicable parser has been found for the attachment selected for license information");
return assignReleaseToLicenseInfoParsingResult(assignFileNameToLicenseInfoParsingResult(noSourceParsingResult("No applicable parser has been found for the attachment"), attachment.getFilename()), release);
} else if (applicableParsers.size() > 1) {
LOGGER.info("More than one parser claims to be able to parse attachment with contend id " + attachmentContentId);
}
List<LicenseInfoParsingResult> results = applicableParsers.stream().map(parser -> wrapTException(() -> parser.getLicenseInfos(attachment, user, release))).flatMap(Collection::stream).collect(Collectors.toList());
filterEmptyLicenses(results);
results = assignReleaseToLicenseInfoParsingResults(results, release);
licenseInfoCache.put(attachmentContentId, results);
return results;
} catch (WrappedTException exception) {
throw exception.getCause();
}
}
Aggregations