use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.
the class InterceptorBuilder method constructParameterizedInterceptorReferences.
/**
* Builds a list of interceptors referenced by the refName in the supplied PackageConfig overriding the properties
* of the referenced interceptor with refParams.
*
* @param interceptorLocator interceptor locator
* @param stackConfig interceptor stack configuration
* @param refParams The overridden interceptor properties
* @return list of interceptors referenced by the refName in the supplied PackageConfig overridden with refParams.
*/
private static List<InterceptorMapping> constructParameterizedInterceptorReferences(InterceptorLocator interceptorLocator, InterceptorStackConfig stackConfig, Map<String, String> refParams, ObjectFactory objectFactory) {
List<InterceptorMapping> result;
Map<String, Map<String, String>> params = new LinkedHashMap<>();
/*
* We strip
*
* <interceptor-ref name="someStack">
* <param name="interceptor1.param1">someValue</param>
* <param name="interceptor1.param2">anotherValue</param>
* </interceptor-ref>
*
* down to map
* interceptor1 -> [param1 -> someValue, param2 -> anotherValue]
*
* or
* <interceptor-ref name="someStack">
* <param name="interceptorStack1.interceptor1.param1">someValue</param>
* <param name="interceptorStack1.interceptor1.param2">anotherValue</param>
* </interceptor-ref>
*
* down to map
* interceptorStack1 -> [interceptor1.param1 -> someValue, interceptor1.param2 -> anotherValue]
*
*/
for (Map.Entry<String, String> entry : refParams.entrySet()) {
String key = entry.getKey();
try {
String name = key.substring(0, key.indexOf('.'));
key = key.substring(key.indexOf('.') + 1);
Map<String, String> map;
if (params.containsKey(name)) {
map = params.get(name);
} else {
map = new LinkedHashMap<>();
}
map.put(key, entry.getValue());
params.put(name, map);
} catch (Exception e) {
LOG.warn("No interceptor found for name = {}", key);
}
}
result = new ArrayList<>(stackConfig.getInterceptors());
for (Map.Entry<String, Map<String, String>> entry : params.entrySet()) {
String key = entry.getKey();
Map<String, String> map = entry.getValue();
Object interceptorCfgObj = interceptorLocator.getInterceptorConfig(key);
/*
* Now we attempt to separate out param that refers to Interceptor
* and Interceptor stack, eg.
*
* <interceptor-ref name="someStack">
* <param name="interceptor1.param1">someValue</param>
* ...
* </interceptor-ref>
*
* vs
*
* <interceptor-ref name="someStack">
* <param name="interceptorStack1.interceptor1.param1">someValue</param>
* ...
* </interceptor-ref>
*/
if (interceptorCfgObj instanceof InterceptorConfig) {
// interceptor-ref param refer to an interceptor
InterceptorConfig cfg = (InterceptorConfig) interceptorCfgObj;
Interceptor interceptor = objectFactory.buildInterceptor(cfg, map);
InterceptorMapping mapping = new InterceptorMapping(key, interceptor);
if (result.contains(mapping)) {
for (int index = 0; index < result.size(); index++) {
InterceptorMapping interceptorMapping = result.get(index);
if (interceptorMapping.getName().equals(key)) {
LOG.debug("Overriding interceptor config [{}] with new mapping {} using new params {}", key, interceptorMapping, map);
result.set(index, mapping);
}
}
} else {
result.add(mapping);
}
} else if (interceptorCfgObj instanceof InterceptorStackConfig) {
// interceptor-ref param refer to an interceptor stack
// If its an interceptor-stack, we call this method recursively until,
// all the params (eg. interceptorStack1.interceptor1.param etc.)
// are resolved down to a specific interceptor.
InterceptorStackConfig stackCfg = (InterceptorStackConfig) interceptorCfgObj;
List<InterceptorMapping> tmpResult = constructParameterizedInterceptorReferences(interceptorLocator, stackCfg, map, objectFactory);
for (InterceptorMapping tmpInterceptorMapping : tmpResult) {
if (result.contains(tmpInterceptorMapping)) {
int index = result.indexOf(tmpInterceptorMapping);
result.set(index, tmpInterceptorMapping);
} else {
result.add(tmpInterceptorMapping);
}
}
}
}
return result;
}
use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.
the class XmlConfigurationProvider method addAction.
protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
String name = actionElement.getAttribute("name");
String className = actionElement.getAttribute("class");
// methodName should be null if it's not set
String methodName = StringUtils.trimToNull(actionElement.getAttribute("method"));
Location location = DomHelper.getLocationObject(actionElement);
if (location == null) {
LOG.warn("Location null for {}", className);
}
// use the default-class-ref from the <package/>
if (StringUtils.isEmpty(className)) {
// if there is a package default-class-ref use that, otherwise use action support
/* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) {
className = packageContext.getDefaultClassRef();
} else {
className = ActionSupport.class.getName();
}*/
} else {
if (!verifyAction(className, name, location)) {
LOG.error("Unable to verify action [{}] with class [{}], from [{}]", name, className, location);
return;
}
}
Map<String, ResultConfig> results;
try {
results = buildResults(actionElement, packageContext);
} catch (ConfigurationException e) {
throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
}
List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
Set<String> allowedMethods = buildAllowedMethods(actionElement, packageContext);
ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className).methodName(methodName).addResultConfigs(results).addInterceptors(interceptorList).addExceptionMappings(exceptionMappings).addParams(XmlHelper.getParams(actionElement)).setStrictMethodInvocation(packageContext.isStrictMethodInvocation()).addAllowedMethod(allowedMethods).location(location).build();
packageContext.addActionConfig(name, actionConfig);
LOG.debug("Loaded {}{} in '{}' package: {}", StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "", name, packageContext.getName(), actionConfig);
}
use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.
the class XmlConfigurationProvider method buildInterceptorList.
protected List<InterceptorMapping> buildInterceptorList(Element element, PackageConfig.Builder context) throws ConfigurationException {
List<InterceptorMapping> interceptorList = new ArrayList<>();
NodeList interceptorRefList = element.getElementsByTagName("interceptor-ref");
for (int i = 0; i < interceptorRefList.getLength(); i++) {
Element interceptorRefElement = (Element) interceptorRefList.item(i);
if (interceptorRefElement.getParentNode().equals(element) || interceptorRefElement.getParentNode().getNodeName().equals(element.getNodeName())) {
List<InterceptorMapping> interceptors = lookupInterceptorReference(context, interceptorRefElement);
interceptorList.addAll(interceptors);
}
}
return interceptorList;
}
use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.
the class XmlConfigurationProvider method loadInterceptorStack.
protected InterceptorStackConfig loadInterceptorStack(Element element, PackageConfig.Builder context) throws ConfigurationException {
String name = element.getAttribute("name");
InterceptorStackConfig.Builder config = new InterceptorStackConfig.Builder(name).location(DomHelper.getLocationObject(element));
NodeList interceptorRefList = element.getElementsByTagName("interceptor-ref");
for (int j = 0; j < interceptorRefList.getLength(); j++) {
Element interceptorRefElement = (Element) interceptorRefList.item(j);
List<InterceptorMapping> interceptors = lookupInterceptorReference(context, interceptorRefElement);
config.addInterceptors(interceptors);
}
return config.build();
}
use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.
the class DefaultActionInvocation method invoke.
/**
* @throws ConfigurationException If no result can be found with the returned code
*/
public String invoke() throws Exception {
if (executed) {
throw new IllegalStateException("Action has already executed");
}
if (asyncManager == null || !asyncManager.hasAsyncActionResult()) {
if (interceptors.hasNext()) {
final InterceptorMapping interceptorMapping = interceptors.next();
Interceptor interceptor = interceptorMapping.getInterceptor();
if (interceptor instanceof WithLazyParams) {
interceptor = lazyParamInjector.injectParams(interceptor, interceptorMapping.getParams(), invocationContext);
}
resultCode = interceptor.intercept(DefaultActionInvocation.this);
} else {
resultCode = invokeActionOnly();
}
} else {
Object asyncActionResult = asyncManager.getAsyncActionResult();
if (asyncActionResult instanceof Throwable) {
throw new Exception((Throwable) asyncActionResult);
}
asyncAction = null;
resultCode = saveResult(proxy.getConfig(), asyncActionResult);
}
if (asyncManager == null || asyncAction == null) {
// return above and flow through again
if (!executed) {
if (preResultListeners != null) {
LOG.trace("Executing PreResultListeners for result [{}]", result);
for (Object preResultListener : preResultListeners) {
PreResultListener listener = (PreResultListener) preResultListener;
listener.beforeResult(this, resultCode);
}
}
// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
executeResult();
}
executed = true;
}
} else {
asyncManager.invokeAsyncAction(asyncAction);
}
return resultCode;
}
Aggregations