1 18 package org.apache.beehive.netui.pageflow.internal; 19 20 import java.util.Map ; 21 import java.util.Enumeration ; 22 import java.util.HashMap ; 23 24 import javax.servlet.ServletContext ; 25 import javax.servlet.jsp.el.ExpressionEvaluator ; 26 import javax.servlet.jsp.el.VariableResolver ; 27 import javax.servlet.jsp.el.ELException ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpSession ; 30 import javax.servlet.http.Cookie ; 31 32 import org.apache.beehive.netui.util.logging.Logger; 33 import org.apache.commons.el.ExpressionEvaluatorImpl; 34 import org.apache.commons.el.EnumeratedMap; 35 36 42 public class InternalExpressionUtils 43 { 44 private static final Logger _logger = Logger.getInstance(InternalExpressionUtils.class); 45 46 public static final boolean evaluateCondition(String expression, 47 Object actionForm, 48 HttpServletRequest request, 49 ServletContext servletContext) 50 throws ELException 51 { 52 return ((Boolean )evaluate(expression, Boolean .class, actionForm, request, servletContext)).booleanValue(); 53 } 54 55 public static final String evaluateMessage(String expression, Object actionForm, HttpServletRequest request, 56 ServletContext servletContext) 57 throws ELException 58 { 59 return (String )evaluate(expression, String .class, actionForm, request, servletContext); 60 } 61 62 63 private InternalExpressionUtils() {} 64 65 private static final Object evaluate(String expression, Class expectedType, Object actionForm, HttpServletRequest request, ServletContext servletContext) 66 throws ELException 67 { 68 ExpressionEvaluator ee = getExpressionEvaluator(); 70 return ee.evaluate(expression, expectedType, getVariableResolver(actionForm, request, servletContext), null); 71 } 72 73 private static final ExpressionEvaluator getExpressionEvaluator() 74 { 75 return new ExpressionEvaluatorImpl(); 76 } 77 78 private static final VariableResolver getVariableResolver(Object actionForm, HttpServletRequest request, ServletContext servletContext) 79 { 80 return new SimpleActionVariableResolver(actionForm, request, servletContext); 81 } 82 83 private static class SimpleActionVariableResolver 84 implements VariableResolver 85 { 86 private Object _actionForm = null; 87 private HttpServletRequest _request = null; 88 private ServletContext _servletContext = null; 89 90 private SimpleActionVariableResolver(Object actionForm, HttpServletRequest request, ServletContext servletContext) 91 { 92 _actionForm = actionForm; 93 _request = request; 94 _servletContext = servletContext; 95 } 96 97 public Object resolveVariable(String name) 98 { 99 if(name.equals("actionForm")) 101 return _actionForm; 102 else if(name.equals("requestScope")) 103 return buildRequestScopeMap(_request); 104 else if(name.equals("sessionScope")) 105 return buildSessionScopeMap(_request); 106 else if(name.equals("applicationScope")) 107 return buildServletContextMap(_servletContext); 108 else if(name.equals("param")) 109 return buildParamMap(_request); 110 else if(name.equals("paramValues")) 111 return buildParamsMap(_request); 112 else if(name.equals("header")) 113 return buildHeaderMap(_request); 114 else if(name.equals("headerValues")) 115 return buildHeadersMap(_request); 116 else if(name.equals("cookie")) 117 return buildCookieMap(_request); 118 else if(name.equals("initParam")) 119 return buildInitParamMap(_servletContext); 120 else if(_request.getAttribute(name) != null) 124 return _request.getAttribute(name); 125 else if(_request.getSession(false) != null && _request.getSession(false).getAttribute(name) != null) 126 return _request.getSession(false).getAttribute(name); 127 else return _servletContext.getAttribute(name); 128 } 129 130 private static final Map buildCookieMap(HttpServletRequest httpServletRequest) 131 { 132 HttpServletRequest servletRequest = httpServletRequest; 133 Map cookieMap = new HashMap (); 134 Cookie [] cookies = servletRequest.getCookies(); 135 for(int i = 0; i < cookies.length; i++) 136 { 137 if(!cookieMap.containsKey(cookies[i].getName())) 138 cookieMap.put(cookies[i].getName(), cookies[i]); 139 } 140 return cookieMap; 141 } 142 143 private static final Map buildHeadersMap(HttpServletRequest httpServletRequest) 144 { 145 final HttpServletRequest _servletRequest = httpServletRequest; 146 return new EnumeratedMap() 147 { 148 public Enumeration enumerateKeys() 149 { 150 return _servletRequest.getHeaderNames(); 151 } 152 153 public Object getValue(Object key) 154 { 155 return (key instanceof String ? _servletRequest.getHeaders((String )key) : null); 156 } 157 158 public boolean isMutable() {return false;} 159 }; 160 } 161 162 private static final Map buildHeaderMap(HttpServletRequest httpServletRequest) 163 { 164 final HttpServletRequest _servletRequest = httpServletRequest; 165 return new EnumeratedMap() 166 { 167 public Enumeration enumerateKeys() 168 { 169 return _servletRequest.getHeaderNames(); 170 } 171 172 public Object getValue(Object key) 173 { 174 return (key instanceof String ? _servletRequest.getHeader((String )key) : null); 175 } 176 177 public boolean isMutable() {return false;} 178 }; 179 } 180 181 private static final Map buildInitParamMap(ServletContext servletContext) 182 { 183 final ServletContext _servletContext = servletContext; 184 return new EnumeratedMap() 185 { 186 public Enumeration enumerateKeys() 187 { 188 return _servletContext.getInitParameterNames(); 189 } 190 191 public Object getValue(Object key) 192 { 193 return (key instanceof String ? _servletContext.getInitParameter((String )key) : null); 194 } 195 196 public boolean isMutable() {return false;} 197 }; 198 } 199 200 private static final Map buildParamsMap(HttpServletRequest servletRequest) 201 { 202 final HttpServletRequest _servletRequest = servletRequest; 203 return new EnumeratedMap() 204 { 205 public Enumeration enumerateKeys() 206 { 207 return _servletRequest.getParameterNames(); 208 } 209 210 public Object getValue(Object key) 211 { 212 return (key instanceof String ? _servletRequest.getParameterValues((String )key) : null); 213 } 214 215 public boolean isMutable() {return false;} 216 }; 217 } 218 219 private static final Map buildParamMap(HttpServletRequest servletRequest) 220 { 221 final HttpServletRequest _servletRequest = servletRequest; 222 return new EnumeratedMap() 223 { 224 public Enumeration enumerateKeys() 225 { 226 return _servletRequest.getParameterNames(); 227 } 228 229 public Object getValue(Object key) 230 { 231 return (key instanceof String ? _servletRequest.getParameter((String )key) : null); 232 } 233 234 public boolean isMutable() {return false;} 235 }; 236 } 237 238 private static final Map buildRequestScopeMap(HttpServletRequest servletRequest) 239 { 240 final HttpServletRequest _servletRequest = servletRequest; 241 return new EnumeratedMap() 242 { 243 public Enumeration enumerateKeys() 244 { 245 return _servletRequest.getAttributeNames(); 246 } 247 248 public Object getValue(Object key) 249 { 250 return (key instanceof String ? _servletRequest.getAttribute((String )key) : null); 251 } 252 253 public boolean isMutable() {return false;} 254 }; 255 } 256 257 private static final Map buildSessionScopeMap(HttpServletRequest servletRequest) 258 { 259 if(servletRequest.getSession(false) == null) 260 return null; 261 262 final HttpSession _session = servletRequest.getSession(false); 263 return new EnumeratedMap() 264 { 265 public Enumeration enumerateKeys() 266 { 267 return _session.getAttributeNames(); 268 } 269 270 public Object getValue(Object key) 271 { 272 return (key instanceof String ? _session.getAttribute((String )key) : null); 273 } 274 275 public boolean isMutable() {return false;} 276 }; 277 } 278 279 private static final Map buildServletContextMap(ServletContext servletContext) 280 { 281 final ServletContext _servletContext = servletContext; 282 return new EnumeratedMap() 283 { 284 public Enumeration enumerateKeys() 285 { 286 return _servletContext.getAttributeNames(); 287 } 288 289 public Object getValue(Object key) 290 { 291 return (key instanceof String ? _servletContext.getAttribute((String )key) : null); 292 } 293 294 public boolean isMutable() {return false;} 295 }; 296 } 297 } 298 } 299 | Popular Tags |