Results 1 to 3 of 3

Thread: Adding SOAP Header

  1. #1
    Join Date
    Nov 2009
    Location
    Brazil
    Posts
    16

    Angry Adding SOAP Header

    Hello Guys,

    I'm facing a problem to add a soap header properly during request.

    I need a Header like this:

    <soapenv:Header>
    <esb:CONSUMIDOR xmlns:m="http://foo.com">
    <IDCONSUMIDOR>DINIZ</IDCONSUMIDOR>
    <USUARIOCANAL>FOO</USUARIOCANAL>
    <PASSWORD>MyPsw</PASSWORD>
    </esb:CONSUMIDOR>
    </soapenv:Header>

    But I'm getting the below wrong header instead:

    Code:
    <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <esb:CONSUMIDOR soapenv:mustUnderstand="0" xmlns:esb="http://foo.com"/>
    </soapenv:Header>
    Take a look at the code below:

    Code:
    (...)
    
    public class WSClient {
    	
    	 private final static String CONSUMIDOR_HEADER_NODE = "<esb:CONSUMIDOR xmlns:m=\"http://foo.com\" xmlns:esb=\"http://foo.com\">";
    	    private final static String _CONSUMIDOR_HEADER_NODE = "</esb:CONSUMIDOR>";
    	    private final static String USERNAME_HEADER_NODE = "<IDCONSUMIDOR>";
    	    private final static String _USERNAME_HEADER_NODE = "</IDCONSUMIDOR>";
    	    private final static String USUARIOCANAL_HEADER_NODE = "<USUARIOCANAL>";
    	    private final static String _USUARIOCANAL_HEADER_NODE = "</USUARIOCANAL>";
    	    private final static String PASSWORD_HEADER_NODE = "<PASSWORD>";
    	    private final static String _PASSWORD_HEADER_NODE = "</PASSWORD>";	    
    	    private StringBuffer headerBuffer = new StringBuffer();
    
    	
        private WebServiceTemplate webServiceTemplate;    
    
        public RootInstraClient(WebServiceTemplate webServiceTemplate) {    	
            this.webServiceTemplate = webServiceTemplate;   
            headerBuffer.append(CONSUMIDOR_HEADER_NODE);
            headerBuffer.append(USERNAME_HEADER_NODE);
            headerBuffer.append("XX");
            headerBuffer.append(_USERNAME_HEADER_NODE);
            headerBuffer.append(USUARIOCANAL_HEADER_NODE);
            headerBuffer.append("YYY");
            headerBuffer.append(_USUARIOCANAL_HEADER_NODE);        
            headerBuffer.append(PASSWORD_HEADER_NODE);
            headerBuffer.append("foo");
            headerBuffer.append(_PASSWORD_HEADER_NODE);
            headerBuffer.append(_CONSUMIDOR_HEADER_NODE);
    
        }
    
        public RESPONSE_INSTRA ROOT_INSTRA(final ROOT_INSTRA rootInstra) throws Exception {
        	RESPONSE_INSTRA response = (RESPONSE_INSTRA)webServiceTemplate.marshalSendAndReceive(rootInstra, new WebServiceMessageCallback() {
    
                public void doWithMessage(WebServiceMessage webServiceMessage) {
    
                    try {               	                	                	                	
                    	SoapMessage soapMessage = (SoapMessage)webServiceMessage;                	
                        SoapHeader soapHeader = soapMessage.getSoapHeader();                    
                        StringSource headerSource = new StringSource(headerBuffer.toString());
                        Transformer transformer = TransformerFactory.newInstance().newTransformer();
                        transformer.transform(headerSource, soapHeader.getResult());                    
                    } catch (Exception e) {
                        new RuntimeException(e);
                    }
    
                }
                
            });    	
    
            return response;
            
        }         
        
    }
    What's missing guys?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Location
    Brazil
    Posts
    16

    Talking Solved

    All,

    Issue was fixed. I had to do something like this:

    Code:
    public Object FOO(final FOO foo) throws Exception {
      Object response = (Object) webServiceTemplate.marshalSendAndReceive(rootInstra, new WebServiceMessageCallback() {
    
         public void doWithMessage(WebServiceMessage webServiceMessage) {
    	try {
    	  SoapMessage soapMessage = (SoapMessage) webServiceMessage;
    	  setHeader(soapMessage);			
    	} 
                 catch (Exception e) {
                   new RuntimeException(e);
    	}
         }
    
        });
    
        return response;
    
    }
    
    private void setHeader(SoapMessage soapMessage) throws TransformerFactoryConfigurationError, TransformerException {
    		
    		SoapHeader soapHeader = soapMessage.getSoapHeader();
    		DOMResult xmlHeader = (DOMResult) soapHeader.getResult();
    		Node headerNode = xmlHeader.getNode();	
    		Element xmlHeaderElement = headerNode.getOwnerDocument().createElementNS("http://foo", "FOO");
    		xmlHeaderElement.setPrefix("diniz");
    
    		Element idConsumidorElement = headerNode.getOwnerDocument().createElementNS("", "fooId");
    		xmlHeaderElement.appendChild(idConsumidorElement);
    		idConsumidorElement.appendChild(headerNode.getOwnerDocument().createTextNode("VALUE_HERE"));		
    		headerNode.appendChild(xmlHeaderElement);
    
    }
    I tried to use the "Transformer" approach, but I was able to add only one additional element. For more than one, it didn't work properly.

    Thank you all.

  3. #3
    Join Date
    Nov 2012
    Posts
    1

    Default

    Hi dinizssa,

    I have followed the steps you said worked for you.
    Unfortunately, it doesn't work for me.
    I have tried also using the transformer way as described in another thread.
    Still no chance.
    Is there any over way to manipulate the header element for authentication purpose?
    My aim is to get elements below in the SOAP header element:

    <AuthHeader>
    <Client>001</Client>
    <UserName>adbc</UserName>
    <Password>xyz</Password>
    </AuthHeader>

    Thank you in advance for your answers and suggestions.
    K

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •