Skip to content

Commit

Permalink
fix(rest): Added JWT token convert to fix the issue with authorities
Browse files Browse the repository at this point in the history
Signed-off-by: Smruti Prakash Sahoo <smruti.sahoo@siemens.com>
  • Loading branch information
smrutis1 authored and keerthi-bl committed Aug 29, 2024
1 parent 714e16e commit b7fa1bb
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,8 @@ public void downloadLicenseInfo(
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());
StringUtils.isBlank(projectVersion) ? "" : "-" + projectVersion, timestamp,
outputFormatInfo.getFileExtension());

String fileName = "";
if (CommonUtils.isNotNullEmptyOrWhitespace(template) && CommonUtils.isNotNullEmptyOrWhitespace(REPORT_FILENAME_MAPPING)) {
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 @@ -49,10 +49,9 @@
@SecurityRequirement(name = "basic")
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 @@ -104,7 +103,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 @@ -126,7 +128,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 @@ -142,22 +144,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 @@ -171,6 +181,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 @@ -183,6 +200,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
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,77 @@
package org.eclipse.sw360.rest.resourceserver.report;

import static org.eclipse.sw360.datahandler.common.WrappedException.wrapTException;
import static org.eclipse.sw360.rest.resourceserver.Sw360ResourceServer.REPORT_FILENAME_MAPPING;

import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.common.SW360Constants;
import org.eclipse.sw360.datahandler.common.SW360Utils;
import org.eclipse.sw360.datahandler.thrift.Source;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage;
import org.eclipse.sw360.datahandler.thrift.attachments.UsageData;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
import org.eclipse.sw360.datahandler.thrift.components.Release;
import org.eclipse.sw360.datahandler.thrift.components.ReleaseLink;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoFile;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatInfo;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectLink;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectService;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.attachment.Sw360AttachmentService;
import org.eclipse.sw360.rest.resourceserver.component.Sw360ComponentService;
import org.eclipse.sw360.rest.resourceserver.license.Sw360LicenseService;
import org.eclipse.sw360.rest.resourceserver.licenseinfo.Sw360LicenseInfoService;
import org.eclipse.sw360.rest.resourceserver.project.Sw360ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Strings;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SW360ReportService {

@NonNull
private final Sw360ProjectService projectService;

@NonNull
private final Sw360LicenseService licenseService;

@NonNull
private final Sw360ComponentService componentService;

@NonNull
private final Sw360AttachmentService attachmentService;

@NonNull
private final Sw360LicenseInfoService licenseInfoService;

ThriftClients thriftClients = new ThriftClients();
ProjectService.Iface projectclient = thriftClients.makeProjectClient();
ComponentService.Iface componentclient = thriftClients.makeComponentClient();
Expand Down Expand Up @@ -129,4 +178,99 @@ public ByteBuffer getLicenseReportStreamFromURl(String token)
public void sendComponentExportSpreadsheetSuccessMail(String emailURL, String email) throws TException {
componentclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}

public ByteBuffer getLicenseInfoBuffer(User sw360User, String id, String generatorClassName, String variant, String template, String externalIds) throws TException {
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<>();

getSelectedAttchIdsAndExcludedLicInfo(sw360User, mappedProjectLinks, releaseIdToExcludedLicenses,
usedAttachmentContentIds, selectedReleaseAndAttachmentIds, excludedLicensesPerAttachments);

String outputGeneratorClassNameWithVariant = generatorClassName + "::" + variant;
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);
return licenseInfoFile.bufferForGeneratedOutput();
}

private void getSelectedAttchIdsAndExcludedLicInfo(User sw360User, List<ProjectLink> mappedProjectLinks,
Map<Source, Set<String>> releaseIdToExcludedLicenses, Map<String, Boolean> usedAttachmentContentIds,
final Map<String, Map<String, Boolean>> selectedReleaseAndAttachmentIds,
final Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachments) {
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);
}
}
})));
}

public String getGenericLicInfoFileName(HttpServletRequest request, User sw360User) throws TException {
final String variant = request.getParameter("variant");
final Project sw360Project = projectService.getProjectForUserById(request.getParameter("projectId"), sw360User);
final String generatorClassName = request.getParameter("generatorClassName");
final String timestamp = SW360Utils.getCreatedOnTime().replaceAll("\\s", "_").replace(":", "_");
final OutputFormatInfo outputFormatInfo = licenseInfoService
.getOutputFormatInfoForGeneratorClass(generatorClassName);
return String.format("%s-%s%s-%s.%s",
Strings.nullToEmpty(variant).equals("DISCLOSURE") ? "LicenseInfo" : "ProjectClearingReport",
sw360Project.getName(),
StringUtils.isBlank(sw360Project.getVersion()) ? "" : "-" + sw360Project.getVersion(), timestamp,
outputFormatInfo.getFileExtension());
}

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());
}
}
Loading

0 comments on commit b7fa1bb

Please sign in to comment.