Search in sources :

Example 61 with Attachment

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

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

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

the class ProjectPortlet method serveAttachmentFileLicenses.

private void serveAttachmentFileLicenses(ResourceRequest request, ResourceResponse response) throws IOException {
    final User user = UserCacheHolder.getUserFromRequest(request);
    final String attachmentContentId = request.getParameter(PortalConstants.ATTACHMENT_ID);
    final ComponentService.Iface componentClient = thriftClients.makeComponentClient();
    final LicenseInfoService.Iface licenseInfoClient = thriftClients.makeLicenseInfoClient();
    try {
        Release release = componentClient.getReleaseById(request.getParameter(PortalConstants.RELEASE_ID), user);
        List<LicenseInfoParsingResult> licenseInfos = licenseInfoClient.getLicenseInfoForAttachment(release, attachmentContentId, user);
        // We generate a JSON-serializable list of licenses here.
        // In addition we remember the license information for exclusion later on
        Map<String, LicenseNameWithText> licenseStore = Maps.newHashMap();
        List<Map<String, String>> licenses = Lists.newArrayList();
        licenseInfos.forEach(licenseInfoResult -> addLicenseInfoResultToJsonSerializableLicensesList(licenseInfoResult, licenses, licenseStore::put));
        licenses.sort((l1, l2) -> Strings.nullToEmpty(l1.get(LICENSE_NAME_WITH_TEXT_NAME)).compareTo(l2.get(LICENSE_NAME_WITH_TEXT_NAME)));
        request.getPortletSession().setAttribute(LICENSE_STORE_KEY_PREFIX + attachmentContentId, licenseStore);
        writeJSON(request, response, OBJECT_MAPPER.writeValueAsString(licenses));
    } catch (TException exception) {
        log.error("Cannot retrieve license information for attachment id " + attachmentContentId + ".", exception);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
    }
}
Also used : WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) Release(org.eclipse.sw360.datahandler.thrift.components.Release)

Example 64 with Attachment

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

the class CompareAttachments method renderAttachmentRow.

private void renderAttachmentRow(JspWriter jspWriter, Attachment attachment, String contextType, String contextId) throws JspException, IOException {
    jspWriter.write("<tr>");
    for (Attachment._Fields field : RELEVANT_FIELDS) {
        FieldMetaData fieldMetaData = Attachment.metaDataMap.get(field);
        Object fieldValue = attachment.getFieldValue(field);
        if (field.equals(Attachment._Fields.FILENAME)) {
            jspWriter.append(String.format("<td>%s", getDisplayString(fieldMetaData.valueMetaData.type, fieldValue)));
            jspWriter.write("<br/>");
            addDownloadLink(pageContext, jspWriter, attachment.getFilename(), attachment.getAttachmentContentId(), contextType, contextId);
            jspWriter.append("</td>");
        } else {
            jspWriter.append(String.format("<td>%s</td>", getDisplayString(fieldMetaData.valueMetaData.type, fieldValue)));
        }
    }
    jspWriter.append("</tr>");
}
Also used : FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 65 with Attachment

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

the class AttachmentAwarePortlet method serveAttachmentSet.

private void serveAttachmentSet(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {
    final String documentType = getDocumentType(request);
    final String documentId = request.getParameter(PortalConstants.DOCUMENT_ID);
    final User user = UserCacheHolder.getUserFromRequest(request);
    // this is the raw attachment data
    List<Attachment> attachments = getAttachments(documentId, documentType, user).stream().sorted(Comparator.comparing(Attachment::getFilename)).collect(Collectors.toList());
    Map<String, Object> data = Maps.newHashMap();
    data.put("data", attachments);
    data.put("attachmentTypes", ATTACHMENT_TYPE_MAP);
    data.put("checkStatuses", CHECK_STATUS_MAP);
    writeJSON(request, response, OBJECT_MAPPER.writeValueAsString(data));
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

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