axis-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Barry <pauljbar...@gmail.com>
Subject Re: change soapenv / get rid of ns1,ns2,...
Date Sat, 25 Jun 2005 19:06:40 GMT
I can get what I need building the SOAP XML message with JDOM first
and then wrapping it in a SOAPEnvelope object like this:

    import java.io.ByteArrayInputStream;

    import org.apache.axis.message.SOAPEnvelope;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.Namespace;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;

    public class Test {

        public static void main(String[] args) throws Exception {
            new Test();
        }
       
        public Test() throws Exception {
            Document doc = new Document();
            
            Element env = new
Element("Envelope","soap","http://schemas.xmlsoap.org/soap/envelope/");
            env.addNamespaceDeclaration(Namespace.getNamespace("xsi","http://www.w3.org/2001/XMLSchema-instance"));
            env.addNamespaceDeclaration(Namespace.getNamespace("xsd","http://www.w3.org/2001/XMLSchema"));
            doc.setRootElement(env);
            
            Element header = new Element("Header",env.getNamespace());
            env.addContent(header);
            
            Element cmsAuthentication = new
Element("CMSAuthentication","http://www.centra.com/CWS");
            header.addContent(cmsAuthentication);
            
            Element username = new
Element("username",cmsAuthentication.getNamespace());
            username.setText("admin");
            cmsAuthentication.addContent(username);
            
            Element password = new
Element("password",cmsAuthentication.getNamespace());
            password.setText("admin");
            cmsAuthentication.addContent(password);        
            
            Element domain = new
Element("Domain",cmsAuthentication.getNamespace());
            cmsAuthentication.addContent(domain);        
            
            Element body = new Element("Body",env.getNamespace());
            env.addContent(body);
            
            Element findUser = new
Element("FindUser","http://www.centra.com/CWS");
            body.addContent(findUser);        
            
            Element user = new Element("user",findUser.getNamespace());
            user.setAttribute("version","3.0");
            user.setAttribute("loginName","pbarry");
            findUser.addContent(user);
            
            XMLOutputter xmlOut = new XMLOutputter();
            xmlOut.setFormat(Format.getPrettyFormat());
            SOAPEnvelope soapEnv = new SOAPEnvelope(new
ByteArrayInputStream(xmlOut.outputString(doc).getBytes()));
            System.out.println(soapEnv);
            
        }
    }

Is there an easier way to do this?

On 6/25/05, Paul Barry <pauljbarry3@gmail.com> wrote:
> I am trying to use the axis classes to construct a SOAP message to
> build a web services client for a 3rd party app.  I need to construct
> a message like this:
> 
> <?xml version="1.0" encoding="utf-8"?>
> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
>     <soap:Header>
>         <CMSAuthentication xmlns="http://www.centra.com/CWS">
>             <username>admin</username>
>             <password>admin</password>
>             <Domain/>
>         </CMSAuthentication>
>     </soap:Header>
>     <soap:Body>
>         <FindUser xmlns="http://www.centra.com/CWS">
>             <user version="3.0" loginName="test"/>
>         </FindUser>
>     </soap:Body>
> </soap:Envelope>
> 
> Here is my code:
> 
>         SOAPEnvelope env = new SOAPEnvelope();
> 
>         SOAPHeaderElement header =
>             new SOAPHeaderElement(new
> QName("http://www.centra.com/CWS", "CMSAuthentication",""));
> 
>         DocumentBuilder builder = factory.newDocumentBuilder();
>         Document doc = builder.newDocument();
>         Element username = doc.createElement("username");
>         username.appendChild(doc.createTextNode("admin"));
> 
>         header.addChildElement(new SOAPHeaderElement(username));
> 
>         doc = builder.newDocument();
>         Element password = doc.createElement("password");
>         password.appendChild(doc.createTextNode("admin"));
>         header.addChildElement(new SOAPHeaderElement(password));
> 
>         doc = builder.newDocument();
>         Element domain = doc.createElement("Domain");
>         header.addChildElement(new SOAPHeaderElement(domain));
> 
>         env.addHeader(header);
> 
>         SOAPBodyElement body = new SOAPBodyElement(
>             new QName("http://www.centra.com/CWS","FindUser",""));
> 
>         doc = builder.newDocument();
>         Element user = doc.createElement("user");
>         user.setAttribute("version","3.0");
>         user.setAttribute("loginName","test");
>         body.addChildElement(new SOAPBodyElement(user));
> 
>         env.addBodyElement(body);
> 
> This unfortunately produces:
> 
> <soapenv:Envelope
> xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>  <soapenv:Header>
>   <ns1:CMSAuthentication
> soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next"
> soapenv:mustUnderstand="0" xmlns:ns1="http://www.centra.com/CWS">
>    <username soapenv:actor="" soapenv:mustUnderstand="0">admin</username>
>    <password soapenv:actor="" soapenv:mustUnderstand="0">admin</password>
>    <Domain soapenv:actor="" soapenv:mustUnderstand="0"/>
>   </ns1:CMSAuthentication>
>  </soapenv:Header>
>  <soapenv:Body>
>   <ns2:FindUser xmlns:ns2="http://www.centra.com/CWS">
>    <user loginName="test" version="3.0"/>
>   </ns2:FindUser>
>  </soapenv:Body>
> </soapenv:Envelope>
> 
> This doesn't work with the 3rd party app.  The two reasons are:
> 
> 1.  The prefix is "soapenv", but the 3rd party app expects it to be "soap".
> 
> 2.  For each element in the header and body there is a prefix, they
> are "ns1" and "ns2".  The 3rd party app expects there to be no prefix.
> 
> I'm sure the Axis-produces SOAP message is compliant and validate,
> unfortunately the 3rd party app doesn't like it and I need to find a
> way to work around it.  For #2, I would think passing in a QName with
> the prefix defined as "" should result in no prefix for those
> elements.  Is there a way I can make this happen?  Also, is there any
> way, other than hacking SOAPConstants, to get it to user "soap"
> instead of "soapenv"?  If I can get Axis to output a SOAP message like
> this following one, then it works fine with the 3rd party app:
> 
> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
>  <soap:Header>
>   <CMSAuthentication
> soap:actor="http://schemas.xmlsoap.org/soap/actor/next"
> soap:mustUnderstand="0" xmlns="http://www.centra.com/CWS">
>    <username soap:actor="" soap:mustUnderstand="0">admin</username>
>    <password soap:actor="" soap:mustUnderstand="0">admin</password>
>    <Domain soap:actor="" soap:mustUnderstand="0"/>
>   </CMSAuthentication>
>  </soap:Header>
>  <soap:Body>
>   <FindUser xmlns="http://www.centra.com/CWS">
>    <user loginName="test" version="3.0"/>
>   </FindUser>
>  </soap:Body>
> </soap:Envelope>
> 
> Thanks in advance,
> 
> Paul
>

Mime
View raw message