Search in sources :

Example 66 with Attachment

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

the class ComponentUploadPortlet method printAttachments.

private void printAttachments(Set<Attachment> attachments, List<Iterable<String>> csvRows, Consumer<ComponentAttachmentCSVRecordBuilder> containingObjectPrinter) {
    if (attachments != null && !attachments.isEmpty()) {
        for (Attachment attachment : attachments) {
            final ComponentAttachmentCSVRecordBuilder componentAttachmentCSVRecordBuilder = ComponentAttachmentCSVRecord.builder();
            containingObjectPrinter.accept(componentAttachmentCSVRecordBuilder);
            componentAttachmentCSVRecordBuilder.fill(attachment);
            csvRows.add(componentAttachmentCSVRecordBuilder.build().getCSVIterable());
        }
    }
}
Also used : Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 67 with Attachment

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

the class AttachmentPortletUtils method serveAttachmentBundle.

private void serveAttachmentBundle(List<AttachmentContent> attachments, ResourceRequest request, ResourceResponse response, Optional<String> downloadFileName) {
    String filename;
    String contentType;
    if (attachments.size() == 1) {
        filename = downloadFileName.orElse(attachments.get(0).getFilename());
        contentType = attachments.get(0).getContentType();
    } else {
        filename = downloadFileName.orElse(DEFAULT_ATTACHMENT_BUNDLE_NAME);
        contentType = "application/zip";
    }
    User user = UserCacheHolder.getUserFromRequest(request);
    try {
        Optional<Object> context = getContextFromRequest(request, user);
        if (context.isPresent()) {
            try (InputStream attachmentStream = getStreamToServeAFile(attachments, user, context.get())) {
                PortletResponseUtil.sendFile(request, response, filename, attachmentStream, contentType);
            } catch (IOException e) {
                log.error("cannot finish writing response", e);
                response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
            }
        } else {
            log.warn("The user=[" + user.getEmail() + "] tried to download attachment=[" + CommonUtils.joinStrings(attachments.stream().map(AttachmentContent::getId).collect(Collectors.toList())) + "] in context=[" + request.getParameter(PortalConstants.CONTEXT_ID) + "] without read permissions");
            response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404");
        }
    } catch (SW360Exception e) {
        log.error("Context was not set properly.", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "400");
    } catch (TException e) {
        log.error("Problem getting the attachment content from the backend", e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
    }
}
Also used : TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User) InputStream(java.io.InputStream) IOException(java.io.IOException) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 68 with Attachment

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

the class AttachmentPortletUtils method createAttachmentContent.

public AttachmentContent createAttachmentContent(ResourceRequest request) throws IOException {
    String filename = request.getParameter("fileName");
    AttachmentContent attachmentContent = new AttachmentContent().setContentType("application/octet-stream").setFilename(filename);
    try {
        attachmentContent = client.makeAttachmentContent(attachmentContent);
    } catch (TException e) {
        log.error("Error creating attachment", e);
        attachmentContent = null;
    }
    return attachmentContent;
}
Also used : TException(org.apache.thrift.TException) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 69 with Attachment

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

the class AttachmentPortletUtils method checkAttachmentExistsFromRequest.

public boolean checkAttachmentExistsFromRequest(ResourceRequest request) {
    ResumableUpload resumableUpload = ResumableUpload.from(request);
    final AttachmentContent attachment = getAttachmentContent(resumableUpload);
    return alreadyHavePart(attachment, resumableUpload);
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 70 with Attachment

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

the class RemoteAttachmentDownloaderTest method testWithBrokenURL.

@Test
public void testWithBrokenURL() throws Exception {
    AttachmentContent attachmentContent = saveRemoteAttachment(TestUtils.BLACK_HOLE_ADDRESS);
    AttachmentContent attachmentGood = saveRemoteAttachment(url);
    assertThat(repository.getOnlyRemoteAttachments(), hasSize(2));
    assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(1));
    assertThat(repository.getOnlyRemoteAttachments(), hasSize(1));
    assertThat(attachmentConnector.getAttachmentStream(attachmentGood, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentGood.getId())))), hasLength(greaterThan(0l)));
    assertThat(repository.getOnlyRemoteAttachments(), hasSize(1));
    assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(0));
    assertThat(repository.getOnlyRemoteAttachments(), hasSize(1));
    try {
        assertThat(attachmentConnector.getAttachmentStream(attachmentContent, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentContent.getId())))), hasLength(greaterThan(0l)));
        fail("expected exception not thrown");
    } catch (SW360Exception e) {
        assertThat(e.getWhy(), containsString(attachmentContent.getId()));
    }
}
Also used : Project(org.eclipse.sw360.datahandler.thrift.projects.Project) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception) 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