use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentStreamConnectorTest method testGetFullStream.
@Test
public void testGetFullStream() throws Exception {
AttachmentContent attachment = mock(AttachmentContent.class);
when(attachment.isOnlyRemote()).thenReturn(false);
when(attachment.isSetPartsCount()).thenReturn(false);
when(attachment.getFilename()).thenReturn("fil");
String attachmentId = "id";
when(attachment.getId()).thenReturn(attachmentId);
AttachmentInputStream full = mock(AttachmentInputStream.class);
when(connector.getAttachment(attachmentId, "fil")).thenReturn(full);
when(full.read()).thenReturn(1, 2, -1);
InputStream attachmentStream = attachmentStreamConnector.getAttachmentStream(attachment, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentId))));
assertThat(attachmentStream.read(), is(1));
assertThat(attachmentStream.read(), is(2));
assertThat(attachmentStream.read(), is(-1));
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentConnector method getSha1FromAttachmentContentId.
public String getSha1FromAttachmentContentId(String attachmentContentId) {
InputStream attachmentStream = null;
try {
AttachmentContent attachmentContent = getAttachmentContent(attachmentContentId);
attachmentStream = readAttachmentStream(attachmentContent);
return sha1Hex(attachmentStream);
} catch (SW360Exception e) {
log.error("Problem retrieving content of attachment", e);
return "";
} catch (IOException e) {
log.error("Problem computing the sha1 checksum", e);
return "";
} finally {
closeQuietly(attachmentStream, log);
}
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentAwarePortlet method serveNewAttachmentId.
private void serveNewAttachmentId(ResourceRequest request, ResourceResponse response) throws IOException {
final AttachmentContent attachment = attachmentPortletUtils.createAttachmentContent(request);
if (attachment == null) {
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404");
} else {
final String attachmentId = attachment.getId();
response.getWriter().write(attachmentId);
}
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent 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.AttachmentContent 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;
}
Aggregations