| 1676 |
ankur.sing |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.support.services.RegisteredUsersGenerator;
|
|
|
4 |
import in.shop2020.thrift.clients.HelperServiceClient;
|
|
|
5 |
import in.shop2020.utils.StatisticsUser;
|
|
|
6 |
|
|
|
7 |
import java.io.ByteArrayOutputStream;
|
|
|
8 |
import java.io.IOException;
|
|
|
9 |
|
|
|
10 |
import javax.servlet.ServletOutputStream;
|
|
|
11 |
import javax.servlet.http.HttpServletRequest;
|
|
|
12 |
import javax.servlet.http.HttpServletResponse;
|
|
|
13 |
import javax.servlet.http.HttpSession;
|
|
|
14 |
|
|
|
15 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
16 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
17 |
|
|
|
18 |
public class RegisteredUsersController implements ServletResponseAware, ServletRequestAware {
|
|
|
19 |
|
|
|
20 |
private HttpServletRequest request;
|
|
|
21 |
private HttpServletResponse response;
|
|
|
22 |
private HttpSession session;
|
|
|
23 |
|
|
|
24 |
private final String authsuccess = "authsuccess";
|
|
|
25 |
private final String authfail = "authfail";
|
|
|
26 |
private String message = "";
|
|
|
27 |
|
|
|
28 |
public RegisteredUsersController() {
|
|
|
29 |
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
@Override
|
|
|
33 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
34 |
this.request = req;
|
|
|
35 |
this.session = req.getSession();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
@Override
|
|
|
39 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
40 |
this.response = res;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public String index() {
|
|
|
44 |
if (getSessionUserName() == null) {
|
|
|
45 |
return authfail;
|
|
|
46 |
} else {
|
|
|
47 |
return authsuccess;
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// Handles the POST request (Form Submission)
|
|
|
52 |
public String create() {
|
|
|
53 |
String username = request.getParameter("username");
|
|
|
54 |
String password = request.getParameter("password");
|
|
|
55 |
if(username != null && password != null) {
|
|
|
56 |
try{
|
|
|
57 |
HelperServiceClient hsc = new HelperServiceClient();
|
|
|
58 |
in.shop2020.utils.HelperService.Client client = hsc.getClient();
|
|
|
59 |
StatisticsUser user = client.authenticateStatisticsUser(username, password);
|
|
|
60 |
session.setAttribute("username", user.getUsername());
|
|
|
61 |
return authsuccess;
|
|
|
62 |
}catch(Exception e){
|
|
|
63 |
e.printStackTrace();
|
|
|
64 |
return authfail;
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
if (getSessionUserName() == null) {
|
|
|
68 |
return authfail;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
RegisteredUsersGenerator usersReportGenerator = new RegisteredUsersGenerator();
|
|
|
73 |
ByteArrayOutputStream baos = usersReportGenerator.generateRegisteredUsersReport();
|
|
|
74 |
|
|
|
75 |
if(baos == null) {
|
|
|
76 |
message = "No registered user exists.";;
|
|
|
77 |
return authsuccess;
|
|
|
78 |
}
|
|
|
79 |
// Preparing XLS file for output
|
|
|
80 |
response.setContentType("application/vnd.ms-excel");
|
|
|
81 |
response.setHeader("Content-disposition", "inline; filename=registered-users" + ".xls");
|
|
|
82 |
ServletOutputStream sos;
|
|
|
83 |
try {
|
|
|
84 |
sos = response.getOutputStream();
|
|
|
85 |
baos.writeTo(sos);
|
|
|
86 |
sos.flush();
|
|
|
87 |
} catch (IOException e) {
|
|
|
88 |
e.printStackTrace();
|
|
|
89 |
}
|
|
|
90 |
return authsuccess;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public String getSessionUserName() {
|
|
|
94 |
return (String) session.getAttribute("username");
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public String getMessage() {
|
|
|
98 |
return message;
|
|
|
99 |
}
|
|
|
100 |
}
|