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