Search in sources :

Example 21 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class CLIParser method getLicenseInfos.

@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
    AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
    LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
    LicenseInfoParsingResult result = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
    InputStream attachmentStream = null;
    try {
        attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
        Document doc = getDocument(attachmentStream);
        Set<String> copyrights = getCopyrights(doc);
        licenseInfo.setCopyrights(copyrights);
        Set<LicenseNameWithText> licenseNamesWithTexts = getLicenseNameWithTexts(doc);
        licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTexts);
        result.setStatus(LicenseInfoRequestStatus.SUCCESS);
    } catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
        log.error(e);
        result.setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing CLI file: " + e.toString());
    } finally {
        closeQuietly(attachmentStream, log);
    }
    return Collections.singletonList(result);
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) 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) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 22 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class CombinedCLIParserTest method testIsApplicableToFailsOnMalformedXML.

@Test
public void testIsApplicableToFailsOnMalformedXML() throws Exception {
    AttachmentContent content = new AttachmentContent().setId("A1").setFilename("a.xml").setContentType("application/xml");
    when(connector.getAttachmentStream(content, new User(), new Project())).thenReturn(new ReaderInputStream(new StringReader("this is not an xml file")));
    assertFalse(parser.isApplicableTo(attachment, new User(), new Project()));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) ReaderInputStream(org.apache.commons.io.input.ReaderInputStream) User(org.eclipse.sw360.datahandler.thrift.users.User) StringReader(java.io.StringReader) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 23 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent 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 24 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent 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 25 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent 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)

Aggregations

AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)52 InputStream (java.io.InputStream)24 Test (org.junit.Test)20 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)13 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)11 IOException (java.io.IOException)10 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)10 User (org.eclipse.sw360.datahandler.thrift.users.User)10 Release (org.eclipse.sw360.datahandler.thrift.components.Release)8 TException (org.apache.thrift.TException)7 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)6 AttachmentInputStream (org.ektorp.AttachmentInputStream)6 OutputStream (java.io.OutputStream)5 FilledAttachment (org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment)5 StringReader (java.io.StringReader)4 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)4 RequestSummary (org.eclipse.sw360.datahandler.thrift.RequestSummary)4 DatabaseConnector (org.eclipse.sw360.datahandler.couchdb.DatabaseConnector)3 LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)3 PipedInputStream (java.io.PipedInputStream)2