use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentStreamConnector method getAttachmentBundleStream.
/**
* It is highly recommended to close this stream after using to avoid connection leak
*/
public <T> InputStream getAttachmentBundleStream(Set<AttachmentContent> attachments, User user, T context) throws IOException, SW360Exception {
assertNotNull(context);
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream(in);
new Thread(() -> {
byte[] buffer = new byte[1024];
int length;
try (ZipOutputStream zip = new ZipOutputStream(out)) {
for (AttachmentContent attachment : attachments) {
// TODO: handle attachments with equal name
ZipEntry zipEntry = new ZipEntry(attachment.getFilename());
zip.putNextEntry(zipEntry);
try (InputStream attachmentStream = getAttachmentStream(attachment, user, context)) {
while ((length = attachmentStream.read(buffer)) >= 0) {
zip.write(buffer, 0, length);
}
} catch (TException e) {
log.error("failed to get AttachmentStream, maybe due to permission problems", e);
}
zip.closeEntry();
}
} catch (IOException e) {
log.error("failed to write zip stream", e);
}
}).start();
return in;
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentHandlerTest method setUp.
@Before
public void setUp() throws Exception {
// Create the database
TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), dbName);
TestUtils.createDatabase(DatabaseSettings.getConfiguredHttpClient(), DatabaseSettings.COUCH_DB_DATABASE);
DatabaseConnector databaseConnector = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName);
databaseConnector.add(new AttachmentContent().setId("A1").setFilename("a.txt").setContentType("text"));
databaseConnector.add(new AttachmentContent().setId("A2").setFilename("b.jpg").setContentType("image"));
handler = new AttachmentHandler();
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentHandlerTest method testGetAttachmentContent.
@Test
public void testGetAttachmentContent() throws Exception {
AttachmentContent attachment = handler.getAttachmentContent("A1");
assertEquals("A1", attachment.id);
assertEquals("a.txt", attachment.filename);
assertEquals("text", attachment.contentType);
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.
the class AttachmentHandlerTest method testVacuum_UnusedIdIsDeleted.
@Test
public void testVacuum_UnusedIdIsDeleted() throws Exception {
final RequestSummary requestSummary = handler.vacuumAttachmentDB(new User("a", "a", "a").setUserGroup(UserGroup.ADMIN), ImmutableSet.of("A1"));
assertThat(requestSummary.requestStatus, is(RequestStatus.SUCCESS));
assertThat(requestSummary.totalElements, is(2));
assertThat(requestSummary.totalAffectedElements, is(1));
final AttachmentContent a1 = handler.getAttachmentContent("A1");
assertNotNull(a1);
exception.expect(SW360Exception.class);
final AttachmentContent a2 = handler.getAttachmentContent("A2");
assert (a2 == null);
}
use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent 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");
}
}
Aggregations