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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations