Search in sources :

Example 1 with Parameterizable

use of com.opensymphony.xwork2.config.entities.Parameterizable in project struts by apache.

the class StaticParametersInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    ActionConfig config = invocation.getProxy().getConfig();
    Object action = invocation.getAction();
    final Map<String, String> parameters = config.getParams();
    LOG.debug("Setting static parameters: {}", parameters);
    // for actions marked as Parameterizable, pass the static parameters directly
    if (action instanceof Parameterizable) {
        ((Parameterizable) action).setParams(parameters);
    }
    if (parameters != null) {
        ActionContext ac = ActionContext.getContext();
        Map<String, Object> contextMap = ac.getContextMap();
        try {
            ReflectionContextState.setCreatingNullObjects(contextMap, true);
            ReflectionContextState.setReportingConversionErrors(contextMap, true);
            final ValueStack stack = ac.getValueStack();
            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
                newStack.getActionContext().withLocale(stack.getActionContext().getLocale());
            }
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                Object val = entry.getValue();
                if (parse && val instanceof String) {
                    val = TextParseUtil.translateVariables(val.toString(), stack);
                }
                try {
                    newStack.setValue(entry.getKey(), val);
                } catch (RuntimeException e) {
                    if (devMode) {
                        String developerNotification = localizedTextProvider.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.getActionContext().withConversionErrors(newStack.getActionContext().getConversionErrors());
            }
            if (merge)
                addParametersToContext(ac, parameters);
        } finally {
            ReflectionContextState.setCreatingNullObjects(contextMap, false);
            ReflectionContextState.setReportingConversionErrors(contextMap, false);
        }
    }
    return invocation.invoke();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ClearableValueStack(com.opensymphony.xwork2.util.ClearableValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionContext(com.opensymphony.xwork2.ActionContext) ClearableValueStack(com.opensymphony.xwork2.util.ClearableValueStack) Parameterizable(com.opensymphony.xwork2.config.entities.Parameterizable) Map(java.util.Map)

Aggregations

ActionContext (com.opensymphony.xwork2.ActionContext)1 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)1 Parameterizable (com.opensymphony.xwork2.config.entities.Parameterizable)1 ClearableValueStack (com.opensymphony.xwork2.util.ClearableValueStack)1 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 Map (java.util.Map)1