1 13 package com.tonbeller.wcf.controller; 14 15 import java.util.Locale ; 16 import java.util.Map ; 17 import java.util.MissingResourceException ; 18 import java.util.StringTokenizer ; 19 20 import javax.faces.context.FacesContext; 21 import javax.faces.el.ValueBinding; 22 import javax.servlet.ServletContext ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 import javax.servlet.http.HttpSession ; 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 37 public class RequestContextImpl extends RequestContext implements ExprContext { 38 protected RequestContextFactory rcf; 39 protected HttpServletRequest request; 40 protected HttpServletResponse response; 41 protected static Logger logger = Logger.getLogger(RequestContextImpl.class); 42 43 public RequestContextImpl(RequestContextFactory rcf, HttpServletRequest request, 44 HttpServletResponse 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 getRequest() { 58 return request; 59 } 60 61 public HttpServletResponse getResponse() { 62 return response; 63 } 64 65 public ServletContext getServletContext() { 66 return getSession().getServletContext(); 67 } 68 69 public HttpSession getSession() { 70 return request.getSession(true); 71 } 72 73 public Locale 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 getParameters() { 86 return request.getParameterMap(); 87 } 88 89 public String [] getParameters(String name) { 90 return request.getParameterValues(name); 91 } 92 93 public String getParameter(String name) { 94 return request.getParameter(name); 95 } 96 97 public Object getModelReference(String 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 return ExprUtils.getModelReference(this, expr); 107 } 108 109 public void setModelReference(String expr, Object 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.setModelReference(this, expr, value); 117 } 118 } 119 120 124 public Object findBean(String name) { 125 Object bean = request.getAttribute(name); 126 if (bean != null) 127 return bean; 128 HttpSession session = getSession(); 129 bean = session.getAttribute(name); 130 if (bean != null) 131 return bean; 132 ServletContext context = getServletContext(); 133 bean = context.getAttribute(name); 134 if (bean != null) 135 return bean; 136 return null; 137 } 138 139 public void setBean(String name, Object bean) { 140 HttpSession session = getSession(); 141 if (bean == null) 142 session.removeAttribute(name); 143 else 144 session.setAttribute(name, bean); 145 } 146 147 public boolean isUserInRole(String roleExpr) { 148 if (roleExpr == null || roleExpr.length() == 0) 149 return true; 150 String ref = (String ) 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 if (isAdmin()) 162 return success; 163 164 String mappedRole = getMappedRole(roleExpr); 166 if (mappedRole != null) 167 roleExpr = mappedRole; 168 if (request == null) 170 return !success; 171 StringTokenizer st = new StringTokenizer (roleExpr, ", ", false); 173 while (st.hasMoreTokens()) { 174 if (internalIsUserInRole(st.nextToken())) 175 return success; 176 } 177 return !success; 178 } 179 180 186 protected boolean internalIsUserInRole(String role) { 187 return request.isUserInRole(role); 188 } 189 190 203 protected String getMappedRole(String role) { 204 try { 205 return rcf.getResources().getString("role." + role); 206 } catch (MissingResourceException e) { 207 return null; 208 } 209 } 210 211 public Resources getResources() { 212 return Resources.instance(getLocale()); 213 } 214 215 public Resources getResources(String bundleName) { 216 return Resources.instance(getLocale(), bundleName); 217 } 218 219 public Resources getResources(Class clasz) { 220 return Resources.instance(getLocale(), clasz); 221 } 222 223 public String getRemoteUser() { 224 return rcf.getRemoteUser(); 225 } 226 227 public String getRemoteDomain() { 228 return rcf.getRemoteDomain(); 229 } 230 231 public boolean isAdmin() { 232 return false; 233 } 234 235 public void setLocale(Locale locale) { 236 rcf.setLocale(request, locale); 237 } 238 239 public Map getFileParameters() { 240 if(request instanceof MultiPartEnabledRequest) 241 return ((MultiPartEnabledRequest)request).getFileParameterMap(); 242 243 return null; 244 } 245 } | Popular Tags |