Search in sources :

Example 1 with LicenseNameWithText

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

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

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

the class ProjectPortletUtils method getExcludedLicensesPerAttachmentIdFromRequest.

/**
 * Returns a map of excluded licenses. They key is an attachment content id, the
 * value is a list of excluded licenses.
 * <p>
 * For this method to work it is crucial that there is a so called
 * "license-store-&lt;attachmentContentId&gt;" map in the session. This map must
 * contain a mapping from a key to a {@link LicenseNameWithText} object.
 *
 * @param attachmentContentIds list of attachment content id to check for exclusions in the
 *                             request
 * @param request              the request containing the excluded licenses as parameters
 * @return a map containing the licenses to exclude
 * @see ProjectPortletUtilsTest for a better understanding
 */
public static Map<String, Set<LicenseNameWithText>> getExcludedLicensesPerAttachmentIdFromRequest(Set<String> attachmentContentIds, ResourceRequest request) {
    Map<String, Set<LicenseNameWithText>> excludedLicenses = Maps.newHashMap();
    for (String attachmentContentId : attachmentContentIds) {
        String[] checkboxes = request.getParameterValues(attachmentContentId);
        String[] keys = request.getParameterValues(attachmentContentId + "_key");
        if (checkboxes == null) {
            // no details present
            continue;
        }
        @SuppressWarnings("unchecked") Map<String, LicenseNameWithText> licenseStore = (Map<String, LicenseNameWithText>) request.getPortletSession().getAttribute(ProjectPortlet.LICENSE_STORE_KEY_PREFIX + attachmentContentId);
        if (licenseStore == null) {
            throw new IllegalStateException("No license store found for attachment content id " + attachmentContentId);
        }
        Set<Integer> includedIds = Arrays.stream(checkboxes).map(s -> Integer.valueOf(s)).collect(Collectors.toSet());
        Set<LicenseNameWithText> licenseNameWithTexts = Sets.newHashSet();
        for (int index = 0; index < keys.length; index++) {
            if (includedIds.contains(index)) {
                // transferred
                continue;
            }
            LicenseNameWithText licenseNameWithText = licenseStore.get(keys[index]);
            if (licenseNameWithText == null) {
                throw new IllegalStateException("No license found for key " + keys[index]);
            }
            licenseNameWithTexts.add(licenseNameWithText);
        }
        excludedLicenses.put(attachmentContentId, licenseNameWithTexts);
    }
    return excludedLicenses;
}
Also used : java.util(java.util) User(org.eclipse.sw360.datahandler.thrift.users.User) ProjectVulnerabilityRating(org.eclipse.sw360.datahandler.thrift.vulnerabilities.ProjectVulnerabilityRating) ProjectLink(org.eclipse.sw360.datahandler.thrift.projects.ProjectLink) Logger(org.apache.log4j.Logger) ResourceRequest(javax.portlet.ResourceRequest) org.eclipse.sw360.datahandler.thrift(org.eclipse.sw360.datahandler.thrift) Lists(com.google.common.collect.Lists) ReleaseLink(org.eclipse.sw360.datahandler.thrift.components.ReleaseLink) CUSTOM_FIELD_PROJECT_GROUP_FILTER(org.eclipse.sw360.portal.common.PortalConstants.CUSTOM_FIELD_PROJECT_GROUP_FILTER) VulnerabilityCheckStatus(org.eclipse.sw360.datahandler.thrift.vulnerabilities.VulnerabilityCheckStatus) CustomFieldHelper(org.eclipse.sw360.portal.common.CustomFieldHelper) PortletRequest(javax.portlet.PortletRequest) Project(org.eclipse.sw360.datahandler.thrift.projects.Project) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) ProjectRelationship(org.eclipse.sw360.datahandler.thrift.projects.ProjectRelationship) SW360Utils(org.eclipse.sw360.datahandler.common.SW360Utils) ImmutableMap(com.google.common.collect.ImmutableMap) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) VulnerabilityRatingForProject(org.eclipse.sw360.datahandler.thrift.vulnerabilities.VulnerabilityRatingForProject) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) AttachmentUsage(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage) UsageData(org.eclipse.sw360.datahandler.thrift.attachments.UsageData) UserCacheHolder(org.eclipse.sw360.portal.users.UserCacheHolder) PortalConstants(org.eclipse.sw360.portal.common.PortalConstants) LicenseInfoUsage(org.eclipse.sw360.datahandler.thrift.attachments.LicenseInfoUsage) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) PortletUtils(org.eclipse.sw360.portal.common.PortletUtils) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 4 with LicenseNameWithText

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

the class ProjectPortletUtils method makeAttachmentUsages.

public static List<AttachmentUsage> makeAttachmentUsages(Project project, Map<String, Set<String>> selectedReleaseAndAttachmentIds, Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachmentId) {
    List<AttachmentUsage> attachmentUsages = Lists.newArrayList();
    for (String releaseId : selectedReleaseAndAttachmentIds.keySet()) {
        for (String attachmentContentId : selectedReleaseAndAttachmentIds.get(releaseId)) {
            AttachmentUsage usage = new AttachmentUsage();
            usage.setUsedBy(Source.projectId(project.getId()));
            usage.setOwner(Source.releaseId(releaseId));
            usage.setAttachmentContentId(attachmentContentId);
            Set<String> licenseIds = CommonUtils.nullToEmptySet(excludedLicensesPerAttachmentId.get(attachmentContentId)).stream().filter(LicenseNameWithText::isSetLicenseName).map(LicenseNameWithText::getLicenseName).collect(Collectors.toSet());
            usage.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(licenseIds)));
            attachmentUsages.add(usage);
        }
    }
    return attachmentUsages;
}
Also used : AttachmentUsage(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) LicenseInfoUsage(org.eclipse.sw360.datahandler.thrift.attachments.LicenseInfoUsage)

Example 5 with LicenseNameWithText

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

the class LicenseInfoHandler method getLicenseInfoFile.

@Override
public LicenseInfoFile getLicenseInfoFile(Project project, User user, String outputGenerator, Map<String, Set<String>> releaseIdsToSelectedAttachmentIds, Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachment) throws TException {
    assertNotNull(project);
    assertNotNull(user);
    assertNotNull(outputGenerator);
    assertNotNull(releaseIdsToSelectedAttachmentIds);
    assertNotNull(excludedLicensesPerAttachment);
    Map<Release, Set<String>> releaseToAttachmentId = mapKeysToReleases(releaseIdsToSelectedAttachmentIds, user);
    Collection<LicenseInfoParsingResult> projectLicenseInfoResults = getAllReleaseLicenseInfos(releaseToAttachmentId, user, excludedLicensesPerAttachment);
    String[] outputGeneratorClassnameAndVariant = outputGenerator.split("::");
    if (outputGeneratorClassnameAndVariant.length != 2) {
        throw new TException("Unsupported output generator value: " + outputGenerator);
    }
    String outputGeneratorClassName = outputGeneratorClassnameAndVariant[0];
    OutputFormatVariant outputGeneratorVariant = Enums.getIfPresent(OutputFormatVariant.class, outputGeneratorClassnameAndVariant[1]).orNull();
    OutputGenerator<?> generator = getOutputGeneratorByClassnameAndVariant(outputGeneratorClassName, outputGeneratorVariant);
    LicenseInfoFile licenseInfoFile = new LicenseInfoFile();
    licenseInfoFile.setOutputFormatInfo(generator.getOutputFormatInfo());
    String licenseInfoHeaderText = (project.isSetLicenseInfoHeaderText()) ? project.getLicenseInfoHeaderText() : getDefaultLicenseInfoHeaderText();
    Object output = generator.generateOutputFile(projectLicenseInfoResults, project.getName(), project.getVersion(), licenseInfoHeaderText);
    if (output instanceof byte[]) {
        licenseInfoFile.setGeneratedOutput((byte[]) output);
    } else if (output instanceof String) {
        licenseInfoFile.setGeneratedOutput(((String) output).getBytes());
    } else {
        throw new TException("Unsupported output generator result: " + output.getClass().getSimpleName());
    }
    return licenseInfoFile;
}
Also used : WrappedException.wrapTException(org.eclipse.sw360.datahandler.common.WrappedException.wrapTException) TException(org.apache.thrift.TException) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) CommonUtils.nullToEmptySet(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet) Release(org.eclipse.sw360.datahandler.thrift.components.Release)

Aggregations

LicenseNameWithText (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText)11 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)6 User (org.eclipse.sw360.datahandler.thrift.users.User)6 Release (org.eclipse.sw360.datahandler.thrift.components.Release)5 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)4 Collectors (java.util.stream.Collectors)3 TException (org.apache.thrift.TException)3 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)3 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)3 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)3 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)3 LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)3 Maps (com.google.common.collect.Maps)2 Sets (com.google.common.collect.Sets)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 java.util (java.util)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2