Search in sources :

Example 1 with AttachmentStreamConnector

use of org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector in project sw360portal by sw360.

the class AttachmentStreamConnectorTest method testTryingToDownloadIfNotAvailable.

@Test
public void testTryingToDownloadIfNotAvailable() throws Exception {
    String id = "11";
    String filename = "filename";
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.isOnlyRemote()).thenReturn(true);
    when(attachment.getId()).thenReturn(id);
    when(attachment.getFilename()).thenReturn(filename);
    InputStream downloadUrlStream = mock(InputStream.class);
    InputStream returnedStream = mock(InputStream.class);
    AttachmentContent rereadAttachment = mock(AttachmentContent.class);
    when(rereadAttachment.getId()).thenReturn(id);
    when(rereadAttachment.getFilename()).thenReturn(filename);
    attachmentStreamConnector = spy(attachmentStreamConnector);
    doReturn(returnedStream).when(attachmentStreamConnector).readAttachmentStream(rereadAttachment);
    doNothing().when(attachmentStreamConnector).uploadAttachmentPart(attachment, 1, downloadUrlStream);
    when(attachmentContentDownloader.download(eq(attachment), Matchers.any(Duration.class))).thenReturn(downloadUrlStream);
    when(connector.get(AttachmentContent.class, id)).thenReturn(rereadAttachment);
    doReturn(rereadAttachment).when(rereadAttachment).setOnlyRemote(anyBoolean());
    assertThat(attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(id)))), sameInstance(returnedStream));
    verify(attachmentContentDownloader).download(eq(attachment), Matchers.any(Duration.class));
    verify(attachmentStreamConnector).uploadAttachment(attachment, downloadUrlStream);
    verify(attachmentStreamConnector).readAttachmentStream(rereadAttachment);
    verify(rereadAttachment).setOnlyRemote(false);
    verify(connector).update(rereadAttachment);
}
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) Duration(org.eclipse.sw360.datahandler.common.Duration) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Test(org.junit.Test)

Example 2 with AttachmentStreamConnector

use of org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector 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

InputStream (java.io.InputStream)2 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)2 UploadPortletRequest (com.liferay.portal.kernel.upload.UploadPortletRequest)1 TException (org.apache.thrift.TException)1 Duration (org.eclipse.sw360.datahandler.common.Duration)1 AttachmentStreamConnector (org.eclipse.sw360.datahandler.couchdb.AttachmentStreamConnector)1 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)1 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)1 AttachmentInputStream (org.ektorp.AttachmentInputStream)1 Test (org.junit.Test)1