Search in sources :

Example 1 with AttachmentConnector

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

the class Sw360AttachmentService method uploadAttachment.

public Attachment uploadAttachment(MultipartFile file, Attachment newAttachment, User sw360User) throws IOException, TException {
    // TODO: shouldn't the fileName be taken from newAttachment?
    String fileName = file.getOriginalFilename();
    String contentType = file.getContentType();
    final AttachmentContent attachmentContent = makeAttachmentContent(fileName, contentType);
    final AttachmentConnector attachmentConnector = getConnector();
    Attachment attachment = new AttachmentFrontendUtils().uploadAttachmentContent(attachmentContent, file.getInputStream(), sw360User);
    attachment.setSha1(attachmentConnector.getSha1FromAttachmentContentId(attachmentContent.getId()));
    AttachmentType attachmentType = newAttachment.getAttachmentType();
    if (attachmentType != null) {
        attachment.setAttachmentType(attachmentType);
    }
    CheckStatus checkStatus = newAttachment.getCheckStatus();
    if (checkStatus != null) {
        attachment.setCheckStatus(checkStatus);
    }
    return attachment;
}
Also used : AttachmentFrontendUtils(org.eclipse.sw360.commonIO.AttachmentFrontendUtils) AttachmentConnector(org.eclipse.sw360.datahandler.couchdb.AttachmentConnector)

Example 2 with AttachmentConnector

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

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

the class FossologyFileHandlerTest method testAFailedSendToFossology.

@Test
public void testAFailedSendToFossology() throws Exception {
    final String id = "41";
    final FilledAttachment filledAttachment = getMockFilledAttachment(id);
    final AttachmentContent attachmentContent = filledAttachment.getAttachmentContent();
    final Release release = mock(Release.class);
    when(componentService.getReleaseById(releaseId, user)).thenReturn(release);
    spyGetFilledSourceAttachment(filledAttachment);
    final InputStream inputStream = mock(InputStream.class);
    when(release.isSetFossologyId()).thenReturn(false);
    when(attachmentConnector.getAttachmentStream(attachmentContent, user, release)).thenReturn(inputStream);
    when(fossologyUploader.uploadToFossology(inputStream, attachmentContent, clearingTeam)).thenReturn(-1);
    assertThat(fossologyFileHandler.sendToFossology(releaseId, user, clearingTeam), is(RequestStatus.FAILURE));
    verify(inputStream).close();
    // unimportant verifies
    verify(componentService, atLeastOnce()).getReleaseById(releaseId, user);
    verify(attachmentConnector).getAttachmentStream(attachmentContent, user, release);
    verify(fossologyUploader).uploadToFossology(inputStream, attachmentContent, clearingTeam);
}
Also used : InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) FilledAttachment(org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Test(org.junit.Test)

Example 4 with AttachmentConnector

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

the class FossologyFileHandlerTest method testSendToFossologyDuplicateDoesNotUpdateTheStatusOnErrors.

@Test
public void testSendToFossologyDuplicateDoesNotUpdateTheStatusOnErrors() throws Exception {
    final FilledAttachment filledAttachment = getMockFilledAttachment("17");
    final Release release = mock(Release.class);
    when(componentService.getReleaseById(releaseId, user)).thenReturn(release);
    when(release.getFossologyId()).thenReturn("41");
    when(release.isSetFossologyId()).thenReturn(true);
    spyGetFilledSourceAttachment(filledAttachment);
    when(fossologyUploader.duplicateInFossology(41, clearingTeam)).thenReturn(false);
    doNothing().when(fossologyFileHandler).setFossologyStatus(eq(release), anyString(), any(FossologyStatus.class));
    doReturn(true).when(fossologyFileHandler).checkSourceAttachment(release, filledAttachment);
    assertThat(fossologyFileHandler.sendToFossology(releaseId, user, clearingTeam), is(RequestStatus.FAILURE));
    verify(fossologyUploader).duplicateInFossology(41, clearingTeam);
    verify(fossologyFileHandler, never()).setFossologyStatus(eq(release), eq(clearingTeam), any(FossologyStatus.class));
    verify(fossologyUploader).getStatusInFossology(eq(41), eq(clearingTeam));
    verify(componentService, never()).updateReleaseFossology(release, user);
    verify(componentService).getReleaseById(releaseId, user);
    verifyNoMoreInteractions(attachmentConnector);
}
Also used : FilledAttachment(org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment) FossologyStatus(org.eclipse.sw360.datahandler.thrift.components.FossologyStatus) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Test(org.junit.Test)

Example 5 with AttachmentConnector

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

the class FossologyFileHandlerTest method testSendToFossologySendsAnAttachment.

@Test
public void testSendToFossologySendsAnAttachment() throws Exception {
    final String id = "41";
    final FilledAttachment filledAttachment = getMockFilledAttachment(id);
    final AttachmentContent attachmentContent = filledAttachment.getAttachmentContent();
    final Release release = mock(Release.class);
    when(componentService.getReleaseById(releaseId, user)).thenReturn(release);
    spyGetFilledSourceAttachment(filledAttachment);
    final InputStream inputStream = mock(InputStream.class);
    when(release.isSetFossologyId()).thenReturn(false);
    when(release.getClearingState()).thenReturn(ClearingState.NEW_CLEARING);
    when(attachmentConnector.getAttachmentStream(attachmentContent, user, release)).thenReturn(inputStream);
    when(fossologyUploader.uploadToFossology(inputStream, attachmentContent, clearingTeam)).thenReturn(1);
    doNothing().when(fossologyFileHandler).setFossologyStatus(eq(release), anyString(), eq(FossologyStatus.SENT), eq("" + 1), eq(id));
    doReturn(true).when(fossologyFileHandler).checkSourceAttachment(release, filledAttachment);
    assertThat(fossologyFileHandler.sendToFossology(releaseId, user, clearingTeam), is(RequestStatus.SUCCESS));
    verify(inputStream).close();
    // the release should be updated
    verify(componentService).updateReleaseFossology(release, user);
    verify(fossologyFileHandler).setFossologyStatus(eq(release), anyString(), eq(FossologyStatus.SENT), eq("" + 1), eq(id));
    // unimportant verifies
    verify(componentService, times(1)).getReleaseById(releaseId, user);
    verify(attachmentConnector).getAttachmentStream(attachmentContent, user, release);
    verify(fossologyUploader).uploadToFossology(inputStream, attachmentContent, clearingTeam);
}
Also used : InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) FilledAttachment(org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Test(org.junit.Test)

Aggregations

FilledAttachment (org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment)4 Release (org.eclipse.sw360.datahandler.thrift.components.Release)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 AttachmentConnector (org.eclipse.sw360.datahandler.couchdb.AttachmentConnector)3 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)3 DatabaseConnector (org.eclipse.sw360.datahandler.couchdb.DatabaseConnector)2 FossologyStatus (org.eclipse.sw360.datahandler.thrift.components.FossologyStatus)2 Before (org.junit.Before)2 IOException (java.io.IOException)1 AttachmentFrontendUtils (org.eclipse.sw360.commonIO.AttachmentFrontendUtils)1 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)1 ThriftClients (org.eclipse.sw360.datahandler.thrift.ThriftClients)1