Search in sources :

Example 1 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class AttachmentRepository method vacuumAttachmentDB.

public RequestSummary vacuumAttachmentDB(User user, final Set<String> usedIds) {
    final RequestSummary requestSummary = new RequestSummary();
    if (!PermissionUtils.isAdmin(user))
        return requestSummary.setRequestStatus(RequestStatus.FAILURE);
    final List<AttachmentContent> allAttachmentContents = getAll();
    final Set<AttachmentContent> unusedAttachmentContents = allAttachmentContents.stream().filter(input -> !usedIds.contains(input.getId())).collect(Collectors.toSet());
    requestSummary.setTotalElements(allAttachmentContents.size());
    requestSummary.setTotalAffectedElements(unusedAttachmentContents.size());
    final List<DocumentOperationResult> documentOperationResults = deleteBulk(unusedAttachmentContents);
    if (documentOperationResults.isEmpty()) {
        requestSummary.setRequestStatus(RequestStatus.SUCCESS);
    } else {
        requestSummary.setRequestStatus(RequestStatus.FAILURE);
    }
    return requestSummary;
}
Also used : DatabaseRepository(org.eclipse.sw360.datahandler.couchdb.DatabaseRepository) User(org.eclipse.sw360.datahandler.thrift.users.User) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary) ViewQuery(org.ektorp.ViewQuery) DocumentOperationResult(org.ektorp.DocumentOperationResult) Set(java.util.Set) PermissionUtils(org.eclipse.sw360.datahandler.permissions.PermissionUtils) Collectors(java.util.stream.Collectors) RequestStatus(org.eclipse.sw360.datahandler.thrift.RequestStatus) Views(org.ektorp.support.Views) List(java.util.List) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) DatabaseConnector(org.eclipse.sw360.datahandler.couchdb.DatabaseConnector) View(org.ektorp.support.View) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary) DocumentOperationResult(org.ektorp.DocumentOperationResult)

Example 2 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class AttachmentHandlerTest method testVacuum_AllIdsUsedIsNoop.

@Test
public void testVacuum_AllIdsUsedIsNoop() throws Exception {
    final RequestSummary requestSummary = handler.vacuumAttachmentDB(new User("a", "a", "a").setUserGroup(UserGroup.ADMIN), ImmutableSet.of("A1", "A2"));
    assertThat(requestSummary.requestStatus, is(RequestStatus.SUCCESS));
    assertThat(requestSummary.totalElements, is(2));
    assertThat(requestSummary.totalAffectedElements, is(0));
    final AttachmentContent a1 = handler.getAttachmentContent("A1");
    assertNotNull(a1);
    final AttachmentContent a2 = handler.getAttachmentContent("A2");
    assertNotNull(a2);
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary)

Example 3 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class AttachmentHandler method getAttachmentContent.

@Override
public AttachmentContent getAttachmentContent(String id) throws TException {
    assertNotEmpty(id);
    AttachmentContent attachment = attachmentRepository.get(id);
    assertNotNull(attachment, "Cannot find " + id + " in database.");
    validateAttachment(attachment);
    return attachment;
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 4 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class AttachmentDatabaseHandler method getAttachmentContent.

public AttachmentContent getAttachmentContent(String id) throws TException {
    AttachmentContent attachment = repository.get(id);
    assertNotNull(attachment, "Cannot find " + id + " in database.");
    validateAttachment(attachment);
    return attachment;
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 5 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class ProjectController method downloadClearingReports.

@RequestMapping(value = PROJECTS_URL + "/{projectId}/attachments/clearingReports", method = RequestMethod.GET, produces = "application/zip")
public void downloadClearingReports(@PathVariable("projectId") String projectId, HttpServletResponse response, OAuth2Authentication oAuth2Authentication) throws TException {
    final User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    final Project project = projectService.getProjectForUserById(projectId, sw360User);
    final String filename = "Clearing-Reports-" + project.getName() + ".zip";
    final Set<Attachment> attachments = project.getAttachments();
    final Set<AttachmentContent> clearingAttachments = new HashSet<>();
    for (final Attachment attachment : attachments) {
        if (attachment.getAttachmentType().equals(AttachmentType.CLEARING_REPORT)) {
            clearingAttachments.add(attachmentService.getAttachmentContent(attachment.getAttachmentContentId()));
        }
    }
    try (InputStream attachmentStream = attachmentService.getStreamToAttachments(clearingAttachments, sw360User, project)) {
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
        FileCopyUtils.copy(attachmentStream, response.getOutputStream());
    } catch (final TException | IOException e) {
        log.error(e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) Project(org.eclipse.sw360.datahandler.thrift.projects.Project) User(org.eclipse.sw360.datahandler.thrift.users.User) InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) IOException(java.io.IOException)

Aggregations

AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)52 InputStream (java.io.InputStream)24 Test (org.junit.Test)20 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)13 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)11 IOException (java.io.IOException)10 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)10 User (org.eclipse.sw360.datahandler.thrift.users.User)10 Release (org.eclipse.sw360.datahandler.thrift.components.Release)8 TException (org.apache.thrift.TException)7 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)6 AttachmentInputStream (org.ektorp.AttachmentInputStream)6 OutputStream (java.io.OutputStream)5 FilledAttachment (org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment)5 StringReader (java.io.StringReader)4 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)4 RequestSummary (org.eclipse.sw360.datahandler.thrift.RequestSummary)4 DatabaseConnector (org.eclipse.sw360.datahandler.couchdb.DatabaseConnector)3 LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)3 PipedInputStream (java.io.PipedInputStream)2