|
| 1 | +/* |
| 2 | + * Copyright 2005-2010 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 java.util.List; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 22 | +import org.springframework.beans.factory.config.BeanDefinitionHolder; |
| 23 | +import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
| 24 | +import org.springframework.beans.factory.parsing.CompositeComponentDefinition; |
| 25 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 26 | +import org.springframework.beans.factory.xml.BeanDefinitionParser; |
| 27 | +import org.springframework.beans.factory.xml.ParserContext; |
| 28 | +import org.springframework.util.xml.DomUtils; |
| 29 | +import org.springframework.ws.server.SmartEndpointInterceptor; |
| 30 | +import org.springframework.ws.server.endpoint.interceptor.DelegatingSmartEndpointInterceptor; |
| 31 | +import org.springframework.ws.server.endpoint.interceptor.PayloadRootSmartEndpointInterceptor; |
| 32 | +import org.springframework.ws.soap.server.endpoint.interceptor.SoapActionSmartEndpointInterceptor; |
| 33 | + |
| 34 | +import org.w3c.dom.Element; |
| 35 | + |
| 36 | +/** |
| 37 | + * Parser for the {@code <sws:interceptors/>} element. |
| 38 | + * |
| 39 | + * @author Arjen Poutsma |
| 40 | + * @since 2.0 |
| 41 | + */ |
| 42 | +class InterceptorsBeanDefinitionParser implements BeanDefinitionParser { |
| 43 | + |
| 44 | + public BeanDefinition parse(Element element, ParserContext parserContext) { |
| 45 | + CompositeComponentDefinition compDefinition = |
| 46 | + new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element)); |
| 47 | + parserContext.pushContainingComponent(compDefinition); |
| 48 | + |
| 49 | + List<Element> childElements = DomUtils.getChildElements(element); |
| 50 | + for (Element childElement : childElements) { |
| 51 | + if ("bean".equals(childElement.getLocalName())) { |
| 52 | + RootBeanDefinition smartInterceptorDef = |
| 53 | + createSmartInterceptorDefinition(DelegatingSmartEndpointInterceptor.class, childElement, |
| 54 | + parserContext); |
| 55 | + BeanDefinitionHolder interceptorDef = createInterceptorDefinition(parserContext, childElement); |
| 56 | + |
| 57 | + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef); |
| 58 | + |
| 59 | + registerSmartInterceptor(parserContext, smartInterceptorDef); |
| 60 | + } |
| 61 | + else if ("payloadRoot".equals(childElement.getLocalName())) { |
| 62 | + List<Element> beanElements = DomUtils.getChildElementsByTagName(childElement, "bean"); |
| 63 | + for (Element beanElement : beanElements) { |
| 64 | + RootBeanDefinition smartInterceptorDef = |
| 65 | + createSmartInterceptorDefinition(PayloadRootSmartEndpointInterceptor.class, childElement, |
| 66 | + parserContext); |
| 67 | + BeanDefinitionHolder interceptorDef = createInterceptorDefinition(parserContext, beanElement); |
| 68 | + |
| 69 | + String namespaceUri = childElement.getAttribute("namespaceUri"); |
| 70 | + String localPart = childElement.getAttribute("localPart"); |
| 71 | + |
| 72 | + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef); |
| 73 | + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, namespaceUri); |
| 74 | + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, localPart); |
| 75 | + |
| 76 | + registerSmartInterceptor(parserContext, smartInterceptorDef); |
| 77 | + } |
| 78 | + } |
| 79 | + else if ("soapAction".equals(childElement.getLocalName())) { |
| 80 | + List<Element> beanElements = DomUtils.getChildElementsByTagName(childElement, "bean"); |
| 81 | + for (Element beanElement : beanElements) { |
| 82 | + RootBeanDefinition smartInterceptorDef = |
| 83 | + createSmartInterceptorDefinition(SoapActionSmartEndpointInterceptor.class, childElement, |
| 84 | + parserContext); |
| 85 | + BeanDefinitionHolder interceptorDef = createInterceptorDefinition(parserContext, beanElement); |
| 86 | + |
| 87 | + String soapAction = childElement.getAttribute("value"); |
| 88 | + |
| 89 | + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, interceptorDef); |
| 90 | + smartInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, soapAction); |
| 91 | + |
| 92 | + registerSmartInterceptor(parserContext, smartInterceptorDef); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + parserContext.popAndRegisterContainingComponent(); |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + private void registerSmartInterceptor(ParserContext parserContext, RootBeanDefinition smartInterceptorDef) { |
| 102 | + String mappedInterceptorName = |
| 103 | + parserContext.getReaderContext().registerWithGeneratedName(smartInterceptorDef); |
| 104 | + parserContext |
| 105 | + .registerComponent(new BeanComponentDefinition(smartInterceptorDef, mappedInterceptorName)); |
| 106 | + } |
| 107 | + |
| 108 | + private BeanDefinitionHolder createInterceptorDefinition(ParserContext parserContext, Element element) { |
| 109 | + BeanDefinitionHolder interceptorDef = |
| 110 | + parserContext.getDelegate().parseBeanDefinitionElement(element); |
| 111 | + interceptorDef = |
| 112 | + parserContext.getDelegate().decorateBeanDefinitionIfRequired(element, interceptorDef); |
| 113 | + return interceptorDef; |
| 114 | + } |
| 115 | + |
| 116 | + private RootBeanDefinition createSmartInterceptorDefinition(Class<? extends SmartEndpointInterceptor> interceptorClass, |
| 117 | + Element element, |
| 118 | + ParserContext parserContext) { |
| 119 | + RootBeanDefinition smartInterceptorDef = new RootBeanDefinition(interceptorClass); |
| 120 | + smartInterceptorDef.setSource(parserContext.extractSource(element)); |
| 121 | + smartInterceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 122 | + return smartInterceptorDef; |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments