1 package com.instantbank.servicing.control.web;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.ServletContext;
5 import javax.servlet.RequestDispatcher;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpSession;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import java.io.PrintWriter;
12 import java.io.StringWriter;
13 import java.io.OutputStreamWriter;
14 import java.io.IOException;
15
16 import java.beans.Beans;
17 import java.util.HashMap;
18 import java.util.Locale;
19
20 import com.instantbank.servicing.control.util.WebKeys;
21 import com.instantbank.servicing.control.util.JSPUtil;
22 import com.instantbank.servicing.control.ServicingEventException;
23
24 import com.instantbank.common.utilcomponents.Debug;
25 import com.instantbank.common.utilcomponents.CommonUtil;
26 import com.instantbank.common.utilcomponents.JNDINames;
27 import com.instantbank.common.utilcomponents.ServicingExceptionMessage;
28
29
30
36 public class MainServlet extends HttpServlet {
37
38 private Debug debug = null;
39
40
41
45 public void init() {
46 debug = new Debug();
47 debug.setDebugginOn(true);
48 debug.setPreMessage("** MainServlet: ");
49 debug.println("MainServlet: Initializing");
50
51
52
53 getScreenFlowManager();
54 getRequestProcessor();
55 }
56
57
58
66 public void doPost
67 (HttpServletRequest request, HttpServletResponse response)
68 throws IOException, ServletException {
69 doGet(request, response);
70 }
71
72
73
81 public void doGet
82 (HttpServletRequest request, HttpServletResponse response)
83 throws IOException, ServletException {
84
85 HttpSession session = request.getSession();
86 debuggingMainServlet(request, response);
87
88 if(controlTimeout(request, response)) {
89 debug.println("invalidate session and return by timeout");
90 session.invalidate();
91 return;
92
93
94 }
95
96 if(request.getRequestURI().endsWith("passport")) {
97
98
99
100 String url
101 = "/Instantbank/main_web/MainDispatcher?externalRequest=true&"
102 + request.getQueryString();
103
104 response.sendRedirect(url);
105 return;
106 }
107
108
109
110 String useCase = request.getParameter("useCase");
111 if(useCase != null) {
112
113 captureCollectionsVariables(request);
114 }
115
116 if(session.isNew()) {
117
118 captureEnvironmentVariables(request);
119 }
120
121 String selectedURL = request.getPathInfo();
122 if(selectedURL.equals("/white")) {
123 return;
124 }
125
126 ScreenFlowManager screenManager = null;
127 ModelManager modelManager = (ModelManager)request.getSession()
128 .getAttribute(WebKeys.ModelManagerKey);
129 if(modelManager == null) {
130 try {
131 modelManager =
132 (ModelManager)Beans.instantiate
133 (this.getClass().getClassLoader(),
134 "com.instantbank.servicing.control.web.ModelManager");
135 }
136 catch(Exception exc) {
137 throw new ServletException
138 (ServicingExceptionMessage.NOT_MODEL_MANAGER + exc.getMessage());
139 }
140 session.setAttribute(WebKeys.ModelManagerKey, modelManager);
141 modelManager.init(getServletContext(), session);
142 }
143
144 Object answer = null;
145 try {
146
147
148 answer = getRequestProcessor().processRequest(request);
149 debug.println("request has been processed");
150
151 }
152 catch(ServicingEventException ex) {
153 debug.println("caught ServicingEventException A :" + ex.toString());
154
155 request.setAttribute(WebKeys.ExceptionOcurred, ex.getMessage());
156 StringWriter sw = new StringWriter();
157 PrintWriter pw = new PrintWriter(sw);
158 ex.printStackTrace(pw);
159 request.setAttribute(WebKeys.ExceptionStackTrace, sw.toString());
160 }
161
162 try {
163
164 getScreenFlowManager().getNextScreen
165 (request, answer, getServletContext());
166
167 }
168 catch(ServicingEventException ex) {
169 debug.println("caught ServicingEventException B:" + ex.toString());
170
171 request.setAttribute(WebKeys.ExceptionOcurred, ex.getMessage());
172 StringWriter sw = new StringWriter();
173 PrintWriter pw = new PrintWriter(sw);
174 ex.printStackTrace(pw);
175 request.setAttribute(WebKeys.ExceptionStackTrace, sw.toString());
176 session.setAttribute(WebKeys.CurrentScreen, "ERROR");
177 }
178
179
180
181 Locale locale =
182 (Locale)request.getSession().getAttribute(WebKeys.LanguageKey);
183 if(locale == null) {
184 locale = Locale.US;
185 }
186
187
188
189
190
191 getServletConfig().getServletContext()
192 .getRequestDispatcher(getScreenFlowManager().getTemplate(locale))
193 .forward(request, response);
194 }
195
196
197
203 private RequestProcessor getRequestProcessor() {
204 RequestProcessor rp = (RequestProcessor)getServletContext()
205 .getAttribute(WebKeys.RequestProcessorKey);
206 if(rp == null) {
207 debug.println("initializing request processor");
208 rp = new RequestProcessor();
209 rp.init(getServletContext());
210 getServletContext().setAttribute
211 (WebKeys.RequestProcessorKey, rp);
212 }
213 return rp;
214 }
215
216
217
223 private ScreenFlowManager getScreenFlowManager() {
224 ScreenFlowManager screenManager =
225 (ScreenFlowManager)getServletContext()
226 .getAttribute(WebKeys.ScreenManagerKey);
227 if(screenManager == null) {
228 debug.println
229 ("Loading screen flow definitions");
230 screenManager = new ScreenFlowManager();
231 screenManager.init(getServletContext());
232 getServletContext().setAttribute
233 (WebKeys.ScreenManagerKey, screenManager);
234 }
235 return screenManager;
236 }
237
238
239
245 private void debuggingMainServlet(HttpServletRequest request,
246 HttpServletResponse response) {
247 String info = null;
248 debug.println("===== SERVICING NEW REQUEST ======");
249
257 info = request.getRequestURI();
258 debug.println("RequestURI is:" + (info == null ? "null" : info));
259 info = request.getQueryString();
260 debug.println("QueryString is:" + (info == null ? "null" : info));
261
267 }
268
269
270
275 private void captureCollectionsVariables(HttpServletRequest request) {
276 HttpSession session = request.getSession();
277
278 String companyName = request.getParameter("companyName");
279 String companyId = request.getParameter("companyId");
280 String version = request.getParameter("version");
281 String optionCollections = request.getParameter("optionCollections");
282 String optionCompany = request.getParameter("optionCompany");
283 String hasException = request.getParameter("hasException");
284 String strUserid = request.getParameter("userId");
285 String useCase = request.getParameter("useCase");
286
287
298 session.setAttribute(WebKeys.CompanyName, companyName);
299 session.setAttribute(WebKeys.CompanyId, companyId);
300 session.setAttribute(WebKeys.Version, version);
301 session.setAttribute(WebKeys.OptionCollections, optionCollections);
302 session.setAttribute(WebKeys.OptionCompany, optionCompany);
303 session.setAttribute(WebKeys.HasException, hasException);
304 session.setAttribute(WebKeys.UseCase, useCase);
305
306 Long userId = null;
307 userId = Long.valueOf(request.getParameter("userId"));
308 session.setAttribute(WebKeys.UserId, userId);
309
310
311 String pUserName = session.getAttribute("_wl_authuser_").toString();
312 String userName;
313 String companyNumber;
314 if(pUserName.lastIndexOf(new String(":")) != -1) {
315 int pUnderScore = pUserName.lastIndexOf(new String(":"));
316 userName = pUserName.substring(0, pUnderScore);
317 companyNumber = pUserName.substring(pUnderScore + 1, pUserName.length());
318 }
319 else {
320 userName = pUserName;
321 companyNumber = "";
322 }
323 session.setAttribute(WebKeys.UserName, userName);
324 session.setAttribute(WebKeys.CompanyNumber, companyNumber);
325
326
352 }
353
354
355
364 private boolean controlTimeout
365 (HttpServletRequest request, HttpServletResponse response)
366 throws IOException, ServletException {
367 boolean existsTimeout = false;
368 HttpSession session = request.getSession();
369
370 debug.println("Session is new ?=" + session.isNew());
371
372 if(session.isNew()) {
373
374
375
376
377 String strCompanySessionTime
378 = request.getParameter("companySessionTime");
379 if((strCompanySessionTime != null)
380 && (!strCompanySessionTime.equals(""))) {
381
382
383
384
385 Long companySessionTime
386 = Long.valueOf(request.getParameter("companySessionTime"));
387 if(companySessionTime.intValue() != 0) {
388
389 session.setMaxInactiveInterval((companySessionTime.intValue()) * 60);
390 }
391 else {
392 session.setMaxInactiveInterval(-1);
393 }
394 }
395 else {
396
397 debug.println("timeout in servicing ear");
398 getServletConfig().getServletContext()
399 .getRequestDispatcher("/control_web/TimeOutPage.jsp")
400 .forward(request, response);
401
402 existsTimeout = true;
403
404
405 }
406 }
407 return existsTimeout;
408 }
409
410
411
419 private void captureEnvironmentVariables(HttpServletRequest request) {
420 HttpSession session = request.getSession();
421
422
423 String externalDebugMode = CommonUtil.getApplicationProperty
424 (JNDINames.EXTERNAL_DEBUG_MODE);
425 session.setAttribute(WebKeys.ExternalDebugMode, externalDebugMode);
426 }
427 }
428
429