use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment 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.Attachment 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.Attachment in project sw360portal by sw360.
the class ProjectPortlet method serveAttachmentFileLicenses.
private void serveAttachmentFileLicenses(ResourceRequest request, ResourceResponse response) throws IOException {
final User user = UserCacheHolder.getUserFromRequest(request);
final String attachmentContentId = request.getParameter(PortalConstants.ATTACHMENT_ID);
final ComponentService.Iface componentClient = thriftClients.makeComponentClient();
final LicenseInfoService.Iface licenseInfoClient = thriftClients.makeLicenseInfoClient();
try {
Release release = componentClient.getReleaseById(request.getParameter(PortalConstants.RELEASE_ID), user);
List<LicenseInfoParsingResult> licenseInfos = licenseInfoClient.getLicenseInfoForAttachment(release, attachmentContentId, user);
// We generate a JSON-serializable list of licenses here.
// In addition we remember the license information for exclusion later on
Map<String, LicenseNameWithText> licenseStore = Maps.newHashMap();
List<Map<String, String>> licenses = Lists.newArrayList();
licenseInfos.forEach(licenseInfoResult -> addLicenseInfoResultToJsonSerializableLicensesList(licenseInfoResult, licenses, licenseStore::put));
licenses.sort((l1, l2) -> Strings.nullToEmpty(l1.get(LICENSE_NAME_WITH_TEXT_NAME)).compareTo(l2.get(LICENSE_NAME_WITH_TEXT_NAME)));
request.getPortletSession().setAttribute(LICENSE_STORE_KEY_PREFIX + attachmentContentId, licenseStore);
writeJSON(request, response, OBJECT_MAPPER.writeValueAsString(licenses));
} catch (TException exception) {
log.error("Cannot retrieve license information for attachment id " + attachmentContentId + ".", exception);
response.setProperty(ResourceResponse.HTTP_STATUS_CODE, "500");
}
}
use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class CompareAttachments method renderAttachmentRow.
private void renderAttachmentRow(JspWriter jspWriter, Attachment attachment, String contextType, String contextId) throws JspException, IOException {
jspWriter.write("<tr>");
for (Attachment._Fields field : RELEVANT_FIELDS) {
FieldMetaData fieldMetaData = Attachment.metaDataMap.get(field);
Object fieldValue = attachment.getFieldValue(field);
if (field.equals(Attachment._Fields.FILENAME)) {
jspWriter.append(String.format("<td>%s", getDisplayString(fieldMetaData.valueMetaData.type, fieldValue)));
jspWriter.write("<br/>");
addDownloadLink(pageContext, jspWriter, attachment.getFilename(), attachment.getAttachmentContentId(), contextType, contextId);
jspWriter.append("</td>");
} else {
jspWriter.append(String.format("<td>%s</td>", getDisplayString(fieldMetaData.valueMetaData.type, fieldValue)));
}
}
jspWriter.append("</tr>");
}
use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment in project sw360portal by sw360.
the class AttachmentAwarePortlet method serveAttachmentSet.
private void serveAttachmentSet(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {
final String documentType = getDocumentType(request);
final String documentId = request.getParameter(PortalConstants.DOCUMENT_ID);
final User user = UserCacheHolder.getUserFromRequest(request);
// this is the raw attachment data
List<Attachment> attachments = getAttachments(documentId, documentType, user).stream().sorted(Comparator.comparing(Attachment::getFilename)).collect(Collectors.toList());
Map<String, Object> data = Maps.newHashMap();
data.put("data", attachments);
data.put("attachmentTypes", ATTACHMENT_TYPE_MAP);
data.put("checkStatuses", CHECK_STATUS_MAP);
writeJSON(request, response, OBJECT_MAPPER.writeValueAsString(data));
}
Aggregations