Search in sources :

Example 26 with Attachment

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

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

Example 28 with Attachment

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

the class AttachmentPortletUtils method getAttachmentContent.

private AttachmentContent getAttachmentContent(ResumableUpload resumableUpload, InputStream stream) throws IOException, TException {
    if (!resumableUpload.isValid()) {
        return null;
    }
    final AttachmentContent attachment = getAttachmentContent(resumableUpload);
    if (resumableUpload.getChunkNumber() == 1) {
        String fileName = resumableUpload.getFilename();
        String contentType = resumableUpload.getFileType();
        if (isNullOrEmpty(contentType)) {
            contentType = guessContentTypeFromStream(stream);
        }
        if (isNullOrEmpty(contentType)) {
            contentType = guessContentTypeFromName(fileName);
        }
        if (isNullOrEmpty(contentType)) {
            contentType = "text";
        }
        int partsCount = resumableUpload.getTotalChunks();
        attachment.setContentType(contentType).setFilename(fileName).setOnlyRemote(false).setPartsCount(Integer.toString(partsCount));
        return updateAttachmentContent(attachment);
    } else {
        return attachment;
    }
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 29 with Attachment

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

the class FossologyUploaderTest method testUploadToFossologyWithBadReturn.

@Test
public void testUploadToFossologyWithBadReturn() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.getId()).thenReturn("id");
    when(attachment.getFilename()).thenReturn("fileName");
    String clearingTeam = "cl";
    final InputStream inputStream = mock(InputStream.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            OutputStream outputStream = (OutputStream) invocation.getArguments()[2];
            outputStream.write("error".getBytes());
            return 0;
        }
    }).when(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam);
    verify(sshConnector).runInFossologyViaSsh(anyString(), eq(inputStream), any(OutputStream.class));
    assertThat(uploadId, is((long) -1));
}
Also used : Answer(org.mockito.stubbing.Answer) InputStream(java.io.InputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OutputStream(java.io.OutputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Test(org.junit.Test)

Example 30 with Attachment

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

the class FossologyUploaderTest method testUploadToFossologyWithEmptyId.

@Test
public void testUploadToFossologyWithEmptyId() throws Exception {
    AttachmentContent attachment = mock(AttachmentContent.class);
    when(attachment.getId()).thenReturn(null);
    when(attachment.getFilename()).thenReturn("fileName");
    String clearingTeam = "cl";
    final InputStream inputStream = mock(InputStream.class);
    final long uploadId = fossologyUploader.uploadToFossology(inputStream, attachment, clearingTeam);
    verify(sshConnector, never()).runInFossologyViaSsh(anyString(), any(InputStream.class), any(OutputStream.class));
    assertThat(uploadId, is((long) -1));
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) 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