KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.MissingResourceException JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import javax.faces.context.FacesContext;
21 import javax.faces.el.ValueBinding;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpSession JavaDoc;
26
27 import org.apache.log4j.Logger;
28
29 import com.tonbeller.tbutils.res.Resources;
30 import com.tonbeller.wcf.convert.Converter;
31 import com.tonbeller.wcf.expr.ExprContext;
32 import com.tonbeller.wcf.expr.ExprUtils;
33 import com.tonbeller.wcf.format.Formatter;
34 /**
35  * @author av
36  */

37 public class RequestContextImpl extends RequestContext implements ExprContext {
38   protected RequestContextFactory rcf;
39   protected HttpServletRequest JavaDoc request;
40   protected HttpServletResponse JavaDoc response;
41   protected static Logger logger = Logger.getLogger(RequestContextImpl.class);
42
43   public RequestContextImpl(RequestContextFactory rcf, HttpServletRequest JavaDoc request,
44       HttpServletResponse JavaDoc response) {
45     this.rcf = rcf;
46     this.request = request;
47     this.response = response;
48   }
49
50   public void invalidate() {
51     super.invalidate();
52     this.rcf = null;
53     this.request = null;
54     this.response = null;
55   }
56
57   public HttpServletRequest JavaDoc getRequest() {
58     return request;
59   }
60
61   public HttpServletResponse JavaDoc getResponse() {
62     return response;
63   }
64
65   public ServletContext JavaDoc getServletContext() {
66     return getSession().getServletContext();
67   }
68
69   public HttpSession JavaDoc getSession() {
70     return request.getSession(true);
71   }
72
73   public Locale JavaDoc getLocale() {
74     return rcf.getLocale();
75   }
76
77   public Formatter getFormatter() {
78     return rcf.getFormatter();
79   }
80
81   public Converter getConverter() {
82     return rcf.getConverter();
83   }
84
85   public Map JavaDoc getParameters() {
86     return request.getParameterMap();
87   }
88
89   public String JavaDoc[] getParameters(String JavaDoc name) {
90     return request.getParameterValues(name);
91   }
92
93   public String JavaDoc getParameter(String JavaDoc name) {
94     return request.getParameter(name);
95   }
96
97   public Object JavaDoc getModelReference(String JavaDoc expr) {
98     if (expr == null || expr.length() == 0)
99       return null;
100     FacesContext fc = FacesContext.getCurrentInstance();
101     if (fc != null && expr.startsWith("#{")) {
102       ValueBinding vb = fc.getApplication().createValueBinding(expr);
103       return vb.getValue(fc);
104     }
105     // ExprUtils.checkExpr(expr);
106
return ExprUtils.getModelReference(this, expr);
107   }
108
109   public void setModelReference(String JavaDoc expr, Object JavaDoc value) {
110     FacesContext fc = FacesContext.getCurrentInstance();
111     if (fc != null && expr.startsWith("#{")) {
112       ValueBinding vb = fc.getApplication().createValueBinding(expr);
113       vb.setValue(fc, value);
114     } else {
115       // ExprUtils.checkExpr(expr);
116
ExprUtils.setModelReference(this, expr, value);
117     }
118   }
119
120   /**
121    * same as JSP PageContext.findAttribute() except it does not search in page
122    * scope.
123    */

124   public Object JavaDoc findBean(String JavaDoc name) {
125     Object JavaDoc bean = request.getAttribute(name);
126     if (bean != null)
127       return bean;
128     HttpSession JavaDoc session = getSession();
129     bean = session.getAttribute(name);
130     if (bean != null)
131       return bean;
132     ServletContext JavaDoc context = getServletContext();
133     bean = context.getAttribute(name);
134     if (bean != null)
135       return bean;
136     return null;
137   }
138
139   public void setBean(String JavaDoc name, Object JavaDoc bean) {
140     HttpSession JavaDoc session = getSession();
141     if (bean == null)
142       session.removeAttribute(name);
143     else
144       session.setAttribute(name, bean);
145   }
146
147   public boolean isUserInRole(String JavaDoc roleExpr) {
148     if (roleExpr == null || roleExpr.length() == 0)
149       return true;
150     // evaluate modelReference
151
String JavaDoc ref = (String JavaDoc) getModelReference(roleExpr);
152     if (ref != null)
153       roleExpr = ref;
154     boolean success = true;
155     if (roleExpr.startsWith("!")) {
156       roleExpr = roleExpr.substring(1);
157       success = false;
158     }
159
160     // root has all roles
161
if (isAdmin())
162       return success;
163
164     // evaluate mapping
165
String JavaDoc mappedRole = getMappedRole(roleExpr);
166     if (mappedRole != null)
167       roleExpr = mappedRole;
168     // test environment?
169
if (request == null)
170       return !success;
171     // loop over roles in comma/space separated list
172
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(roleExpr, ", ", false);
173     while (st.hasMoreTokens()) {
174       if (internalIsUserInRole(st.nextToken()))
175         return success;
176     }
177     return !success;
178   }
179
180   /**
181    * allows derived classes to use another authentification framework.
182    * Roles may be looked up in a database for example.
183    *
184    * @param role the role to test
185    */

186   protected boolean internalIsUserInRole(String JavaDoc role) {
187     return request.isUserInRole(role);
188   }
189
190   /**
191    * maps a role to another role. The application may define fine grained,
192    * virtual roles for buttons, components etc. These roles may be mapped to a
193    * few real roles like "user", "expert" etc.
194    * <p />
195    *
196    * @param role
197    * name of the virtual fine grained role, e.g. "mayPressButton23role"
198    * @return a real role, e.g. "expertUser" to allow only experts to press
199    * button23 or null, if no such mapping exists and the role is defined
200    * elsewhere. May return a comma separated list of roles, e.g.
201    * "expertUser,adminUser" to allow access to any of the roles.
202    */

203   protected String JavaDoc getMappedRole(String JavaDoc role) {
204     try {
205       return rcf.getResources().getString("role." + role);
206     } catch (MissingResourceException JavaDoc e) {
207       return null;
208     }
209   }
210
211   public Resources getResources() {
212     return Resources.instance(getLocale());
213   }
214
215   public Resources getResources(String JavaDoc bundleName) {
216     return Resources.instance(getLocale(), bundleName);
217   }
218
219   public Resources getResources(Class JavaDoc clasz) {
220     return Resources.instance(getLocale(), clasz);
221   }
222
223   public String JavaDoc getRemoteUser() {
224     return rcf.getRemoteUser();
225   }
226
227   public String JavaDoc getRemoteDomain() {
228     return rcf.getRemoteDomain();
229   }
230
231   public boolean isAdmin() {
232     return false;
233   }
234
235   public void setLocale(Locale JavaDoc locale) {
236     rcf.setLocale(request, locale);
237   }
238
239   public Map JavaDoc getFileParameters() {
240     if(request instanceof MultiPartEnabledRequest)
241       return ((MultiPartEnabledRequest)request).getFileParameterMap();
242
243     return null;
244   }
245 }
Popular Tags