Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rest) - Rest API for downloading components report. #1965

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@
import org.eclipse.sw360.datahandler.thrift.vulnerabilities.ReleaseVulnerabilityRelation;
import org.eclipse.sw360.datahandler.thrift.vulnerabilities.VulnerabilityCheckStatus;
import org.eclipse.sw360.datahandler.thrift.vulnerabilities.VulnerabilityService;
import org.eclipse.sw360.exporter.ComponentExporter;
import org.eclipse.sw360.mail.MailConstants;
import org.eclipse.sw360.mail.MailUtil;
import org.apache.logging.log4j.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.apache.logging.log4j.LogManager;
import org.apache.thrift.TException;
Expand All @@ -68,6 +70,7 @@
import java.io.InputStream;
import org.spdx.library.InvalidSPDXAnalysisException;
import java.net.MalformedURLException;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand Down Expand Up @@ -2844,4 +2847,43 @@ public Map<String, String> getReleasesName(String releases) {
}
return releaseNames;
}

public String getComponentReportInEmail(User user,boolean extendedByReleases) throws TException {
try {
List<Component> componentlist = getRecentComponentsSummary(-1, user);
ComponentExporter exporter = getComponentExporterObject(componentlist,user, extendedByReleases);
return exporter.makeExcelExportForProject(componentlist, user);
}catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}

private ComponentExporter getComponentExporterObject(List<Component> componentList ,User user,
boolean extendedByRelease) throws SW360Exception {
ThriftClients thriftClients = new ThriftClients();
return new ComponentExporter(thriftClients.makeComponentClient(), componentList, user,extendedByRelease);
}

public ByteBuffer downloadExcel(User user,boolean extendedByReleases,String token) throws SW360Exception {
try {
ThriftClients thriftClients = new ThriftClients();
ComponentExporter exporter = new ComponentExporter(thriftClients.makeComponentClient(), user,
extendedByReleases);
InputStream stream = exporter.downloadExcelSheet(token);
return ByteBuffer.wrap(IOUtils.toByteArray(stream));
} catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}

public ByteBuffer getComponentReportDataStream(User user, boolean extendedByReleases) throws TException{
try {
List<Component> componentlist = getRecentComponentsSummary(-1, user);
ComponentExporter exporter = getComponentExporterObject(componentlist, user, extendedByReleases);
InputStream stream = exporter.makeExcelExport(componentlist);
return ByteBuffer.wrap(IOUtils.toByteArray(stream));
}catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
*/
public class ProjectDatabaseHandler extends AttachmentAwareDatabaseHandler {

private static final String PROJECTS = "projects";
private static final Logger log = LogManager.getLogger(ProjectDatabaseHandler.class);
private static final int DELETION_SANITY_CHECK_THRESHOLD = 5;
private static final String DUMMY_NEW_PROJECT_ID = "newproject";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.cloudant.client.api.CloudantClient;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -717,4 +718,19 @@ public Map<PaginationData, List<Component>> getRecentComponentsSummaryWithPagina
public void sendExportSpreadsheetSuccessMail(String url, String recepient) throws TException {
handler.sendExportSpreadsheetSuccessMail(url, recepient);
}

@Override
public ByteBuffer downloadExcel(User user, boolean extendedByReleases, String token) throws TException {
return handler.downloadExcel(user,extendedByReleases,token);
}

@Override
public ByteBuffer getComponentReportDataStream(User user, boolean extendedByReleases) throws TException {
return handler.getComponentReportDataStream(user,extendedByReleases);
}

@Override
public String getComponentReportInEmail(User user, boolean extendedByReleases) throws TException {
return handler.getComponentReportInEmail(user,extendedByReleases);
}
}
12 changes: 12 additions & 0 deletions libraries/datahandler/src/main/thrift/components.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -911,4 +911,16 @@ service ComponentService {
* Send email to the user once spreadsheet export completed
*/
void sendExportSpreadsheetSuccessMail(1: string url, 2: string userEmail);
/*
* download api
*/
binary downloadExcel(1:User user,2:bool extendedByReleases,3:string token) throws (1: SW360Exception exp);
/*
* get report data stream
*/
binary getComponentReportDataStream(1: User user, 2: bool extendedByReleases) throws (1: SW360Exception exp);
/*
* get component report in mail
*/
string getComponentReportInEmail(1: User user, 2: bool extendedByReleases) throws (1: SW360Exception exp);
}
32 changes: 32 additions & 0 deletions rest/resource-server/src/docs/asciidoc/components.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ include::{snippets}/should_document_get_mycomponents_components/http-response.ad
===== Links
include::{snippets}/should_document_get_mycomponents_components/links.adoc[]

<<<<<<< HEAD
[[resources-component-get-component-vulnerabilities]]
==== Listing component vulnerabilities

Expand Down Expand Up @@ -509,3 +510,34 @@ include::{snippets}/should_document_import_sbom_for_component/curl-request.adoc[

===== Example response
include::{snippets}/should_document_import_sbom_for_component/http-response.adoc[]

[[resources-components-download-report]]
==== Downloading component report

A `GET` request help to download the components report.

===== Request parameter
include::{snippets}/should_document_get_component_report/request-parameters.adoc[]

===== Example request
include::{snippets}/should_document_get_component_report/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_get_component_report/http-response.adoc[]

[[resources-components-download-report-mail_req]]
==== Downloading component report with mail request

A `GET` request help to download the components report with mail request.

===== Request parameter
include::{snippets}/should_document_get_component_report_with_mail_req/request-parameters.adoc[]

===== Response structure
include::{snippets}/should_document_get_component_report_with_mail_req/response-fields.adoc[]

===== Example request
include::{snippets}/should_document_get_component_report_with_mail_req/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_get_component_report_with_mail_req/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@
@BasePathAwareController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource>{
private static final String COMPONENTS = "components";

public static final String REPORTS_URL = "/reports";
private static final String PROJECTS = "projects";

public static final String REPORTS_URL = "/reports";

private static String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

@NonNull
private final RestControllerHelper restControllerHelper;

@NonNull
private final SW360ReportService sw360ReportService;

Expand All @@ -59,49 +62,108 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {

private List<String> mimeTypeList = Arrays.asList("xls","xlsx");

@RequestMapping(value = REPORTS_URL + "/myprojectreports", method = RequestMethod.GET)
@RequestMapping(value = REPORTS_URL , method = RequestMethod.GET)
public void getProjectReport(@RequestParam(value = "withlinkedreleases", required = false, defaultValue = "false") boolean withLinkedReleases,
@RequestParam(value = "mimetype", required = false, defaultValue = "xlsx") String mimeType,
@RequestParam(value = "mailrequest", required = false, defaultValue="false") boolean mailRequest, HttpServletRequest request,
@RequestParam(value = "mailrequest", required = false, defaultValue="false") boolean mailRequest,
@RequestParam(value = "module", required = true) String module,HttpServletRequest request,
HttpServletResponse response) throws TException{

final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
try {
if(validateMimeType(mimeType)) {
if(mailRequest) {
StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String ctx = request.getContextPath();
String base = url.substring(0, url.length() - uri.length() + ctx.length()) + "/";

String projectPath = sw360ReportService.getUploadedProjectPath(sw360User, withLinkedReleases);

String backendURL = base + "api/reports/download?user="+sw360User.getEmail()+"&extendedByReleases="+withLinkedReleases+"&token=";
URL emailURL = new URL(backendURL+projectPath);
switch (module) {
case PROJECTS :
getProjectReports(withLinkedReleases, mailRequest, response, request,sw360User,module);
break;

if(!CommonUtils.isNullEmptyOrWhitespace(projectPath)) {
sw360ReportService.sendExportSpreadsheetSuccessMail(emailURL.toString(), sw360User.getEmail());
}
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "E-mail sent succesfully to the end user.");
responseJson.addProperty("url", emailURL.toString());
responseJson.toString();
response.getWriter().write(responseJson.toString());
}else {
downloadExcelReport(withLinkedReleases, response, sw360User);
case COMPONENTS :
getComponentsReports(withLinkedReleases,mailRequest, response, request, sw360User,module);
break;
default:
break;
}
}else {
} else {
throw new TException("Error : Mimetype either should be : xls/xlsx");
}
}catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse response,
User user) throws TException, IOException {
private void getProjectReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,HttpServletRequest request,
User sw360User, String module) throws TException{
try {
if(mailRequest) {
String base = getBaseUrl(request);

String projectPath = sw360ReportService.getUploadedProjectPath(sw360User, withLinkedReleases);

String backendURL = base + "api/reports/download?user="+sw360User.getEmail() + "&module=projects" +"&extendedByReleases="+withLinkedReleases+"&token=";
URL emailURL = new URL(backendURL+projectPath);

if(!CommonUtils.isNullEmptyOrWhitespace(projectPath)) {
sw360ReportService.sendExportSpreadsheetSuccessMail(emailURL.toString(), sw360User.getEmail());
}
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "E-mail sent succesfully to the end user.");
responseJson.addProperty("url", emailURL.toString());
responseJson.toString();
response.getWriter().write(responseJson.toString());
}else {
downloadExcelReport(withLinkedReleases, response, sw360User,module);
}
}catch (Exception e) {
throw new TException(e.getMessage());
}
}

private String getBaseUrl(HttpServletRequest request) {
StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String ctx = request.getContextPath();
return url.substring(0, url.length() - uri.length() + ctx.length()) + "/";
}

private void getComponentsReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,
HttpServletRequest request, User sw360User, String module) throws TException{
try {
if (mailRequest) {
String base = getBaseUrl(request);
String componentPath = sw360ReportService.getUploadedComponentPath(sw360User,withLinkedReleases);
String backendURL =base + "api/reports/download?user=" + sw360User.getEmail()+ "&module=components" + "&extendedByReleases=" + withLinkedReleases + "&token=";
URL emailURL = new URL(backendURL+componentPath);

if (!CommonUtils.isNullEmptyOrWhitespace(componentPath)) {
sw360ReportService.sendComponentExportSpreadsheetSuccessMail(emailURL.toString(), sw360User.getEmail());
}
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "E-mail sent succesfully to the end user.");
responseJson.addProperty("url", emailURL.toString());
responseJson.toString();
response.getWriter().write(responseJson.toString());
} else {
downloadExcelReport(withLinkedReleases, response, sw360User,module);
}
}catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void downloadExcelReport(boolean withLinkedReleases, HttpServletResponse response,
User user,String module) throws TException, IOException {
try {
ByteBuffer buffer = sw360ReportService.getProjectBuffer(user,withLinkedReleases);
ByteBuffer buffer = null;
switch (module) {
case PROJECTS:
buffer = sw360ReportService.getProjectBuffer(user,withLinkedReleases);
break;
case COMPONENTS:
buffer = sw360ReportService.getComponentBuffer(user,withLinkedReleases);
break;
default:
break;
}
if(null==buffer) {
throw new TException("No data available for the user "+ user.getEmail());
}
Expand All @@ -121,22 +183,34 @@ private void copyDataStreamToResponse(HttpServletResponse response, ByteBuffer b
private boolean validateMimeType(String mimeType) {
return mimeTypeList.contains(mimeType);
}

@RequestMapping(value = REPORTS_URL + "/download", method = RequestMethod.GET)
public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws TException{
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
String module = request.getParameter("module");
String token = request.getParameter("token");
String extendedByReleases = request.getParameter("extendedByReleases");
User user=restControllerHelper.getUserByEmail(sw360User.getEmail());
String fileConstant="projects-%s.xlsx";
try {
ByteBuffer buffer = sw360ReportService.getReportStreamFromURl(user,Boolean.valueOf(extendedByReleases), token);
String filename = String.format("projects-%s.xlsx", SW360Utils.getCreatedOn());
ByteBuffer buffer = null;
switch (module) {
case PROJECTS:
buffer = sw360ReportService.getReportStreamFromURl(user,Boolean.valueOf(extendedByReleases), token);
break;
case COMPONENTS:
fileConstant="components-%s.xlsx";
buffer = sw360ReportService.getComponentReportStreamFromURl(user, Boolean.valueOf(extendedByReleases), token);
break;
default:
break;
}
String filename = String.format(fileConstant, SW360Utils.getCreatedOn());
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
copyDataStreamToResponse(response, buffer);
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

}
}
Loading