Announcement Announcement Module
Collapse
No announcement yet.
Add SOAP header to incoming msg w/no header Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add SOAP header to incoming msg w/no header

    I need to add an element to an incoming SOAP header. But when no SOAP header is present, how do you add one? I am using an interceptor and am OK if there is an existing header element.

    Thanks -- Sam

  • #2
    You can always call SoapMessage.getEnvelope().getHeader(). This will create a new header for you if necessary.

    Comment


    • #3
      <soapenv:Header> not present in XML

      This only seems to work if the SOAP header element is present in the XML. Am I correct in that assessment?

      Comment


      • #4
        We add a SOAP envelope including a SOAP header to incomimg (raw) XML messages in a web service client application now. The following is an inner class that implements WebServiceMessageCallback, that's used in one of the overloaded WebServiceTemplate's sendAndReceive methods. The document passed contains the raw XML. The newly formed SOAP messages are then forwarded on to a server.

        Code:
        	class RequestWebServiceMessageCallback implements WebServiceMessageCallback {
        		private final Document document;
        		private final PapSession papSession;
        
        		public RequestWebServiceMessageCallback(Document document, PapSession papSession) {
        			this.document = document;
        			this.papSession = papSession;
        		}
        
        		public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
        			try {
        				SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
        				SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
        				addSoapHeader(soapMessage);
        				addSoapBody(soapMessage);
        				logSoapMessage(soapMessage);
        			} catch (SOAPException e) {
        				throw new NestableRuntimeException(e);
        			}
        		}
        
        		private void addSoapHeader(SOAPMessage soapMessage) throws SOAPException {
        			SOAPPart soapPart = soapMessage.getSOAPPart();
        			SOAPEnvelope envelope = soapPart.getEnvelope();
        			Channel channel = papSession.getChannel();
        			Name name = envelope.createName(channel.getClass().getSimpleName(), null, PapConstants.NAMESPACE_SOAP_HEADER);
        			SOAPHeader soapHeader = soapMessage.getSOAPHeader();
        			// soapHeader.addAttribute(name, channel.name());
        			// soapHeaderElement.setAttribute("name", channel.name());
        			SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(name);
        			soapHeaderElement.setValue(channel.name());
        		}
        
        		private void addSoapBody(SOAPMessage soapMessage) throws SOAPException {
        			Element root = document.getDocumentElement();
        			if (root.hasAttributeNS(PapConstants.NAMESPACE_XMLNS_URI, "xmlns")) {
        				schemaLocation.set(root.getAttributeNS(PapConstants.NAMESPACE_SCHEMA_INSTANCE_URI, "schemaLocation"));
        			}
        			SOAPBody soapBody = soapMessage.getSOAPBody();
        			soapBody.addDocument(document);
        		}
        	}
        We do similar things on the server side as well - handleRequest and handleResponse methods in EndpointInterceptor's
        Alan
        Last edited by Alan Stewart; Jun 19th, 2008, 06:44 PM.

        Comment

        Working...
        X