Soap Headers using cxf-codegen-plugin

30 Views Asked by At

I am using cxf-codegen-plugin to generate my WSDL classes in Java, I want to add Soap Headers to the request, but they are not generating in the plugin. How do I add them?

This is my defined plugin:

<plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>4.0.2</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <defaultOptions>
                            <extraargs>
                                <extraarg>-p</extraarg>
                                <extraarg>com.smartcode.springenterpriseapi.stub.target</extraarg>
                            </extraargs>
                        </defaultOptions>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>${project.basedir}/src/main/resources/enterprise.wsdl</wsdl>
                                <extraargs>
                                    <extraarg>-autoNameResolution</extraarg>
                                </extraargs>
                            </wsdlOption>
                        </wsdlOptions>
                        <includes>
                            <include>${project.build.directory}/generated-sources/cxf/**</include>
                        </includes>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I have been looking to see if there is an configuration or how you can add it to the Java object.

1

There are 1 best solutions below

0
Julie On

I figured out the answer. I thought the generated classes from cxf-codegen-plugin would somehow establish the relationship from the soap envelope with the body and the headers, but it doesn't, you have to add an interceptor.

public class HeaderInterceptor extends AbstractPhaseInterceptor <SoapMessage>  {

  public HeaderInterceptor() {
    super(Phase.WRITE);
  }

  @Override
  public void handleMessage(SoapMessage message) throws Fault {
    CustomHeader header = new CustomHeader();
    header.setHeaderValue("Header Value");

    SoapHeader soapHeader = new SoapHeader(new QName("http://example.com/namespace", "CustomHeader"), header, new JAXBDataBinding());
    List < Header > headers = message.getHeaders();
    headers.add(header);
  }
}

Then, you can call it when you establish your client.

 Client client = ClientProxy.getClient(soap);
 HeaderInterceptor interceptor = new HeaderInterceptor();
 client.getOutInterceptors().add(interceptor);
 HTTPConduit http = (HTTPConduit) client.getConduit();
 final HTTPClientPolicy policy = http.getClient();
 policy.setAutoRedirect(true);