SWS-670 - sws:interceptors

This commit is contained in:
Arjen Poutsma
2010-12-17 13:41:37 +00:00
parent df9d82ac0f
commit 16dfd55af0
15 changed files with 801 additions and 68 deletions

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.config;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ws.server.endpoint.interceptor.DelegatingSmartEndpointInterceptor;
import org.springframework.ws.server.endpoint.interceptor.PayloadRootSmartEndpointInterceptor;
import org.springframework.ws.soap.server.endpoint.interceptor.SoapActionSmartEndpointInterceptor;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class InterceptorsBeanDefinitionParserTest {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception {
applicationContext = new ClassPathXmlApplicationContext("interceptorsBeanDefinitionParserTest.xml", getClass());
}
@Test
public void namespace() throws Exception {
Map<String, ?> result = applicationContext.getBeansOfType(DelegatingSmartEndpointInterceptor.class);
assertEquals("no smart interceptors found", 5, result.size());
result = applicationContext.getBeansOfType(PayloadRootSmartEndpointInterceptor.class);
assertEquals("no interceptors found", 2, result.size());
result = applicationContext.getBeansOfType(SoapActionSmartEndpointInterceptor.class);
assertEquals("no interceptors found", 2, result.size());
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.server.endpoint.interceptor;
import org.springframework.ws.MockWebServiceMessage;
import org.springframework.ws.MockWebServiceMessageFactory;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointInterceptor;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PayloadRootSmartEndpointInterceptorTest {
private EndpointInterceptor delegate;
private String namespaceUri;
private String localPart;
private MessageContext messageContext;
@Before
public void setUp() {
delegate = new EndpointInterceptorAdapter();
namespaceUri = "http://springframework.org/spring-ws";
localPart = "element";
MockWebServiceMessage request = new MockWebServiceMessage("<" + localPart + " xmlns=\"" + namespaceUri + "\" />");
messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
}
@Test(expected = IllegalArgumentException.class)
public void neitherNamespaceNorLocalPart() {
new PayloadRootSmartEndpointInterceptor(delegate, null, null);
}
@Test
public void shouldInterceptFullMatch() throws Exception {
PayloadRootSmartEndpointInterceptor interceptor =
new PayloadRootSmartEndpointInterceptor(delegate, namespaceUri, localPart);
boolean result = interceptor.shouldIntercept(messageContext, null);
assertTrue("Interceptor should apply", result);
}
@Test
public void shouldInterceptFullNonMatch() throws Exception {
PayloadRootSmartEndpointInterceptor interceptor =
new PayloadRootSmartEndpointInterceptor(delegate, "http://springframework.org/other", localPart);
boolean result = interceptor.shouldIntercept(messageContext, null);
assertFalse("Interceptor should apply", result);
}
@Test
public void shouldInterceptNamespaceUriMatch() throws Exception {
PayloadRootSmartEndpointInterceptor interceptor =
new PayloadRootSmartEndpointInterceptor(delegate, namespaceUri, null);
boolean result = interceptor.shouldIntercept(messageContext, null);
assertTrue("Interceptor should apply", result);
}
@Test
public void shouldInterceptLocalPartMatch() throws Exception {
PayloadRootSmartEndpointInterceptor interceptor =
new PayloadRootSmartEndpointInterceptor(delegate, null, localPart);
boolean result = interceptor.shouldIntercept(messageContext, null);
assertTrue("Interceptor should apply", result);
}
}

View File

@@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,90 +17,108 @@
package org.springframework.ws.server.endpoint.mapping;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.ws.MockWebServiceMessageFactory;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.ws.server.EndpointInvocationChain;
import org.springframework.ws.server.endpoint.interceptor.DelegatingSmartEndpointInterceptor;
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
/**
* Test case for {@link AbstractEndpointMapping}.
*/
public class EndpointMappingTest {
private MessageContext mockContext;
private MessageContext messageContext;
@Before
public void setUp() throws Exception {
mockContext = createMock(MessageContext.class);
messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
}
@Test
public void testDefaultEndpoint() throws Exception {
public void defaultEndpoint() throws Exception {
Object defaultEndpoint = new Object();
AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
@Override
protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
Assert.assertEquals("Invalid request passed", mockContext, givenRequest);
assertEquals("Invalid request passed", messageContext, givenRequest);
return null;
}
};
mapping.setDefaultEndpoint(defaultEndpoint);
replay(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(mockContext);
Assert.assertNotNull("No EndpointInvocatioChain returned", result);
Assert.assertEquals("Default Endpoint not returned", defaultEndpoint, result.getEndpoint());
verify(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
assertNotNull("No EndpointInvocatioChain returned", result);
assertEquals("Default Endpoint not returned", defaultEndpoint, result.getEndpoint());
}
@Test
public void testEndpoint() throws Exception {
public void endpoint() throws Exception {
final Object endpoint = new Object();
AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
@Override
protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
Assert.assertEquals("Invalid request passed", mockContext, givenRequest);
assertEquals("Invalid request passed", messageContext, givenRequest);
return endpoint;
}
};
replay(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(mockContext);
Assert.assertNotNull("No EndpointInvocatioChain returned", result);
Assert.assertEquals("Unexpected Endpoint returned", endpoint, result.getEndpoint());
verify(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
assertNotNull("No EndpointInvocationChain returned", result);
assertEquals("Unexpected Endpoint returned", endpoint, result.getEndpoint());
}
@Test
public void testEndpointInterceptors() throws Exception {
public void endpointInterceptors() throws Exception {
final Object endpoint = new Object();
EndpointInterceptor interceptor = new EndpointInterceptorAdapter();
AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
@Override
protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
Assert.assertEquals("Invalid request passed", mockContext, givenRequest);
assertEquals("Invalid request passed", messageContext, givenRequest);
return endpoint;
}
};
replay(mockContext);
mapping.setInterceptors(new EndpointInterceptor[]{interceptor});
EndpointInvocationChain result = mapping.getEndpoint(mockContext);
Assert.assertEquals("Unexpected amount of EndpointInterceptors returned", 1, result.getInterceptors().length);
Assert.assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
verify(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
assertEquals("Unexpected amount of EndpointInterceptors returned", 1, result.getInterceptors().length);
assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
}
@Test
public void testEndpointBeanName() throws Exception {
public void smartEndpointInterceptors() throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("smartInterceptor", MySmartEndpointInterceptor.class);
final Object endpoint = new Object();
EndpointInterceptor interceptor = new EndpointInterceptorAdapter();
AbstractEndpointMapping mapping = new AbstractEndpointMapping() {
@Override
protected Object getEndpointInternal(MessageContext givenRequest) throws Exception {
assertEquals("Invalid request passed", messageContext, givenRequest);
return endpoint;
}
};
mapping.setApplicationContext(applicationContext);
mapping.setInterceptors(new EndpointInterceptor[]{interceptor});
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
assertEquals("Unexpected amount of EndpointInterceptors returned", 2, result.getInterceptors().length);
assertEquals("Unexpected EndpointInterceptor returned", interceptor, result.getInterceptors()[0]);
assertTrue("Unexpected EndpointInterceptor returned",
result.getInterceptors()[1] instanceof MySmartEndpointInterceptor);
}
@Test
public void endpointBeanName() throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("endpoint", Object.class);
@@ -108,22 +126,18 @@ public class EndpointMappingTest {
@Override
protected Object getEndpointInternal(MessageContext message) throws Exception {
Assert.assertEquals("Invalid request", mockContext, message);
assertEquals("Invalid request", messageContext, message);
return "endpoint";
}
};
mapping.setApplicationContext(applicationContext);
replay(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(mockContext);
Assert.assertNotNull("No endpoint returned", result);
verify(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
assertNotNull("No endpoint returned", result);
}
@Test
public void testEndpointInvalidBeanName() throws Exception {
public void endpointInvalidBeanName() throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerSingleton("endpoint", Object.class);
@@ -131,23 +145,19 @@ public class EndpointMappingTest {
@Override
protected Object getEndpointInternal(MessageContext message) throws Exception {
Assert.assertEquals("Invalid request", mockContext, message);
assertEquals("Invalid request", messageContext, message);
return "noSuchBean";
}
};
mapping.setApplicationContext(applicationContext);
replay(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
EndpointInvocationChain result = mapping.getEndpoint(mockContext);
Assert.assertNull("No endpoint returned", result);
verify(mockContext);
assertNull("No endpoint returned", result);
}
@Test
public void testEndpointPrototype() throws Exception {
public void endpointPrototype() throws Exception {
StaticApplicationContext applicationContext = new StaticApplicationContext();
applicationContext.registerPrototype("endpoint", MyEndpoint.class);
@@ -155,29 +165,32 @@ public class EndpointMappingTest {
@Override
protected Object getEndpointInternal(MessageContext message) throws Exception {
Assert.assertEquals("Invalid request", mockContext, message);
assertEquals("Invalid request", messageContext, message);
return "endpoint";
}
};
mapping.setApplicationContext(applicationContext);
replay(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(mockContext);
Assert.assertNotNull("No endpoint returned", result);
result = mapping.getEndpoint(mockContext);
Assert.assertNotNull("No endpoint returned", result);
Assert.assertEquals("Prototype endpoint was not constructed twice", 2, MyEndpoint.constrCount);
verify(mockContext);
EndpointInvocationChain result = mapping.getEndpoint(messageContext);
assertNotNull("No endpoint returned", result);
result = mapping.getEndpoint(messageContext);
assertNotNull("No endpoint returned", result);
assertEquals("Prototype endpoint was not constructed twice", 2, MyEndpoint.constructorCount);
}
private static class MyEndpoint {
private static int constrCount;
private static int constructorCount;
private MyEndpoint() {
constrCount++;
constructorCount++;
}
}
private static class MySmartEndpointInterceptor extends DelegatingSmartEndpointInterceptor {
private MySmartEndpointInterceptor() {
super(new EndpointInterceptorAdapter());
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.soap.server.endpoint.interceptor;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.EndpointInterceptor;
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class SoapActionSmartEndpointInterceptorTest {
private EndpointInterceptor delegate;
private String soapAction;
private MessageContext messageContext;
@Before
public void setUp() {
delegate = new EndpointInterceptorAdapter();
soapAction = "http://springframework.org/spring-ws";
SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
messageFactory.afterPropertiesSet();
SaajSoapMessage request = messageFactory.createWebServiceMessage();
request.setSoapAction(soapAction);
messageContext = new DefaultMessageContext(request, messageFactory);
}
@Test(expected = IllegalArgumentException.class)
public void neitherNamespaceNorLocalPart() {
new SoapActionSmartEndpointInterceptor(delegate, null);
}
@Test
public void shouldInterceptMatch() throws Exception {
SoapActionSmartEndpointInterceptor interceptor = new SoapActionSmartEndpointInterceptor(delegate, soapAction);
boolean result = interceptor.shouldIntercept(messageContext, null);
assertTrue("Interceptor should apply", result);
}
@Test
public void shouldInterceptNonMatch() throws Exception {
SoapActionSmartEndpointInterceptor interceptor =
new SoapActionSmartEndpointInterceptor(delegate, "http://springframework.org/other");
boolean result = interceptor.shouldIntercept(messageContext, null);
assertFalse("Interceptor should apply", result);
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://static.springframework.org/schema/web-services/web-services-2.0.xsd">
<sws:interceptors>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<sws:payloadRoot namespaceUri="http://www.springframework.org/spring-ws">
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</sws:payloadRoot>
<sws:soapAction value="mySoapAction">
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>
</sws:soapAction>
</sws:interceptors>
</beans>