use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.
the class SpringObjectFactoryTest method testFallsBackToDefaultObjectFactoryInterceptorBuilding.
public void testFallsBackToDefaultObjectFactoryInterceptorBuilding() throws Exception {
InterceptorConfig iConfig = new InterceptorConfig.Builder("timer", ModelDrivenInterceptor.class.getName()).build();
Interceptor interceptor = objectFactory.buildInterceptor(iConfig, new HashMap<String, String>());
assertEquals(ModelDrivenInterceptor.class, interceptor.getClass());
}
use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.
the class InterceptorBuilder method constructInterceptorReference.
/**
* Builds a list of interceptors referenced by the refName in the supplied PackageConfig (InterceptorMapping object).
*
* @param interceptorLocator interceptor locator
* @param refName reference name
* @param refParams reference parameters
* @param location location
* @param objectFactory object factory
* @return list of interceptors referenced by the refName in the supplied PackageConfig (InterceptorMapping object).
* @throws ConfigurationException in case of any configuration errors
*/
public static List<InterceptorMapping> constructInterceptorReference(InterceptorLocator interceptorLocator, String refName, Map<String, String> refParams, Location location, ObjectFactory objectFactory) throws ConfigurationException {
Object referencedConfig = interceptorLocator.getInterceptorConfig(refName);
List<InterceptorMapping> result = new ArrayList<>();
if (referencedConfig == null) {
throw new ConfigurationException("Unable to find interceptor class referenced by ref-name " + refName, location);
} else {
if (referencedConfig instanceof InterceptorConfig) {
InterceptorConfig config = (InterceptorConfig) referencedConfig;
Interceptor inter;
try {
inter = objectFactory.buildInterceptor(config, refParams);
result.add(new InterceptorMapping(refName, inter, refParams));
} catch (ConfigurationException ex) {
LOG.warn(new ParameterizedMessage("Unable to load config class {} at {} probably due to a missing jar, which might be fine if you never plan to use the {} interceptor", config.getClassName(), ex.getLocation(), config.getName()), ex);
}
} else if (referencedConfig instanceof InterceptorStackConfig) {
InterceptorStackConfig stackConfig = (InterceptorStackConfig) referencedConfig;
if ((refParams != null) && (refParams.size() > 0)) {
result = constructParameterizedInterceptorReferences(interceptorLocator, stackConfig, refParams, objectFactory);
} else {
result.addAll(stackConfig.getInterceptors());
}
} else {
LOG.error("Got unexpected type for interceptor {}. Got {}", refName, referencedConfig);
}
}
return result;
}
use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.
the class PlexusObjectFactory method buildInterceptor.
/* (non-Javadoc)
* @see com.opensymphony.xwork2.ObjectFactory#buildInterceptor(com.opensymphony.xwork2.config.entities.InterceptorConfig, java.util.Map)
*/
public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map interceptorRefParams) throws ConfigurationException {
String interceptorClassName = interceptorConfig.getClassName();
Map thisInterceptorClassParams = interceptorConfig.getParams();
Map params = (thisInterceptorClassParams == null) ? new HashMap() : new HashMap(thisInterceptorClassParams);
params.putAll(interceptorRefParams);
String message;
Throwable cause;
try {
Map extraContext = new HashMap();
extraContext.put(PLEXUS_COMPONENT_TYPE, Interceptor.class.getName());
Interceptor interceptor = (Interceptor) buildBean(interceptorClassName, extraContext);
reflectionProvider.setProperties(params, interceptor);
interceptor.init();
return interceptor;
} 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);
}
use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.
the class XmlConfigurationProviderInterceptorsSpringTest method testInterceptorsLoadedFromSpringApplicationContext.
public void testInterceptorsLoadedFromSpringApplicationContext() throws ConfigurationException {
sac.registerSingleton("noop-interceptor", NoOpInterceptor.class, new MutablePropertyValues());
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-interceptors-spring.xml";
// Expect a ConfigurationException to be thrown if the interceptor reference
// cannot be resolved
ConfigurationProvider provider = buildConfigurationProvider(filename);
// execute the configuration
provider.init(configuration);
provider.loadPackages();
PackageConfig pkg = configuration.getPackageConfig("default");
Map interceptorConfigs = pkg.getInterceptorConfigs();
// assertions for size
assertEquals(1, interceptorConfigs.size());
// assertions for interceptors
InterceptorConfig seen = (InterceptorConfig) interceptorConfigs.get("noop");
assertEquals("noop-interceptor", seen.getClassName());
}
use of com.opensymphony.xwork2.config.entities.InterceptorConfig in project struts by apache.
the class InterceptorBuilderTest method testBuildInterceptor_2.
/**
* Try to test this
* <interceptor-ref name="interceptorStack1">
* <param name="interceptorStack2.interceptor1.param1">interceptor1_value1</param>
* <param name="interceptorStack2.interceptor1.param2">interceptor1_value2</param>
* <param name="interceptorStack3.interceptor2.param1">interceptor2_value1</param>
* <param name="interceptorStack3.interceptor2.param2">interceptor2_value2</param>
* </interceptor-ref>
*
* @throws Exception
*/
public void testBuildInterceptor_2() throws Exception {
InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("interceptorStack1").build();
InterceptorStackConfig interceptorStackConfig2 = new InterceptorStackConfig.Builder("interceptorStack2").build();
InterceptorStackConfig interceptorStackConfig3 = new InterceptorStackConfig.Builder("interceptorStack3").build();
InterceptorConfig interceptorConfig1 = new InterceptorConfig.Builder("interceptor1", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor1").build();
InterceptorConfig interceptorConfig2 = new InterceptorConfig.Builder("interceptor2", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor2").build();
PackageConfig packageConfig = new PackageConfig.Builder("package1").namespace("/namspace").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorStackConfig(interceptorStackConfig1).addInterceptorStackConfig(interceptorStackConfig2).addInterceptorStackConfig(interceptorStackConfig3).build();
List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "interceptorStack1", new LinkedHashMap<String, String>() {
private static final long serialVersionUID = -5819935102242042570L;
{
put("interceptorStack2.interceptor1.param1", "interceptor1_value1");
put("interceptorStack2.interceptor1.param2", "interceptor1_value2");
put("interceptorStack3.interceptor2.param1", "interceptor2_value1");
put("interceptorStack3.interceptor2.param2", "interceptor2_value2");
}
}, null, objectFactory);
assertEquals(interceptorMappings.size(), 2);
assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getName(), "interceptor1");
assertNotNull(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor());
assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor().getClass(), MockInterceptor1.class);
assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam1(), "interceptor1_value1");
assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam2(), "interceptor1_value2");
assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getName(), "interceptor2");
assertNotNull(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor());
assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor().getClass(), MockInterceptor2.class);
assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam1(), "interceptor2_value1");
assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam2(), "interceptor2_value2");
}
Aggregations