Skip to content

Commit 9f9e06d

Browse files
committed
SWS-713 - Order of interceptor calls is dependent on the way the interceptors are included
1 parent 761c354 commit 9f9e06d

File tree

5 files changed

+173
-82
lines changed

5 files changed

+173
-82
lines changed

core/src/main/java/org/springframework/ws/config/InterceptorsBeanDefinitionParser.java

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -74,73 +74,71 @@ else if ("ref".equals(childElement.getLocalName())) {
7474

7575
}
7676
else if ("payloadRoot".equals(childElement.getLocalName())) {
77-
// bean elements
78-
List<Element> beanElements = DomUtils.getChildElementsByTagName(childElement, "bean");
79-
for (Element beanElement : beanElements) {
80-
RootBeanDefinition smartInterceptorDef =
81-
createSmartInterceptorDefinition(PayloadRootSmartSoapEndpointInterceptor.class, childElement,
82-
parserContext);
83-
BeanDefinitionHolder interceptorDef = createInterceptorDefinition(parserContext, beanElement);
84-
85-
String namespaceUri = childElement.getAttribute("namespaceUri");
86-
String localPart = childElement.getAttribute("localPart");
87-
88-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef);
89-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, namespaceUri);
90-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, localPart);
91-
92-
registerSmartInterceptor(parserContext, smartInterceptorDef);
93-
}
94-
95-
// ref elements
96-
List<Element> refElements = DomUtils.getChildElementsByTagName(childElement, "ref");
97-
for (Element refElement : refElements) {
98-
RootBeanDefinition smartInterceptorDef =
99-
createSmartInterceptorDefinition(PayloadRootSmartSoapEndpointInterceptor.class, childElement,
100-
parserContext);
101-
BeanReference interceptorRef = createInterceptorReference(parserContext, refElement);
102-
103-
String namespaceUri = childElement.getAttribute("namespaceUri");
104-
String localPart = childElement.getAttribute("localPart");
105-
106-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef);
107-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, namespaceUri);
108-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, localPart);
109-
110-
registerSmartInterceptor(parserContext, smartInterceptorDef);
77+
List<Element> payloadRootChildren = DomUtils.getChildElements(childElement);
78+
for (Element payloadRootChild : payloadRootChildren) {
79+
if ("bean".equals(payloadRootChild.getLocalName())) {
80+
RootBeanDefinition smartInterceptorDef =
81+
createSmartInterceptorDefinition(PayloadRootSmartSoapEndpointInterceptor.class,
82+
childElement, parserContext);
83+
BeanDefinitionHolder interceptorDef =
84+
createInterceptorDefinition(parserContext, payloadRootChild);
85+
86+
String namespaceUri = childElement.getAttribute("namespaceUri");
87+
String localPart = childElement.getAttribute("localPart");
88+
89+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef);
90+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, namespaceUri);
91+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, localPart);
92+
93+
registerSmartInterceptor(parserContext, smartInterceptorDef);
94+
}
95+
else if ("ref".equals(payloadRootChild.getLocalName())) {
96+
RootBeanDefinition smartInterceptorDef =
97+
createSmartInterceptorDefinition(PayloadRootSmartSoapEndpointInterceptor.class,
98+
childElement, parserContext);
99+
BeanReference interceptorRef = createInterceptorReference(parserContext, payloadRootChild);
100+
101+
String namespaceUri = childElement.getAttribute("namespaceUri");
102+
String localPart = childElement.getAttribute("localPart");
103+
104+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef);
105+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, namespaceUri);
106+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, localPart);
107+
108+
registerSmartInterceptor(parserContext, smartInterceptorDef);
109+
}
111110
}
112111
}
113112
else if ("soapAction".equals(childElement.getLocalName())) {
114-
// bean elements
115-
List<Element> beanElements = DomUtils.getChildElementsByTagName(childElement, "bean");
116-
for (Element beanElement : beanElements) {
117-
RootBeanDefinition smartInterceptorDef =
118-
createSmartInterceptorDefinition(SoapActionSmartEndpointInterceptor.class, childElement,
119-
parserContext);
120-
BeanDefinitionHolder interceptorDef = createInterceptorDefinition(parserContext, beanElement);
121-
122-
String soapAction = childElement.getAttribute("value");
123-
124-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef);
125-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, soapAction);
126-
127-
registerSmartInterceptor(parserContext, smartInterceptorDef);
128-
}
129-
130-
// ref elements
131-
List<Element> refElements = DomUtils.getChildElementsByTagName(childElement, "ref");
132-
for (Element refElement : refElements) {
133-
RootBeanDefinition smartInterceptorDef =
134-
createSmartInterceptorDefinition(SoapActionSmartEndpointInterceptor.class, childElement,
135-
parserContext);
136-
BeanReference interceptorRef = createInterceptorReference(parserContext, refElement);
137-
138-
String soapAction = childElement.getAttribute("value");
139-
140-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef);
141-
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, soapAction);
142-
143-
registerSmartInterceptor(parserContext, smartInterceptorDef);
113+
List<Element> soapActionChildren = DomUtils.getChildElements(childElement);
114+
for (Element soapActionChild : soapActionChildren) {
115+
if ("bean".equals(soapActionChild.getLocalName())) {
116+
RootBeanDefinition smartInterceptorDef =
117+
createSmartInterceptorDefinition(SoapActionSmartEndpointInterceptor.class, childElement,
118+
parserContext);
119+
BeanDefinitionHolder interceptorDef =
120+
createInterceptorDefinition(parserContext, soapActionChild);
121+
122+
String soapAction = childElement.getAttribute("value");
123+
124+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef);
125+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, soapAction);
126+
127+
registerSmartInterceptor(parserContext, smartInterceptorDef);
128+
}
129+
else if ("ref".equals(soapActionChild.getLocalName())) {
130+
RootBeanDefinition smartInterceptorDef =
131+
createSmartInterceptorDefinition(SoapActionSmartEndpointInterceptor.class, childElement,
132+
parserContext);
133+
BeanReference interceptorRef = createInterceptorReference(parserContext, soapActionChild);
134+
135+
String soapAction = childElement.getAttribute("value");
136+
137+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef);
138+
smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, soapAction);
139+
140+
registerSmartInterceptor(parserContext, smartInterceptorDef);
141+
}
144142
}
145143
}
146144
}
@@ -150,17 +148,13 @@ else if ("soapAction".equals(childElement.getLocalName())) {
150148
}
151149

152150
private void registerSmartInterceptor(ParserContext parserContext, RootBeanDefinition smartInterceptorDef) {
153-
String mappedInterceptorName =
154-
parserContext.getReaderContext().registerWithGeneratedName(smartInterceptorDef);
155-
parserContext
156-
.registerComponent(new BeanComponentDefinition(smartInterceptorDef, mappedInterceptorName));
151+
String mappedInterceptorName = parserContext.getReaderContext().registerWithGeneratedName(smartInterceptorDef);
152+
parserContext.registerComponent(new BeanComponentDefinition(smartInterceptorDef, mappedInterceptorName));
157153
}
158154

159155
private BeanDefinitionHolder createInterceptorDefinition(ParserContext parserContext, Element element) {
160-
BeanDefinitionHolder interceptorDef =
161-
parserContext.getDelegate().parseBeanDefinitionElement(element);
162-
interceptorDef =
163-
parserContext.getDelegate().decorateBeanDefinitionIfRequired(element, interceptorDef);
156+
BeanDefinitionHolder interceptorDef = parserContext.getDelegate().parseBeanDefinitionElement(element);
157+
interceptorDef = parserContext.getDelegate().decorateBeanDefinitionIfRequired(element, interceptorDef);
164158
return interceptorDef;
165159
}
166160

core/src/main/java/org/springframework/ws/server/endpoint/interceptor/DelegatingSmartEndpointInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public DelegatingSmartEndpointInterceptor(EndpointInterceptor delegate) {
4545

4646
/**
4747
* Returns the delegate.
48-
* @return
48+
* @return the delegate
4949
*/
50-
protected EndpointInterceptor getDelegate() {
50+
public EndpointInterceptor getDelegate() {
5151
return delegate;
5252
}
5353

core/src/test/java/org/springframework/ws/config/InterceptorsBeanDefinitionParserTest.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.ws.config;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.Map;
2022

2123
import org.springframework.context.ApplicationContext;
@@ -24,22 +26,16 @@
2426
import org.springframework.ws.soap.server.endpoint.interceptor.PayloadRootSmartSoapEndpointInterceptor;
2527
import org.springframework.ws.soap.server.endpoint.interceptor.SoapActionSmartEndpointInterceptor;
2628

27-
import org.junit.Before;
2829
import org.junit.Test;
2930

3031
import static org.junit.Assert.assertEquals;
3132

3233
public class InterceptorsBeanDefinitionParserTest {
3334

34-
private ApplicationContext applicationContext;
35-
36-
@Before
37-
public void setUp() throws Exception {
38-
applicationContext = new ClassPathXmlApplicationContext("interceptorsBeanDefinitionParserTest.xml", getClass());
39-
}
40-
4135
@Test
4236
public void namespace() throws Exception {
37+
ApplicationContext applicationContext =
38+
new ClassPathXmlApplicationContext("interceptorsBeanDefinitionParserTest.xml", getClass());
4339
Map<String, ?> result = applicationContext.getBeansOfType(DelegatingSmartEndpointInterceptor.class);
4440
assertEquals("no smart interceptors found", 8, result.size());
4541

@@ -50,4 +46,20 @@ public void namespace() throws Exception {
5046
assertEquals("no interceptors found", 3, result.size());
5147
}
5248

49+
@Test
50+
public void ordering() throws Exception {
51+
ApplicationContext applicationContext =
52+
new ClassPathXmlApplicationContext("interceptorsBeanDefinitionParserOrderTest.xml", getClass());
53+
54+
List<DelegatingSmartEndpointInterceptor> interceptors = new ArrayList<DelegatingSmartEndpointInterceptor>(
55+
applicationContext.getBeansOfType(DelegatingSmartEndpointInterceptor.class).values());
56+
assertEquals("not enough smart interceptors found", 6, interceptors.size());
57+
58+
for (int i = 0; i < interceptors.size(); i++) {
59+
DelegatingSmartEndpointInterceptor delegatingInterceptor = interceptors.get(i);
60+
MyInterceptor interceptor = (MyInterceptor) delegatingInterceptor.getDelegate();
61+
assertEquals("Invalid ordering found for [" + delegatingInterceptor + "]", i, interceptor.getOrder());
62+
}
63+
}
64+
5365
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2005-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.config;
18+
19+
import org.springframework.ws.context.MessageContext;
20+
import org.springframework.ws.server.EndpointInterceptor;
21+
22+
/**
23+
* @author Arjen Poutsma
24+
*/
25+
public class MyInterceptor implements EndpointInterceptor {
26+
27+
private int order;
28+
29+
public int getOrder() {
30+
return order;
31+
}
32+
33+
public void setOrder(int order) {
34+
this.order = order;
35+
}
36+
37+
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
38+
return true;
39+
}
40+
41+
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
42+
return true;
43+
}
44+
45+
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
46+
return true;
47+
}
48+
49+
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) {
50+
}
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:p="http://www.springframework.org/schema/p"
5+
xmlns:sws="http://www.springframework.org/schema/web-services"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7+
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services.xsd">
8+
9+
<sws:interceptors>
10+
<ref bean="externalGlobalInterceptor"/>
11+
<bean class="org.springframework.ws.config.MyInterceptor">
12+
<property name="order" value="1"/>
13+
</bean>
14+
<sws:payloadRoot namespaceUri="http://www.springframework.org/spring-ws">
15+
<ref bean="externalPayloadRootInterceptor"/>
16+
<bean class="org.springframework.ws.config.MyInterceptor">
17+
<property name="order" value="3"/>
18+
</bean>
19+
</sws:payloadRoot>
20+
<sws:soapAction value="mySoapAction">
21+
<ref local="externalSoapActionInterceptor"/>
22+
<bean class="org.springframework.ws.config.MyInterceptor">
23+
<property name="order" value="5"/>
24+
</bean>
25+
</sws:soapAction>
26+
</sws:interceptors>
27+
28+
<bean id="externalGlobalInterceptor" class="org.springframework.ws.config.MyInterceptor" p:order="0"/>
29+
<bean id="externalPayloadRootInterceptor" class="org.springframework.ws.config.MyInterceptor" p:order="2"/>
30+
<bean id="externalSoapActionInterceptor" class="org.springframework.ws.config.MyInterceptor" p:order="4"/>
31+
32+
</beans>
33+
34+

0 commit comments

Comments
 (0)