| 1676 |
ankur.sing |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.support.services.RegisteredUsersGenerator;
|
| 1941 |
ankur.sing |
4 |
import in.shop2020.support.utils.ReportsUtils;
|
| 1676 |
ankur.sing |
5 |
|
|
|
6 |
import java.io.ByteArrayOutputStream;
|
|
|
7 |
import java.io.IOException;
|
|
|
8 |
|
| 1941 |
ankur.sing |
9 |
import javax.servlet.ServletContext;
|
| 1676 |
ankur.sing |
10 |
import javax.servlet.ServletOutputStream;
|
| 1941 |
ankur.sing |
11 |
import javax.servlet.http.HttpServletRequest;
|
|
|
12 |
import javax.servlet.http.HttpServletResponse;
|
|
|
13 |
import javax.servlet.http.HttpSession;
|
| 1676 |
ankur.sing |
14 |
|
| 1891 |
ankur.sing |
15 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
16 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 3936 |
chandransh |
17 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
18 |
import org.apache.struts2.convention.annotation.Results;
|
| 1941 |
ankur.sing |
19 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
20 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
21 |
import org.apache.struts2.util.ServletContextAware;
|
| 3105 |
chandransh |
22 |
import org.slf4j.Logger;
|
|
|
23 |
import org.slf4j.LoggerFactory;
|
| 1676 |
ankur.sing |
24 |
|
|
|
25 |
|
| 1891 |
ankur.sing |
26 |
@InterceptorRefs({
|
| 1941 |
ankur.sing |
27 |
@InterceptorRef("defaultStack"),
|
| 1891 |
ankur.sing |
28 |
@InterceptorRef("login")
|
|
|
29 |
})
|
| 3936 |
chandransh |
30 |
@Results({
|
|
|
31 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
32 |
})
|
| 1941 |
ankur.sing |
33 |
public class RegisteredUsersController implements ServletRequestAware, ServletResponseAware, ServletContextAware {
|
| 3105 |
chandransh |
34 |
|
|
|
35 |
private static Logger logger = LoggerFactory.getLogger(RegisteredUsersController.class);
|
| 1891 |
ankur.sing |
36 |
|
| 1941 |
ankur.sing |
37 |
private HttpServletRequest request;
|
|
|
38 |
private HttpSession session;
|
|
|
39 |
private HttpServletResponse response;
|
|
|
40 |
private ServletContext context;
|
|
|
41 |
|
| 1676 |
ankur.sing |
42 |
private final String authsuccess = "authsuccess";
|
|
|
43 |
private String message = "";
|
|
|
44 |
|
|
|
45 |
public RegisteredUsersController() {
|
|
|
46 |
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public String index() {
|
| 1941 |
ankur.sing |
50 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
| 3936 |
chandransh |
51 |
return "authfail";
|
| 1676 |
ankur.sing |
52 |
}
|
| 1891 |
ankur.sing |
53 |
return authsuccess;
|
| 1676 |
ankur.sing |
54 |
}
|
|
|
55 |
|
|
|
56 |
// Handles the POST request (Form Submission)
|
|
|
57 |
public String create() {
|
|
|
58 |
RegisteredUsersGenerator usersReportGenerator = new RegisteredUsersGenerator();
|
|
|
59 |
ByteArrayOutputStream baos = usersReportGenerator.generateRegisteredUsersReport();
|
|
|
60 |
|
|
|
61 |
if(baos == null) {
|
|
|
62 |
message = "No registered user exists.";;
|
|
|
63 |
return authsuccess;
|
|
|
64 |
}
|
|
|
65 |
// Preparing XLS file for output
|
| 1941 |
ankur.sing |
66 |
response.setContentType("application/vnd.ms-excel");
|
|
|
67 |
response.setHeader("Content-disposition", "inline; filename=registered-users" + ".xls");
|
| 1676 |
ankur.sing |
68 |
ServletOutputStream sos;
|
|
|
69 |
try {
|
| 1941 |
ankur.sing |
70 |
sos = response.getOutputStream();
|
| 1676 |
ankur.sing |
71 |
baos.writeTo(sos);
|
|
|
72 |
sos.flush();
|
|
|
73 |
} catch (IOException e) {
|
| 3105 |
chandransh |
74 |
logger.error("Error streaming registered users report");
|
| 1676 |
ankur.sing |
75 |
}
|
|
|
76 |
return authsuccess;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public String getMessage() {
|
|
|
80 |
return message;
|
|
|
81 |
}
|
| 1941 |
ankur.sing |
82 |
|
|
|
83 |
@Override
|
|
|
84 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
85 |
this.request = req;
|
|
|
86 |
this.session = req.getSession();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
@Override
|
|
|
90 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
91 |
this.response = res;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
@Override
|
|
|
95 |
public void setServletContext(ServletContext context) {
|
|
|
96 |
this.context = context;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public String getServletContextPath() {
|
|
|
100 |
return context.getContextPath();
|
|
|
101 |
}
|
| 1676 |
ankur.sing |
102 |
}
|