Search in sources :

Example 31 with Attachment

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

the class FossologyUploaderTest method testUploadToFossologyReportsErrors.

@Test
public void testUploadToFossologyReportsErrors() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.getId()).thenReturn("id");
    when(attachment.getFilename()).thenReturn("fileName");
    String clearingTeam = "cl";
    final InputStream inputStream = mock(InputStream.class);
    doReturn(-1).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam);
    verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    assertThat(uploadId, is((long) -1));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 32 with Attachment

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

Example 33 with Attachment

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

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

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

the class CombinedCLIParserTest method testIsApplicableTo.

@Test
public void testIsApplicableTo() throws Exception {
    when(connector.getAttachmentStream(content, new User(), new Project())).thenReturn(makeAttachmentContentStream(TEST_XML_FILENAME));
    assertTrue(parser.isApplicableTo(attachment, new User(), new Project()));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) User(org.eclipse.sw360.datahandler.thrift.users.User) Test(org.junit.Test)

Aggregations

Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)65 Test (org.junit.Test)40 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)38 User (org.eclipse.sw360.datahandler.thrift.users.User)27 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)22 InputStream (java.io.InputStream)19 Release (org.eclipse.sw360.datahandler.thrift.components.Release)15 IOException (java.io.IOException)13 PortletRequest (javax.portlet.PortletRequest)11 TestUserCacheHolder (org.eclipse.sw360.portal.TestUserCacheHolder)9 StringReader (java.io.StringReader)8 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)8 TException (org.apache.thrift.TException)8 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)8 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)7 OutputStream (java.io.OutputStream)5 AttachmentInputStream (org.ektorp.AttachmentInputStream)5 ResponseEntity (org.springframework.http.ResponseEntity)5 ComponentService (org.eclipse.sw360.datahandler.thrift.components.ComponentService)4 Maps (com.google.common.collect.Maps)3