-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerServiceImpl.java
More file actions
361 lines (281 loc) · 12 KB
/
ControllerServiceImpl.java
File metadata and controls
361 lines (281 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package Frameworkpackage;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ControllerServiceImpl extends CommonFunctions {
private static final int MegaBytes = 1024 * 1024;
public void serveRequest(HttpServletRequest request, HttpServletResponse response)
{
response.addHeader("Access-Control-Allow-Origin", "*");
Connection con = null;
response.addHeader("Access-Control-Allow-Origin", "*");
HashMap<String, Object> mapFromRequest = null;
String reqStartTime = null;
try {
Date startDatetime = new Date();
String action = request.getParameter("a") == null ? request.getParameter("actionName")
: request.getParameter("a");
action = action == null ? "showHomePage" : action;
if(action.contains("?"))
{
action = action.split("\\?")[0];
}
String logMessage="Action Flag receieved is " + action;
con = getConnectionJDBC();
con.setAutoCommit(false);
//logger.debug("Connection Opened Succesfully");
reqStartTime = getDateTime(con);
//logger.debug("Datetime From DB Received as" + reqStartTime);
//System.out.println(actions);
boolean isBypassed = lstbypassedActions.contains(action);
logMessage+=" and action "+"is ByPassed value :-" + isBypassed;
if ((action == null || action.equals("")) && request.getSession().getAttribute(username_constant) != null) {
logMessage+=" Will Redirect Back to Homepage";
response.sendRedirect("?a=showHomePage"); // No logged-in user found, so redirect to login page.
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0);
mapFromRequest = getMapfromRequest(request, reqStartTime, webPortal, con);
logger.debug(logMessage);
return;
}
request.getSession().setAttribute("projectName",CommonFunctions.projectName);
// send to login if session is null and the action is also not bypassed
if (!isBypassed && request.getSession().getAttribute(username_constant) == null) {
logMessage+="Session Found as Null and the action is also not bypassed";
String app_code="";
if(request.getSession().getAttribute("logoutappshortcode")!=null)
{
app_code=(String) request.getSession().getAttribute("logoutappshortcode");
}
else
{
app_code=request.getParameter("app_code");
}
response.sendRedirect("frameworkjsps/Login.jsp?app_code="+app_code); // No logged-in user found, so redirect to login page.
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
mapFromRequest = getMapfromRequest(request, reqStartTime, webPortal, con);
return;
}
// if the action is not bypassed then check if this action is mapped to list of
// actions available for this role
//HashSet<String> allowedActionsForThisRole = (HashSet<String>) request.getSession().getAttribute("actions");
String userId="0";
HashSet<String> allowedActionsForThisRole=null;
HashSet<Integer> allowedReportsForThisRole=null;
if(request.getSession().getAttribute("userdetails")!=null)
{
userId = ((HashMap<String, String>) request.getSession().getAttribute("userdetails")).get("user_id");
allowedActionsForThisRole= getActionsForthisUserDecoupled(Long.valueOf(userId), con,CommonFunctions.roles);
}
//logger.info("List of Allowed Actions for this role" + allowedActionsForThisRole);
if (allowedActionsForThisRole == null) {
allowedActionsForThisRole = new HashSet<String>();
}
if (isBypassed && action != null) {
allowedActionsForThisRole.add(action);
}
if (action != null && !allowedActionsForThisRole.contains(action) ) {
logMessage+="Redirecting to Unauthorized Page as Action is "+action ;
RequestDispatcher dispatcher = request.getRequestDispatcher("frameworkjsps/unAuthorized.jsp");
dispatcher.forward(request, response);
mapFromRequest = getMapfromRequest(request, reqStartTime, webPortal, con);
logger.debug(logMessage);
return;
}
String reportId=request.getParameter("report_id");
if (action != null && action.equals("showReport") && !lstbypassedReports.contains(reportId) && !allowedReportsForThisRole.contains(Integer.valueOf(reportId))) {
logger.debug("Redirecting to Unauthorized Page as Report is "+reportId + "Allowed Reports for this role is "+allowedReportsForThisRole);
RequestDispatcher dispatcher = request.getRequestDispatcher("frameworkjsps/unAuthorized.jsp");
dispatcher.forward(request, response);
mapFromRequest = getMapfromRequest(request, reqStartTime, webPortal, con);
return;
}
// code for authorization ends
//HashMap<String, String> classandmethodInfo = getClassNameAndMethodNameUsingJDBC(action, con);
mapFromRequest = getMapfromRequest(request, reqStartTime, webPortal, con);
FrmActionService frmAction= (FrmActionService)actions.get(action);
// added so that threads donot over lap with each other
if(request.getSession().getAttribute("userdetails")!=null)
{
logMessage+="session is not null";
HashMap<String, String> hm= (HashMap<String, String>) request.getSession().getAttribute("userdetails");
logMessage+="getting userdetails"+hm;
Integer threads_overlap=0;
if(hm.get("threads_overlap")!=null)
{
threads_overlap=Integer.parseInt(hm.get("threads_overlap").toString());
}
if(CommonFunctions.threadSleep>threads_overlap)
{
Thread.sleep(CommonFunctions.threadSleep * 1000);
}
else
{
Thread.sleep(threads_overlap* 1000);
}
}
Class<?>[] paramString = new Class[2];
paramString[0] = HttpServletRequest.class;
paramString[1] = Connection.class;
Class<?> cls = Class.forName(frmAction.getClassName());
Object obj = cls.newInstance();
//Thread.sleep(4000);
Method method = cls.getDeclaredMethod(frmAction.getActionName(), paramString);
CustomResultObject rs = null;
rs = (CustomResultObject) method.invoke(obj, request, con);
if (rs.getViewName() != null) {
List<String> roleIds =getRoleIds(Long.valueOf(userId), con);
logMessage+="Found a view Name so redirecting to " + rs.getViewName();
HashMap<String, Object> hm = rs.getReturnObject();
hm.put("contentJspName", rs.getViewName());
hm.put(username_constant, request.getSession().getAttribute(username_constant));
hm.put("elementsDB",getElementsNewLogic(roleIds,CommonFunctions.elements,CommonFunctions.roles));
request.setAttribute("outputObject", hm);
if (isBypassed) {
RequestDispatcher dispatcher = request.getRequestDispatcher("frameworkjsps/model.jsp");
dispatcher.forward(request, response);
} else if(rs.disablemodaljsp)
{
RequestDispatcher dispatcher = request.getRequestDispatcher(rs.getViewName());
HttpSession session = request.getSession();
Cookie sessionCookie = new Cookie("JSESSIONID", session.getId());
sessionCookie.setMaxAge(60*60*24); // 1 day
response.addCookie(sessionCookie);
dispatcher.forward(request, response);
}
else
{
RequestDispatcher dispatcher = request.getRequestDispatcher("frameworkjsps/model.jsp");
HttpSession session = request.getSession();
Cookie sessionCookie = new Cookie("JSESSIONID", session.getId());
sessionCookie.setMaxAge(60*60*24); // 1 day
response.addCookie(sessionCookie);
dispatcher.forward(request, response);
}
} else if (rs.getAjaxData() != null) // its ajax data
{
//logger.info("Ajax Call so returning Ajax content:- " + rs.getAjaxData());
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "*");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(rs.getAjaxData());
} else // its a file
{
logMessage+="Its not ajax nor a view name, Unless its a file download something is wrong";
String filepath = rs.getReturnObject().get(filename_constant).toString();
logMessage+="Found a File So returning " + "BufferedImagesFolder/" + filepath;
response.sendRedirect("BufferedImagesFolder/" + filepath);
}
con.commit();
Date EndTime = new Date();
logMessage+="Request End Time " + EndTime;
logMessage+="\nTime Taken For Request -- " + (EndTime.getTime() - startDatetime.getTime());
logger.debug(logMessage);
} catch (Exception e) {
try {
logger.error(e);
e.printStackTrace();
con.rollback();
mapFromRequest = getMapfromRequest(request, reqStartTime, webPortal, con);
RequestDispatcher dispatcher = request.getRequestDispatcher("frameworkjsps/errorPage.jsp");
dispatcher.forward(request, response);
}
catch(Exception m) {m.printStackTrace();}
}
finally
{
try
{
con.close();
makeAuditTrailEntry(mapFromRequest, reqStartTime, webPortal);
}
catch(Exception m)
{
m.printStackTrace();
}
}
}
public void makeAuditTrailEntryDelayed(final HashMap<String, Object> hmFromRequest, final String reqStartTime,
final String responseJson) throws SQLException, ClassNotFoundException {
if (!CommonFunctions.isAuditEnabled) {
return;
}
new Thread(new Runnable() {
public void run() {
try {
makeAuditTrailEntry(hmFromRequest, reqStartTime, responseJson);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
// TODO Auto-generated catch block
}
}
}).start();
}
public HashMap<String, Object> getMapfromRequest(HttpServletRequest request, String reqStartTime,
String responseJson, Connection con) throws ClassNotFoundException, SQLException {
StringBuffer requestURL = request.getRequestURL();
if (request.getQueryString() != null) {
requestURL.append("?").append(request.getQueryString());
}
String completeURL = requestURL.toString();
String Parameters = "";
String username = "";
for (Map.Entry<String, String[]> entry :
((Map<String, String[]>)request.getParameterMap()).entrySet()) {
String name = entry.getKey();
String value = entry.getValue()[0];
Parameters += "\n" + name + " " + value;
if (name.equals("user_id")) {
username = dao.getUserDetailsByUserId(value, con).get("username");
}
}
String ipAddress = request.getHeader("X-FORWARDED-FOR") == null ? request.getRemoteAddr()
: request.getHeader("X-FORWARDED-FOR");
String session_username = request.getSession().getAttribute(username_constant) == null ? ""
: request.getSession().getAttribute(username_constant).toString();
String browserInfo = request.getHeader("User-Agent");
HashMap<String, Object> hmDetails = new HashMap<>();
hmDetails.put("sessionUsername", session_username);
if (session_username.equals("")) {
hmDetails.put("sessionUsername", username);
}
hmDetails.put("completeURL", completeURL);
hmDetails.put("Parameters", Parameters);
hmDetails.put("reqStartTime", reqStartTime);
hmDetails.put("ipAddress", ipAddress);
hmDetails.put("browserInfo", browserInfo);
hmDetails.put("responseJson", responseJson);
return hmDetails;
}
public void makeAuditTrailEntry(HashMap<String, Object> hmDetails, String reqStartTime, String responseJson)
throws SQLException, ClassNotFoundException {
try
{
if (!CommonFunctions.isAuditEnabled) {
return;
}
Connection con = getConnectionJDBC();
dao.makeAuditTrailEntryDB(hmDetails, con);
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
ControllerDaoImpl dao = new ControllerDaoImpl();
}