Search in sources :

Example 1 with WrappedTException

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();
    }
}
Also used : org.eclipse.sw360.licenseinfo.parsers(org.eclipse.sw360.licenseinfo.parsers) java.util(java.util) User(org.eclipse.sw360.datahandler.thrift.users.User) Release(org.eclipse.sw360.datahandler.thrift.components.Release) AttachmentDatabaseHandler(org.eclipse.sw360.attachments.db.AttachmentDatabaseHandler) LicenseNameWithTextUtils(org.eclipse.sw360.licenseinfo.util.LicenseNameWithTextUtils) Enums(com.google.common.base.Enums) Logger(org.apache.log4j.Logger) Lists(com.google.common.collect.Lists) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) org.eclipse.sw360.licenseinfo.outputGenerators(org.eclipse.sw360.licenseinfo.outputGenerators) Project(org.eclipse.sw360.datahandler.thrift.projects.Project) WrappedException.wrapTException(org.eclipse.sw360.datahandler.common.WrappedException.wrapTException) MalformedURLException(java.net.MalformedURLException) TException(org.apache.thrift.TException) ComponentDatabaseHandler(org.eclipse.sw360.datahandler.db.ComponentDatabaseHandler) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) REPORT(org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant.REPORT) DISCLOSURE(org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant.DISCLOSURE) Sets(com.google.common.collect.Sets) DatabaseSettings(org.eclipse.sw360.datahandler.common.DatabaseSettings) TimeUnit(java.util.concurrent.TimeUnit) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) Entry(java.util.Map.Entry) org.eclipse.sw360.datahandler.thrift.licenseinfo(org.eclipse.sw360.datahandler.thrift.licenseinfo) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) SW360Assert.assertNotNull(org.eclipse.sw360.datahandler.common.SW360Assert.assertNotNull) CommonUtils.nullToEmptySet(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Enums (com.google.common.base.Enums)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 MalformedURLException (java.net.MalformedURLException)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Logger (org.apache.log4j.Logger)1 TException (org.apache.thrift.TException)1 AttachmentDatabaseHandler (org.eclipse.sw360.attachments.db.AttachmentDatabaseHandler)1 CommonUtils (org.eclipse.sw360.datahandler.common.CommonUtils)1 CommonUtils.nullToEmptySet (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet)1 DatabaseSettings (org.eclipse.sw360.datahandler.common.DatabaseSettings)1 SW360Assert.assertNotNull (org.eclipse.sw360.datahandler.common.SW360Assert.assertNotNull)1 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)1