WW-4667 Applies params to all instances of interceptor defined in stack

This commit is contained in:
Lukasz Lenart
2016-09-06 09:07:02 +02:00
parent cc0d52f216
commit 6f5ddca471
2 changed files with 54 additions and 6 deletions
@@ -184,12 +184,14 @@ public class InterceptorBuilder {
Interceptor interceptor = objectFactory.buildInterceptor(cfg, map);
InterceptorMapping mapping = new InterceptorMapping(key, interceptor);
if (result != null && result.contains(mapping)) {
// if an existing interceptor mapping exists,
// we remove from the result Set, just to make sure
// there's always one unique mapping.
int index = result.indexOf(mapping);
result.set(index, mapping);
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 [#0] with new mapping #1 using new params #2", key, interceptorMapping, map);
result.set(index, mapping);
}
}
} else {
result.add(mapping);
}
@@ -9,6 +9,7 @@ import com.opensymphony.xwork2.config.entities.InterceptorStackConfig;
import com.opensymphony.xwork2.config.entities.PackageConfig;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
@@ -78,6 +79,51 @@ public class InterceptorBuilderTest extends XWorkTestCase {
assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam2(), "interceptor2_value2");
}
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");
}
/**
* Try to test this
* <interceptor-ref name="interceptorStack1">