From eb54ee8e7c5e67f13e25eec963e5cb0bd8406d80 Mon Sep 17 00:00:00 2001 From: Tareq Abedrabbo Date: Thu, 3 Feb 2011 12:35:38 +0000 Subject: [PATCH] SWS-684 - Added support for elements inside the element --- .../InterceptorsBeanDefinitionParser.java | 74 +++++++++++++++++++ .../ws/config/web-services-2.0.xsd | 29 +++++++- .../InterceptorsBeanDefinitionParserTest.java | 6 +- .../interceptorsBeanDefinitionParserTest.xml | 7 ++ 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/org/springframework/ws/config/InterceptorsBeanDefinitionParser.java b/core/src/main/java/org/springframework/ws/config/InterceptorsBeanDefinitionParser.java index 8c699d91..08dca70e 100644 --- a/core/src/main/java/org/springframework/ws/config/InterceptorsBeanDefinitionParser.java +++ b/core/src/main/java/org/springframework/ws/config/InterceptorsBeanDefinitionParser.java @@ -20,11 +20,14 @@ import java.util.List; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; +import org.springframework.beans.factory.config.BeanReference; +import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.parsing.CompositeComponentDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; import org.springframework.ws.server.SmartEndpointInterceptor; import org.springframework.ws.soap.server.endpoint.interceptor.DelegatingSmartSoapEndpointInterceptor; @@ -58,7 +61,20 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { registerSmartInterceptor(parserContext, smartInterceptorDef); } + else if ("ref".equals(childElement.getLocalName())) { + RootBeanDefinition smartInterceptorDef = + createSmartInterceptorDefinition(DelegatingSmartSoapEndpointInterceptor.class, childElement, + parserContext); + + BeanReference interceptorRef = createInterceptorReference(parserContext, childElement); + + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef); + + registerSmartInterceptor(parserContext, smartInterceptorDef); + + } else if ("payloadRoot".equals(childElement.getLocalName())) { + // bean elements List beanElements = DomUtils.getChildElementsByTagName(childElement, "bean"); for (Element beanElement : beanElements) { RootBeanDefinition smartInterceptorDef = @@ -75,8 +91,27 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { registerSmartInterceptor(parserContext, smartInterceptorDef); } + + // ref elements + List refElements = DomUtils.getChildElementsByTagName(childElement, "ref"); + for (Element refElement : refElements) { + RootBeanDefinition smartInterceptorDef = + createSmartInterceptorDefinition(PayloadRootSmartSoapEndpointInterceptor.class, childElement, + parserContext); + BeanReference interceptorRef = createInterceptorReference(parserContext, refElement); + + String namespaceUri = childElement.getAttribute("namespaceUri"); + String localPart = childElement.getAttribute("localPart"); + + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef); + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, namespaceUri); + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, localPart); + + registerSmartInterceptor(parserContext, smartInterceptorDef); + } } else if ("soapAction".equals(childElement.getLocalName())) { + // bean elements List beanElements = DomUtils.getChildElementsByTagName(childElement, "bean"); for (Element beanElement : beanElements) { RootBeanDefinition smartInterceptorDef = @@ -91,6 +126,22 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { registerSmartInterceptor(parserContext, smartInterceptorDef); } + + // ref elements + List refElements = DomUtils.getChildElementsByTagName(childElement, "ref"); + for (Element refElement : refElements) { + RootBeanDefinition smartInterceptorDef = + createSmartInterceptorDefinition(SoapActionSmartEndpointInterceptor.class, childElement, + parserContext); + BeanReference interceptorRef = createInterceptorReference(parserContext, refElement); + + String soapAction = childElement.getAttribute("value"); + + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorRef); + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, soapAction); + + registerSmartInterceptor(parserContext, smartInterceptorDef); + } } } @@ -113,6 +164,26 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { return interceptorDef; } + private BeanReference createInterceptorReference(ParserContext parserContext, Element element) { + // A generic reference to any name of any bean. + String refName = element.getAttribute("bean"); + if (!StringUtils.hasLength(refName)) { + // A reference to the id of another bean in the same XML file. + refName = element.getAttribute("local"); + if (!StringUtils.hasLength(refName)) { + error(parserContext, "Either 'bean' or 'local' is required for element", element); + return null; + } + } + if (!StringUtils.hasText(refName)) { + error(parserContext, " element contains empty target attribute", element); + return null; + } + RuntimeBeanReference ref = new RuntimeBeanReference(refName); + ref.setSource(parserContext.extractSource(element)); + return ref; + } + private RootBeanDefinition createSmartInterceptorDefinition(Class interceptorClass, Element element, ParserContext parserContext) { @@ -122,4 +193,7 @@ class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { return smartInterceptorDef; } + private void error(ParserContext parserContext, String message, Object source) { + parserContext.getDelegate().getReaderContext().error(message, source); + } } diff --git a/core/src/main/resources/org/springframework/ws/config/web-services-2.0.xsd b/core/src/main/resources/org/springframework/ws/config/web-services-2.0.xsd index 610579cc..2fd8203d 100644 --- a/core/src/main/resources/org/springframework/ws/config/web-services-2.0.xsd +++ b/core/src/main/resources/org/springframework/ws/config/web-services-2.0.xsd @@ -71,6 +71,13 @@ + + + + Registers a reference to an interceptor that intercepts every request. + + + - + The interceptor's bean definition. - + + + + The interceptor's bean reference. + + + + @@ -97,13 +111,20 @@ ]]> - + The interceptor's bean definition. - + + + + The interceptor's bean reference. + + + + diff --git a/core/src/test/java/org/springframework/ws/config/InterceptorsBeanDefinitionParserTest.java b/core/src/test/java/org/springframework/ws/config/InterceptorsBeanDefinitionParserTest.java index d66f867e..e2a69118 100644 --- a/core/src/test/java/org/springframework/ws/config/InterceptorsBeanDefinitionParserTest.java +++ b/core/src/test/java/org/springframework/ws/config/InterceptorsBeanDefinitionParserTest.java @@ -41,13 +41,13 @@ public class InterceptorsBeanDefinitionParserTest { @Test public void namespace() throws Exception { Map result = applicationContext.getBeansOfType(DelegatingSmartEndpointInterceptor.class); - assertEquals("no smart interceptors found", 5, result.size()); + assertEquals("no smart interceptors found", 8, result.size()); result = applicationContext.getBeansOfType(PayloadRootSmartSoapEndpointInterceptor.class); - assertEquals("no interceptors found", 2, result.size()); + assertEquals("no interceptors found", 3, result.size()); result = applicationContext.getBeansOfType(SoapActionSmartEndpointInterceptor.class); - assertEquals("no interceptors found", 2, result.size()); + assertEquals("no interceptors found", 3, result.size()); } } diff --git a/core/src/test/resources/org/springframework/ws/config/interceptorsBeanDefinitionParserTest.xml b/core/src/test/resources/org/springframework/ws/config/interceptorsBeanDefinitionParserTest.xml index e1d51b38..9a9d60cd 100644 --- a/core/src/test/resources/org/springframework/ws/config/interceptorsBeanDefinitionParserTest.xml +++ b/core/src/test/resources/org/springframework/ws/config/interceptorsBeanDefinitionParserTest.xml @@ -7,16 +7,23 @@ + + + + + + +