use of com.opensymphony.xwork2.config.entities.InterceptorStackConfig 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.InterceptorStackConfig 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.InterceptorStackConfig in project struts by apache.
the class XmlConfigurationProviderInterceptorsTest method testBasicInterceptors.
public void testBasicInterceptors() throws ConfigurationException {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-interceptors-basic.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename);
// setup expectations
// the test interceptor with a parameter
Map<String, String> params = new HashMap<>();
params.put("foo", "expectedFoo");
InterceptorConfig paramsInterceptor = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).addParams(params).build();
// the default interceptor stack
InterceptorStackConfig defaultStack = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).build();
// the derivative interceptor stack
InterceptorStackConfig derivativeStack = new InterceptorStackConfig.Builder("derivativeStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).addInterceptor(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>()))).build();
// execute the configuration
provider.init(configuration);
provider.loadPackages();
PackageConfig pkg = configuration.getPackageConfig("default");
Map interceptorConfigs = pkg.getInterceptorConfigs();
// assertions for size
assertEquals(5, interceptorConfigs.size());
// assertions for interceptors
assertEquals(noopInterceptor, interceptorConfigs.get("noop"));
assertEquals(loggingInterceptor, interceptorConfigs.get("logging"));
assertEquals(paramsInterceptor, interceptorConfigs.get("test"));
// assertions for interceptor stacks
assertEquals(defaultStack, interceptorConfigs.get("defaultStack"));
assertEquals(derivativeStack, interceptorConfigs.get("derivativeStack"));
}
use of com.opensymphony.xwork2.config.entities.InterceptorStackConfig in project struts by apache.
the class Dispatcher method cleanup.
/**
* Releases all instances bound to this dispatcher instance.
*/
public void cleanup() {
// clean up ObjectFactory
ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class);
if (objectFactory == null) {
LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed");
}
if (objectFactory instanceof ObjectFactoryDestroyable) {
try {
((ObjectFactoryDestroyable) objectFactory).destroy();
} catch (Exception e) {
// catch any exception that may occurred during destroy() and log it
LOG.error("Exception occurred while destroying ObjectFactory [{}]", objectFactory.toString(), e);
}
}
// clean up Dispatcher itself for this thread
instance.set(null);
servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null);
// clean up DispatcherListeners
if (!dispatcherListeners.isEmpty()) {
for (DispatcherListener l : dispatcherListeners) {
l.dispatcherDestroyed(this);
}
}
// clean up all interceptors by calling their destroy() method
Set<Interceptor> interceptors = new HashSet<>();
Collection<PackageConfig> packageConfigs = configurationManager.getConfiguration().getPackageConfigs().values();
for (PackageConfig packageConfig : packageConfigs) {
for (Object config : packageConfig.getAllInterceptorConfigs().values()) {
if (config instanceof InterceptorStackConfig) {
for (InterceptorMapping interceptorMapping : ((InterceptorStackConfig) config).getInterceptors()) {
interceptors.add(interceptorMapping.getInterceptor());
}
}
}
}
for (Interceptor interceptor : interceptors) {
interceptor.destroy();
}
// Clear container holder when application is unloaded / server shutdown
ContainerHolder.clear();
// cleanup action context
ActionContext.clear();
// clean up configuration
configurationManager.destroyConfiguration();
configurationManager = null;
}
use of com.opensymphony.xwork2.config.entities.InterceptorStackConfig in project struts by apache.
the class InterceptorBuilderTest method testMultipleSameInterceptors.
public void testMultipleSameInterceptors() throws Exception {
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();
InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("multiStack").addInterceptor(new InterceptorMapping(interceptorConfig1.getName(), objectFactory.buildInterceptor(interceptorConfig1, Collections.<String, String>emptyMap()))).addInterceptor(new InterceptorMapping(interceptorConfig2.getName(), objectFactory.buildInterceptor(interceptorConfig2, Collections.<String, String>emptyMap()))).addInterceptor(new InterceptorMapping(interceptorConfig1.getName(), objectFactory.buildInterceptor(interceptorConfig1, Collections.<String, String>emptyMap()))).build();
PackageConfig packageConfig = new PackageConfig.Builder("package1").namespace("/namespace").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorConfig(interceptorConfig1).addInterceptorStackConfig(interceptorStackConfig1).build();
List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "multiStack", new LinkedHashMap<String, String>() {
{
put("interceptor1.param1", "interceptor1_value1");
put("interceptor1.param2", "interceptor1_value2");
}
}, null, objectFactory);
assertEquals(interceptorMappings.size(), 3);
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(((InterceptorMapping) interceptorMappings.get(2)).getName(), "interceptor1");
assertNotNull(((InterceptorMapping) interceptorMappings.get(2)).getInterceptor());
assertEquals(((InterceptorMapping) interceptorMappings.get(2)).getInterceptor().getClass(), MockInterceptor1.class);
assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(2)).getInterceptor()).getParam1(), "interceptor1_value1");
assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(2)).getInterceptor()).getParam2(), "interceptor1_value2");
}
Aggregations