Search in sources :

Example 11 with AttachmentContent

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

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

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

the class AttachmentAwarePortlet method serveNewAttachmentId.

private void serveNewAttachmentId(ResourceRequest request, ResourceResponse response) throws IOException {
    final AttachmentContent attachment = attachmentPortletUtils.createAttachmentContent(request);
    if (attachment == null) {
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404");
    } else {
        final String attachmentId = attachment.getId();
        response.getWriter().write(attachmentId);
    }
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 14 with AttachmentContent

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

the class RemoteAttachmentDownloader method retrieveRemoteAttachments.

public static int retrieveRemoteAttachments(Supplier<HttpClient> httpClient, String dbAttachments, Duration downloadTimeout) throws MalformedURLException {
    AttachmentConnector attachmentConnector = new AttachmentConnector(httpClient, dbAttachments, downloadTimeout);
    AttachmentRepository attachmentRepository = new AttachmentRepository(new DatabaseConnector(httpClient, dbAttachments));
    List<AttachmentContent> remoteAttachments = attachmentRepository.getOnlyRemoteAttachments();
    log.info(format("we have %d remote attachments to retrieve", remoteAttachments.size()));
    int count = 0;
    for (AttachmentContent attachmentContent : remoteAttachments) {
        if (!attachmentContent.isOnlyRemote()) {
            log.info(format("skipping attachment (%s), which should already be available", attachmentContent.getId()));
            continue;
        }
        String attachmentContentId = attachmentContent.getId();
        log.info(format("retrieving attachment (%s) {filename=%s}", attachmentContentId, attachmentContent.getFilename()));
        log.debug("url is " + attachmentContent.getRemoteUrl());
        InputStream content = null;
        try {
            content = attachmentConnector.unsafeGetAttachmentStream(attachmentContent);
            if (content == null) {
                log.error("null content retrieving attachment " + attachmentContentId);
                continue;
            }
            try {
                long length = length(content);
                log.info(format("retrieved attachment (%s), it was %d bytes long", attachmentContentId, length));
                count++;
            } catch (IOException e) {
                log.error("attachment was downloaded but somehow not available in database " + attachmentContentId, e);
            }
        } catch (SW360Exception e) {
            log.error("cannot retrieve attachment " + attachmentContentId, e);
        } finally {
            closeQuietly(content, log);
        }
    }
    return count;
}
Also used : DatabaseConnector(org.eclipse.sw360.datahandler.couchdb.DatabaseConnector) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) IOException(java.io.IOException) AttachmentConnector(org.eclipse.sw360.datahandler.couchdb.AttachmentConnector) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 15 with AttachmentContent

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

the class AttachmentPortletUtils method uploadAttachmentPartFromRequest.

private boolean uploadAttachmentPartFromRequest(PortletRequest request, String fileUploadName) throws IOException, TException {
    final UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
    final InputStream stream = uploadPortletRequest.getFileAsStream(fileUploadName);
    final ResumableUpload resumableUpload = ResumableUpload.from(uploadPortletRequest);
    AttachmentContent attachment = null;
    if (resumableUpload.isValid()) {
        final AttachmentStreamConnector attachmentStreamConnector = getConnector();
        attachment = getAttachmentContent(resumableUpload, stream);
        if (attachment != null) {
            try {
                attachmentStreamConnector.uploadAttachmentPart(attachment, resumableUpload.getChunkNumber(), stream);
            } catch (TException e) {
                log.error("Error saving attachment part", e);
                return false;
            }
        }
    }
    return attachment != null;
}
Also used : TException(org.apache.thrift.TException) AttachmentStreamConnector(org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) UploadPortletRequest(com.liferay.portal.kernel.upload.UploadPortletRequest)

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