001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.servlets;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/servlets/AppServer.java $
024: //$Author: Dan $
025: //$Revision: 11 $
026: //$Modtime: 8/24/04 4:20p $
027: /////////////////////////
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.util.*;
032:
033: import javax.servlet.ServletConfig;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.*;
036:
037: import com.salmonllc.html.HtmlPage;
038: import com.salmonllc.html.HtmlPageBase;
039: import com.salmonllc.jsp.JspServlet;
040: import com.salmonllc.properties.Props;
041: import com.salmonllc.util.MessageLog;
042: import com.salmonllc.util.URLGenerator;
043: import com.salmonllc.personalization.Skin;
044:
045: /**
046: * This servlet is the entry point for all servlet based pages in the framework. JSP Pages do not use it.<BR>
047: * It will handle get and post calls from the browser and direct them to the appropriate com.salmonllc.html.HtmlPage class<BR>
048: * The page will be located through additional path info appended to this servlets URL.<BR>
049: * Example: http://hostname/webapp/AppServer/app1/Page1<BR>
050: * will load the page Page1 for the application app1.<BR>
051: * <BR>
052: * The page should be a subclass of com.salmonllc.html.HtmlPageBase and should reside in a package named with the following convention:<BR>
053: * [basepackage].applicationname<BR>
054: * where [basepackage] is the value of the ApplicationPackage property in the properties file and applicationname is the name of the application specified in the URL.<BR>
055: * Example: If the system properties file has the token: ApplicationPackage=com.mycompany.apps and the URL specified is: http://hostname/com.salmonllc.servlets.AppServer/app1/Page1 the class: com.mycompany.apps.app1.Page1 would be loaded.<BR><BR>
056: *<BR>
057: * If the class does not exist or isn't a subclass of com.salmonllc.html.HtmlPageBase the page manager will return an error to the browser.
058: * dan
059: */
060:
061: public class AppServer extends HttpServlet {
062: int _count = 0;
063: boolean _schedulerStarted = false;
064: boolean _doDumps = false;
065:
066: /**
067: * This method handles get events from the browser.
068: */
069: public void doGet(HttpServletRequest req, HttpServletResponse res)
070: throws ServletException, IOException {
071: JspServlet.setUpApplicationContext(getServletContext(), req);
072: MessageLog.writeInfoMessage("doGet(): Path="
073: + req.getPathInfo(), this );
074: if (_doDumps)
075: dumpHeaders(req);
076:
077: //if (!_schedulerStarted) {
078: //String path = req.getPathInfo();
079: //_schedulerStarted = true;
080:
081: //String redirect = URLGenerator.generateServerURL(req) + "/" + URLGenerator.generateServletBaseURL(req) + "/com.salmonllc.servlets.Scheduler?ret=" + path;
082: //PrintWriter pw = res.getWriter();
083: //res.setStatus(res.SC_OK);
084: //res.sendRedirect(redirect);
085: //pw.close();
086: //return;
087: //}
088:
089: long time = System.currentTimeMillis();
090: res.setHeader("Pragma", "no-cache");
091: res.setDateHeader("Expires", 0);
092: res.setHeader("Cache-Control", "no-cache");
093:
094: res.setStatus(HttpServletResponse.SC_OK);
095:
096: PrintWriter pw = res.getWriter();
097: try {
098: HtmlPageBase p = getPage(req, res);
099: if (p != null) {
100: synchronized (p) {
101: if (p.getSubPageName() == null) {
102: if (p.getContentType() == null)
103: res.setContentType("text/html");
104: else
105: res.setContentType(p.getContentType());
106: } else if (p instanceof HtmlPage) {
107: String type = ((HtmlPage) p)
108: .getSubPageMimeType(p.getSubPageName());
109: if (type == null)
110: res.setContentType("text/html");
111: else
112: res.setContentType(type);
113: } else {
114: if (p.getContentType() == null)
115: res.setContentType("text/html");
116: else
117: res.setContentType(p.getContentType());
118: }
119: if (req.getParameterValues("iFrameSubmitFormParms") != null)
120: p.processParms(true);
121: p.setCurrentRequest(req);
122: p.setCurrentResponse(res);
123: p.generateHTML(pw);
124:
125: }
126: } else {
127: if (!res.isCommitted()) {
128: res.setContentType("text/html");
129: pw.println("<HTML><HEAD></HEAD><BODY>");
130: pw.println("Error: Page not found");
131: pw.println("</BODY></HTML>");
132: }
133: }
134: } catch (Exception e) {
135: HtmlPageBase.displayError("Error in get to url", e, pw);
136: MessageLog.writeErrorMessage("Error in get to url:"
137: + req.getPathInfo(), e, this );
138: }
139: //pw.flush();
140: //pw.close();
141: com.salmonllc.jsp.JspServlet.addPageHit(System
142: .currentTimeMillis()
143: - time);
144: }
145:
146: /**
147: * This method handles post events from the browser.
148: */
149: public void doPost(HttpServletRequest req, HttpServletResponse res)
150: throws ServletException, IOException {
151: long time = System.currentTimeMillis();
152: JspServlet.setUpApplicationContext(getServletContext(), req);
153: MessageLog.writeInfoMessage("doPost(): Path="
154: + req.getPathInfo(), this );
155: if (_doDumps)
156: dumpHeaders(req);
157:
158: res.setContentType("text/html");
159: res.setHeader("Pragma", "no-cache");
160: res.setDateHeader("Expires", 0);
161: res.setHeader("Cache-Control", "no-cache");
162:
163: res.setStatus(HttpServletResponse.SC_OK);
164:
165: res.setContentType("text/html");
166: PrintWriter pw = res.getWriter();
167: try {
168: HtmlPageBase p = getPage(req, res);
169: if (p != null) {
170: synchronized (p) {
171: p.setCurrentRequest(req);
172: p.setCurrentResponse(res);
173: p.processParms(false);
174: if (!p.getHistoryFixUp()) {
175: //p.generateHTML(pw);
176: String url = p.getPageName();
177: if (p.getSubPageName() != null)
178: url += "-" + p.getSubPageName();
179: String path = req.getPathInfo();
180: if (path != null) {
181: int ndx = path.lastIndexOf("/" + url + "/");
182: if (ndx > 0 && !path.endsWith(url)) {
183: ndx += url.length() + 2;
184: int pos = ndx + url.length() + 2;
185: url = "../" + url + "/"
186: + path.substring(ndx);
187: while ((pos = path.indexOf("/", pos)) != -1) {
188: url = "../" + url;
189: pos = pos + 1;
190: }
191:
192: }
193: }
194: String queryString = req.getQueryString();
195: if (queryString != null) {
196: int index = queryString
197: .indexOf("pagePostSerialID=");
198: if (index > -1) {
199: if (index > 0)
200: index--;
201: queryString = queryString.substring(0,
202: index);
203: if (queryString.equals(""))
204: queryString = null;
205: }
206:
207: if (queryString != null) {
208: index = queryString
209: .indexOf("iFrameSubmitFormParms=true");
210: if (index > -1) {
211: if (index > 0)
212: index--;
213: queryString = queryString
214: .substring(0, index);
215: if (queryString.equals(""))
216: queryString = null;
217: }
218: }
219: }
220:
221: if (queryString != null) {
222: url += "?" + queryString;
223: url += "&pagePostSerialID=" + _count++;
224: } else
225: url += "?pagePostSerialID=" + _count++;
226:
227: //pw.println("<HTML><HEAD></HEAD><BODY>");
228: //pw.println("<SCRIPT>");
229: //pw.println(" location.replace('" + url + "');");
230: //pw.println("</SCRIPT>");
231: //pw.println("</BODY></HTML>");
232:
233: com.salmonllc.html.HttpServletResponseWrapper w = (com.salmonllc.html.HttpServletResponseWrapper) p
234: .getCurrentResponse();
235: if (!w.getRedirectSent()
236: && !p.getDisableRedirect())
237: w.sendRedirect(url, false);
238: else
239: p.generateHTML(pw);
240: } else {
241: pw.println("<HTML><HEAD></HEAD><BODY>");
242: pw.println("<SCRIPT>");
243: pw.println(" window.history.go(-1);");
244: pw.println("</SCRIPT>");
245: pw.println("</BODY></HTML>");
246: }
247: }
248: } else
249: pw.println("Error: Page not found");
250: } catch (Exception e) {
251: HtmlPageBase.displayError("Error in post to url", e, pw);
252: MessageLog.writeErrorMessage("Error in post to url:"
253: + req.getPathInfo(), e, this );
254: }
255:
256: pw.close();
257: com.salmonllc.jsp.JspServlet.addPageHit(System
258: .currentTimeMillis()
259: - time);
260:
261: }
262:
263: private void dumpHeaders(HttpServletRequest req) {
264:
265: StringBuffer out = new StringBuffer();
266: out.append("\n---------------------------------\n");
267: out.append(req.getMethod() + " " + req.getPathInfo());
268: out.append('\n');
269: out.append("-----------Headers---------------");
270: out.append('\n');
271:
272: Enumeration e = req.getHeaderNames();
273: while (e.hasMoreElements()) {
274: String s = (String) e.nextElement();
275: out.append(s + "=" + req.getHeader(s));
276: out.append('\n');
277: }
278:
279: out.append("------------Parms-----------------");
280: out.append('\n');
281: e = req.getParameterNames();
282: while (e.hasMoreElements()) {
283: String s = (String) e.nextElement();
284: out.append(s + "=" + req.getParameter(s));
285: out.append('\n');
286: }
287: out.append("-------------End------------------");
288: MessageLog.writeDebugMessage(out.toString(), this );
289:
290: }
291:
292: private HtmlPageBase getPage(HttpServletRequest req,
293: HttpServletResponse res) throws Exception {
294: String app = null;
295: String page = null;
296: String subPage = null;
297:
298: boolean sessExp = false;
299:
300: String sessID = req.getRequestedSessionId();
301: boolean sessValid = req.isRequestedSessionIdValid();
302:
303: if (sessID != null && !sessValid)
304: sessExp = true;
305:
306: String path = req.getPathInfo();
307: HttpSession sess = req.getSession(true);
308:
309: synchronized (sess) {
310: if (sess.getAttribute("AppServer_SessExp") != null) {
311: sessExp = true;
312: sess.removeAttribute("AppServer_SessExp");
313: }
314:
315: try {
316: StringTokenizer t = new StringTokenizer(path, "/");
317: app = t.nextToken();
318: page = t.nextToken();
319: } catch (Exception e) {
320: Props pr = Props.getSystemProps();
321: if (app == null) {
322: app = pr.getProperty(Props.SYS_DEFAULT_APP);
323: }
324:
325: if (app == null)
326: return null;
327:
328: pr = Props.getProps(app, null);
329: page = pr.getProperty(Props.SYS_DEFAULT_PAGE);
330:
331: if (page == null)
332: page = pr.getProperty(app + "."
333: + Props.SYS_DEFAULT_PAGE);
334:
335: if (page == null)
336: return null;
337:
338: if (sessExp)
339: sess.setAttribute("AppServer_SessExp", "Yes");
340:
341: PrintWriter pw = res.getWriter();
342: res.setStatus(HttpServletResponse.SC_OK);
343: res.sendRedirect(URLGenerator.generateServerURL(req)
344: + "/"
345: + URLGenerator.generateServletBaseURL(req)
346: + "/com.salmonllc.servlets.AppServer/" + app
347: + "/" + page);
348: pw.close();
349: return null;
350: }
351:
352: int pos = page.indexOf("-");
353: if (pos > -1) {
354: subPage = page.substring(pos + 1);
355: page = page.substring(0, pos);
356: }
357:
358: Props p = Props.getProps(app, page);
359: String cName = p.getProperty(Props.SYS_APP_PACKAGE);
360: String cName2 = cName;
361: if (cName == null) {
362: cName = app + "." + page;
363: cName2 = page;
364: } else {
365: cName += "." + app + "." + page;
366: cName2 += "." + page;
367: }
368:
369: HtmlPageBase ret = null;
370: Object o = null;
371: String sesName = null;
372:
373: if (cName.endsWith("genPgNdx")) {
374: sesName = "$page$"
375: + cName.substring(0, cName.length() - 8);
376: o = sess.getAttribute(sesName);
377: cName = cName.substring(0, cName.lastIndexOf("_"));
378: cName2 = cName2.substring(0, cName2.lastIndexOf("_"));
379: } else if (cName.endsWith("_")) {
380: cName = cName.substring(0, cName.length() - 1);
381: int i = 0;
382: while (true) {
383: sesName = "$page$" + cName + "_" + i;
384: if (sess.getAttribute(sesName) == null) {
385: String redirect = req.getServletPath() + "/"
386: + app + "/" + page + i + "genPgNdx";
387: if (req.getQueryString() != null)
388: redirect += "?" + req.getQueryString();
389: res.sendRedirect(redirect);
390: return null;
391: }
392: ;
393: i++;
394: }
395: } else {
396: sesName = "$page$" + cName;
397: o = sess.getAttribute(sesName);
398: }
399:
400: boolean init = false;
401:
402: if (o != null)
403: ret = (HtmlPageBase) o;
404: else {
405: Class c = null;
406:
407: try {
408: c = Class.forName(cName);
409: } catch (LinkageError e) {
410: try {
411: c = Class.forName(cName2);
412: } catch (ClassNotFoundException eX) {
413: MessageLog.writeErrorMessage(
414: "Couldn't find page class:" + cName
415: + " or " + cName2, eX, this );
416: return null;
417: } catch (LinkageError eX) {
418: MessageLog.writeErrorMessage(
419: "Couldn't find page class:" + cName
420: + " or " + cName2, eX, this );
421: return null;
422: }
423: } catch (ClassNotFoundException e) {
424: try {
425: c = Class.forName(cName2);
426: } catch (ClassNotFoundException eX) {
427: MessageLog.writeErrorMessage(
428: "Couldn't find page class:" + cName
429: + " or " + cName2, eX, this );
430: return null;
431: }
432: } catch (Exception e) {
433: MessageLog.writeErrorMessage(
434: "Execption getting page:" + cName, e, this );
435: throw (e);
436: }
437:
438: Object h = sess
439: .getAttribute(HtmlPageBase.APPLICATION_ALIAS_SESSION_KEY);
440: String appAlias = null;
441: if (h != null)
442: appAlias = (String) ((Hashtable) h).get(app);
443:
444: ret = (HtmlPageBase) c.newInstance();
445: ret.setPageName(page);
446: ret.setOrigApplicationName(app);
447: if (appAlias != null)
448: ret.setApplicationName(appAlias);
449: else
450: ret.setApplicationName(app);
451: ret.setSession(sess);
452: Skin sk = ret.getSkin();
453: if (sk != null)
454: ret.applySkin(sk, false);
455: ret.loadProperties();
456: sess.setAttribute(sesName, ret);
457: init = true;
458: }
459:
460: ret.setSubPageName(subPage);
461: ret.setCurrentRequest(req);
462: ret.setCurrentResponse(res);
463: ret.setSession(sess);
464: ret.setSessionExpired(sessExp);
465: ret.setServerURL(URLGenerator.generateServerURL(req));
466: ret.setServletBaseURL(URLGenerator
467: .generateServletBaseURL(req));
468: ret.setUpLanguagePreferences();
469: if (init) {
470: ret.initialize();
471: if (ret instanceof HtmlPage)
472: ((HtmlPage) ret).setFormType();
473: }
474:
475: return ret;
476: }
477: }
478:
479: /**
480: * This method occurs when the servlet is initialized
481: */
482: public void init(ServletConfig s) throws ServletException {
483: Properties p = System.getProperties();
484: //p.put("server.root","D:\\IBMVJava3.5\\ide\\project_resources\\IBM Websphere Test Environment");
485: if (p.getProperty("server.root") == null) {
486: String serverRoot = s.getInitParameter("server.root");
487: if (serverRoot != null)
488: p.put("server.root", serverRoot);
489: }
490: super.init(s);
491: }
492: }
|