Search in sources :

Example 36 with AttachmentContent

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

the class AttachmentStreamConnector method getAttachmentBundleStream.

/**
 * It is highly recommended to close this stream after using to avoid connection leak
 */
public <T> InputStream getAttachmentBundleStream(Set<AttachmentContent> attachments, User user, T context) throws IOException, SW360Exception {
    assertNotNull(context);
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(in);
    new Thread(() -> {
        byte[] buffer = new byte[1024];
        int length;
        try (ZipOutputStream zip = new ZipOutputStream(out)) {
            for (AttachmentContent attachment : attachments) {
                // TODO: handle attachments with equal name
                ZipEntry zipEntry = new ZipEntry(attachment.getFilename());
                zip.putNextEntry(zipEntry);
                try (InputStream attachmentStream = getAttachmentStream(attachment, user, context)) {
                    while ((length = attachmentStream.read(buffer)) >= 0) {
                        zip.write(buffer, 0, length);
                    }
                } catch (TException e) {
                    log.error("failed to get AttachmentStream, maybe due to permission problems", e);
                }
                zip.closeEntry();
            }
        } catch (IOException e) {
            log.error("failed to write zip stream", e);
        }
    }).start();
    return in;
}
Also used : TException(org.apache.thrift.TException) ZipOutputStream(java.util.zip.ZipOutputStream) ConcatClosingInputStream(org.eclipse.sw360.datahandler.common.ConcatClosingInputStream) PipedInputStream(java.io.PipedInputStream) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Example 37 with AttachmentContent

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

the class AttachmentHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    // Create the database
    TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName);
    TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE);
    DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName);
    databaseConnector.add(new AttachmentContent().setId("A1").setFilename("a.txt").setContentType("text"));
    databaseConnector.add(new AttachmentContent().setId("A2").setFilename("b.jpg").setContentType("image"));
    handler = new AttachmentHandler();
}
Also used : DatabaseConnector(org.eclipse.sw360.datahandler.couchdb.DatabaseConnector) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 38 with AttachmentContent

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

the class AttachmentHandlerTest method testGetAttachmentContent.

@Test
public void testGetAttachmentContent() throws Exception {
    AttachmentContent attachment = handler.getAttachmentContent("A1");
    assertEquals("A1", attachment.id);
    assertEquals("a.txt", attachment.filename);
    assertEquals("text", attachment.contentType);
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 39 with AttachmentContent

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

the class AttachmentHandlerTest method testVacuum_UnusedIdIsDeleted.

@Test
public void testVacuum_UnusedIdIsDeleted() throws Exception {
    final RequestSummary requestSummary = handler.vacuumAttachmentDB(new User("a", "a", "a").setUserGroup(UserGroup.ADMIN), ImmutableSet.of("A1"));
    assertThat(requestSummary.requestStatus, is(RequestStatus.SUCCESS));
    assertThat(requestSummary.totalElements, is(2));
    assertThat(requestSummary.totalAffectedElements, is(1));
    final AttachmentContent a1 = handler.getAttachmentContent("A1");
    assertNotNull(a1);
    exception.expect(SW360Exception.class);
    final AttachmentContent a2 = handler.getAttachmentContent("A2");
    assert (a2 == null);
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary)

Example 40 with AttachmentContent

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

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