001: package simpleorm.simplewebapp.core;
002:
003: import simpleorm.simplewebapp.context.WPageContextServlet;
004: import simpleorm.simplewebapp.requestlet.WPropertiesRenderer;
005: import simpleorm.simplewebapp.requestlet.WXmlRenderer;
006: import simpleorm.simplewebapp.requestlet.WPageRenderer;
007:
008: import javax.servlet.http.HttpServlet;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011: import javax.servlet.ServletException;
012: import java.io.IOException;
013:
014: /**
015: * Parent of all servelets.
016: */
017: public class WServlet extends HttpServlet {
018:
019: public @Override
020: void doGet(HttpServletRequest req, HttpServletResponse res)
021: throws ServletException, IOException {
022: doGetPost(req, res);
023:
024: // System.err.println("WTestServelet DoGet uri " + req.getRequestURI()
025: // + " url " + req.getRequestURL()
026: // + " spath " + req.getServletPath() // NOT THIS ONE
027: // + " cpth " + req.getContextPath() + " pinfo " + req.getPathInfo()
028: // + " qstr " + req.getQueryString());
029: //
030: //
031: // String path = req.getRequestURI();
032: // String ctx = req.getContextPath();
033: // if ( path.startsWith(ctx) )
034: // path = path.substring(ctx.length());
035: // int end = path.lastIndexOf(".");
036: // //if (end >=0)
037: // // path = path.substring(0, end);
038: // System.err.println("==> " + path);
039:
040: // http://localhost:8080/plain_eg/foo/baz/bar.swb?xxx=yyy
041: // <url-pattern>/foo/*</url-pattern>
042: // WTestServelet DoGet uri /plain_eg/foo/baz/bar.swb
043: // url http://localhost:8080/plain_eg/foo/baz/bar.swb
044: // spath /foo cpth /plain_eg pinfo /baz/bar.swb qstr xxx=yyy
045: // ==> /foo/baz/bar
046:
047: }
048:
049: public @Override
050: void doPost(HttpServletRequest req, HttpServletResponse res)
051: throws ServletException, IOException {
052: doGetPost(req, res);
053: }
054:
055: private static class Parsed {
056: String ext, reqUrl;
057: WMenuItemGlobal mitem;
058: }
059:
060: void doGetPost(HttpServletRequest req, HttpServletResponse res)
061: throws ServletException, IOException {
062: WPage page = null;
063: try {
064: try {
065: Parsed parsed = parseUrl(req);
066:
067: page = parsed.mitem.newPage(); // Beware that a lot of initialization happens here
068:
069: WPageStructure struct = page.getPageStructure();
070: page.setPageContext(new WPageContextServlet(page, req,
071: res));
072:
073: req.setAttribute("wPage", page);
074:
075: if (!page.getPageSecurity().isAuthorized()) {
076: // todo Many issues, the buffer, Realms etc.
077: // Does not actually work in Tomcat.
078: // The 401 is sent to the browser, which prompts for a login, but Tomcat then refuses to use it to set the role.
079: // Only works iff protections specified via web.xml.
080: // Adding a "somebody" role that is always required at the web.xml level is a workarround.
081: // But I suspect that container level security is just useless in general.
082: res.setHeader("WWW-Authenticate",
083: "Basic realm=\"Agile UI: tim/Password1\"");
084: res.sendError(res.SC_UNAUTHORIZED,
085: "Cannot Access Page");
086: // The message in the second parameter is displayed to user on error page
087: return;
088: }
089:
090: struct.doMain();
091:
092: if (struct.getActualRedirectUrl() != null)
093: page.getPageContext().redirect(
094: struct.getActualRedirectUrl());
095: else if (!struct.isSuppressRender()) {
096: renderResult(parsed, page, req, res);
097: }
098: } finally {
099: if (page != null)
100: page.getPageStructure().doFinalize();
101: }
102: } catch (RuntimeException re) {
103: throw re;
104: } catch (Exception ex) {
105: throw new WException(ex);
106: }
107: }
108:
109: private Parsed parseUrl(HttpServletRequest req) {
110: WServlet.Parsed parsed = new WServlet.Parsed();
111: // if request was http://localhost:8080/plain_eg/swb/baz/bar.swb?xxx=yyy ...
112: // <url-pattern>/swb/*</url-pattern>
113: parsed.reqUrl = req.getRequestURL() + ""; // whole string, including http: but not ?xxx=yyy
114: String pinfo = req.getPathInfo(); // /baz/bar.swb -- ie. no context nor servlet prefix
115: if (pinfo == null || "/".equals(pinfo))
116: throw new WException("Bad Url " + parsed.reqUrl);
117: String menuName = pinfo.substring(1);
118: if (menuName.indexOf("/") >= 0)
119: menuName = menuName.substring(0, menuName.indexOf("/"));
120:
121: // Assumes exactly two level menus
122: WMenuParentGlobal menus = WGlobalState.getMenus();
123: WMenuParentGlobal menu = (WMenuParentGlobal) menus
124: .findSubMenuNotNull(menuName);
125:
126: int extx = pinfo.lastIndexOf(".");
127: if (extx < 0)
128: throw new WException("No Extension " + parsed.reqUrl);
129: String base = pinfo.substring(0, extx); // eg /Simple/manuallisttest.swb. Ie. includes menu name.
130: parsed.ext = pinfo.substring(extx + 1);
131:
132: parsed.mitem = menu.findBySwbUrlNotNull("/swb" + base + ".swb");
133: return parsed;
134: }
135:
136: private void renderResult(Parsed parsed, WPage page,
137: HttpServletRequest req, HttpServletResponse res)
138: throws InstantiationException, IllegalAccessException {
139: WPageRenderer pager = null;
140: if ("swb".equals(parsed.ext)) {
141: pager = page.getPageItem().getRenderer().newInstance(); // normally WJspRenderer();
142: } else if ("properties".equals(parsed.ext)) {
143: pager = new WPropertiesRenderer();
144: } else if ("xml".equals(parsed.ext)) {
145: pager = new WXmlRenderer();
146: } else
147: throw new WException("Unknown Extension " + parsed.reqUrl);
148:
149: pager.initRequestlet(req, res, this , true);
150: pager.setPage(page).doGetPost();
151: }
152:
153: }
|