Announcement Announcement Module
Collapse
No announcement yet.
How to validate incoming SOAP message against a schema with Spring WS and Integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to validate incoming SOAP message against a schema with Spring WS and Integration

    Hello,

    I am using Spring WS with Spring Integration. I would like to validate the incoming soap message against our schema. Below is my current config:

    Code:
    <int:channel id="input"/>
    <int:channel id="output"/>
    
    <!-- Inbound gateway -->
    <int-ws:inbound-gateway id="in-gateway"
                                       request-channel="input"
                                       reply-channel="output"
                                       header-mapper="customHeaderMapper"
                                       marshaller="custommarshaller"
                                       unmarshaller="custommashaller"/>
    
    <int:service-activator input-channel="input" ref="component_to_call" />
    ...
    I read about <int-xml:validating-filter > against the schema. However, I am not sure to to connect that piece to the above config. I need some help.

    Thanks.

  • #2
    Hi!

    If you are using unmarshaller <ws:inbound-gateway>, the <xml:validating-filter> doesn't make sence: you are loosing XML content after unmarshalling.
    I suggest you to take a look into Spring-WS: http://static.springsource.org/sprin...r.html#d5e1451
    In this case you can handle validation before a SoapMessage will come to the <ws:inbound-gateway>.

    HTH,
    Artem

    Comment


    • #3
      The validation needs to be done in the web container, before it reaches Spring Integration; see

      http://static.springsource.org/sprin...nt-interceptor

      and specifically

      http://static.springsource.org/sprin...r.html#d5e1451

      But see the note about Postel's law in the second link.

      Comment


      • #4
        Originally posted by Cleric View Post
        Hi!

        If you are using unmarshaller <ws:inbound-gateway>, the <xml:validating-filter> doesn't make sence: you are loosing XML content after unmarshalling.
        I suggest you to take a look into Spring-WS: http://static.springsource.org/sprin...r.html#d5e1451
        In this case you can handle validation before a SoapMessage will come to the <ws:inbound-gateway>.

        HTH,
        Artem
        Could you give me an example how to config to validate before it hit the <ws:inbound-gateway> ?

        Does it mean to define the validation like this in your applicationCOntext.xml ?

        Code:
        <bean id="validatingInterceptor"
                class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
            <property name="schema" value="/WEB-INF/orders.xsd"/>
            <property name="validateRequest" value="false"/>
            <property name="validateResponse" value="true"/>
        </bean>
        Thanks,
        Last edited by Saiyandt; Jun 23rd, 2013, 08:47 AM.

        Comment


        • #5
          As discussed in the document we referred you to, this will validate the outbound but not the inbound message but, yes, that's essentially how you add validation.

          Comment


          • #6
            Originally posted by Gary Russell View Post
            As discussed in the document we referred you to, this will validate the outbound but not the inbound message but, yes, that's essentially how you add validation.
            Thanks for your reply. However, if I want to do validate before inbound, is there anyway to do that ?

            Thanks,

            Comment


            • #7
              ???

              use

              Code:
              <property name="validateRequest" value="true"/>
              instead of

              Code:
              <property name="validateRequest" value="false"/>

              Comment


              • #8
                Originally posted by Gary Russell View Post
                ???

                use

                Code:
                <property name="validateRequest" value="true"/>
                instead of

                Code:
                <property name="validateRequest" value="false"/>
                By turn that to true, does it still validate XML before it hits the <ws:inbound > ? The reason is I want to validate the XML before the inbound unmarshall the xml.

                Thanks,

                Comment


                • #9
                  Of course, that's what I said in post #3.

                  Comment


                  • #10
                    I tried to follow the validation schema the way you suggested. I am getting the following error. I am using WAS 6.1 with spring-WS and spring Integration. Do you have any idea why I get the error ? Is it because of the library conflict ?

                    ---- Begin backtrace for Nested Throwables
                    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.ws.soap.server.endpoint.inter ceptor.DelegatingSmartSoapEndpointInterceptor#0': Cannot create inner bean 'validatingInterceptor' of type [org.springframework.ws.soap.server.endpoint.interc eptor.PayloadValidatingInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'validatingInterceptor' defined in class path resource [spring-ws.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaLoader.loadGrammar([Lcom/sun/org/apache/xerces/internal/xni/parser/XMLInputSourceV
                    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBean(BeanDefinitio nValueResolver.java:281)
                    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:120)
                    at org.springframework.beans.factory.support.Construc torResolver.resolveConstructorArguments(Constructo rResolver.java:616)
                    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:148)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1035)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:939)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
                    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
                    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
                    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
                    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
                    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:585)
                    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:913)
                    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:464)
                    at org.springframework.web.context.ContextLoader.conf igureAndRefreshWebApplicationContext(ContextLoader .java:385)
                    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:284)
                    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 111)
                    at com.ibm.ws.wswebcontainer.webapp.WebApp.notifyServ letContextCreated(WebApp.java:917)
                    at com.ibm.ws.webcontainer.webapp.WebApp.commonInitia lizationFinish(WebApp.java:386)
                    at com.ibm.ws.wswebcontainer.webapp.WebApp.initialize (WebApp.java:338)
                    at com.ibm.ws.wswebcontainer.webapp.WebGroup.addWebAp plication(WebGroup.java:93)
                    at com.ibm.ws.wswebcontainer.VirtualHost.addWebApplic ation(VirtualHost.java:162)
                    at com.ibm.ws.wswebcontainer.WebContainer.addWebApp(W ebContainer.java:673)
                    at com.ibm.ws.wswebcontainer.WebContainer.addWebAppli cation(WebContainer.java:626)
                    at com.ibm.ws.webcontainer.component.WebContainerImpl .install(WebContainerImpl.java:395)
                    at com.ibm.ws.webcontainer.component.WebContainerImpl .start(WebContainerImpl.java:611)
                    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st art(ApplicationMgrImpl.java:1274)
                    at com.ibm.ws.runtime.component.DeployedApplicationIm pl.fireDeployedObjectStart(DeployedApplicationImpl .java:1164)
                    at com.ibm.ws.runtime.component.DeployedModuleImpl.st art(DeployedModuleImpl.java:591)
                    at com.ibm.ws.runtime.component.DeployedApplicationIm pl.start(DeployedApplicationImpl.java:831)
                    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st artApplication(ApplicationMgrImpl.java:945)
                    at com.ibm.ws.runtime.component.ApplicationMgrImpl$1. run(ApplicationMgrImpl.java:1461)
                    at com.ibm.ws.security.auth.ContextManagerImpl.runAs( ContextManagerImpl.java:4309)
                    at com.ibm.ws.security.auth.ContextManagerImpl.runAsS ystem(ContextManagerImpl.java:4407)
                    at com.ibm.ws.security.core.SecurityContext.runAsSyst em(SecurityContext.java:245)
                    at com.ibm.ws.runtime.component.ApplicationMgrImpl.st artApplication(ApplicationMgrImpl.java:1466)
                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
                    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:79)
                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
                    ...

                    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'validatingInterceptor' defined in class path resource [spring-ws.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaLoader.loadGrammar([Lcom/sun/org/apache/xerces/internal/xni/parser/XMLInputSourceV
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1455)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
                    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveInnerBean(BeanDefinitio nValueResolver.java:270)
                    ... 87 more
                    Caused by: java.lang.NoSuchMethodError: com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaLoader.loadGrammar([Lcom/sun/org/apache/xerces/internal/xni/parser/XMLInputSourceV
                    at com.sun.org.apache.xerces.internal.jaxp.validation .XMLSchemaFactory.newSchema(XMLSchemaFactory.java: 206)
                    at org.springframework.xml.validation.SchemaLoaderUti ls.loadSchema(SchemaLoaderUtils.java:78)
                    at org.springframework.xml.validation.Jaxp13Validator Factory.createValidator(Jaxp13ValidatorFactory.jav a:41)
                    at org.springframework.xml.validation.XmlValidatorFac tory.createValidator(XmlValidatorFactory.java:92)
                    at org.springframework.ws.server.endpoint.interceptor .AbstractValidatingInterceptor.afterPropertiesSet( AbstractValidatingInterceptor.java:165)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1514)
                    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1452)
                    ... 90 more
                    Last edited by Gary Russell; Jun 27th, 2013, 10:38 AM.

                    Comment


                    • #11
                      Why are you declaring a DelegatingSmartSoapEndpointInterceptor?

                      Can you show your configuration?

                      Comment


                      • #12
                        Originally posted by Gary Russell View Post
                        Why are you declaring a DelegatingSmartSoapEndpointInterceptor?

                        Can you show your configuration?
                        This is where I define validation. I also have another config file where I define all the spring - integration stuff. This file is imported to applicationContext.

                        Code:
                        <?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:util="http://www.springframework.org/schema/util"
                        		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.1.xsd
                        							http://www.springframework.org/schema/util
                                					http://www.springframework.org/schema/util/spring-util-3.1.xsd
                                					http://www.springframework.org/schema/web-services
                               						http://www.springframework.org/schema/web-services/web-services-2.0.xsd">
                        	<!-- SOAP 1.2, Spring Implementation
                        	<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
                        		<property name="soapVersion">
                        			<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12" />
                        		</property>
                        	</bean>
                        	-->
                        	<!-- SOAP 1.1 SUN Implementation -->
                        	<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
                        		<property name="messageFactory">
                        			<bean class="com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl">
                        			</bean>
                        		</property>
                        	</bean>
                        
                        	<bean id="marshaller" class="com.test.parser.AnnotationJaxb2Marshaller">
                        		<property name="packagesToScan">
                        			<list>
                        				<!-- scan ALL classes under schema package -->				
                        				<value>com.test.schema</value>
                        			</list>
                        		</property>
                        		<property name="marshallerProperties">
                        			<map>
                        				<entry><!-- Format the output XML string -->
                        					<key><util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT" /></key>
                        					<value type="java.lang.Boolean">true</value>
                        				</entry>
                        				<entry><!-- encoding="UTF-8" -->
                        					<key><util:constant static-field="javax.xml.bind.Marshaller.JAXB_ENCODING" /></key>
                        					<value>UTF-8</value>
                        				</entry>
                        			</map>
                        		</property>
                        	</bean>
                        	
                        	<bean id="essMessageParser" class="com.test.parser.Jaxb2Util">
                                <property name="jaxb2Marshaller" ref="marshaller"/>
                            </bean>
                            
                            <!-- Validate xml xsd -->
                            <sws:interceptors>
                              <bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
                                    <property name="schema" value="classpath:/wsdl/schemas/TestService.xsd" />
                                    <property name="validateRequest" value="true" />
                                    <property name="validateResponse" value="false" />
                                  </bean>
                            </sws:interceptors>
                            
                            
                        </beans>

                        Comment


                        • #13
                          Sorry; my mistake, I forgot about the <sws:interceptors/> element; that's where that bean comes from.

                          It looks like you have an old version of xerces on the classpath.

                          Caused by: java.lang.NoSuchMethodError: com/sun/org/apache/xerces/internal/impl/xs/XMLSchemaLoader.loadGrammar([Lcom/sun/org/apache/xerces/internal/xni/parser/XMLInputSourceV

                          Comment


                          • #14
                            So I can remove the lib and let maven download again? I can post the maven configuration.

                            Comment


                            • #15
                              Your appserver (WAS) probably has the old version on its classpath; you would likely need to consult the vendor documentation to avoid this issue.

                              Comment

                              Working...
                              X