Skip to content

Commit 5f421d2

Browse files
authored
Merge pull request #2260 from siemens/feat/ReadConfigFileForDownloadReport
feat(rest) : Remove mail-request parameter and read from config file while downloading reports. Reviewed by: mishra.gaurav@siemens.com Tested by: kumar.nikesh@siemens.com
2 parents c0150b3 + 4bfabe4 commit 5f421d2

File tree

9 files changed

+79
-167
lines changed

9 files changed

+79
-167
lines changed

backend/src-common/src/main/resources/sw360.properties

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ textForRejectedClearingRequest= your clearing request with id: %s for the projec
124124
enable.sw360.change.log=false
125125
sw360changelog.output.path=sw360changelog/sw360changelog
126126
auto.set.ecc.status=false
127-
send.project.spreadsheet.export.to.mail.enabled=false
128-
send.component.spreadsheet.export.to.mail.enabled=false
127+
128+
##This property is used to enable mail request for projects/components report.
129+
#send.project.spreadsheet.export.to.mail.enabled=false
130+
#send.component.spreadsheet.export.to.mail.enabled=false
129131

130132
## This property is used to enable the bulk release deleting feature.
131133
#bulk.release.deleting.enabled=true

libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/SW360Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public class SW360Constants {
123123
public static final String SRC_ATTACHMENT_UPLOADER_EMAIL;
124124
public static final String SRC_ATTACHMENT_DOWNLOAD_LOCATION;
125125
public static final String PREFERRED_CLEARING_DATE_LIMIT;
126+
public static final Boolean MAIL_REQUEST_FOR_PROJECT_REPORT;
127+
public static final Boolean MAIL_REQUEST_FOR_COMPONENT_REPORT;
126128

127129
/**
128130
* Hashmap containing the name field for each type.
@@ -228,6 +230,8 @@ private SW360Constants() {
228230
SRC_ATTACHMENT_UPLOADER_EMAIL = props.getProperty("source.code.attachment.uploader.email", "");
229231
SRC_ATTACHMENT_DOWNLOAD_LOCATION = props.getProperty("src.attachment.download.location", "");
230232
PREFERRED_CLEARING_DATE_LIMIT = props.getProperty("preferred.clearing.date.limit","");
233+
MAIL_REQUEST_FOR_PROJECT_REPORT = Boolean.parseBoolean(props.getProperty("send.project.spreadsheet.export.to.mail.enabled", "false"));
234+
MAIL_REQUEST_FOR_COMPONENT_REPORT = Boolean.parseBoolean(props.getProperty("send.component.spreadsheet.export.to.mail.enabled", "false"));
231235
}
232236

233237
private static Map.Entry<String, String> pair(TFieldIdEnum field, String displayName){

rest/resource-server/src/docs/asciidoc/components.adoc

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -560,20 +560,3 @@ include::{snippets}/should_document_get_component_report/curl-request.adoc[]
560560

561561
===== Example response
562562
include::{snippets}/should_document_get_component_report/http-response.adoc[]
563-
564-
[[resources-components-download-report-mail_req]]
565-
==== Downloading component report with mail request
566-
567-
A `GET` request help to download the components report with mail request.
568-
569-
===== Request parameter
570-
include::{snippets}/should_document_get_component_report_with_mail_req/request-parameters.adoc[]
571-
572-
===== Response structure
573-
include::{snippets}/should_document_get_component_report_with_mail_req/response-fields.adoc[]
574-
575-
===== Example request
576-
include::{snippets}/should_document_get_component_report_with_mail_req/curl-request.adoc[]
577-
578-
===== Example response
579-
include::{snippets}/should_document_get_component_report_with_mail_req/http-response.adoc[]

rest/resource-server/src/docs/asciidoc/projects.adoc

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -851,28 +851,11 @@ include::{snippets}/should_document_create_summary_administration/http-response.
851851
[[resources-projects-download-report]]
852852
==== Downloading project report
853853

854-
A `GET` request help to download the projects report with mail request false.
855-
856-
===== Request parameter
857-
include::{snippets}/should_document_get_project_report_without_mail_req/request-parameters.adoc[]
858-
859-
===== Example request
860-
include::{snippets}/should_document_get_project_report_without_mail_req/curl-request.adoc[]
861-
862-
===== Example response
863-
include::{snippets}/should_document_get_project_report_without_mail_req/http-response.adoc[]
864-
865-
[[resources-projects-download-report-mail-request]]
866-
==== Downloading project report with mail request
867-
868-
A `GET` request help to download the projects report with mail request true.
854+
A `GET` request help to download the projects report.
869855

870856
===== Request parameter
871857
include::{snippets}/should_document_get_project_report/request-parameters.adoc[]
872858

873-
===== Response structure
874-
include::{snippets}/should_document_get_project_report/response-fields.adoc[]
875-
876859
===== Example request
877860
include::{snippets}/should_document_get_project_report/curl-request.adoc[]
878861

@@ -932,29 +915,11 @@ include::{snippets}/should_document_update_project_with_network/http-response.ad
932915
[[resources-projects-download-licenseclearing-report]]
933916
==== Downloading project license clearing report
934917

935-
A `GET` request help to download the projects report with mail request false.
936-
937-
===== Request parameter
938-
include::{snippets}/should_document_get_project_licenseclearing_spreadsheet_without_mail_req/request-parameters.adoc[]
939-
940-
===== Example request
941-
include::{snippets}/should_document_get_project_licenseclearing_spreadsheet_without_mail_req/curl-request.adoc[]
942-
943-
===== Example response
944-
include::{snippets}/should_document_get_project_licenseclearing_spreadsheet_without_mail_req/http-response.adoc[]
945-
946-
947-
[[resources-projects-download-licenseclearing-report-mail-request]]
948-
==== Downloading project license clearing report with mail request
949-
950-
A `GET` request help to download the projects report with mail request true.
918+
A `GET` request help to download project license clearing report.
951919

952920
===== Request parameter
953921
include::{snippets}/should_document_get_project_licenseclearing_spreadsheet/request-parameters.adoc[]
954922

955-
===== Response structure
956-
include::{snippets}/should_document_get_project_licenseclearing_spreadsheet/response-fields.adoc[]
957-
958923
===== Example request
959924
include::{snippets}/should_document_get_project_licenseclearing_spreadsheet/curl-request.adoc[]
960925

@@ -977,3 +942,4 @@ include::{snippets}/should_document_create_clearing_request/curl-request.adoc[]
977942

978943
===== Example response
979944
include::{snippets}/should_document_create_clearing_request/http-response.adoc[]
945+

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/report/SW360ReportController.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.ByteBuffer;
1212
import java.util.Arrays;
1313
import java.util.List;
14+
import java.util.Properties;
1415

1516
import javax.servlet.http.HttpServletRequest;
1617
import javax.servlet.http.HttpServletResponse;
@@ -22,6 +23,8 @@
2223
import io.swagger.v3.oas.annotations.responses.ApiResponse;
2324
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
2425
import org.apache.thrift.TException;
26+
import org.eclipse.sw360.datahandler.common.CommonUtils;
27+
import org.eclipse.sw360.datahandler.common.SW360Constants;
2528
import org.eclipse.sw360.datahandler.common.SW360Utils;
2629
import org.eclipse.sw360.datahandler.thrift.users.User;
2730
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
@@ -79,8 +82,6 @@ public void getProjectReport(
7982
@RequestParam(value = "withlinkedreleases", required = false, defaultValue = "false") boolean withLinkedReleases,
8083
@Parameter(description = "Report download format.", schema = @Schema(allowableValues = {"xls", "xlsx"}))
8184
@RequestParam(value = "mimetype", required = false, defaultValue = "xlsx") String mimeType,
82-
@Parameter(description = "Downloading project report required mail link.")
83-
@RequestParam(value = "mailrequest", required = false, defaultValue = "false") boolean mailRequest,
8485
@Parameter(description = "Project id.")
8586
@RequestParam(value = "projectId", required = false) String projectId,
8687
@Parameter(description = "Module name.", schema = @Schema(allowableValues = {PROJECTS, COMPONENTS}))
@@ -93,17 +94,19 @@ public void getProjectReport(
9394
try {
9495
if (validateMimeType(mimeType)) {
9596
switch (module) {
96-
case PROJECTS:
97-
getProjectReports(withLinkedReleases, mailRequest, response, request, sw360User, module, projectId);
98-
break;
99-
case COMPONENTS:
100-
getComponentsReports(withLinkedReleases, mailRequest, response, request, sw360User, module);
101-
break;
102-
case LICENSES:
103-
getLicensesReports(response, sw360User, module);
104-
break;
105-
default:
106-
break;
97+
case PROJECTS:
98+
getProjectReports(withLinkedReleases, SW360Constants.MAIL_REQUEST_FOR_PROJECT_REPORT, response,
99+
request, sw360User, module, projectId);
100+
break;
101+
case COMPONENTS:
102+
getComponentsReports(withLinkedReleases, SW360Constants.MAIL_REQUEST_FOR_COMPONENT_REPORT, response,
103+
request, sw360User, module);
104+
break;
105+
case LICENSES:
106+
getLicensesReports(response, sw360User, module);
107+
break;
108+
default:
109+
break;
107110
}
108111
} else {
109112
throw new TException("Error : Mimetype either should be : xls/xlsx");
@@ -119,7 +122,7 @@ private void getProjectReports(boolean withLinkedReleases, boolean mailRequest,
119122
if (mailRequest) {
120123
sw360ReportService.getUploadedProjectPath(sw360User, withLinkedReleases,getBaseUrl(request), projectId);
121124
JsonObject responseJson = new JsonObject();
122-
responseJson.addProperty("response", "Project report download link will get send to the end user.");
125+
responseJson.addProperty("response", "The downloaded report link will be send to the end user.");
123126
response.getWriter().write(responseJson.toString());
124127
} else {
125128
downloadExcelReport(withLinkedReleases, response, sw360User, module, projectId);
@@ -210,6 +213,7 @@ private boolean validateMimeType(String mimeType) {
210213
)
211214
@GetMapping(value = REPORTS_URL + "/download")
212215
public void downloadExcel(
216+
HttpServletRequest request,
213217
HttpServletResponse response,
214218
@Parameter(description = "Module name.", schema = @Schema(allowableValues = {PROJECTS, COMPONENTS}))
215219
@RequestParam(value = "module", required = true) String module,
@@ -218,9 +222,7 @@ public void downloadExcel(
218222
@Parameter(description = "Extended by releases.")
219223
@RequestParam(value = "extendedByReleases", required = false, defaultValue = "false") boolean extendedByReleases
220224
) throws TException {
221-
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
222-
User user = restControllerHelper.getUserByEmail(sw360User.getEmail());
223-
String fileConstant = module+"-%s.xlsx";
225+
final User user = restControllerHelper.getUserByEmail(request.getParameter("user"));
224226
try {
225227
ByteBuffer buffer = null;
226228
switch (module) {
@@ -239,9 +241,16 @@ public void downloadExcel(
239241
if (null == buffer) {
240242
throw new TException("No data available for the user " + user.getEmail());
241243
}
242-
String filename = String.format(fileConstant, SW360Utils.getCreatedOn());
244+
String fileName;
245+
if(module.equals(LICENSES)) {
246+
fileName = String.format("licenses-%s.xlsx", SW360Utils.getCreatedOn());
247+
} else if(module.equals(PROJECTS)) {
248+
fileName = sw360ReportService.getDocumentName(user, request.getParameter("projectId"));
249+
}else {
250+
fileName = sw360ReportService.getDocumentName(user, null);
251+
}
243252
response.setContentType(CONTENT_TYPE);
244-
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
253+
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
245254
copyDataStreamToResponse(response, buffer);
246255
} catch (Exception e) {
247256
throw new TException(e.getMessage());

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/report/SW360ReportService.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,43 @@ public class SW360ReportService {
3333
LicenseService.Iface licenseClient = thriftClients.makeLicenseClient();
3434

3535
public ByteBuffer getProjectBuffer(User user, boolean extendedByReleases, String projectId) throws TException {
36+
if (projectId != null && validateProject(projectId, user)) {
37+
throw new TException("No project record found for the project Id : " + projectId);
38+
}
3639
return projectclient.getReportDataStream(user, extendedByReleases, projectId);
3740
}
3841

42+
private boolean validateProject(String projectId, User user) throws TException {
43+
boolean validProject = true;
44+
try {
45+
Project project = projectclient.getProjectById(projectId, user);
46+
if (project == null) {
47+
return false;
48+
}
49+
} catch (Exception e) {
50+
validProject = false;
51+
}
52+
return validProject;
53+
}
54+
3955
public String getDocumentName(User user, String projectId) throws TException {
40-
if (projectId != null) {
56+
if (projectId != null && !projectId.equalsIgnoreCase("null")) {
4157
Project project = projectclient.getProjectById(projectId, user);
4258
return String.format("project-%s-%s-%s.xlsx", project.getName(), project.getVersion(), SW360Utils.getCreatedOn());
4359
}
4460
return String.format("projects-%s.xlsx", SW360Utils.getCreatedOn());
4561
}
4662

47-
public void getUploadedProjectPath(User user, boolean withLinkedReleases, String base, String projectId){
63+
public void getUploadedProjectPath(User user, boolean withLinkedReleases, String base, String projectId)
64+
throws TException {
65+
if (projectId!=null && !validateProject(projectId, user)) {
66+
throw new TException("No project record found for the project Id : " + projectId);
67+
}
4868
Runnable asyncRunnable = () -> wrapTException(() -> {
4969
try {
5070
String projectPath = projectclient.getReportInEmail(user, withLinkedReleases, projectId);
5171
String backendURL = base + "api/reports/download?user=" + user.getEmail() + "&module=projects"
52-
+ "&extendedByReleases=" + withLinkedReleases + "&token=";
72+
+ "&extendedByReleases=" + withLinkedReleases + "&projectId=" + projectId + "&token=";
5373
URL emailURL = new URL(backendURL + projectPath);
5474
if (!CommonUtils.isNullEmptyOrWhitespace(projectPath)) {
5575
sendExportSpreadsheetSuccessMail(emailURL.toString(), user.getEmail());

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/security/ResourceServerConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void configure(HttpSecurity http) throws Exception {
9696
.antMatchers(HttpMethod.GET, "/health").permitAll()
9797
.antMatchers(HttpMethod.GET, "/info").hasAuthority("WRITE")
9898
.antMatchers(HttpMethod.GET, "/api").permitAll()
99+
.antMatchers(HttpMethod.GET, "/api/reports/download").permitAll()
99100
.antMatchers(HttpMethod.GET, "/api/**").hasAuthority("READ")
100101
.antMatchers(HttpMethod.POST, "/api/**").hasAuthority("WRITE")
101102
.antMatchers(HttpMethod.PUT, "/api/**").hasAuthority("WRITE")

rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ComponentSpecTest.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,39 +1355,15 @@ public void should_document_get_component_report() throws Exception{
13551355
.header("Authorization", "Bearer " + accessToken)
13561356
.param("withlinkedreleases", "true")
13571357
.param("mimetype", "xlsx")
1358-
.param("mailrequest", "false")
13591358
.param("module", "components")
13601359
.accept(MediaTypes.HAL_JSON))
13611360
.andExpect(status().isOk())
13621361
.andDo(this.documentationHandler.document(
13631362
requestParameters(
13641363
parameterWithName("withlinkedreleases").description("Projects with linked releases. Possible values are `<true|false>`"),
13651364
parameterWithName("mimetype").description("Projects download format. Possible values are `<xls|xlsx>`"),
1366-
parameterWithName("mailrequest").description("Downloading project report requirted mail link. Possible values are `<true|false>`"),
13671365
parameterWithName("module").description("module represent the project or component. Possible values are `<components|projects>`")
13681366
)));
13691367
}
13701368

1371-
@Test
1372-
public void should_document_get_component_report_with_mail_req() throws Exception{
1373-
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
1374-
mockMvc.perform(get("/api/reports")
1375-
.header("Authorization", "Bearer " + accessToken)
1376-
.param("withlinkedreleases", "true")
1377-
.param("mimetype", "xlsx")
1378-
.param("mailrequest", "true")
1379-
.param("module", "components")
1380-
.accept(MediaTypes.HAL_JSON))
1381-
.andExpect(status().isOk())
1382-
.andDo(this.documentationHandler.document(
1383-
requestParameters(
1384-
parameterWithName("withlinkedreleases").description("components with linked releases. Possible values are `<true|false>`"),
1385-
parameterWithName("mimetype").description("components download format. Possible values are `<xls|xlsx>`"),
1386-
parameterWithName("module").description("module represent the project or component. Possible values are `<components|projects>`"),
1387-
parameterWithName("mailrequest").description("Downloading components report requirted mail link. Possible values are `<true|false>`")
1388-
),responseFields(
1389-
subsectionWithPath("response").description("The response message displayed").optional()
1390-
)
1391-
));
1392-
}
13931369
}

0 commit comments

Comments
 (0)