1 18 package org.apache.beehive.netui.pageflow; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.jsp.el.VariableResolver ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import org.apache.beehive.netui.pageflow.internal.InternalUtils; 31 import org.apache.beehive.netui.script.Expression; 32 import org.apache.beehive.netui.script.ExpressionEvaluator; 33 import org.apache.beehive.netui.script.ExpressionEvaluatorFactory; 34 import org.apache.beehive.netui.script.ExpressionUpdateException; 35 import org.apache.beehive.netui.script.common.ImplicitObjectUtil; 36 import org.apache.beehive.netui.util.Bundle; 37 import org.apache.beehive.netui.util.logging.Logger; 38 39 import org.apache.commons.beanutils.BeanUtils; 40 import org.apache.struts.action.ActionForm; 41 42 54 public class ProcessPopulate 55 { 56 59 public static final String IDMAP_PARAMETER_NAME = "netuiIdMap"; 60 61 private static final Logger _logger = Logger.getInstance(ProcessPopulate.class); 62 63 private static final String PAGE_FLOW_CONTEXT = "pageFlow"; 65 private static final String GLOBAL_APP_CONTEXT = "globalApp"; 66 67 private static final String WLW_TAG_HANDLER_PREFIX = "wlw-"; 68 private static final String WLW_TAG_HANDLER_SUFFIX = ":"; 69 70 private static final Map handlerMap = new HashMap (); 71 72 79 public final static class ExpressionUpdateNode 80 { 81 public String expression = null; 82 public String [] values = null; 83 84 private ExpressionUpdateNode() {} 86 87 public String toString() 88 { 89 InternalStringBuilder buf = new InternalStringBuilder(); 90 buf.append("expression: " + expression + "\n"); 91 if(values != null) 92 for(int i = 0; i < values.length; i++) 93 buf.append("value[" + i + "]: " + values[i]); 94 else buf.append("values are null"); 95 96 return buf.toString(); 97 } 98 } 99 100 113 public static void registerPrefixHandler(String prefix, RequestParameterHandler handler) 114 { 115 synchronized(handlerMap) 117 { 118 String msg = "Register RequestParameterHandler with\n\tprefix: " + prefix + "\n\thandler: " + (handler != null ? handler.getClass().getName(): null); 119 120 if(_logger.isInfoEnabled()) _logger.info(msg); 121 122 if (handlerMap.get(prefix) == null) 123 handlerMap.put(prefix, handler); 124 } 125 } 126 127 130 public static String writeHandlerName(String handler, String expression) 131 { 132 if(!ExpressionEvaluatorFactory.getInstance().isExpression(expression)) 133 throw new IllegalArgumentException (Bundle.getErrorString("ProcessPopulate_handler_nonAtomicExpression", new Object [] {expression})); 134 135 if(!handlerMap.containsKey(handler)) 136 throw new IllegalStateException (Bundle.getErrorString("ProcessPopulate_handler_notRegistered", new Object [] {handler})); 137 138 InternalStringBuilder buf = new InternalStringBuilder(); 139 buf.append(WLW_TAG_HANDLER_PREFIX); 140 buf.append(handler); 141 buf.append(WLW_TAG_HANDLER_SUFFIX); 142 buf.append(expression); 143 144 return buf.toString(); 145 } 146 147 161 public static void populate(HttpServletRequest request, HttpServletResponse response, ActionForm form, boolean requestHasPopulated) 162 throws ServletException 163 { 164 String key = null; 165 Map strutsProperties = null; 166 ExpressionEvaluator ee = ExpressionEvaluatorFactory.getInstance(); 167 168 boolean isMultipart = false; 170 171 Map params = MultipartRequestUtils.handleMultipartRequest(request, form); 173 174 if(params != null) 176 isMultipart = true; 177 else params = request.getParameterMap(); 178 179 if(params == null) 180 { 181 if(_logger.isWarnEnabled()) _logger.warn("An error occurred checking a request for multipart status. No model values were updated."); 182 return; 183 } 184 185 186 VariableResolver variableResolver = ImplicitObjectUtil.getUpdateVariableResolver(form, request, response, true); 187 188 189 Iterator iterator = params.keySet().iterator(); 190 while (iterator.hasNext()) 191 { 192 key = (String ) iterator.next(); 193 String expr = null; 194 195 expr = key; 197 if (_logger.isDebugEnabled()) 198 _logger.debug("key: " + key + " value type: " + params.get(key).getClass().getName() + " value: " + params.get(key)); 199 200 try 201 { 202 Object paramsValue = params.get(key); 203 if (ee.containsExpression(expr)) 204 { 205 Object updateValue = null; 206 if (!isMultipart || paramsValue instanceof String []) 207 { 208 String [] values = (String []) paramsValue; 209 210 if (expr.startsWith(WLW_TAG_HANDLER_PREFIX)) 212 { 213 if (_logger.isDebugEnabled()) _logger.debug("Found an expression requiring a TAG HANDLER"); 214 215 ExpressionUpdateNode node = doTagHandler(key, expr, values, request); 216 217 expr = node.expression; 218 values = node.values; 219 } 220 221 if (values != null && values.length == 1) 222 updateValue = values[0]; 223 else 224 updateValue = values; 225 } 226 else 228 { 229 updateValue = params.get(key); 230 } 231 232 try 233 { 234 if (ee.isExpression(expr)) 236 { 237 if (!requestHasPopulated) 239 ee.update(expr, updateValue, variableResolver, true); 240 else 242 { 243 Expression pe = ee.parseExpression(expr); 244 String contextName = pe.getContext(); 245 if (!contextName.equals(PAGE_FLOW_CONTEXT) && !contextName.equals(GLOBAL_APP_CONTEXT)) 246 ee.update(expr, updateValue, variableResolver, true); 247 } 248 } 249 } 250 catch (ExpressionUpdateException e) 252 { 253 String s = Bundle.getString("ExprUpdateError", new Object []{expr, e}); 254 255 System.err.println(s); 257 if (_logger.isErrorEnabled()) _logger.error(s); 258 259 InternalUtils.addBindingUpdateError(request, expr, s, e); 261 } 262 } 263 else 264 { 265 if (_logger.isDebugEnabled()) _logger.debug("HTTP request parameter key \"" + key + "\" is not an expression, handle with Struts"); 266 267 if (strutsProperties == null) 268 strutsProperties = new HashMap (); 269 270 strutsProperties.put(key, paramsValue); 271 } 272 } 273 catch (Exception e) 275 { 276 String s = Bundle.getString("ProcessPopulate_exprUpdateError", new Object []{expr, e}); 277 279 System.err.println(s); 280 281 if (_logger.isWarnEnabled()) _logger.warn(s, e); 282 283 InternalUtils.addBindingUpdateError(request, expr, s, e); 285 } 286 } 287 288 handleStrutsProperties(strutsProperties, form); 289 } 290 291 297 static final ExpressionUpdateNode doTagHandler(String key, String expression, String [] values, HttpServletRequest request) 298 { 299 String [] _values = values; 301 302 String expr = expression; 304 305 if(_logger.isDebugEnabled()) _logger.debug("Found prefixed tag; handlerName: " + key.substring(WLW_TAG_HANDLER_PREFIX.length(), key.indexOf(WLW_TAG_HANDLER_SUFFIX))); 306 307 String handlerName = expression.substring(WLW_TAG_HANDLER_PREFIX.length(), expression.indexOf(WLW_TAG_HANDLER_SUFFIX)); 308 309 RequestParameterHandler handler = (RequestParameterHandler)handlerMap.get(handlerName); 311 312 if(handler != null) 313 { 314 expr = expression.substring(expression.indexOf(WLW_TAG_HANDLER_SUFFIX)+1); 315 316 if(_logger.isDebugEnabled()) _logger.debug("found handler for prefix \"" + handlerName + "\" type: " + 317 (handler != null ? handler.getClass().getName() : null) + "\n\t" + 318 "key: \"" + key + "\" expr: \"" + expr + "\""); 319 320 ExpressionUpdateNode node = new ExpressionUpdateNode(); 321 node.expression = expr; 322 node.values = _values; 323 324 handler.process(request, key, expression, node); 326 327 return node; 328 } 329 else throw new IllegalStateException ("Request parameter references a tag handler prefix \"" + 330 handlerName + "\" that is not registered for expression \"" + key + "\""); 331 } 332 333 private static final void handleStrutsProperties(Map strutsProperties, ActionForm form) 335 { 336 if(strutsProperties != null) 337 { 338 if(_logger.isDebugEnabled()) _logger.debug("Handle Struts request parameters."); 339 340 try 342 { 343 BeanUtils.populate(form, strutsProperties); 344 } 345 catch(Exception e) 346 { 347 throw new RuntimeException ("Exception processing bean and request parameters: ", e); 348 } 349 } 350 } 351 } 352 | Popular Tags |