Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add soap header in java

i have a NO-.net webservice from oracle To access i need to add the soap header. How can i add the soap header in java?

Authenticator.setDefault(new ProxyAuthenticator("username", "password"));
                System.getProperties().put("proxySet", "true");
                System.setProperty("http.proxyHost", "IP");
                System.setProperty("http.proxyPort", "port");




                proxy = new RegPresMed_Service(new URL("webservice")).getRegPresMed();
                ((BindingProvider) proxy).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "realwebservice");
                ((BindingProvider) proxy).getRequestContext().put("com.sun.xml.ws.request.timeout", new Integer(60000));
                ((BindingProvider) proxy).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "webserviceUsername");
                ((BindingProvider) proxy).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "webservicePassword");

is this necessary?

 ((BindingProvider) proxy).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "webserviceUsername");
                    ((BindingProvider) proxy).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "webservicePassword");

my soap header is like this:

<wsse:Security soapenv:mustUnderstand="1"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken wsu:Id="UsernameToken-6"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>username</wsse:Username>
        <wsse:Password
Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
        <wsse:Nonce
EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">randomnaumber==</wsse:Nonce>
        <wsu:Created>dateCreated</wsu:Created>
    </wsse:UsernameToken>
</wsse:Security>
like image 689
Luis Avatar asked May 12 '11 10:05

Luis


People also ask

How do I add a SOAP header in WSDL?

You can add soap header information to method calls by decorating the methods in the proxy class generated from the wsdl with the SoapHeader attribute. For example wsdl.exe will generate client proxy class Reference. cs for the web service reference when you "Add Web Reference".

How do I create a SOAP request in Java?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

What is a SOAP header?

The SOAP <Header> is an optional element in a SOAP message. It is used to pass application-related information that is to be processed by SOAP nodes along the message path. The immediate child elements of the <Header> element are called header blocks.


2 Answers

i Did it, just follow this tutorial. helps a lot

Is a copy from javadb (because is down)
http://informatictips.blogspot.pt/2013/09/using-message-handler-to-alter-soap.html

or

http://www.javadb.com/using-a-message-handler-to-alter-the-soap-header-in-a-web-service-client

like image 95
Luis Avatar answered Oct 04 '22 20:10

Luis


I struggled to get this working. That's why I'll add a complete solution here:

My objective is to add this header to the SOAP envelope:

   <soapenv:Header>
      <urn:OTAuthentication>
         <urn:AuthenticationToken>TOKEN</urn:AuthenticationToken>
      </urn:OTAuthentication>
   </soapenv:Header>
  1. First create a SOAPHeaderHandler class.

    import java.util.Set;
    import java.util.TreeSet;
    
    import javax.xml.namespace.QName;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPFactory;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.handler.soap.SOAPHandler;
    import javax.xml.ws.handler.soap.SOAPMessageContext;
    
    public class SOAPHeaderHandler implements SOAPHandler<SOAPMessageContext> {
    
        private final String authenticatedToken;
    
        public SOAPHeaderHandler(String authenticatedToken) {
            this.authenticatedToken = authenticatedToken;
        }
    
        public boolean handleMessage(SOAPMessageContext context) {
            Boolean outboundProperty =
                    (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outboundProperty.booleanValue()) {
                try {
                    SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
                    SOAPFactory factory = SOAPFactory.newInstance();
                    String prefix = "urn";
                    String uri = "urn:api.ecm.opentext.com";
                    SOAPElement securityElem =
                            factory.createElement("OTAuthentication", prefix, uri);
                    SOAPElement tokenElem =
                            factory.createElement("AuthenticationToken", prefix, uri);
                    tokenElem.addTextNode(authenticatedToken);
                    securityElem.addChildElement(tokenElem);
                    SOAPHeader header = envelope.addHeader();
                    header.addChildElement(securityElem);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                // inbound
            }
            return true;
        }
    
        public Set<QName> getHeaders() {
            return new TreeSet();
        }
    
        public boolean handleFault(SOAPMessageContext context) {
            return false;
        }
    
        public void close(MessageContext context) {
            //
        }
    }
    
    1. Add the handler to the proxy. Note that according javax.xml.ws.Binding's documentation: "If the returned chain is modified a call to setHandlerChain is required to configure the binding instance with the new chain."

    Authentication_Service authentication_Service = new Authentication_Service();
    
    Authentication basicHttpBindingAuthentication = authentication_Service.getBasicHttpBindingAuthentication();
    String authenticatedToken = "TOKEN";
    
    List<Handler> handlerChain = ((BindingProvider)basicHttpBindingAuthentication).getBinding().getHandlerChain();
    handlerChain.add(new SOAPHeaderHandler(authenticatedToken));
    ((BindingProvider)basicHttpBindingAuthentication).getBinding().setHandlerChain(handlerChain);
    
like image 4
edubriguenti Avatar answered Oct 04 '22 21:10

edubriguenti