Announcement Announcement Module
Collapse
No announcement yet.
How do I extract information from a soap header and pass it to the endpoint Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    As stated in the Spring Reference guide you mention:
    "the Spring container will create a brand new instance of the.... bean using the '...' bean definition for each and every HTTP request".

    Where did you read about a different behaviour?

    You have only to pay attention to not reassign the variable, referring to the bean, in your code but only change its internal state to pass information from interceptor to endpoint.

    Comment


    • #17
      Never mind I must have been reading the wrong section.

      Thanks Lucian.

      Comment


      • #18
        Originally posted by Rahul Mishra View Post
        Did you happen to create a JIRA for this issue?
        No I didn't do that yet, so please go ahead and do it .

        Injecting various objects as method parameters (such as MessageContext, and maybe others), is a major feature, and therefore not something I can do in the 1.5 timeline though. It will be something for the next major release.

        Comment


        • #19
          I can't understand why open a JIRA issue for something that can be already done as required by Rahul Mishra.
          I explain how:

          1)I define a request scoped bean both in the interceptor and in the endpoint that can be also an AbstractMarshallingPayloadEndpoint with automatic marshalling/unmarshalling of SOAP body
          2)In the interceptor I read the SOAP Header and can Unmarshal it to a object using Jibx , JAXB or what I want es:
          Code:
          	public boolean handleRequest(MessageContext messageContext, Object endpoint)
          			throws Exception {
              	System.out.println("Interceptor invocato ");
          	    AxiomSoapMessage axiomMsg = (AxiomSoapMessage)messageContext.getRequest();
          	    SOAPEnvelope axiomEnvelope = axiomMsg.getAxiomMessage().getSOAPEnvelope();
          	    SOAPHeader header=axiomEnvelope.getHeader();
          	    QName elementQName = new QName( "http://www.openspcoop.org/integrazione","integrazione");
          	    OMElement element=null;
          	    if (header!=null)
          	    	element=header.getFirstChildWithName(elementQName);
              
          	    if (element!=null)
          	    {
          	    	IUnmarshallingContext ctx = BindingDirectory.getFactory(IntegrazioneImpl.class).createUnmarshallingContext();
          	    	Integrazione appo = (Integrazione) ctx.unmarshalDocument(new StringReader(element.toString()));
               //integra is my request scoped bean
               integra.setInte(appo);
          	    }
          		return true;
          	}
          3)In the endpoint in the variable referring to the request scoped bean I have the info of the SOAP header.

          With client interceptor this works also for Web Services clients If I need.

          Comment


          • #20
            Hi luciano61,

            I am not sure how to unmarshall soap header elements can you explain it a bit. I a, trying to get a particular soap header element named session. after that i want to unmarshall it using jaxb. but not exaxtly sure how to do it. Here what my interceptor code looks like
            Code:
            	public boolean handleRequest(MessageContext messageContext, Object endpoint)
            			throws Exception {
            		System.out.println("Intercepted");
            		SoapHeader soapHeader = ((SaajSoapMessage) messageContext.getRequest()).getEnvelope().getHeader();
            		
            		Iterator i = soapHeader.examineAllHeaderElements();
            		SessionHeader sessionHeader = null 
            		for (Iterator iterator = soapHeader.examineAllHeaderElements(); iterator.hasNext();) {
            			SoapHeaderElement headerElement = (SoapHeaderElement) iterator.next();
            			if(headerElement.getName().getLocalPart().equals("session")){
            // i want to unmarshall this element into Session Header. 
            }
            }
            }

            Secondly can you please provide configuration settings for the code provided in ur last post especially for the request scope bean and its injection in other beans.

            thanks,
            Muein
            Last edited by mmuzamil; May 15th, 2009, 05:41 AM.

            Comment


            • #21
              Just to add to my last post i thought of creating object manually for now and setting it in the request scope bean. I defined a request scoped bean which is being referred by interceptor and endpoint both. Then i created the Java object manually in interceptor and i set it into the request scoped bean. But i am still not able to access this object in the endpoint. I think there is some issue in my configurations.

              Code:
              	@Override
              	public boolean handleRequest(MessageContext messageContext, Object endpoint)
              			throws Exception {
              		System.out.println("Intercepted");
              		SoapHeader soapHeader = ((SaajSoapMessage) messageContext.getRequest()).getEnvelope().getHeader();
              		
              		SessionHeader sessionHeader = new SessionHeader(); 
              		for (Iterator iterator = soapHeader.examineAllHeaderElements(); iterator.hasNext();) {
              			SoapHeaderElement headerElement = (SoapHeaderElement) iterator.next();
              			if(headerElement.getName().getLocalPart().equals("session")){
              				System.out.println(headerElement.toString());
              				
              				DOMResult r = (DOMResult)headerElement.getResult();
              				Node sessionNode = r.getNode();
              				
              				NodeList childNodes = sessionNode.getChildNodes();
              				for (int j = 0; j < childNodes.getLength(); j++) {
              					Node childNode = childNodes.item(j);
              					if(childNode.getNodeType()== Node.ELEMENT_NODE && childNode.getLocalName().equals("sessionId")){
              						sessionHeader.setSessionId(childNode.getTextContent());
              					}
              				}
              			}
              		}
              		header.setSessionHeader(sessionHeader);
              		return true;
              
              	}
              Configuration
              Code:
              	<!-- Setting for Annotation endpoint -->
              	<bean id="addOperationEndpoint" class="com.conformity.webservice.endpoints.AddOperationEndPoint">
              		<property name="service" ref="conformityWebServiceMediator"/>
              		<property name="header" ref="sessionHeader"/>
              	</bean>
              	<bean id="sessionHeader" class="com.conformity.webservice.endpoints.SessionHeaderWrapper"
              		scope="request">
              	<!-- this next element effects the proxying of the surrounding bean -->
              		<aop:scoped-proxy />
              	</bean>
              	<bean id="soapHeaderInterceptor" class="com.conformity.webservice.interceptor.HeaderItemInterceptor">
              		<property name="unmarshaller" ref="marshaller" />
              		<property name="header" ref="sessionHeader"/>
              	</bean>
              Web.xml
              Code:
                  <servlet-mapping>
                      <servlet-name>spring-ws</servlet-name>
                      <url-pattern>/conformity-ws/*</url-pattern>
                  </servlet-mapping>	
              	<listener>
              		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
              	</listener>
              	<filter>
              	<filter-name>requestContextFilter</filter-name>
              		<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
              	</filter>
              	<filter-mapping>
              		<filter-name>requestContextFilter</filter-name>
              		<url-pattern>/conformity-ws/*</url-pattern>
              	</filter-mapping>
              Can anyone please tell me what is wrong here?

              Thanks,
              Muein

              Comment


              • #22
                For unmarshalling you define a marshaller in configuration
                Code:
                <!-- using Jibx -->
                	<bean id="integrazioneMarshaller"
                		class="org.springframework.oxm.jibx.JibxMarshaller">
                		<property name="targetClass">
                			<value>
                				it.almaviva.model.springws.integrazione.IntegrazioneImpl
                			</value>
                		</property>
                	</bean>
                
                <!-- using JAXB2 -->
                	<bean id="jaxbMarshaller"
                		class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                		<property name="contextPath" value="it.almaviva.model.jaxb" />
                		<property name="mtomEnabled" value="true" />
                	</bean>
                and then call unmarshal on the SoapHeaderElement in your code:
                Code:
                ...
                import org.springframework.oxm.Unmarshaller;
                ...
                private Unmarshaller unmarshaller=null;
                ...
                SoapHeaderElement element=....
                YourHeaderType myHead = (YourHeaderType ) unmarshaller.unmarshal(element.getSource());
                for the request scoped bean configuration here is a sample
                Code:
                	<bean id="integrazione"
                		class="it.almaviva.model.springws.integrazione.IntegrazioneImpl"
                		scope="request">
                		<property name="tipoCorrelazione" value="coll" />
                		<aop:scoped-proxy proxy-target-class="false" />
                	</bean>
                ...
                	<bean id="getContattiByCittaAsynchSimmMarshallingEndpoint"
                		class="it.almaviva.springws.GetContattiByCittaAsynchSimmMarshallingEndpoint"
                		p:marshaller-ref="statorichiestaMarshaller"
                		p:unmarshaller-ref="nomecittaasyncUnMarshaller"
                		p:integra-ref="integrazione" p:jmsTemplate-ref="jmsTemplate" />
                ...
                	<bean
                		class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
                		<property name="interceptors">
                			<list>
                				<ref bean="integrazioneInterceptor" />
                			</list>
                		</property>
                		<property name="endpointMap">
                			<map>
                				<entry
                					key="{http://luciano.mytest.com/schema/spcoop}nomecittaasyncparm"
                					value-ref="getContattiByCittaAsynchSimmMarshallingEndpoint" />
                				<entry
                	...
                			</map>
                		</property>
                	</bean>
                ...
                	<bean id="integrazioneInterceptor"
                		class="it.almaviva.springws.interceptors.IntegrationHeaderServerInterceptor"
                		p:integra-ref="integrazione" />
                Luciano

                Comment


                • #23
                  Thanks for the reply. I have been able to unmarshall Soap header into Java object. But i am still not able to access the Soapheader in endpoint. I have looked at your configurations and i am still not sure what am i missing .

                  Can you please also share the code of your it.almaviva.model.springws.integrazione.Integrazio neImpl class.

                  Here is my configuration file for ur reference.

                  Code:
                  	<bean id="addOperationEndpoint" class="com.conformity.webservice.endpoints.AddOperationEndPoint">
                  		<property name="service" ref="conformityWebServiceMediator"/>
                  		<property name="header" ref="sessionHeaderWrapper"/>
                  	</bean>
                  	<bean id="sessionHeaderWrapper" class="com.conformity.webservice.endpoints.SessionHeaderWrapper"
                  		scope="request">
                  	<!-- this next element effects the proxying of the surrounding bean -->
                  		<aop:scoped-proxy />
                  	</bean>
                  	<bean id="soapHeaderInterceptor" class="com.conformity.webservice.interceptor.HeaderItemInterceptor">
                  		<property name="unmarshaller" ref="marshaller" />
                  		<property name="header" ref="sessionHeaderWrapper"/>
                  	</bean>
                  SessionHeaderWrapper
                  ---------------------
                  Code:
                  public class SessionHeaderWrapper {
                  	SessionHeader sessionHeader;
                  	public SessionHeader getSessionHeader() {
                  		return sessionHeader;
                  	}
                  	public void setSessionHeader(SessionHeader sessionHeader) {
                  		this.sessionHeader = sessionHeader;
                  	}
                  }
                  Interceptor
                  -----------
                  Code:
                  	@Override
                  	public boolean handleRequest(MessageContext messageContext, Object endpoint)
                  			throws Exception {
                  		System.out.println("Intercepted");
                  		SoapHeader soapHeader = ((SaajSoapMessage) messageContext.getRequest()).getEnvelope().getHeader();
                  		
                  //		SessionHeader sessionHeader = new SessionHeader(); 
                  		SessionHeader sessionHeader = null; 
                  		for (Iterator iterator = soapHeader.examineAllHeaderElements(); iterator.hasNext();) {
                  			SoapHeaderElement headerElement = (SoapHeaderElement) iterator.next();
                  			if(headerElement.getName().getLocalPart().equals("sessionHeader")){
                  				System.out.println(headerElement.toString());
                  				JAXBElement<SessionHeader> sessionHeaderJaxb = (JAXBElement<SessionHeader>) unmarshaller.unmarshal(headerElement.getSource());
                  				sessionHeader = sessionHeaderJaxb.getValue();
                  
                  		header.setSessionHeader(sessionHeader);
                  		return true;
                  
                  	}
                  I really appreciate ur help.

                  Thanks,
                  Muein

                  Comment


                  • #24
                    Alright, it is working now. Actually it was working earlier as well the problem was that i wasn't using the scoped bean within my end point. i was just looking at the bean using debugger which is showing a null value.

                    Once i got this bean within my endpoint it started showing header values.

                    Thanks for you help.
                    Muein

                    Comment


                    • #25
                      Hi All,

                      Good day Spring people!

                      I'm currently implenting Spring web services and I kind of stuck in this same situation as well. It is good thing to know I can use a request scope bean for this scenario but I noticed that this has been a year ago. I wonder if the new release of Spring web service already implemented a different approach to handle this?

                      Thank you!

                      Comment


                      • #26
                        Hi All,

                        I am trying to extract the header information before passing to endpoint.. Can someone please provide me with good example for it..

                        Thanks in advance

                        Comment

                        Working...
                        X