Search in sources :

Example 1 with LicenseInfo

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo in project sw360portal by sw360.

the class LicenseInfoResourceProcessor method process.

@Override
public Resource<LicenseInfo> process(Resource<LicenseInfo> resource) {
    LicenseInfo licenseInfo = resource.getContent();
    Link selfLink = linkTo(LicenseInfoController.class).slash("api" + LicenseInfoController.LICENSE_INFO_URL).withSelfRel();
    resource.add(selfLink);
    return resource;
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) Link(org.springframework.hateoas.Link)

Example 2 with LicenseInfo

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo 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 3 with LicenseInfo

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo in project sw360portal by sw360.

the class ProjectPortlet method downloadLicenseInfo.

private void downloadLicenseInfo(ResourceRequest request, ResourceResponse response) throws IOException {
    final User user = UserCacheHolder.getUserFromRequest(request);
    final String projectId = request.getParameter(PROJECT_ID);
    final String outputGenerator = request.getParameter(PortalConstants.LICENSE_INFO_SELECTED_OUTPUT_FORMAT);
    final Map<String, Set<String>> selectedReleaseAndAttachmentIds = ProjectPortletUtils.getSelectedReleaseAndAttachmentIdsFromRequest(request);
    final Set<String> attachmentIds = selectedReleaseAndAttachmentIds.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
    final Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachmentId = ProjectPortletUtils.getExcludedLicensesPerAttachmentIdFromRequest(attachmentIds, request);
    try {
        final LicenseInfoService.Iface licenseInfoClient = thriftClients.makeLicenseInfoClient();
        final ProjectService.Iface projectClient = thriftClients.makeProjectClient();
        Project project = projectClient.getProjectById(projectId, user);
        LicenseInfoFile licenseInfoFile = licenseInfoClient.getLicenseInfoFile(project, user, outputGenerator, selectedReleaseAndAttachmentIds, excludedLicensesPerAttachmentId);
        try {
            replaceAttachmentUsages(user, selectedReleaseAndAttachmentIds, excludedLicensesPerAttachmentId, project);
        } catch (TException e) {
            // there's no need to abort the user's desired action just because the ancillary action of storing selection failed
            log.warn("LicenseInfo usage is not stored due to exception: ", e);
        }
        OutputFormatInfo outputFormatInfo = licenseInfoFile.getOutputFormatInfo();
        String filename = String.format("LicenseInfo-%s-%s.%s", project.getName(), SW360Utils.getCreatedOn(), outputFormatInfo.getFileExtension());
        String mimetype = outputFormatInfo.getMimeType();
        if (isNullOrEmpty(mimetype)) {
            mimetype = URLConnection.guessContentTypeFromName(filename);
        }
        PortletResponseUtil.sendFile(request, response, filename, licenseInfoFile.getGeneratedOutput(), mimetype);
    } catch (TException e) {
        log.error("Error getting LicenseInfo file for project with id " + projectId + " and generator " + outputGenerator, e);
        response.setProperty(ResourceResponse.HTTP_STATUS_CODE, Integer.toString(HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
    }
}
Also used : WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User)

Example 4 with LicenseInfo

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo in project sw360portal by sw360.

the class CLIParser method getLicenseInfos.

@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
    AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
    LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
    LicenseInfoParsingResult result = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
    InputStream attachmentStream = null;
    try {
        attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
        Document doc = getDocument(attachmentStream);
        Set<String> copyrights = getCopyrights(doc);
        licenseInfo.setCopyrights(copyrights);
        Set<LicenseNameWithText> licenseNamesWithTexts = getLicenseNameWithTexts(doc);
        licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTexts);
        result.setStatus(LicenseInfoRequestStatus.SUCCESS);
    } catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
        log.error(e);
        result.setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing CLI file: " + e.toString());
    } finally {
        closeQuietly(attachmentStream, log);
    }
    return Collections.singletonList(result);
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) IOException(java.io.IOException) Document(org.w3c.dom.Document) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 5 with LicenseInfo

use of org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo in project sw360portal by sw360.

the class LicenseInfoHandlerTest method testThatEmptyLicensesAreFiltered.

@Test
public void testThatEmptyLicensesAreFiltered() {
    LicenseInfoParsingResult emptyResult = new LicenseInfoParsingResult();
    LicenseInfoParsingResult emptyLicenseInfo = new LicenseInfoParsingResult();
    emptyLicenseInfo.setLicenseInfo(new LicenseInfo());
    LicenseInfoParsingResult parsingResults = new LicenseInfoParsingResult();
    LicenseInfo licenseInfo = new LicenseInfo();
    // @formatter:off
    licenseInfo.setLicenseNamesWithTexts(ImmutableSet.of(createLicense("nameOnly", null, null), createLicense(null, null, null), createLicense(null, "textOnly", null), createLicense("", null, null), createLicense(null, null, "ackOnly"), createLicense("", "", ""), createLicense("name", "text", "ack")));
    // @formatter:on
    parsingResults.setLicenseInfo(licenseInfo);
    handler.filterEmptyLicenses(ImmutableList.of(emptyResult, emptyLicenseInfo, parsingResults));
    // @formatter:off
    Assert.assertThat(parsingResults.getLicenseInfo().getLicenseNamesWithTexts(), Matchers.containsInAnyOrder(createLicense("nameOnly", null, null), createLicense(null, "textOnly", null), createLicense(null, null, "ackOnly"), createLicense("name", "text", "ack")));
// @formatter:on
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) Test(org.junit.Test)

Aggregations

LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)6 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)5 Release (org.eclipse.sw360.datahandler.thrift.components.Release)2 User (org.eclipse.sw360.datahandler.thrift.users.User)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 TException (org.apache.thrift.TException)1 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)1 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)1 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)1 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)1 LicenseInfoFile (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoFile)1 LicenseNameWithText (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText)1 OutputFormatInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatInfo)1 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)1 NotNull (org.jetbrains.annotations.NotNull)1 InvalidSPDXAnalysisException (org.spdx.rdfparser.InvalidSPDXAnalysisException)1