KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > RequestContext


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.controller;
14
15 import java.util.Locale JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import javax.servlet.ServletContext JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.servlet.http.HttpSession JavaDoc;
22
23 import com.tonbeller.tbutils.res.Resources;
24 import com.tonbeller.wcf.convert.Converter;
25 import com.tonbeller.wcf.expr.ExprContext;
26 import com.tonbeller.wcf.format.Formatter;
27
28 /**
29  * Created on 05.11.2002
30  *
31  * @author av
32  */

33 public abstract class RequestContext implements ExprContext {
34   public abstract HttpServletRequest JavaDoc getRequest();
35   public abstract HttpServletResponse JavaDoc getResponse();
36   public abstract ServletContext JavaDoc getServletContext();
37   public abstract HttpSession JavaDoc getSession();
38
39   public abstract Converter getConverter();
40   public abstract Formatter getFormatter();
41
42   private static ThreadLocalStack instanceStack = new ThreadLocalStack();
43
44   private static final String JavaDoc RESPONSE_COMPLETE_KEY = RequestContext.class.getName() + ".responseComplete";
45
46   public abstract Map JavaDoc getFileParameters();
47
48   protected RequestContext() {
49   }
50   
51   /**
52    * returns the RequestContext associated with the current thread / request
53    * @throws EmptyThreadLocalStackException if there is no instance set via #setInstance()
54    * @see RequestContextFactoryFinder#createContext(HttpServletRequest, HttpServletResponse, boolean)
55    */

56   public static RequestContext instance() throws EmptyThreadLocalStackException {
57     return (RequestContext) instanceStack.peek(true);
58   }
59   
60   /**
61    * returns the RequestContext associated with the current thread / request
62    * @param
63    * @see RequestContextFactoryFinder#createContext(HttpServletRequest, HttpServletResponse, boolean)
64    */

65   public static RequestContext instance(boolean failIfEmpty) throws EmptyThreadLocalStackException {
66     return (RequestContext) instanceStack.peek(failIfEmpty);
67   }
68
69   /**
70    * pushes a thread local instance. The context
71    * will be returned by {@link #instance()}. This method is public for test purpose only.
72    */

73   public static void setInstance(RequestContext context) {
74     instanceStack.push(context);
75   }
76
77   /**
78    * invalidate context after request processing is complete. This must be called exactly
79    * as many times RequestContextFactoryFinder.createContext is called.
80    * <p />
81    * @see RequestContextFactoryFinder#createContext(HttpServletRequest, HttpServletResponse, boolean)
82    */

83   public void invalidate() {
84     instanceStack.pop();
85   }
86
87   /**
88    * returns the request parameters
89    * @return same as servlet 2.3 HttpServletRequest.getParameterMap()
90    */

91   public abstract Map JavaDoc getParameters();
92   public abstract String JavaDoc[] getParameters(String JavaDoc name);
93   public abstract String JavaDoc getParameter(String JavaDoc name);
94
95   /**
96    * the locale preferred by the user
97    */

98   public abstract Locale JavaDoc getLocale();
99
100   /**
101    * change the Locale for this session including
102    * the Locale for JSTL &lt;fmt:message &gt; tag
103    */

104   public abstract void setLocale(Locale JavaDoc locale);
105
106   public abstract Resources getResources();
107   public abstract Resources getResources(String JavaDoc bundleName);
108   public abstract Resources getResources(Class JavaDoc clasz);
109
110   /**
111    * evaluates a JSP EL expression. Example #{user.profile}
112    */

113   public abstract Object JavaDoc getModelReference(String JavaDoc expr);
114   public abstract void setModelReference(String JavaDoc expr, Object JavaDoc value);
115
116
117   /**
118    * evaluates a role expression.
119    * <p />
120    * If <code>roleExpr</code> is a JSP EL expression
121    * (e.g. ${bean.stringProperty}), then the result
122    * of the EL is used as the role name.
123    * <p />
124    * If <code>roleExpr</code> starts with "!" the result
125    * is inverted, e.g. all users except members of the
126    * role will be allowed to access the component.
127    * <p />
128    * Roles may be mapped in resources.properties (or in
129    * System.properties or user.properties - see
130    * {@link com.tonbeller.wcf.utils.Resources Resources}
131    * for details). The format is <code>role.[role1]=[role2]</code>
132    * (not including the brackets).
133    * If such an entry is present, role2 will be checked instead
134    * of role1.
135    * <p />
136    * If roleExpr == null, returns true (i.e. everybody is member
137    * of the null role).
138    */

139   public abstract boolean isUserInRole(String JavaDoc roleExpr);
140
141   /**
142    * returns the remote User Name without its domain name.
143    * In Oracle AS the HttpServletRequest.getRemoteUser() will
144    * return a String like "domain\\user". This function returns
145    * the name part only.
146    *
147    * @return name without domain
148    */

149   public abstract String JavaDoc getRemoteUser();
150   public abstract String JavaDoc getRemoteDomain();
151
152   /**
153    * allows derived classes to specify a root user who may do everything.
154    * The default implementation returns false always.
155    * @return
156    */

157   public abstract boolean isAdmin();
158
159   /**
160    * if a RequestListener has called setError() or sentRedirect() on
161    * the response it must inform the controller not to forward
162    * to the JSP
163    */

164   public void setResponseComplete(boolean complete) {
165     getRequest().setAttribute(RESPONSE_COMPLETE_KEY, Boolean.toString(complete));
166   }
167   public boolean isResponseComplete() {
168     return "true".equals(getRequest().getAttribute(RESPONSE_COMPLETE_KEY));
169   }
170 }
171
Popular Tags