Skip to content

Commit

Permalink
fix(rest) : Move GenerateLicenseInfoFile rest end point to SW360repor…
Browse files Browse the repository at this point in the history
…tcontroller

Signed-off-by: Keerthi B L <[email protected]>
  • Loading branch information
keerthi-bl committed Jun 13, 2024
1 parent 20c818a commit 6479bf0
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1208,113 +1208,6 @@ public ResponseEntity<CollectionModel<EntityModel<License>>> getLicensesOfReleas
return new ResponseEntity<>(resources, status);
}

@Operation(
summary = "Download license info for the project.",
description = "Set the request parameter `&template=<TEMPLATE_NAME>` for variant `REPORT` to choose " +
"specific template.",
tags = {"Projects"}
)
@RequestMapping(value = PROJECTS_URL + "/{id}/licenseinfo", method = RequestMethod.GET)
public void downloadLicenseInfo(
@Parameter(description = "Project ID.", example = "376576")
@PathVariable("id") String id,
@Parameter(description = "Output generator class",
schema = @Schema(type = "string",
allowableValues = {"DocxGenerator", "XhtmlGenerator",
"TextGenerator"}
))
@RequestParam("generatorClassName") String generatorClassName,
@Parameter(description = "Variant of the report",
schema = @Schema(implementation = OutputFormatVariant.class))
@RequestParam("variant") String variant,
@Parameter(description = "The external Ids of the project", example = "376577")
@RequestParam(value = "externalIds", required = false) String externalIds,
@RequestParam(value = "template", required = false) String template,
HttpServletResponse response
) throws TException, IOException {
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
final Project sw360Project = projectService.getProjectForUserById(id, sw360User);

List<ProjectLink> mappedProjectLinks = projectService.createLinkedProjects(sw360Project,
projectService.filterAndSortAttachments(SW360Constants.LICENSE_INFO_ATTACHMENT_TYPES), true, sw360User);

List<AttachmentUsage> attchmntUsg = attachmentService.getAttachemntUsages(id);

Map<Source, Set<String>> releaseIdToExcludedLicenses = attchmntUsg.stream()
.collect(Collectors.toMap(AttachmentUsage::getOwner,
x -> x.getUsageData().getLicenseInfo().getExcludedLicenseIds(), (li1, li2) -> li1));

Map<String, Boolean> usedAttachmentContentIds = attchmntUsg.stream()
.collect(Collectors.toMap(AttachmentUsage::getAttachmentContentId, attUsage -> {
if (attUsage.isSetUsageData()
&& attUsage.getUsageData().getSetField().equals(UsageData._Fields.LICENSE_INFO)) {
return Boolean.valueOf(attUsage.getUsageData().getLicenseInfo().isIncludeConcludedLicense());
}
return Boolean.FALSE;
}, (li1, li2) -> li1));

final Map<String, Map<String, Boolean>> selectedReleaseAndAttachmentIds = new HashMap<>();
final Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachments = new HashMap<>();


mappedProjectLinks.forEach(projectLink -> wrapTException(() ->
projectLink.getLinkedReleases().stream().filter(ReleaseLink::isSetAttachments).forEach(releaseLink -> {
String releaseLinkId = releaseLink.getId();
Set<String> excludedLicenseIds = releaseIdToExcludedLicenses.get(Source.releaseId(releaseLinkId));

if (!selectedReleaseAndAttachmentIds.containsKey(releaseLinkId)) {
selectedReleaseAndAttachmentIds.put(releaseLinkId, new HashMap<>());
}
final List<Attachment> attachments = releaseLink.getAttachments();
Release release = componentService.getReleaseById(releaseLinkId, sw360User);
for (final Attachment attachment : attachments) {
String attachemntContentId = attachment.getAttachmentContentId();
if (usedAttachmentContentIds.containsKey(attachemntContentId)) {
boolean includeConcludedLicense = usedAttachmentContentIds.get(attachemntContentId);
List<LicenseInfoParsingResult> licenseInfoParsingResult = licenseInfoService
.getLicenseInfoForAttachment(release, sw360User, attachemntContentId, includeConcludedLicense);
excludedLicensesPerAttachments.put(attachemntContentId,
getExcludedLicenses(excludedLicenseIds, licenseInfoParsingResult));
selectedReleaseAndAttachmentIds.get(releaseLinkId).put(attachemntContentId,
includeConcludedLicense);
}
}
})));

final String projectName = sw360Project.getName();
final String projectVersion = sw360Project.getVersion();
final String timestamp = SW360Utils.getCreatedOnTime().replaceAll("\\s", "_").replace(":", "_");
String outputGeneratorClassNameWithVariant = generatorClassName+"::"+variant;
final OutputFormatInfo outputFormatInfo = licenseInfoService.getOutputFormatInfoForGeneratorClass(generatorClassName);
final String filename = String.format("%s-%s%s-%s.%s", Strings.nullToEmpty(variant).equals("DISCLOSURE") ? "LicenseInfo" : "ProjectClearingReport", projectName,
StringUtils.isBlank(projectVersion) ? "" : "-" + projectVersion, timestamp,
outputFormatInfo.getFileExtension());

String fileName = "";
if (CommonUtils.isNotNullEmptyOrWhitespace(template) && CommonUtils.isNotNullEmptyOrWhitespace(REPORT_FILENAME_MAPPING)) {
Map<String, String> orgToTemplate = Arrays.stream(REPORT_FILENAME_MAPPING.split(","))
.collect(Collectors.toMap(k -> k.split(":")[0], v -> v.split(":")[1]));
fileName = orgToTemplate.get(template);
}

final LicenseInfoFile licenseInfoFile = licenseInfoService.getLicenseInfoFile(sw360Project, sw360User, outputGeneratorClassNameWithVariant, selectedReleaseAndAttachmentIds, excludedLicensesPerAttachments, externalIds, fileName);
byte[] byteContent = licenseInfoFile.bufferForGeneratedOutput().array();
response.setContentType(outputFormatInfo.getMimeType());
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
FileCopyUtils.copy(byteContent, response.getOutputStream());
}

private Set<LicenseNameWithText> getExcludedLicenses(Set<String> excludedLicenseIds,
List<LicenseInfoParsingResult> licenseInfoParsingResult) {

Predicate<LicenseNameWithText> filteredLicense = licenseNameWithText -> excludedLicenseIds
.contains(licenseNameWithText.getLicenseName());
Function<LicenseInfo, Stream<LicenseNameWithText>> streamLicenseNameWithTexts = licenseInfo -> licenseInfo
.getLicenseNamesWithTexts().stream();
return licenseInfoParsingResult.stream().map(LicenseInfoParsingResult::getLicenseInfo)
.flatMap(streamLicenseNameWithTexts).filter(filteredLicense).collect(Collectors.toSet());
}

@Operation(
description = "Get all attachment information of a project.",
tags = {"Projects"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ public Function<ProjectLink, ProjectLink> createProjectLinkMapper(Function<Relea
};
}

protected List<ProjectLink> createLinkedProjects(Project project,
public List<ProjectLink> createLinkedProjects(Project project,
Function<ProjectLink, ProjectLink> projectLinkMapper, boolean deep, User user) {
final Collection<ProjectLink> linkedProjects = SW360Utils
.flattenProjectLinkTree(SW360Utils.getLinkedProjects(project, deep, new ThriftClients(), log, user));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@
@SecurityRequirement(name = "tokenAuth")
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource> {
private static final String COMPONENTS = "components";

private static final String PROJECTS = "projects";

private static final String LICENSES = "licenses";
private static final String LICENSE_INFO = "licenseInfo";

public static final String REPORTS_URL = "/reports";

Expand Down Expand Up @@ -103,7 +102,10 @@ public void getProjectReport(
request, sw360User, module);
break;
case LICENSES:
getLicensesReports(response, sw360User, module);
getLicensesReports(request, response, sw360User, module);
break;
case LICENSE_INFO:
getLicensesInfoReports(request, response, sw360User, module, projectId);
break;
default:
break;
Expand All @@ -125,7 +127,7 @@ private void getProjectReports(boolean withLinkedReleases, boolean mailRequest,
responseJson.addProperty("response", "The downloaded report link will be send to the end user.");
response.getWriter().write(responseJson.toString());
} else {
downloadExcelReport(withLinkedReleases, response, sw360User, module, projectId);
downloadExcelReport(withLinkedReleases, request, response, sw360User, module, projectId);
}
} catch (Exception e) {
throw new TException(e.getMessage());
Expand All @@ -141,22 +143,30 @@ private void getComponentsReports(boolean withLinkedReleases, boolean mailReques
responseJson.addProperty("response", "Component report download link will get send to the end user.");
response.getWriter().write(responseJson.toString());
} else {
downloadExcelReport(withLinkedReleases, response, sw360User, module, null);
downloadExcelReport(withLinkedReleases, request, response, sw360User, module, null);
}
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void getLicensesReports(HttpServletResponse response, User sw360User, String module) throws TException {
private void getLicensesReports(HttpServletRequest request, HttpServletResponse response, User sw360User, String module) throws TException {
try {
downloadExcelReport(false, response, sw360User, module, null);
downloadExcelReport(false, request, response, sw360User, module, null);
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse response, User user, String module, String projectId)
private void getLicensesInfoReports(HttpServletRequest request, HttpServletResponse response, User sw360User, String module, String projectId) throws TException {
try {
downloadExcelReport(false, request, response, sw360User, module, projectId);
}catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void downloadExcelReport(boolean withLinkedReleases, HttpServletRequest request , HttpServletResponse response, User user, String module, String projectId)
throws TException {
try {
ByteBuffer buffer = null;
Expand All @@ -170,6 +180,13 @@ private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse
case LICENSES:
buffer = sw360ReportService.getLicenseBuffer();
break;
case LICENSE_INFO:
final String generatorClassName = request.getParameter("generatorClassName");
final String variant = request.getParameter("variant");
final String template = request.getParameter("template");
final String externalIds = request.getParameter("externalIds");
buffer = sw360ReportService.getLicenseInfoBuffer(user, projectId, generatorClassName, variant, template, externalIds);
break;
default:
break;
}
Expand All @@ -182,6 +199,8 @@ private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse
fileName = String.format("licenses-%s.xlsx", SW360Utils.getCreatedOn());
} else if(module.equals(PROJECTS)) {
fileName = sw360ReportService.getDocumentName(user, projectId);
} else if(module.equals(LICENSE_INFO)) {
fileName = sw360ReportService.getGenericLicInfoFileName(request, user);
}else {
fileName = sw360ReportService.getDocumentName(user, null);
}
Expand Down
Loading

0 comments on commit 6479bf0

Please sign in to comment.