001: package simpleorm.simplewebapp.context;
002:
003: import simpleorm.simplewebapp.core.WException;
004: import simpleorm.simplewebapp.core.WPage;
005:
006: import java.net.URLEncoder;
007: import java.io.OutputStream;
008: import java.util.List;
009:
010: import org.apache.commons.fileupload.FileItem;
011:
012: /**
013: * Peer (one to one) with WPage, Provides all the interfacing between WPage and the server.
014: * Attempts to unify the JSP Page context with the Servlet
015: * doGet|Post parameters.<p>
016: *
017: * It is always created, even if there is no page context at all (eg. if during unit tests) in which
018: * case its methods return dummy values.<p>
019: *
020: * PageContext is an ugly class because it is JSP specific, and cannot
021: * be easily used from servlets.
022: * However, its methods are many and have a nasty relationship to
023: * Servlet ones.<p>
024: * <p/>
025: * For example, PageContext.getOut() returns a different, incompatible
026: * class to HttpServelet.getWriter().
027: * It is unclear what the relationship of other fields such as getAttribute
028: * really are.<p>
029: * <p/>
030: * This class also provides an opportunity to mock out the web server entriely
031: * and unit test just the WebBean.<p>
032: * <p/>
033: * Subtypes for JSP, Servlet, Test cases.
034: */
035: abstract public class WPageContext {
036: WPage page;
037:
038: protected WPageContext(WPage page) {
039: this .page = page;
040: }
041:
042: /** Ie. the get/post parameters to the webpage,
043: * or artificial parameters to test cases.
044: */
045: abstract public String getParameter(String key);
046:
047: /** Overriden to Redirects to a new page, typically after a successful CRUD form. */
048: public void redirect(String url) {
049: page.getLogger().info("Redirecting to " + url);
050: }
051:
052: /** Absolute URL of refering page to go back for CRUD etc. */
053: public String getRefererUrl() {
054: return "/";
055: }
056:
057: /** Add context and session id to urls iff appropriate.
058: * (Adds Web Context iff starts with "/", adds session IDs if cookieless sessions used. */
059: public String encodeContextUrl(String url) {
060: return addContextToUrl(url);
061: }
062:
063: /** Adds context to localUrl if stars with a "/" */
064: String addContextToUrl(String url) {
065: if (url.startsWith("/")) {
066: return (getContextPath() + url);
067: } else
068: return url;
069: }
070:
071: /** Web context, ie text after the host name. Overriden. */
072: String getContextPath() {
073: return "/dummyCtx";
074: }
075:
076: String getCharacterEncoding() {
077: return "UTF-8";
078: }
079:
080: /** returns url?name=value&name=value; encoding name and value.
081: * Called after encodeContextUrl. */
082: public String addParameterToUrl(String url, String name,
083: String value) {
084: StringBuffer urlBuf = new StringBuffer(url);
085: if (urlBuf.indexOf("?") == -1)
086: urlBuf.append("?");
087: else
088: urlBuf.append("&");
089:
090: String enc = getCharacterEncoding();
091: try {
092: urlBuf.append(URLEncoder.encode(name, enc));
093: urlBuf.append("=");
094: urlBuf.append(URLEncoder.encode(value, enc));
095: } catch (Exception ex) {
096: throw new WException(ex);
097: }
098: return urlBuf.toString();
099: }
100:
101: /** Overriden -- Ie. the username of the current logged in user. */
102: public String getRemoteUser() {
103: return null;
104: }
105:
106: /** Overrriden. */
107: public boolean isUserInRole(String role) {
108: return true;
109: }
110:
111: /**
112: * Dirty way to output to the JSP.
113: * Note that the form is not in a good state to receive output in onProcess().
114: * We keep the print variable hidden as it is inconsistent between JSP and Servlets.
115: */
116: public void println(String msg) {
117: page.getLogger().info(msg);
118: }
119:
120: public OutputStream openDownloadStream(String fileName)
121: throws Exception {
122: return null;
123: }
124:
125: public List<FileItem> parseUploadFileItems() throws Exception {
126: return null;
127: }
128:
129: }
|