Announcement Announcement Module
Collapse
No announcement yet.
Add custom elements to SOAP header Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add custom elements to SOAP header

    I have a problem when I try to add custom elements to SOAP header. I found a solution from this forum earlier and it works fine. However, there is a problem when I add all the required elements. Here's the header part of the message:

    Code:
    <SOAP-ENV:Header>
    <Security>
       <UsernameToken>
        <Username>username</Username>
        <Password>password</Password>
       </UsernameToken>
      </Security>
      <timeStamp>timestamp</timeStamp>
      <checkSum>md5hash</checkSum>
     </SOAP-ENV:Header>
    And here's the code how I'm adding the custom elements:

    Code:
    public class GetReportWebServiceClient extends WebServiceGatewaySupport {
    
    ...
    
    Source source = this.marshallObject(object);
    
    this.getWebServiceTemplate().sendSourceAndReceiveToResult("myIpAddress", source, new WebServiceMessageCallback() {
    
    	public void doWithMessage(WebServiceMessage message)
    			throws IOException, TransformerException {
    
    		String timestamp = "foo";
    		String md5hash = "bar";
    
    		try {
    			StringBuffer sbHeader = new StringBuffer();
    		            sbHeader.append("<Security>")
    		                .append("<UsernameToken>")
    		                .append("<Username>myUsername</Username>")
    		                .append("<Password>myPassword</Password>")
    		                .append("</UsernameToken>")
    		                .append("</Security>")
    		                .append("<timeStamp>" + timestamp + "</timeStamp>")
    		                .append("<checkSum">" + md5hash + "</checkSum>");
    
    	            StringSource stringSource = new StringSource(sbHeader.toString());
    	            SoapEnvelope soapEnvelope = ((SoapMessage) message).getEnvelope();
    
    	            soapEnvelope.addAttribute(new QName("xmlns"), "http://www.w3.org/2001/XMLSchema");
    	            soapEnvelope.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");
    
    	            SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
    	            Transformer transformer = TransformerFactory.newInstance().newTransformer();
    	            transformer.transform(stringSource, soapHeader.getResult());
    
    
    		} catch (Exception e) {
    			throw new TransformerException(e.getMessage());
    		}
    
    	}
    	}, stringResult);
    ...
    }
    But I get an error saying that the (SOAP header) XML is not well formed:

    Code:
    Caused by: org.springframework.ws.client.WebServiceTransformerException: Transformation error: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.; nested exception is javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
    	at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:510)
    	at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:440)
    	at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:395)
    	at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:341)
    	at com.sofor.customer.kuluttavavirasto.marek.service.asiakastieto.GetReportWebServiceClient.sendSoapRequest(GetReportWebServiceClient.java:84)
    	at com.sofor.customer.kuluttavavirasto.marek.service.asiakastieto.GetReportWebServiceClient.getOrganisationData(GetReportWebServiceClient.java:59)
    	at com.sofor.customer.kuluttavavirasto.marek.applogic.TourOperatorManagerImpl.getOrganisationData(TourOperatorManagerImpl.java:307)
    	at com.sofor.customer.kuluttavavirasto.marek.applogic.TourOperatorManagerImpl.saveTourOperator(TourOperatorManagerImpl.java:98)
    	at sun.reflect.GeneratedMethodAccessor113.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:203)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
    	at $Proxy3.saveTourOperator(Unknown Source)
    	at sun.reflect.GeneratedMethodAccessor113.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at org.apache.wicket.proxy.LazyInitProxyFactory$JdkHandler.invoke(LazyInitProxyFactory.java:416)
    	at org.apache.wicket.proxy.$Proxy21.saveTourOperator(Unknown Source)
    	at com.sofor.customer.kuluttavavirasto.marek.web.touroperator.TourOperatorPage$TourOperatorForm$2.onSubmit(TourOperatorPage.java:209)
    	at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1324)
    	at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:776)
    	... 32 more
    As far as I understood, the problem is that the XML is not well formed since there are more than one root elements: "security", "timeStamp" and "checkSum". If I move the timestamp and checksum inside the security element, everything works just fine.

    Is there something I can do to solve my problem?
Working...
X