Skip to content

Commit

Permalink
feat(rest) : asynchronous end point for report download
Browse files Browse the repository at this point in the history
Signed-off-by: Keerthi B L <[email protected]>
  • Loading branch information
keerthi-bl committed Jul 19, 2023
1 parent 3822b90 commit 982765a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.server.RepresentationModelProcessor;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.JsonObject;
Expand All @@ -39,13 +38,13 @@

@BasePathAwareController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource>{
public class SW360ReportController implements RepresentationModelProcessor<RepositoryLinksResource> {
private static final String COMPONENTS = "components";

private static final String PROJECTS = "projects";
private static final String PROJECTS = "projects";

public static final String REPORTS_URL = "/reports";

public static final String REPORTS_URL = "/reports";

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

@NonNull
Expand All @@ -59,121 +58,95 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(SW360ReportController.class).slash("api/" + REPORTS_URL).withRel("reports"));
return resource;
}

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

@RequestMapping(value = REPORTS_URL , method = RequestMethod.GET)
public void getProjectReport(@RequestParam(value = "withlinkedreleases", required = false, defaultValue = "false") boolean withLinkedReleases,

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

@GetMapping(value = REPORTS_URL)
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,
@RequestParam(value = "module", required = true) String module,HttpServletRequest request,
HttpServletResponse response) throws TException{
@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)) {
switch (module) {
case PROJECTS :
getProjectReports(withLinkedReleases, mailRequest, response, request,sw360User,module);
if (validateMimeType(mimeType)) {
switch (module) {
case PROJECTS:
getProjectReports(withLinkedReleases, mailRequest, response, request, sw360User, module);
break;
case COMPONENTS :
getComponentsReports(withLinkedReleases,mailRequest, response, request, sw360User,module);

case COMPONENTS:
getComponentsReports(withLinkedReleases, mailRequest, response, request, sw360User, module);
break;
default:
break;
}
} else {
throw new TException("Error : Mimetype either should be : xls/xlsx");
}
}catch (Exception e) {
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void getProjectReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,
HttpServletRequest request, User sw360User, String module) throws TException {
try {
if (mailRequest) {
sw360ReportService.getUploadedProjectPath(sw360User, withLinkedReleases,getBaseUrl(request));
JsonObject responseJson = new JsonObject();
responseJson.addProperty("response", "Project report download link will get send to the end user.");
response.getWriter().write(responseJson.toString());
} else {
downloadExcelReport(withLinkedReleases, response, sw360User, module);
}
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

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 = 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());
}
private void getComponentsReports(boolean withLinkedReleases, boolean mailRequest, HttpServletResponse response,
HttpServletRequest request, User sw360User, String module) throws TException {
try {
if (mailRequest) {
sw360ReportService.getUploadedComponentPath(sw360User, withLinkedReleases, getBaseUrl(request));
JsonObject responseJson = new JsonObject();
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);
}
} 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 = 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());
}
response.setContentType(CONTENT_TYPE);
String filename = String.format("projects-%s.xlsx", SW360Utils.getCreatedOn());
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
copyDataStreamToResponse(response, buffer);
}catch(Exception e) {
throw new TException(e.getMessage());
}
} catch (Exception e) {
throw new TException(e.getMessage());
}
}

private void copyDataStreamToResponse(HttpServletResponse response, ByteBuffer buffer) throws IOException {
Expand All @@ -184,27 +157,28 @@ 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");
@GetMapping(value = REPORTS_URL + "/download")
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";
User user = restControllerHelper.getUserByEmail(sw360User.getEmail());
String fileConstant = "projects-%s.xlsx";
try {
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;
}
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));
Expand All @@ -213,4 +187,11 @@ public void downloadExcel(HttpServletRequest request,HttpServletResponse respons
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()) + "/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
*/
package org.eclipse.sw360.rest.resourceserver.report;

import static org.eclipse.sw360.datahandler.common.WrappedException.wrapTException;

import java.net.URL;
import java.nio.ByteBuffer;
import java.util.List;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectService;
Expand All @@ -20,41 +23,69 @@
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SW360ReportService {
ThriftClients thriftClients = new ThriftClients();

ThriftClients thriftClients = new ThriftClients();
ProjectService.Iface projectclient = thriftClients.makeProjectClient();
ComponentService.Iface componentclient = thriftClients.makeComponentClient();

public ByteBuffer getProjectBuffer(User user, boolean extendedByReleases)
throws TException {
public ByteBuffer getProjectBuffer(User user, boolean extendedByReleases) throws TException {
return projectclient.getReportDataStream(user, extendedByReleases);
}

public String getUploadedProjectPath(User user, boolean extendedByReleases) throws TException{
return projectclient.getReportInEmail(user, extendedByReleases);
public void getUploadedProjectPath(User user, boolean withLinkedReleases, String base){
Runnable asyncRunnable = () -> wrapTException(() -> {
try {
String projectPath = projectclient.getReportInEmail(user, withLinkedReleases);
String backendURL = base + "api/reports/download?user=" + user.getEmail() + "&module=projects"
+ "&extendedByReleases=" + withLinkedReleases + "&token=";
URL emailURL = new URL(backendURL + projectPath);
if (!CommonUtils.isNullEmptyOrWhitespace(projectPath)) {
sendExportSpreadsheetSuccessMail(emailURL.toString(), user.getEmail());
}
} catch (Exception exp) {
throw new TException(exp.getMessage());
}
});
Thread asyncThread = new Thread(asyncRunnable);
asyncThread.start();
}

public ByteBuffer getReportStreamFromURl(User user, boolean extendedByReleases, String token) throws TException {
return projectclient.downloadExcel(user, extendedByReleases, token);
}

public void sendExportSpreadsheetSuccessMail(String emailURL, String email) throws TException {
projectclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}

public void getUploadedComponentPath(User sw360User, boolean withLinkedReleases, String base) {
Runnable asyncRunnable = () -> wrapTException(() -> {
try {
String componentPath = componentclient.getComponentReportInEmail(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)) {
sendComponentExportSpreadsheetSuccessMail(emailURL.toString(), sw360User.getEmail());
}
} catch (Exception exp) {
throw new TException(exp.getMessage());
}
});
Thread asyncThread = new Thread(asyncRunnable);
asyncThread.start();
}

public ByteBuffer getReportStreamFromURl(User user,boolean extendedByReleases, String token)
throws TException{
return projectclient.downloadExcel(user,extendedByReleases, token);

public ByteBuffer getComponentBuffer(User sw360User, boolean withLinkedReleases) throws TException {
return componentclient.getComponentReportDataStream(sw360User, withLinkedReleases);
}
public void sendExportSpreadsheetSuccessMail(String emailURL, String email) throws TException{
projectclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}

public String getUploadedComponentPath(User sw360User, boolean withLinkedReleases) throws TException{
return componentclient.getComponentReportInEmail(sw360User, withLinkedReleases);
}
public ByteBuffer getComponentReportStreamFromURl(User user, boolean extendedByReleases, String token)
throws TException {
return componentclient.downloadExcel(user, extendedByReleases, token);
}

public ByteBuffer getComponentBuffer(User sw360User, boolean withLinkedReleases) throws TException{
return componentclient.getComponentReportDataStream(sw360User, withLinkedReleases);
}

public ByteBuffer getComponentReportStreamFromURl(User user,boolean extendedByReleases, String token)
throws TException{
return componentclient.downloadExcel(user,extendedByReleases, token);
public void sendComponentExportSpreadsheetSuccessMail(String emailURL, String email) throws TException {
componentclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}
public void sendComponentExportSpreadsheetSuccessMail(String emailURL, String email) throws TException{
componentclient.sendExportSpreadsheetSuccessMail(emailURL, email);
}
}

0 comments on commit 982765a

Please sign in to comment.