3

I have a wsdl that defines a soap header that needs to be passed when calling the web service.

The sample SOAP Header is:

<soapenv:Header>
   <AuthenticationInfo>
      <userName>User</userName>
      <password/>
   </AuthenticationInfo>
</soapenv:Header>

CXF's wsdl2java generated an "AuthenticationInfo" java class that I can create and populate with a username and password, but I don't know the proper way to pass that to the CXF Client when calling the web service.

ScArcher2
  • 85,501
  • 44
  • 121
  • 160
  • Example which generates client methods with arguments for a header: https://github.com/skjolber/mockito-soap-cxf – ThomasRS Aug 04 '17 at 15:02

5 Answers5

13

While generating the proxy class using Apache CXF using adding the extendedSoapHeaders with true will generate the PortType Class with the Request and Header argument.

<wsdlOption>              
<wsdl>${project.basedir}/src/main/resources/wsdl/sample.wsdl</wsdl>
 <!-- enables processing of implicit SOAP headers, default is false -->
<extendedSoapHeaders>true</extendedSoapHeaders>
</wsdlOption>
soumitra chatterjee
  • 2,268
  • 9
  • 26
  • 48
4

Well, the most simple way to do this would be create an ArrayList of Header objects and add all your parameters or a Map<String,Object> and add all your headers as map.put("param1",param1).

Finally get your request context and add this arraylist of map as

requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
soapHeaders); 

If you're trying to pass custom soap headers, refer THIS LINK.

The general pitfalls have been mentioned in THIS DISCUSSION. It might be helpful to you.

afrin216
  • 2,295
  • 1
  • 14
  • 17
  • This works. I found a few other options in the FAQ. I ended up going with the wsdl2java -exsh true method. http://cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest%2Fresponse%3F – ScArcher2 Aug 02 '12 at 14:50
  • @Afrin Thanks. Also Does `StorageMessage` in the request translate to this piece of code snippet in java `Header actionHeader = new Header(new QName("uri:org.apache.cxf", "Action"), "StorageMessage", new JAXBDataBinding(String.class));` ? – james2611nov Apr 15 '15 at 20:10
2

Found myself in same situation: wsdl2java generated the header class, and I needed to add it as a SOAP header to the outgoing SOAP request.

My solution in code was as follows (reusing original question's AuthenticationInfo as the header class name):

import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.headers.Header;

AuthenticationInfo ai = new AuthenticationInfo();
ai.setUserName("User");
ai.setPassword("");

List<Header> soapHeaders = new ArrayList<Header>();

Header h1 = new Header(new QName("http://namespace/of/AuthenticationInfo", "AuthenticationInfo"), 
                       ai, new JAXBDataBinding(AuthenticationInfo.class));

soapHeaders.add(h1);

ClientProxy.getClient(port).getRequestContext().put(Header.HEADER_LIST, soapHeaders);
Alex
  • 10,470
  • 8
  • 40
  • 62
1

If the SOAP header is defined in the WSDL then it can either be specified implicit or explicit.

CXF provides the wsdl2java tool for generating a Java service interface from a WSDL. In the case of explicit headers, the SOAP headers are automatically detected and made available as part of the service interface that gets generated.

If the SOAP headers have been defined implicitly, then you need to enable the -exsh option which triggers processing of implicit SOAP headers. Again the SOAP headers will be made available as part of the service Java interface that gets generated. If you want a concrete example you can checkout a blog post I made on how to add a cxf soap header.

Note that CXF also supports other ways of adding SOAP headers.

CodeNotFound
  • 1,051
  • 2
  • 22
  • 47
0

With CXF 3.4.4

you can add an header configuring security like this

<soapenv:Header>
    <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
    <wsse:UsernameToken xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
    <wsse:Username>MY-USER</wsse:Username>
    <wsse:Password Type="wsse:PasswordText">MY-PWD</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>

Using an interceptor, as described here: http://cxf.apache.org/docs/ws-security.html

Client serviceClient = ClientProxy.getClient ( port );
        
Endpoint cxfEndpoint = serviceClient.getEndpoint ();
        
Map<String, Object> outProps = new HashMap<> ();
outProps.put ( WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN );
outProps.put ( WSHandlerConstants.USER, "MY-USER" );
outProps.put ( WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT );
outProps.put ( WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordCallback.class.getName () ); 
        
WSS4JOutInterceptor requestInterceptor = new WSS4JOutInterceptor ( outProps );
cxfEndpoint.getOutInterceptors ().add ( requestInterceptor );
    
MyRequest request = new MyRequest ();
    
Object [] res = serviceClient.invoke ( "operation-name", request );
MyResponse out = ( MyResponse ) res [0];

Where "operation-name" is the name of the WebService operation to invoke

The WSPasswordCallback is:

import org.apache.wss4j.common.ext.WSPasswordCallback;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;

public class ClientPasswordCallback implements CallbackHandler {

    public void handle ( Callback [] callbacks ) throws IOException, UnsupportedCallbackException {

        WSPasswordCallback passwordCallback = ( WSPasswordCallback ) callbacks [0];

        // Set the password for our message.
        passwordCallback.setPassword ( "MY-PWD" );
    }
}
Massimo Da Ros
  • 393
  • 3
  • 11