All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opensymphony.xwork2.interceptor.StaticParametersInterceptor Maven / Gradle / Ivy

Go to download

XWork is an command-pattern framework that is used to power WebWork as well as other applications. XWork provides an Inversion of Control container, a powerful expression language, data type conversion, validation, and pluggable configuration.

The newest version!
/*
 * Copyright (c) 2002-2006 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.xwork2.interceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.Parameterizable;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;


/**
 * 
 *
 * This interceptor populates the action with the static parameters defined in the action configuration. If the action
 * implements {@link Parameterizable}, a map of the static parameters will be also be passed directly to the action.
 *
 * 

Parameters are typically defined with <param> elements within xwork.xml. * * * *

Interceptor parameters: * * * *

    * *
  • None
  • * *
* * * *

Extending the interceptor: * * * *

There are no extension points to this interceptor. * * * *

Example code: * *

 * 
 * <action name="someAction" class="com.examples.SomeAction">
 *     <interceptor-ref name="staticParams">
 *          <param name="parse">true</param>
 *          <param name="overwrite">false</param>
 *     </interceptor-ref>
 *     <result name="success">good_result.ftl</result>
 * </action>
 * 
 * 
* * @author Patrick Lightbody */ public class StaticParametersInterceptor extends AbstractInterceptor { private boolean parse; private boolean overwrite; static boolean devMode = false; private static final Logger LOG = LoggerFactory.getLogger(StaticParametersInterceptor.class); @Inject("devMode") public static void setDevMode(String mode) { devMode = "true".equals(mode); } public void setParse(String value) { this.parse = Boolean.valueOf(value).booleanValue(); } /** * Overwrites already existing parameters from other sources. * Static parameters are the successor over previously set parameters, if true. * * @param value */ public void setOverwrite(String value) { this.overwrite = Boolean.valueOf(value).booleanValue(); } @Override public String intercept(ActionInvocation invocation) throws Exception { ActionConfig config = invocation.getProxy().getConfig(); Object action = invocation.getAction(); final Map parameters = config.getParams(); if (LOG.isDebugEnabled()) { 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 contextMap = ac.getContextMap(); try { ReflectionContextState.setCreatingNullObjects(contextMap, true); ReflectionContextState.setReportingConversionErrors(contextMap, true); final ValueStack stack = ac.getValueStack(); for (Map.Entry entry : parameters.entrySet()) { Object val = entry.getValue(); if (parse && val instanceof String) { val = TextParseUtil.translateVariables(val.toString(), stack); } try { stack.setValue(entry.getKey(), val); } 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); } } } } addParametersToContext(ac, parameters); } finally { ReflectionContextState.setCreatingNullObjects(contextMap, false); ReflectionContextState.setReportingConversionErrors(contextMap, false); } } return invocation.invoke(); } /** * @param ac The action context * @return the parameters from the action mapping in the context. If none found, returns * an empty map. */ protected Map retrieveParameters(ActionContext ac) { ActionConfig config = ac.getActionInvocation().getProxy().getConfig(); if (config != null) { return config.getParams(); } else { return Collections.emptyMap(); } } /** * Adds the parameters into context's ParameterMap. * As default, static parameters will not overwrite existing paramaters from other sources. * If you want the static parameters as successor over already existing parameters, set overwrite to true. * * @param ac The action context * @param newParams The parameter map to apply */ protected void addParametersToContext(ActionContext ac, Map newParams) { Map previousParams = ac.getParameters(); Map combinedParams; if ( overwrite ) { if (previousParams != null) { combinedParams = new TreeMap(previousParams); } else { combinedParams = new TreeMap(); } if ( newParams != null) { combinedParams.putAll(newParams); } } else { if (newParams != null) { combinedParams = new TreeMap(newParams); } else { combinedParams = new TreeMap(); } if ( previousParams != null) { combinedParams.putAll(previousParams); } } ac.setParameters(combinedParams); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy