Search in sources :

Example 1 with OutputFormatInfo

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

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

the class ProjectPortlet method prepareLicenseInfo.

private void prepareLicenseInfo(RenderRequest request, RenderResponse response) throws IOException, PortletException {
    User user = UserCacheHolder.getUserFromRequest(request);
    String id = request.getParameter(PROJECT_ID);
    request.setAttribute(PortalConstants.SW360_USER, user);
    request.setAttribute(DOCUMENT_TYPE, SW360Constants.TYPE_PROJECT);
    request.setAttribute(PROJECT_LINK_TABLE_MODE, PROJECT_LINK_TABLE_MODE_LICENSE_INFO);
    if (id != null) {
        try {
            ProjectService.Iface client = thriftClients.makeProjectClient();
            Project project = client.getProjectById(id, user);
            request.setAttribute(PROJECT, project);
            request.setAttribute(DOCUMENT_ID, id);
            LicenseInfoService.Iface licenseInfoClient = thriftClients.makeLicenseInfoClient();
            List<OutputFormatInfo> outputFormats = licenseInfoClient.getPossibleOutputFormats();
            request.setAttribute(PortalConstants.LICENSE_INFO_OUTPUT_FORMATS, outputFormats);
            List<ProjectLink> mappedProjectLinks = createLinkedProjects(project, filterAndSortAttachments(SW360Constants.LICENSE_INFO_ATTACHMENT_TYPES), true, user);
            request.setAttribute(PROJECT_LIST, mappedProjectLinks);
            addProjectBreadcrumb(request, response, project);
            AttachmentService.Iface attachmentClient = thriftClients.makeAttachmentClient();
            Map<Source, Set<String>> containedAttachments = ProjectPortletUtils.extractContainedAttachments(mappedProjectLinks);
            Map<Map<Source, String>, Integer> attachmentUsages = attachmentClient.getAttachmentUsageCount(containedAttachments, UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet())));
            Map<String, Integer> countMap = attachmentUsages.entrySet().stream().collect(Collectors.toMap(entry -> {
                Entry<Source, String> key = entry.getKey().entrySet().iterator().next();
                return key.getKey().getFieldValue() + "_" + key.getValue();
            }, entry -> entry.getValue()));
            request.setAttribute(ATTACHMENT_USAGE_COUNT_MAP, countMap);
        } catch (TException e) {
            log.error("Error fetching project from backend!", e);
            setSW360SessionError(request, ErrorMessages.ERROR_GETTING_PROJECT);
        }
    }
}
Also used : ReleaseClearingStatusData(org.eclipse.sw360.datahandler.thrift.components.ReleaseClearingStatusData) User(org.eclipse.sw360.datahandler.thrift.users.User) Release(org.eclipse.sw360.datahandler.thrift.components.Release) TSimpleJSONProtocol(org.apache.thrift.protocol.TSimpleJSONProtocol) TSerializer(org.apache.thrift.TSerializer) ProjectExporter(org.eclipse.sw360.exporter.ProjectExporter) Logger(org.apache.log4j.Logger) JSONFactoryUtil.createJSONObject(com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONObject) SW360Utils.printName(org.eclipse.sw360.datahandler.common.SW360Utils.printName) CONTENT_TYPE_OPENXML_SPREADSHEET(org.eclipse.sw360.datahandler.common.SW360Constants.CONTENT_TYPE_OPENXML_SPREADSHEET) LifeRayUserSession(org.eclipse.sw360.portal.users.LifeRayUserSession) com.google.common.collect(com.google.common.collect) UserUtils(org.eclipse.sw360.portal.users.UserUtils) PrintWriter(java.io.PrintWriter) CveSearchService(org.eclipse.sw360.datahandler.thrift.cvesearch.CveSearchService) Predicate(java.util.function.Predicate) PermissionUtils(org.eclipse.sw360.datahandler.permissions.PermissionUtils) org.eclipse.sw360.portal.common(org.eclipse.sw360.portal.common) VulnerabilityUpdateStatus(org.eclipse.sw360.datahandler.thrift.cvesearch.VulnerabilityUpdateStatus) Collectors(java.util.stream.Collectors) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) VendorService(org.eclipse.sw360.datahandler.thrift.vendors.VendorService) Entry(java.util.Map.Entry) Organization(com.liferay.portal.model.Organization) NotNull(org.jetbrains.annotations.NotNull) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) java.util(java.util) Strings.nullToEmpty(com.google.common.base.Strings.nullToEmpty) JSONException(com.liferay.portal.kernel.json.JSONException) PortletResponseUtil(com.liferay.portal.kernel.portlet.PortletResponseUtil) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Function(java.util.function.Function) Strings(com.google.common.base.Strings) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) org.eclipse.sw360.datahandler.thrift.vulnerabilities(org.eclipse.sw360.datahandler.thrift.vulnerabilities) org.eclipse.sw360.datahandler.thrift(org.eclipse.sw360.datahandler.thrift) ReleaseLink(org.eclipse.sw360.datahandler.thrift.components.ReleaseLink) org.eclipse.sw360.datahandler.thrift.projects(org.eclipse.sw360.datahandler.thrift.projects) SessionMessages(com.liferay.portal.kernel.servlet.SessionMessages) URLConnection(java.net.URLConnection) BiConsumer(java.util.function.BiConsumer) javax.portlet(javax.portlet) org.eclipse.sw360.datahandler.thrift.attachments(org.eclipse.sw360.datahandler.thrift.attachments) org.eclipse.sw360.datahandler.common(org.eclipse.sw360.datahandler.common) RequestedAction(org.eclipse.sw360.datahandler.thrift.users.RequestedAction) JSONFactoryUtil.createJSONArray(com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONArray) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Vendor(org.eclipse.sw360.datahandler.thrift.vendors.Vendor) HttpServletResponse(javax.servlet.http.HttpServletResponse) TException(org.apache.thrift.TException) JSONFactoryUtil(com.liferay.portal.kernel.json.JSONFactoryUtil) IOException(java.io.IOException) JSONObject(com.liferay.portal.kernel.json.JSONObject) FossologyAwarePortlet(org.eclipse.sw360.portal.portlets.FossologyAwarePortlet) ReleaseExporter(org.eclipse.sw360.exporter.ReleaseExporter) UserCacheHolder(org.eclipse.sw360.portal.users.UserCacheHolder) PortalConstants(org.eclipse.sw360.portal.common.PortalConstants) JSONArray(com.liferay.portal.kernel.json.JSONArray) org.eclipse.sw360.datahandler.thrift.licenseinfo(org.eclipse.sw360.datahandler.thrift.licenseinfo) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) TException(org.apache.thrift.TException) User(org.eclipse.sw360.datahandler.thrift.users.User) Entry(java.util.Map.Entry)

Example 3 with OutputFormatInfo

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

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

the class DisplayOutputFormats method writeOptions.

private void writeOptions(Collection<OutputFormatInfo> options) throws IOException {
    JspWriter jspWriter = getJspContext().getOut();
    for (OutputFormatInfo option : options) {
        String optionDescription = option.getDescription();
        String optionValue = option.getGeneratorClassName() + "::" + option.getVariant();
        jspWriter.write(String.format("<option value=\"%s\" class=\"textlabel stackedLabel\" \" +\n" + "                            (selected ? \"selected=\\\"selected\\\" \" : \"\") +\n" + "                            \">%s</option>", optionValue, optionDescription));
    }
}
Also used : JspWriter(javax.servlet.jsp.JspWriter) OutputFormatInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatInfo)

Aggregations

User (org.eclipse.sw360.datahandler.thrift.users.User)3 TException (org.apache.thrift.TException)2 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)2 Release (org.eclipse.sw360.datahandler.thrift.components.Release)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Strings (com.google.common.base.Strings)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Strings.nullToEmpty (com.google.common.base.Strings.nullToEmpty)1 com.google.common.collect (com.google.common.collect)1 JSONArray (com.liferay.portal.kernel.json.JSONArray)1 JSONException (com.liferay.portal.kernel.json.JSONException)1 JSONFactoryUtil (com.liferay.portal.kernel.json.JSONFactoryUtil)1 JSONFactoryUtil.createJSONArray (com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONArray)1 JSONFactoryUtil.createJSONObject (com.liferay.portal.kernel.json.JSONFactoryUtil.createJSONObject)1 JSONObject (com.liferay.portal.kernel.json.JSONObject)1 PortletResponseUtil (com.liferay.portal.kernel.portlet.PortletResponseUtil)1 SessionMessages (com.liferay.portal.kernel.servlet.SessionMessages)1 Organization (com.liferay.portal.model.Organization)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1