Wednesday, September 2, 2015

ADF SOAP Webservices Namespace change

We can see that the webservices created in ADF, is changing the response namespaces frequently when there are some new deployment happens. To avoid this particular issue, please follow the below workarounds in the ADF SOAP Webservice project.

Scenario 1:- Wrapper Webservice.

If we are creating wrapper webservice in ADF based on webservice proxy, please go to the proxy classes and locate “package-info.java”.

We can see the below line in the java class.

@javax.xml.bind.annotation.XmlSchema(namespace = "http://abc.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)

We can change the line to below lines for setting the static namespace name.

@javax.xml.bind.annotation.XmlSchema(namespace = "http:// abc.com ", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,xmlns = {
@XmlNs(prefix = "customname", namespaceURI = "http:// abc.com ")})

This will change the response body tag with “customname” namespace.
Also we need to follow the below steps to change the parent nodes namespace.
Please add the below files the directory where you are creating the wrapper webservice.

hander-chain.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
   <handler-chain>
      <handler>
         <handler-name>SOAPBodyHandler</handler-name>
         <handler-class>com.abc.SOAPBodyHandler</handler-class>
      </handler>
   </handler-chain>
</handler-chains>

SOAPBodyHandler.java

package com.abc;

import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;


public class SOAPBodyHandler implements SOAPHandler<SOAPMessageContext>
{
    static final String DESIRED_NS_PREFIX = "custname";
    static final String DESIRED_NS_URI = "http:// com.abc /";
    static final String UNWANTED_NS_PREFIX = "ns3";
   
    public SOAPBodyHandler() {
        super();
    }
    @Override
    public Set<QName> getHeaders() {
        return null;
    }
    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        if ((Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) { //Check here that the message being intercepted is an outbound message from your service, otherwise ignore.
                try {   
                  SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
                    SOAPBody body = envelope.getBody();
                    Iterator iter = body.getChildElements();
                    while (iter.hasNext()) {
                        Object object = iter.next();
                        if (object instanceof SOAPElement) {
                            SOAPElement element = (SOAPElement) object;
                            element.removeNamespaceDeclaration(element.getPrefix());
                            element.setPrefix("custname");
                        }
                    }
                   
                } catch (SOAPException ex) {
                    Logger.getLogger(SOAPBodyHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            return true; //indicates to the context to proceed with (normal)message processing
    }
    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }
    @Override
    public void close(javax.xml.ws.handler.MessageContext context) {
    }
}

Please change the package names accordingly based on your project package.

Please add the below lines in your webservice project class’s annotation.

@HandlerChain(name = "SOAPBodyHandler",file="handler-chain.xml")

Example:-

import javax.jws.HandlerChain;

@HandlerChain(name = "SOAPBodyHandler",file="handler-chain.xml")
Public class MyClass
{


Deploy the application and execute the webservices and you can see the changes. This is mainly happening when we are creating SOAP services for mobile  (Titanium Applications) and Android device, need the complete namespace of response tags.

No comments:

Post a Comment