Search in sources :

Example 6 with Attachment

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

the class ProjectController method downloadLicenseInfo.

@RequestMapping(value = PROJECTS_URL + "/{id}/licenseinfo", method = RequestMethod.GET)
public void downloadLicenseInfo(@PathVariable("id") String id, OAuth2Authentication oAuth2Authentication, @RequestParam("generatorClassName") String generatorClassName, HttpServletResponse response) throws TException, IOException {
    final User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    final Project sw360Project = projectService.getProjectForUserById(id, sw360User);
    final Map<String, Set<String>> selectedReleaseAndAttachmentIds = new HashMap<>();
    for (final String releaseId : sw360Project.getReleaseIdToUsage().keySet()) {
        final Release release = releaseService.getReleaseForUserById(releaseId, sw360User);
        if (release.isSetAttachments()) {
            if (!selectedReleaseAndAttachmentIds.containsKey(releaseId)) {
                selectedReleaseAndAttachmentIds.put(releaseId, new HashSet<>());
            }
            final Set<Attachment> attachments = release.getAttachments();
            for (final Attachment attachment : attachments) {
                selectedReleaseAndAttachmentIds.get(releaseId).add(attachment.getAttachmentContentId());
            }
        }
    }
    // TODO: implement method to determine excluded licenses
    final Map<String, Set<LicenseNameWithText>> excludedLicenses = new HashMap<>();
    final String projectName = sw360Project.getName();
    final String timestamp = SW360Utils.getCreatedOn();
    final OutputFormatInfo outputFormatInfo = licenseInfoService.getOutputFormatInfoForGeneratorClass(generatorClassName);
    final String filename = String.format("LicenseInfo-%s-%s.%s", projectName, timestamp, outputFormatInfo.getFileExtension());
    final LicenseInfoFile licenseInfoFile = licenseInfoService.getLicenseInfoFile(sw360Project, sw360User, generatorClassName, selectedReleaseAndAttachmentIds, excludedLicenses);
    byte[] byteContent = licenseInfoFile.bufferForGeneratedOutput().array();
    response.setContentType(outputFormatInfo.getMimeType());
    response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
    FileCopyUtils.copy(byteContent, response.getOutputStream());
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) LicenseInfoFile(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoFile) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) OutputFormatInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatInfo) Project(org.eclipse.sw360.datahandler.thrift.projects.Project) Release(org.eclipse.sw360.datahandler.thrift.components.Release)

Example 7 with Attachment

use of org.eclipse.sw360.datahandler.thrift.attachments.Attachment 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)

Example 8 with Attachment

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

the class Sw360AttachmentService method downloadAttachmentWithContext.

public void downloadAttachmentWithContext(Object context, String attachmentId, HttpServletResponse response, OAuth2Authentication oAuth2Authentication) {
    User sw360User = restControllerHelper.getSw360UserFromAuthentication(oAuth2Authentication);
    AttachmentContent attachmentContent = getAttachmentContent(attachmentId);
    String filename = attachmentContent.getFilename();
    String contentType = attachmentContent.getContentType();
    try (InputStream attachmentStream = getStreamToAttachments(Collections.singleton(attachmentContent), sw360User, context)) {
        response.setContentType(contentType);
        response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
        FileCopyUtils.copy(attachmentStream, response.getOutputStream());
    } catch (TException | IOException e) {
        log.error(e.getMessage());
    }
}
Also used : TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 9 with Attachment

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

the class Sw360AttachmentService method uploadAttachment.

public Attachment uploadAttachment(MultipartFile file, Attachment newAttachment, User sw360User) throws IOException, TException {
    // TODO: shouldn't the fileName be taken from newAttachment?
    String fileName = file.getOriginalFilename();
    String contentType = file.getContentType();
    final AttachmentContent attachmentContent = makeAttachmentContent(fileName, contentType);
    final AttachmentConnector attachmentConnector = getConnector();
    Attachment attachment = new AttachmentFrontendUtils().uploadAttachmentContent(attachmentContent, file.getInputStream(), sw360User);
    attachment.setSha1(attachmentConnector.getSha1FromAttachmentContentId(attachmentContent.getId()));
    AttachmentType attachmentType = newAttachment.getAttachmentType();
    if (attachmentType != null) {
        attachment.setAttachmentType(attachmentType);
    }
    CheckStatus checkStatus = newAttachment.getCheckStatus();
    if (checkStatus != null) {
        attachment.setCheckStatus(checkStatus);
    }
    return attachment;
}
Also used : AttachmentFrontendUtils(org.eclipse.sw360.commonIO.AttachmentFrontendUtils) AttachmentConnector(org.eclipse.sw360.datahandler.couchdb.AttachmentConnector)

Example 10 with Attachment

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

the class RestControllerHelper method createHalReleaseResource.

public HalResource<Release> createHalReleaseResource(Release release, boolean verbose) {
    HalResource<Release> halRelease = new HalResource<>(release);
    Link componentLink = linkTo(ReleaseController.class).slash("api" + ComponentController.COMPONENTS_URL + "/" + release.getComponentId()).withRel("component");
    halRelease.add(componentLink);
    release.setComponentId(null);
    if (verbose) {
        if (release.getModerators() != null) {
            Set<String> moderators = release.getModerators();
            this.addEmbeddedModerators(halRelease, moderators);
            release.setModerators(null);
        }
        if (release.getAttachments() != null) {
            Set<Attachment> attachments = release.getAttachments();
            this.addEmbeddedAttachments(halRelease, attachments);
            release.setAttachments(null);
        }
        if (release.getVendor() != null) {
            Vendor vendor = release.getVendor();
            HalResource<Vendor> vendorHalResource = this.addEmbeddedVendor(vendor.getFullname());
            halRelease.addEmbeddedResource("sw360:vendors", vendorHalResource);
            release.setVendor(null);
        }
        if (release.getMainLicenseIds() != null) {
            this.addEmbeddedLicenses(halRelease, release.getMainLicenseIds());
            release.setMainLicenseIds(null);
        }
    }
    return halRelease;
}
Also used : Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Vendor(org.eclipse.sw360.datahandler.thrift.vendors.Vendor) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Link(org.springframework.hateoas.Link)

Aggregations

Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)65 Test (org.junit.Test)40 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)38 User (org.eclipse.sw360.datahandler.thrift.users.User)27 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)22 InputStream (java.io.InputStream)19 Release (org.eclipse.sw360.datahandler.thrift.components.Release)15 IOException (java.io.IOException)13 PortletRequest (javax.portlet.PortletRequest)11 TestUserCacheHolder (org.eclipse.sw360.portal.TestUserCacheHolder)9 StringReader (java.io.StringReader)8 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)8 TException (org.apache.thrift.TException)8 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)8 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)7 OutputStream (java.io.OutputStream)5 AttachmentInputStream (org.ektorp.AttachmentInputStream)5 ResponseEntity (org.springframework.http.ResponseEntity)5 ComponentService (org.eclipse.sw360.datahandler.thrift.components.ComponentService)4 Maps (com.google.common.collect.Maps)3