javawsdljaxws-maven-plugin

Customizing prefix and namespace location in soap request generated using wsdl file


I am struggling to understand why code-generated soap request on the left is not working, but if I tweak it to what's on the right, then it works?

enter image description here Now that I know what needs to be done to make it work, how do I fix it ?

I added jaxws-maven-plugin to my java project:

     <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                   <sourceDestDir>src/main/java</sourceDestDir>
                   <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
                   <wsdlFiles>
                       <wsdlFile>Flattened_Integrator7.0.wsdl</wsdlFile>
                   </wsdlFiles>
                   <keep>true</keep>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Notice in the picture above without prefix wsse, it doesn't work.

It has to be that word. And it exists in wsdl file. enter image description here Does anyone know how:

  1. I can force namespace prefix for "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" to be wsse
  2. Force code to generate namespaces in soap envelope and not in Security section

Solution

  • So, I had to manually add prefix/namespace to envelope and rename all children's prefixes to wsse.

    Here is how I did it:

    @Component
    public class RequestClient {
    
        private static final String WSSE_PREFIX = "wsse";
        private static final String WSSE_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
        private static final String NS2_PREFIX = "ns2";
        private static final String NS2_NAMESPACE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
    
        private buildSoaprequest(){
            ...
            SOAPEnvelope soapEnvelope = soapMessage.getSOAPPart().getEnvelope();
            soapEnvelope.addNamespaceDeclaration(WSSE_PREFIX, WSSE_NAMESPACE);
            soapEnvelope.addNamespaceDeclaration(NS2_PREFIX, NS2_NAMESPACE);
            SOAPHeader soapHeader = soapMessage.getSOAPHeader();
            removeUndesiredBodyNamespaceEntries(soapHeader.getChildElements());
            soapHeader.setPrefix(WSSE_PREFIX);
            addDesiredBodyNamespaceEntries(soapHeader.getChildElements());              
            soapMessage.saveChanges();
            ...
        }
    
        private void addDesiredBodyNamespaceEntries(Iterator childElements) {
            while (childElements.hasNext()) {
              final Object childElementNode = childElements.next();
              if (childElementNode instanceof SOAPElement) {
                SOAPElement soapElement = (SOAPElement) childElementNode;
                soapElement.setPrefix(WSSE_PREFIX); 
                addDesiredBodyNamespaceEntries(soapElement.getChildElements());
              }
            }
          }
    
        private void removeUndesiredBodyNamespaceEntries(Iterator childElements) {
            while (childElements.hasNext()) {
              final Object childElementNode = childElements.next();
              if (childElementNode instanceof SOAPElement) {
                SOAPElement soapElement = (SOAPElement) childElementNode;
    
                //remove any prefix/namespace entries added by JAX-WS in the body element
                //it cannot be null, so it will leave wsse
                for (String prefix : getNamespacePrefixList(soapElement.getNamespacePrefixes())) {
                  if (prefix != null) {
                    soapElement.removeNamespaceDeclaration(prefix);
                  }
                }
                // recursively remove prefix/namespace entries in child elements
                removeUndesiredBodyNamespaceEntries(soapElement.getChildElements());
              }
            }
          }
    
         private Set<String> getNamespacePrefixList(Iterator namespacePrefixIter) {
            Set<String> namespacePrefixesSet = new HashSet<>();
            while (namespacePrefixIter.hasNext()) {
              namespacePrefixesSet.add((String) namespacePrefixIter.next());
            }
            return namespacePrefixesSet;
          }