Search in sources :

Example 11 with LicenseInfoParsingResult

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.

the class CombinedCLIParser method getLicenseInfos.

@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
    AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
    InputStream attachmentStream = null;
    List<LicenseInfoParsingResult> parsingResults = new ArrayList<>();
    Map<String, Release> releasesByExternalId = prepareReleasesByExternalId(getCorrelationKey());
    try {
        attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
        Document doc = getDocument(attachmentStream);
        Map<String, Set<String>> copyrightSetsByExternalId = getCopyrightSetsByExternalIdsMap(doc);
        Map<String, Set<LicenseNameWithText>> licenseNamesWithTextsByExternalId = getLicenseNamesWithTextsByExternalIdsMap(doc);
        Set<String> allExternalIds = Sets.union(copyrightSetsByExternalId.keySet(), licenseNamesWithTextsByExternalId.keySet());
        allExternalIds.forEach(extId -> {
            LicenseInfoParsingResult parsingResult = getLicenseInfoParsingResultForExternalId(attachmentContent, releasesByExternalId, copyrightSetsByExternalId, licenseNamesWithTextsByExternalId, extId);
            parsingResults.add(parsingResult);
        });
    } catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
        log.error(e);
        parsingResults.add(new LicenseInfoParsingResult().setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing combined CLI file: " + e.toString()));
    } finally {
        closeQuietly(attachmentStream, log);
    }
    return parsingResults;
}
Also used : InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) IOException(java.io.IOException) Document(org.w3c.dom.Document) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Release(org.eclipse.sw360.datahandler.thrift.components.Release) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 12 with LicenseInfoParsingResult

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.

the class CombinedCLIParser method getLicenseInfoParsingResultForExternalId.

@NotNull
private LicenseInfoParsingResult getLicenseInfoParsingResultForExternalId(AttachmentContent attachmentContent, Map<String, Release> releasesByExternalId, Map<String, Set<String>> copyrightSetsByExternalId, Map<String, Set<LicenseNameWithText>> licenseNamesWithTextsByExternalId, String extId) {
    LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
    licenseInfo.setCopyrights(copyrightSetsByExternalId.get(extId));
    licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTextsByExternalId.get(extId));
    LicenseInfoParsingResult parsingResult = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
    Release release = releasesByExternalId.get(extId);
    if (release != null) {
        parsingResult.setVendor(release.isSetVendor() ? release.getVendor().getShortname() : "");
        parsingResult.setName(release.getName());
        parsingResult.setVersion(release.getVersion());
    } else {
        parsingResult.setName("No info found for external component ID " + extId);
    }
    parsingResult.setStatus(LicenseInfoRequestStatus.SUCCESS);
    return parsingResult;
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) Release(org.eclipse.sw360.datahandler.thrift.components.Release) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with LicenseInfoParsingResult

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.

the class SPDXParser method getLicenseInfo.

public <T> LicenseInfoParsingResult getLicenseInfo(Attachment attachment, User user, T context) throws TException {
    AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
    final Optional<SpdxDocument> spdxDocument = openAsSpdx(attachmentContent, user, context);
    if (!spdxDocument.isPresent()) {
        return new LicenseInfoParsingResult().setStatus(LicenseInfoRequestStatus.FAILURE);
    }
    return getLicenseInfoFromSpdx(attachmentContent, spdxDocument.get());
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)

Example 14 with LicenseInfoParsingResult

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.

the class ProjectPortlet method serveAttachmentFileLicenses.

private void serveAttachmentFileLicenses(ResourceRequest request, ResourceResponse response) throws IOException {
    final User user = UserCacheHolder.getUserFromRequest(request);
    final String attachmentContentId = request.getParameter(PortalConstants.ATTACHMENT_ID);
    final ComponentService.Iface componentClient = thriftClients.makeComponentClient();
    final LicenseInfoService.Iface licenseInfoClient = thriftClients.makeLicenseInfoClient();
    try {
        Release release = componentClient.getReleaseById(request.getParameter(PortalConstants.RELEASE_ID), user);
        List<LicenseInfoParsingResult> licenseInfos = licenseInfoClient.getLicenseInfoForAttachment(release, attachmentContentId, user);
        // We generate a JSON-serializable list of licenses here.
        // In addition we remember the license information for exclusion later on
        Map<String, LicenseNameWithText> licenseStore = Maps.newHashMap();
        List<Map<String, String>> licenses = Lists.newArrayList();
        licenseInfos.forEach(licenseInfoResult -> addLicenseInfoResultToJsonSerializableLicensesList(licenseInfoResult, licenses, licenseStore::put));
        licenses.sort((l1, l2) -> Strings.nullToEmpty(l1.get(LICENSE_NAME_WITH_TEXT_NAME)).compareTo(l2.get(LICENSE_NAME_WITH_TEXT_NAME)));
        request.getPortletSession().setAttribute(LICENSE_STORE_KEY_PREFIX + attachmentContentId, licenseStore);
        writeJSON(request, response, OBJECT_MAPPER.writeValueAsString(licenses));
    } catch (TException exception) {
        log.error("Cannot retrieve license information for attachment id " + attachmentContentId + ".", exception);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
    }
}
Also used : WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) Release(org.eclipse.sw360.datahandler.thrift.components.Release)

Example 15 with LicenseInfoParsingResult

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult in project sw360portal by sw360.

the class SPDXParserTools method getLicenseInfoFromSpdx.

protected static LicenseInfoParsingResult getLicenseInfoFromSpdx(AttachmentContent attachmentContent, SpdxDocument doc) {
    LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
    licenseInfo.setLicenseNamesWithTexts(new HashSet<>());
    licenseInfo.setCopyrights(new HashSet<>());
    try {
        for (SpdxItem spdxItem : doc.getDocumentDescribes()) {
            licenseInfo.getLicenseNamesWithTexts().addAll(getAllLicenseTexts(spdxItem, true).collect(Collectors.toSet()));
            licenseInfo.getCopyrights().addAll(getAllCopyrights(spdxItem).collect(Collectors.toSet()));
        }
    } catch (UncheckedInvalidSPDXAnalysisException e) {
        return new LicenseInfoParsingResult().setStatus(LicenseInfoRequestStatus.FAILURE).setMessage(e.getInvalidSPDXAnalysisExceptionCause().getMessage());
    } catch (InvalidSPDXAnalysisException e) {
        return new LicenseInfoParsingResult().setStatus(LicenseInfoRequestStatus.FAILURE).setMessage(e.getMessage());
    }
    return new LicenseInfoParsingResult().setLicenseInfo(licenseInfo).setStatus(LicenseInfoRequestStatus.SUCCESS);
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) SpdxItem(org.spdx.rdfparser.model.SpdxItem) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) InvalidSPDXAnalysisException(org.spdx.rdfparser.InvalidSPDXAnalysisException)

Aggregations

LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)12 Test (org.junit.Test)7 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)5 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)5 Release (org.eclipse.sw360.datahandler.thrift.components.Release)5 LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)5 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)5 User (org.eclipse.sw360.datahandler.thrift.users.User)5 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)4 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)4 TException (org.apache.thrift.TException)4 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)4 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 StringReader (java.io.StringReader)3 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)3 LicenseNameWithText (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText)3 Maps (com.google.common.collect.Maps)2 Sets (com.google.common.collect.Sets)2