1 package com.instantbank.lettertemplate.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.lettertemplate.control.util.WebKeys;
21 import com.instantbank.lettertemplate.control.util.JSPUtil;
22 import com.instantbank.lettertemplate.control.LetterTemplateEventException;
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.LetterTemplateExceptionMessage;
28
29
35 public class MainServlet extends HttpServlet {
36
37 private Debug debug = null;
38
39
40
44 public void init() {
45 debug = new Debug();
46 debug.setDebugginOn(true);
47 debug.setPreMessage("** MainServlet: ");
48 debug.println("MainServlet: Initializing");
49
50
51
52 getScreenFlowManager();
53 getRequestProcessor();
54
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.lettertemplate.control.web.ModelManager");
135 }
136 catch(Exception exc) {
137 throw new ServletException
138 (LetterTemplateExceptionMessage.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(LetterTemplateEventException ex) {
153 debug.println("caught LetterTemplateEventException 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(LetterTemplateEventException ex) {
169 debug.println("caught LetterTemplateEventException 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("======= LETTERS 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
312 String pUserName = request.getRemoteUser();
313 String userName;
314 String companyNumber;
315 if(pUserName.lastIndexOf(new String(":")) != -1) {
316 int pUnderScore = pUserName.lastIndexOf(new String(":"));
317 userName = pUserName.substring(0, pUnderScore);
318 companyNumber = pUserName.substring(pUnderScore + 1, pUserName.length());
319 }
320 else {
321 userName = pUserName;
322 companyNumber = "";
323 }
324 session.setAttribute(WebKeys.UserName, userName);
325 session.setAttribute(WebKeys.CompanyNumber, companyNumber);
326
327
353 }
354
355
356
365 private boolean controlTimeout
366 (HttpServletRequest request, HttpServletResponse response)
367 throws IOException, ServletException {
368 boolean existsTimeout = false;
369 HttpSession session = request.getSession();
370
371 debug.println("Session is new ?=" + session.isNew());
372
373 if(session.isNew()) {
374
375
376
377
378 String strCompanySessionTime
379 = request.getParameter("companySessionTime");
380 if((strCompanySessionTime != null)
381 && (!strCompanySessionTime.equals(""))) {
382
383
384 Long companySessionTime
385 = Long.valueOf(request.getParameter("companySessionTime"));
386 if(companySessionTime.intValue() != 0) {
387
388 session.setMaxInactiveInterval((companySessionTime.intValue()) * 60);
389 }
390 else {
391 session.setMaxInactiveInterval(-1);
392 }
393 }
394 else {
395
396 debug.println("timeout in lettertemplate ear");
397 getServletConfig().getServletContext()
398 .getRequestDispatcher("/control_web/TimeOutPage.jsp")
399 .forward(request, response);
400
401 existsTimeout = true;
402
403
404 }
405 }
406 return existsTimeout;
407 }
408
409
410
418 private void captureEnvironmentVariables(HttpServletRequest request) {
419 HttpSession session = request.getSession();
420
421
422 String externalDebugMode = CommonUtil.getApplicationProperty
423 (JNDINames.EXTERNAL_DEBUG_MODE);
424 session.setAttribute(WebKeys.ExternalDebugMode, externalDebugMode);
425
426
427 String imagesTemporalPath = CommonUtil.getApplicationProperty
428 (JNDINames.IMAGES_TEMPORAL_PATH);
429 session.setAttribute(WebKeys.ImagesTemporalPath, imagesTemporalPath);
430
431
432 String rtf2foConfigPath = CommonUtil.getApplicationProperty
433 (JNDINames.RTF2FO_CONFIG_PATH);
434 session.setAttribute
435 (WebKeys.RtfToFoConfigPath, rtf2foConfigPath);
436
437
438 String WLInitialContextFactory = CommonUtil.getApplicationProperty
439 (JNDINames.WLInitialContextFactory);
440 session.setAttribute
441 (WebKeys.WLInitialContextFactory, WLInitialContextFactory);
442
443
444 String ZipLettersFileMaxSize = CommonUtil.getApplicationProperty
445 (JNDINames.ZipLettersFileMaxSize);
446 session.setAttribute
447 (WebKeys.ZipLettersFileMaxSize, ZipLettersFileMaxSize);
448
449
450
451 }
452 }
453
454