use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class ComponentUploadPortlet method printAttachments.
private void printAttachments(Set<Attachment> attachments, List<Iterable<String>> csvRows, Consumer<ComponentAttachmentCSVRecordBuilder> containingObjectPrinter) {
if (attachments != null && !attachments.isEmpty()) {
for (Attachment attachment : attachments) {
final ComponentAttachmentCSVRecordBuilder componentAttachmentCSVRecordBuilder = ComponentAttachmentCSVRecord.builder();
containingObjectPrinter.accept(componentAttachmentCSVRecordBuilder);
componentAttachmentCSVRecordBuilder.fill(attachment);
csvRows.add(componentAttachmentCSVRecordBuilder.build().getCSVIterable());
}
}
}
use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class AttachmentPortletUtils method serveAttachmentBundle.
private void serveAttachmentBundle(List<AttachmentContent> attachments, ResourceRequest request, ResourceResponse response, Optional<String> downloadFileName) {
String filename;
String contentType;
if (attachments.size() == 1) {
filename = downloadFileName.orElse(attachments.get(0).getFilename());
contentType = attachments.get(0).getContentType();
} else {
filename = downloadFileName.orElse(DEFAULT_ATTACHMENT_BUNDLE_NAME);
contentType = "application/zip";
}
User user = UserCacheHolder.getUserFromRequest(request);
try {
Optional<Object> context = getContextFromRequest(request, user);
if (context.isPresent()) {
try (InputStream attachmentStream = getStreamToServeAFile(attachments, user, context.get())) {
PortletResponseUtil.sendFile(request, response, filename, attachmentStream, contentType);
} catch (IOException e) {
log.error("cannot finish writing response", e);
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
}
} else {
log.warn("The user=[" + user.getEmail() + "] tried to download attachment=[" + CommonUtils.joinStrings(attachments.stream().map(AttachmentContent::getId).collect(Collectors.toList())) + "] in context=[" + request.getParameter(PortalConstants.CONTEXT_ID) + "] without read permissions");
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "404");
}
} catch (SW360Exception e) {
log.error("Context was not set properly.", e);
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "400");
} catch (TException e) {
log.error("Problem getting the attachment content from the backend", e);
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
}
}
use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class AttachmentPortletUtils method createAttachmentContent.
public AttachmentContent createAttachmentContent(ResourceRequest request) throws IOException {
String filename = request.getParameter("fileName");
AttachmentContent attachmentContent = new AttachmentContent().setContentType("application/octet-stream").setFilename(filename);
try {
attachmentContent = client.makeAttachmentContent(attachmentContent);
} catch (TException e) {
log.error("Error creating attachment", e);
attachmentContent = null;
}
return attachmentContent;
}
use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class AttachmentPortletUtils method checkAttachmentExistsFromRequest.
public boolean checkAttachmentExistsFromRequest(ResourceRequest request) {
ResumableUpload resumableUpload = ResumableUpload.from(request);
final AttachmentContent attachment = getAttachmentContent(resumableUpload);
return alreadyHavePart(attachment, resumableUpload);
}
use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class RemoteAttachmentDownloaderTest method testWithBrokenURL.
@Test
public void testWithBrokenURL() throws Exception {
AttachmentContent attachmentContent = saveRemoteAttachment(TestUtils.BLACK_HOLE_ADDRESS);
AttachmentContent attachmentGood = saveRemoteAttachment(url);
assertThat(repository.getOnlyRemoteAttachments(), hasSize(2));
assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(1));
assertThat(repository.getOnlyRemoteAttachments(), hasSize(1));
assertThat(attachmentConnector.getAttachmentStream(attachmentGood, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentGood.getId())))), hasLength(greaterThan(0l)));
assertThat(repository.getOnlyRemoteAttachments(), hasSize(1));
assertThat(retrieveRemoteAttachments(DatabaseSettings.getConfiguredHttpClient(), dbName, downloadTimeout), is(0));
assertThat(repository.getOnlyRemoteAttachments(), hasSize(1));
try {
assertThat(attachmentConnector.getAttachmentStream(attachmentContent, dummyUser, new Project().setVisbility(Visibility.ME_AND_MODERATORS).setCreatedBy(dummyUser.getEmail()).setAttachments(Collections.singleton(new Attachment().setAttachmentContentId(attachmentContent.getId())))), hasLength(greaterThan(0l)));
fail("expected exception not thrown");
} catch (SW360Exception e) {
assertThat(e.getWhy(), containsString(attachmentContent.getId()));
}
}
Aggregations