com.opensymphony.xwork2.interceptor.AliasInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xwork Show documentation
Show all versions of xwork Show documentation
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.config.entities.ActionConfig;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import java.util.Map;
/**
*
*
* The aim of this Interceptor is to alias a named parameter to a different named parameter. By acting as the glue
* between actions sharing similiar parameters (but with different names), it can help greatly with action chaining.
*
* Action's alias expressions should be in the form of #{ "name1" : "alias1", "name2" : "alias2" }
.
* This means that assuming an action (or something else in the stack) has a value for the expression named name1 and the
* action this interceptor is applied to has a setter named alias1, alias1 will be set with the value from
* name1.
*
*
*
* Interceptor parameters:
*
*
*
*
*
* - aliasesKey (optional) - the name of the action parameter to look for the alias map (by default this is
* aliases).
*
*
*
*
*
* Extending the interceptor:
*
*
*
*
*
* This interceptor does not have any known extension points.
*
*
*
* Example code:
*
*
*
* <action name="someAction" class="com.examples.SomeAction">
* <!-- The value for the foo parameter will be applied as if it were named bar -->
* <param name="aliases">#{ 'foo' : 'bar' }</param>
*
* <interceptor-ref name="alias"/>
* <interceptor-ref name="basicStack"/>
* <result name="success">good_result.ftl</result>
* </action>
*
*
*
* @author Matthew Payne
*/
public class AliasInterceptor extends AbstractInterceptor {
private static final Logger log = LoggerFactory.getLogger(AliasInterceptor.class);
private static final String DEFAULT_ALIAS_KEY = "aliases";
protected String aliasesKey = DEFAULT_ALIAS_KEY;
/**
* Sets the name of the action parameter to look for the alias map.
*
* Default is aliases
.
*
* @param aliasesKey the name of the action parameter
*/
public void setAliasesKey(String aliasesKey) {
this.aliasesKey = aliasesKey;
}
@Override public String intercept(ActionInvocation invocation) throws Exception {
ActionConfig config = invocation.getProxy().getConfig();
ActionContext ac = invocation.getInvocationContext();
// get the action's parameters
final Map 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) {
// 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 contextParameters = (Map) stack.getContext().get("parameters");
if (null != contextParameters) {
value = contextParameters.get(name);
}
}
if (null != value) {
stack.setValue(alias, value);
}
}
} else {
log.debug("invalid alias expression:" + aliasesKey);
}
}
return invocation.invoke();
}
}