@Override public String doIntercept(ActionInvocation invocation) throws Exception { Object action = invocation.getAction(); if (!(action instanceof NoParameters)) { ActionContext ac = invocation.getInvocationContext(); final Map<String, Object> parameters = retrieveParameters(ac); if (LOG.isDebugEnabled()) { LOG.debug("Setting params " + getParameterLogMap(parameters)); } if (parameters != null) { Map<String, Object> contextMap = ac.getContextMap(); try { ReflectionContextState.setCreatingNullObjects(contextMap, true); ReflectionContextState.setDenyMethodExecution(contextMap, true); ReflectionContextState.setReportingConversionErrors(contextMap, true); ValueStack stack = ac.getValueStack(); setParameters(action, stack, parameters); } finally { ReflectionContextState.setCreatingNullObjects(contextMap, false); ReflectionContextState.setDenyMethodExecution(contextMap, false); ReflectionContextState.setReportingConversionErrors(contextMap, false); } } } return invocation.invoke(); }
public void testGenericPropertiesFromGetter() { GenericsBean gb = new GenericsBean(); ValueStack stack = ac.getValueStack(); stack.push(gb); assertEquals(1, gb.getGetterList().size()); assertEquals("42.42", stack.findValue("getterList.get(0).toString()")); assertEquals(new Double(42.42), stack.findValue("getterList.get(0)")); assertEquals(new Double(42.42), gb.getGetterList().get(0)); }
public void testGenericProperties() { GenericsBean gb = new GenericsBean(); ValueStack stack = ac.getValueStack(); stack.push(gb); String[] value = new String[] {"123.12", "123.45"}; stack.setValue("doubles", value); assertEquals(2, gb.getDoubles().size()); assertEquals(Double.class, gb.getDoubles().get(0).getClass()); assertEquals(new Double(123.12), gb.getDoubles().get(0)); assertEquals(new Double(123.45), gb.getDoubles().get(1)); }
@Override public String intercept(ActionInvocation invocation) throws Exception { ActionContext invocationContext = invocation.getInvocationContext(); Map<String, Object> conversionErrors = invocationContext.getConversionErrors(); ValueStack stack = invocationContext.getValueStack(); HashMap<Object, Object> fakie = null; for (Map.Entry<String, Object> entry : conversionErrors.entrySet()) { String propertyName = entry.getKey(); Object value = entry.getValue(); if (shouldAddError(propertyName, value)) { String message = XWorkConverter.getConversionErrorMessage(propertyName, stack); Object action = invocation.getAction(); if (action instanceof ValidationAware) { ValidationAware va = (ValidationAware) action; va.addFieldError(propertyName, message); } if (fakie == null) { fakie = new HashMap<Object, Object>(); } fakie.put(propertyName, getOverrideExpr(invocation, value)); } } if (fakie != null) { // if there were some errors, put the original (fake) values in place right before the result stack.getContext().put(ORIGINAL_PROPERTY_OVERRIDE, fakie); invocation.addPreResultListener( new PreResultListener() { public void beforeResult(ActionInvocation invocation, String resultCode) { Map<Object, Object> fakie = (Map<Object, Object>) invocation.getInvocationContext().get(ORIGINAL_PROPERTY_OVERRIDE); if (fakie != null) { invocation.getStack().setExprOverrides(fakie); } } }); } return invocation.invoke(); }
public void testGenericPropertiesFromSetter() { GenericsBean gb = new GenericsBean(); ValueStack stack = ac.getValueStack(); stack.push(gb); stack.setValue("genericMap[123.12]", "66"); stack.setValue("genericMap[456.12]", "42"); assertEquals(2, gb.getGenericMap().size()); assertEquals("66", stack.findValue("genericMap.get(123.12).toString()")); assertEquals("42", stack.findValue("genericMap.get(456.12).toString()")); assertEquals(66, stack.findValue("genericMap.get(123.12)")); assertEquals(42, stack.findValue("genericMap.get(456.12)")); assertEquals(true, stack.findValue("genericMap.containsValue(66)")); assertEquals(true, stack.findValue("genericMap.containsValue(42)")); assertEquals(true, stack.findValue("genericMap.containsKey(123.12)")); assertEquals(true, stack.findValue("genericMap.containsKey(456.12)")); }
@Override protected void setUp() throws Exception { super.setUp(); this.request = new MockHttpServletRequest(); this.response = new MockHttpServletResponse(); ActionContext context = ActionContext.getContext(); ValueStack stack = context.getValueStack(); ActionContext.setContext(context); context.put(StrutsStatics.HTTP_REQUEST, this.request); context.put(StrutsStatics.HTTP_RESPONSE, this.response); MockServletContext servletContext = new MockServletContext(); context.put(StrutsStatics.SERVLET_CONTEXT, servletContext); this.invocation = new MockActionInvocationEx(); this.invocation.setInvocationContext(context); this.invocation.setStack(stack); }
// FIXME: Implement nested Generics such as: List of Generics List, Map of Generic keys/values, // etc... public void no_testGenericPropertiesWithNestedGenerics() { GenericsBean gb = new GenericsBean(); ValueStack stack = ac.getValueStack(); stack.push(gb); stack.setValue("extendedMap[123.12]", new String[] {"1", "2", "3", "4"}); stack.setValue("extendedMap[456.12]", new String[] {"5", "6", "7", "8", "9"}); System.out.println("gb.getExtendedMap(): " + gb.getExtendedMap()); assertEquals(2, gb.getExtendedMap().size()); System.out.println(stack.findValue("extendedMap")); assertEquals(4, stack.findValue("extendedMap.get(123.12).size")); assertEquals(5, stack.findValue("extendedMap.get(456.12).size")); assertEquals("1", stack.findValue("extendedMap.get(123.12).get(0)")); assertEquals("5", stack.findValue("extendedMap.get(456.12).get(0)")); assertEquals(Integer.class, stack.findValue("extendedMap.get(123.12).get(0).class")); assertEquals(Integer.class, stack.findValue("extendedMap.get(456.12).get(0).class")); assertEquals(List.class, stack.findValue("extendedMap.get(123.12).class")); assertEquals(List.class, stack.findValue("extendedMap.get(456.12).class")); }
public void testIncludeParameterInResultWithConditionParseOn() throws Exception { ResultConfig resultConfig = new ResultConfig.Builder("", "") .addParam("actionName", "someActionName") .addParam("namespace", "someNamespace") .addParam("encode", "true") .addParam("parse", "true") .addParam("location", "someLocation") .addParam("prependServletContext", "true") .addParam("method", "someMethod") .addParam("statusCode", "333") .addParam("param1", "${#value1}") .addParam("param2", "${#value2}") .addParam("param3", "${#value3}") .addParam("anchor", "${#fragment}") .build(); ActionContext context = ActionContext.getContext(); ValueStack stack = context.getValueStack(); context.getContextMap().put("value1", "value 1"); context.getContextMap().put("value2", "value 2"); context.getContextMap().put("value3", "value 3"); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); context.put(ServletActionContext.HTTP_REQUEST, req); context.put(ServletActionContext.HTTP_RESPONSE, res); Map<String, ResultConfig> results = new HashMap<String, ResultConfig>(); results.put("myResult", resultConfig); ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build(); ServletActionRedirectResult result = new ServletActionRedirectResult(); result.setActionName("myAction"); result.setNamespace("/myNamespace"); result.setParse(true); result.setEncode(false); result.setPrependServletContext(false); result.setAnchor("fragment"); result.setUrlHelper(new DefaultUrlHelper()); IMocksControl control = createControl(); ActionProxy mockActionProxy = control.createMock(ActionProxy.class); ActionInvocation mockInvocation = control.createMock(ActionInvocation.class); expect(mockInvocation.getProxy()).andReturn(mockActionProxy); expect(mockInvocation.getResultCode()).andReturn("myResult"); expect(mockActionProxy.getConfig()).andReturn(actionConfig); expect(mockInvocation.getInvocationContext()).andReturn(context); expect(mockInvocation.getStack()).andReturn(stack).anyTimes(); control.replay(); result.setActionMapper(container.getInstance(ActionMapper.class)); result.execute(mockInvocation); assertEquals( "/myNamespace/myAction.action?param1=value+1¶m2=value+2¶m3=value+3#fragment", res.getRedirectedUrl()); control.verify(); }
/** * Load Action class for mapping and invoke the appropriate Action method, or go directly to the * Result. * * <p>This method first creates the action context from the given parameters, and then loads an * <tt>ActionProxy</tt> from the given action name and namespace. After that, the Action method is * executed and output channels through the response object. Actions not found are sent back to * the user via the {@link Dispatcher#sendError} method, using the 404 return code. All other * errors are reported by throwing a ServletException. * * @param request the HttpServletRequest object * @param response the HttpServletResponse object * @param mapping the action mapping object * @throws ServletException when an unknown error occurs (not a 404, but typically something that * would end up as a 5xx by the servlet container) * @param context Our ServletContext object */ public void serviceAction( HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException { Map<String, Object> extraContext = createContextMap(request, response, mapping, context); // If there was a previous value stack, then create a new copy and pass it in to be used by the // new Action ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY); boolean nullStack = stack == null; if (nullStack) { ActionContext ctx = ActionContext.getContext(); if (ctx != null) { stack = ctx.getValueStack(); } } if (stack != null) { extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack)); } String timerKey = "Handling request from Dispatcher"; try { UtilTimerStack.push(timerKey); String namespace = mapping.getNamespace(); String name = mapping.getName(); String method = mapping.getMethod(); Configuration config = configurationManager.getConfiguration(); ActionProxy proxy = config .getContainer() .getInstance(ActionProxyFactory.class) .createActionProxy(namespace, name, method, extraContext, true, false); request.setAttribute( ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack()); // if the ActionMapping says to go straight to a result, do it! if (mapping.getResult() != null) { Result result = mapping.getResult(); result.execute(proxy.getInvocation()); } else { proxy.execute(); } // If there was a previous value stack then set it back onto the request if (!nullStack) { request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); } } catch (ConfigurationException e) { // WW-2874 Only log error if in devMode if (devMode) { String reqStr = request.getRequestURI(); if (request.getQueryString() != null) { reqStr = reqStr + "?" + request.getQueryString(); } LOG.error("Could not find action or result\n" + reqStr, e); } else { if (LOG.isWarnEnabled()) { LOG.warn("Could not find action or result", e); } } sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e); } catch (Exception e) { if (handleException || devMode) { sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e); } else { throw new ServletException(e); } } finally { UtilTimerStack.pop(timerKey); } }
@Override public String intercept(ActionInvocation invocation) throws Exception { ActionConfig config = invocation.getProxy().getConfig(); ActionContext ac = invocation.getInvocationContext(); Object action = invocation.getAction(); // get the action's parameters final Map<String, String> parameters = config.getParams(); if (parameters.containsKey(aliasesKey)) { String aliasExpression = parameters.get(aliasesKey); ValueStack stack = ac.getValueStack(); Object obj = stack.findValue(aliasExpression); if (obj != null && obj instanceof Map) { // get secure stack ValueStack newStack = valueStackFactory.createValueStack(stack); boolean clearableStack = newStack instanceof ClearableValueStack; if (clearableStack) { // if the stack's context can be cleared, do that to prevent OGNL // from having access to objects in the stack, see XW-641 ((ClearableValueStack) newStack).clearContextValues(); Map<String, Object> context = newStack.getContext(); ReflectionContextState.setCreatingNullObjects(context, true); ReflectionContextState.setDenyMethodExecution(context, true); ReflectionContextState.setReportingConversionErrors(context, true); // keep locale from original context context.put(ActionContext.LOCALE, stack.getContext().get(ActionContext.LOCALE)); } // override Map aliases = (Map) obj; for (Object o : aliases.entrySet()) { Map.Entry entry = (Map.Entry) o; String name = entry.getKey().toString(); String alias = (String) entry.getValue(); Object value = stack.findValue(name); if (null == value) { // workaround Map<String, Object> contextParameters = ActionContext.getContext().getParameters(); if (null != contextParameters) { value = contextParameters.get(name); } } if (null != value) { try { newStack.setValue(alias, value); } catch (RuntimeException e) { if (devMode) { String developerNotification = LocalizedTextUtil.findText( ParametersInterceptor.class, "devmode.notification", ActionContext.getContext().getLocale(), "Developer Notification:\n{0}", new Object[] { "Unexpected Exception caught setting '" + entry.getKey() + "' on '" + action.getClass() + ": " + e.getMessage() }); LOG.error(developerNotification); if (action instanceof ValidationAware) { ((ValidationAware) action).addActionMessage(developerNotification); } } } } } if (clearableStack && (stack.getContext() != null) && (newStack.getContext() != null)) stack .getContext() .put( ActionContext.CONVERSION_ERRORS, newStack.getContext().get(ActionContext.CONVERSION_ERRORS)); } else { LOG.debug("invalid alias expression:" + aliasesKey); } } return invocation.invoke(); }