use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class DefaultInterceptorFactory method buildInterceptor.
public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map<String, String> interceptorRefParams) throws ConfigurationException {
String interceptorClassName = interceptorConfig.getClassName();
Map<String, String> thisInterceptorClassParams = interceptorConfig.getParams();
Map<String, String> params = (thisInterceptorClassParams == null) ? new HashMap<String, String>() : new HashMap<>(thisInterceptorClassParams);
params.putAll(interceptorRefParams);
String message;
Throwable cause;
try {
// interceptor instances are long-lived and used across user sessions, so don't try to pass in any extra context
Object o = objectFactory.buildBean(interceptorClassName, null);
if (o instanceof WithLazyParams) {
LOG.debug("Interceptor {} is marked with interface {} and params will be set during action invocation", interceptorClassName, WithLazyParams.class.getName());
} else {
reflectionProvider.setProperties(params, o);
}
if (o instanceof Interceptor) {
Interceptor interceptor = (Interceptor) o;
interceptor.init();
return interceptor;
}
throw new ConfigurationException("Class [" + interceptorClassName + "] does not implement Interceptor", interceptorConfig);
} catch (InstantiationException e) {
cause = e;
message = "Unable to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
} catch (IllegalAccessException e) {
cause = e;
message = "IllegalAccessException while attempting to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
} catch (ClassCastException e) {
cause = e;
message = "Class [" + interceptorClassName + "] does not implement com.opensymphony.xwork2.interceptor.Interceptor";
} catch (Exception e) {
cause = e;
message = "Caught Exception while registering Interceptor class " + interceptorClassName;
} catch (NoClassDefFoundError e) {
cause = e;
message = "Could not load class " + interceptorClassName + ". Perhaps it exists but certain dependencies are not available?";
}
throw new ConfigurationException(message, cause, interceptorConfig);
}
use of com.opensymphony.xwork2.interceptor.Interceptor 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;
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class ValidateAction method testNoParametersAction.
public void testNoParametersAction() throws Exception {
ParametersInterceptor interceptor = new ParametersInterceptor();
interceptor.init();
MockActionInvocation mai = new MockActionInvocation();
Action action = new NoParametersAction();
mai.setAction(action);
interceptor.doIntercept(mai);
interceptor.destroy();
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class XmlConfigurationProviderInterceptorParamOverridingTest method testInterceptorParamOveriding.
public void testInterceptorParamOveriding() throws Exception {
DefaultConfiguration conf = new DefaultConfiguration();
final XmlConfigurationProvider p = new StrutsXmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-param-overriding.xml");
DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
factory.setContainer(container);
factory.setFileManager(new DefaultFileManager());
p.setFileManagerFactory(factory);
conf.reloadContainer(new ArrayList<ContainerProvider>() {
{
add(new StrutsDefaultConfigurationProvider());
add(p);
}
});
RuntimeConfiguration rtConf = conf.getRuntimeConfiguration();
ActionConfig actionOne = rtConf.getActionConfig("", "actionOne");
ActionConfig actionTwo = rtConf.getActionConfig("", "actionTwo");
List<InterceptorMapping> actionOneInterceptors = actionOne.getInterceptors();
List<InterceptorMapping> actionTwoInterceptors = actionTwo.getInterceptors();
assertNotNull(actionOne);
assertNotNull(actionTwo);
assertNotNull(actionOneInterceptors);
assertNotNull(actionTwoInterceptors);
assertEquals(actionOneInterceptors.size(), 3);
assertEquals(actionTwoInterceptors.size(), 3);
InterceptorMapping actionOneInterceptorMapping1 = actionOneInterceptors.get(0);
InterceptorMapping actionOneInterceptorMapping2 = actionOneInterceptors.get(1);
InterceptorMapping actionOneInterceptorMapping3 = actionOneInterceptors.get(2);
InterceptorMapping actionTwoInterceptorMapping1 = actionTwoInterceptors.get(0);
InterceptorMapping actionTwoInterceptorMapping2 = actionTwoInterceptors.get(1);
InterceptorMapping actionTwoInterceptorMapping3 = actionTwoInterceptors.get(2);
assertNotNull(actionOneInterceptorMapping1);
assertNotNull(actionOneInterceptorMapping2);
assertNotNull(actionOneInterceptorMapping3);
assertNotNull(actionTwoInterceptorMapping1);
assertNotNull(actionTwoInterceptorMapping2);
assertNotNull(actionTwoInterceptorMapping3);
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamOne(), "i1p1");
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamTwo(), "i1p2");
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamOne(), "i2p1");
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamTwo(), null);
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamOne(), null);
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamTwo(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamOne(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamTwo(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamOne(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamTwo(), "i2p2");
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamOne(), "i3p1");
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamTwo(), "i3p2");
}
use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.
the class XmlConfigurationProviderInterceptorStackParamOverridingTest method testInterceptorStackParamOveriding.
public void testInterceptorStackParamOveriding() throws Exception {
DefaultConfiguration conf = new DefaultConfiguration();
final XmlConfigurationProvider p = new StrutsXmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-stack-param-overriding.xml");
DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
factory.setContainer(container);
factory.setFileManager(new DefaultFileManager());
p.setFileManagerFactory(factory);
configurationManager.addContainerProvider(p);
conf.reloadContainer(new ArrayList<ContainerProvider>() {
{
add(new StrutsDefaultConfigurationProvider());
add(p);
}
});
RuntimeConfiguration rtConf = conf.getRuntimeConfiguration();
ActionConfig actionOne = rtConf.getActionConfig("", "actionOne");
ActionConfig actionTwo = rtConf.getActionConfig("", "actionTwo");
List actionOneInterceptors = actionOne.getInterceptors();
List actionTwoInterceptors = actionTwo.getInterceptors();
assertNotNull(actionOne);
assertNotNull(actionTwo);
assertNotNull(actionOneInterceptors);
assertNotNull(actionTwoInterceptors);
assertEquals(actionOneInterceptors.size(), 3);
assertEquals(actionTwoInterceptors.size(), 3);
InterceptorMapping actionOneInterceptorMapping1 = (InterceptorMapping) actionOneInterceptors.get(0);
InterceptorMapping actionOneInterceptorMapping2 = (InterceptorMapping) actionOneInterceptors.get(1);
InterceptorMapping actionOneInterceptorMapping3 = (InterceptorMapping) actionOneInterceptors.get(2);
InterceptorMapping actionTwoInterceptorMapping1 = (InterceptorMapping) actionTwoInterceptors.get(0);
InterceptorMapping actionTwoInterceptorMapping2 = (InterceptorMapping) actionTwoInterceptors.get(1);
InterceptorMapping actionTwoInterceptorMapping3 = (InterceptorMapping) actionTwoInterceptors.get(2);
assertNotNull(actionOneInterceptorMapping1);
assertNotNull(actionOneInterceptorMapping2);
assertNotNull(actionOneInterceptorMapping3);
assertNotNull(actionTwoInterceptorMapping1);
assertNotNull(actionTwoInterceptorMapping2);
assertNotNull(actionTwoInterceptorMapping3);
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamOne(), "i1p1");
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamTwo(), "i1p2");
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamOne(), "i2p1");
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamTwo(), null);
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamOne(), null);
assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamTwo(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamOne(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamTwo(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamOne(), null);
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamTwo(), "i2p2");
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamOne(), "i3p1");
assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamTwo(), "i3p2");
}
Aggregations