Search in sources :

Example 16 with Attachment

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

the class AttachmentStreamConnectorTest method testGetFullStream.

@Test
public void testGetFullStream() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.isOnlyRemote()).thenReturn(false);
    when(attachment.isSetPartsCount()).thenReturn(false);
    when(attachment.getFilename()).thenReturn("fil");
    String attachmentId = "id";
    when(attachment.getId()).thenReturn(attachmentId);
    AttachmentInputStream full = mock(AttachmentInputStream.class);
    when(connector.getAttachment(attachmentId, "fil")).thenReturn(full);
    when(full.read()).thenReturn(1, 2, -1);
    InputStream attachmentStream = attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentId))));
    assertThat(attachmentStream.read(), is(1));
    assertThat(attachmentStream.read(), is(2));
    assertThat(attachmentStream.read(), is(-1));
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) AttachmentInputStream(org.ektorp.AttachmentInputStream) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Test(org.junit.Test)

Example 17 with Attachment

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

the class AttachmentSpecTest method before.

@Before
public void before() throws TException {
    List<Attachment> attachmentList = new ArrayList<>();
    attachment = new Attachment();
    attachment.setAttachmentContentId("76537653");
    attachment.setFilename("spring-core-4.3.4.RELEASE.jar");
    attachment.setSha1("da373e491d3863477568896089ee9457bc316783");
    attachment.setAttachmentType(AttachmentType.BINARY_SELF);
    attachment.setCreatedTeam("Clearing Team 1");
    attachment.setCreatedComment("please check before Christmas :)");
    attachment.setCreatedOn("2016-12-18");
    attachment.setCreatedBy("admin@sw360.org");
    attachment.setCheckedTeam("Clearing Team 2");
    attachment.setCheckedComment("everything looks good");
    attachment.setCheckedOn("2016-12-18");
    attachment.setCheckStatus(CheckStatus.ACCEPTED);
    attachmentList.add(attachment);
    Release release = new Release();
    release.setId("874687");
    release.setName("Spring Core 4.3.4");
    release.setCpeid("cpe:/a:pivotal:spring-core:4.3.4:");
    release.setReleaseDate("2016-12-07");
    release.setVersion("4.3.4");
    release.setCreatedOn("2016-12-18");
    release.setCreatedBy("admin@sw360.org");
    release.setModerators(new HashSet<>(Arrays.asList(testUserId, testUserPassword)));
    release.setComponentId("678dstzd8");
    release.setClearingState(ClearingState.APPROVED);
    AttachmentInfo attachmentInfo = new AttachmentInfo(attachment, release);
    given(this.attachmentServiceMock.getAttachmentByIdForUser(eq(attachment.getAttachmentContentId()), anyObject())).willReturn(attachmentInfo);
    User user = new User();
    user.setId("admin@sw360.org");
    user.setEmail("admin@sw360.org");
    user.setFullname("John Doe");
    given(this.userServiceMock.getUserByEmail("admin@sw360.org")).willReturn(user);
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) ArrayList(java.util.ArrayList) AttachmentInfo(org.eclipse.sw360.rest.resourceserver.attachment.AttachmentInfo) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Before(org.junit.Before)

Example 18 with Attachment

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

the class AttachmentConnector method getSha1FromAttachmentContentId.

public String getSha1FromAttachmentContentId(String attachmentContentId) {
    InputStream attachmentStream = null;
    try {
        AttachmentContent attachmentContent = getAttachmentContent(attachmentContentId);
        attachmentStream = readAttachmentStream(attachmentContent);
        return sha1Hex(attachmentStream);
    } catch (SW360Exception e) {
        log.error("Problem retrieving content of attachment", e);
        return "";
    } catch (IOException e) {
        log.error("Problem computing the sha1 checksum", e);
        return "";
    } finally {
        closeQuietly(attachmentStream, log);
    }
}
Also used : InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) IOException(java.io.IOException) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 19 with Attachment

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

the class AttachmentConnector method setSha1ForAttachments.

public void setSha1ForAttachments(Set<Attachment> attachments) {
    for (Attachment attachment : attachments) {
        if (isNullOrEmpty(attachment.getSha1())) {
            String sha1 = getSha1FromAttachmentContentId(attachment.getAttachmentContentId());
            attachment.setSha1(sha1);
        }
    }
}
Also used : Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 20 with Attachment

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

the class ProjectPortletUtils method extractContainedAttachments.

/**
 * Walks through a list of project links and extracts all release attachments
 * with their owner. The returned map is a mapping from a release to its
 * attachment content ids.
 *
 * @param projectLinks
 *            list of project links to walk through
 *
 * @return map of releases and their attachment content ids
 */
public static Map<Source, Set<String>> extractContainedAttachments(Collection<ProjectLink> projectLinks) {
    Map<Source, Set<String>> attachments = Maps.newHashMap();
    for (ProjectLink projectLink : projectLinks) {
        for (ReleaseLink releaseLink : projectLink.linkedReleases) {
            Set<String> attachmentIds = attachments.getOrDefault(Source.releaseId(releaseLink.getId()), Sets.newHashSet());
            attachmentIds.addAll(releaseLink.getAttachments().stream().map(a -> a.getAttachmentContentId()).collect(Collectors.toList()));
            attachments.put(Source.releaseId(releaseLink.getId()), attachmentIds);
        }
    }
    return attachments;
}
Also used : ProjectLink(org.eclipse.sw360.datahandler.thrift.projects.ProjectLink) ReleaseLink(org.eclipse.sw360.datahandler.thrift.components.ReleaseLink)

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