Java Client for web2py SOAP web service

103 views
Skip to first unread message

Pengfei Yu

unread,
Nov 20, 2014, 9:49:18 AM11/20/14
to web...@googlegroups.com
Hi,

I am using the SOAP service provided by web2py to create a simple web service for my web application. The document provide a example of using "pysimplesoap" to consume the created SOAP web service. The code is like below:

>>> from gluon.contrib.pysimplesoap.client import SoapClient
>>> client = SoapClient(wsdl="http://localhost:8000/app/default/call/soap?WSDL")
>>> print client.MyAdd(a=1,b=2)
{'result': 3}
My question is for this simple web service, can we use other languages like JAVA to consume it in the client side. Does the simple web service allow JAVA clients like JAX-WS or JAVA Axis to access it?
My cooperator want me to create a web service for my application and they are using JAVA.

Thanks!

Dave S

unread,
Nov 21, 2014, 6:16:45 PM11/21/14
to web...@googlegroups.com

I have a Java client  talking to my Web2Py service.

My imports include "javax.xml.soap.xml", and i instantiate using SOAPConnectionFactory.

I also have a Python client that talks to the same service using suds rather than pysimplesoap, so I would expect any typical client library to work.  Mariano seems to have a very good library for us to leverage.

/dps


Dave S

unread,
Nov 21, 2014, 6:25:02 PM11/21/14
to web...@googlegroups.com


On Friday, November 21, 2014 3:16:45 PM UTC-8, Dave S wrote:
[...]
My imports include "javax.xml.soap.xml", and i instantiate using SOAPConnectionFactory.


Boy, I bungled that line, even when proof-reading.

"import javax.xml.soap.*;"

(which might also need
"import javax.xml.transform.*;"
"import javax.xml.transform.stream.*;"
)
 
I also have a Python client that talks to the same service using suds rather than pysimplesoap, so I would expect any typical client library to work.  Mariano seems to have a very good library for us to leverage.


/dps, "re-do"
 

Pengfei Yu

unread,
Nov 24, 2014, 3:24:25 PM11/24/14
to web...@googlegroups.com
Hi Dave, 

Thanks very much for your reply! Could you give a more detailed example for an implementation for Java client? A block of java codes from your Java client talking to web2py service will be perfect. Even with the most simple example from the web2py's SOAP service document is good enough.

Thanks!

Pengfei Yu

unread,
Nov 25, 2014, 11:52:20 AM11/25/14
to web...@googlegroups.com
I can make the client work if using python suds and pysimplesoap. But when I use JAVA-WS client, I always got the error:

javax.xml.ws.WebServiceException: Failed to access the WSDL at: https://aa.bb.cc.dd/XXXX/default/call/soap?WSDL. It failed with: 
Got java.security.cert.CertificateException: No subject alternative names present while opening stream from https://aa.bb.cc.dd/XXXX/default/call/soap?WSDL.

Here aa.bb.cc.dd is a public ip address. The web2py application is host on amazon EC2.

Thanks!

Dave S

unread,
Nov 25, 2014, 2:26:47 PM11/25/14
to web...@googlegroups.com


On Monday, November 24, 2014 12:24:25 PM UTC-8, Pengfei Yu wrote:
Hi Dave, 

Thanks very much for your reply! Could you give a more detailed example for an implementation for Java client? A block of java codes from your Java client talking to web2py service will be perfect. Even with the most simple example from the web2py's SOAP service document is good enough.


The usage looks something like this:
    try {
       
String url = "http://" + ServerIP + "/" +
                   
AppID + "/default/call/soap";
       
// Create SOAP Connection
       
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
       
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

       
// Send SOAP Message to SOAP Server
       
SOAPMessage soapResponse = soapConnection.call(
        createSOAPRequest
(tdName, tdiskStatus),
        url
);

       
// Process the SOAP Response
        printSOAPResponse
(soapResponse);

        soapConnection
.close();
   
} catch (SOAPException ex) {
       
System.out.println("main: soap " + ex.getMessage());
       
Logger.getLogger(LogTDSClient.class.getName()).log(Level.SEVERE, null, ex);
   
} catch (UnsupportedOperationException ex) {  
       
System.out.println("main: uns" + ex.getMessage());
       
Logger.getLogger(LogTDSClient.class.getName()).log(Level.SEVERE, null, ex);
   
} catch (Exception ex) {
       
System.out.println("main: oops  " + ex.getMessage());
       
Logger.getLogger(LogTDSClient.class.getName()).log(Level.SEVERE, null, ex);
   
}


 and the interesting part:

    private static SOAPMessage createSOAPRequest(String tName, String tStatus) throws Exception {
       
MessageFactory messageFactory = MessageFactory.newInstance();
       
SOAPMessage soapMessage = messageFactory.createMessage();
       
SOAPPart soapPart = soapMessage.getSOAPPart();

       
String serverURI = "http://your-url:here";
       
String authHack = "ZGxv-usual-base64stuff";

       
       
// SOAP Envelope
       
SOAPEnvelope envelope = soapPart.getEnvelope();
       
// your-url:here corresponds to serverIP above    
        envelope
.addNamespaceDeclaration("ns0", "http://your-url:here/AppID/default/call/soap");
       


       
// SOAP Body
       
SOAPBody soapBody = envelope.getBody();
       
SOAPElement soapBodyElem = soapBody.addChildElement("YourSoapFunc", "ns0");
       
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("YourSoapParam");
        soapBodyElem1
.addTextNode("YourSoapParamString");
       
       
       
MimeHeaders headers = soapMessage.getMimeHeaders();
        headers
.addHeader("SOAPACTION", "");
        headers
.addHeader("Authorization", "Basic " + authHack);
       
Iterator outHeaders = headers.getAllHeaders();
       
while (false & outHeaders.hasNext()) {
       
MimeHeader outH = (MimeHeader) outHeaders.next();
       
// System.out.println(outH.getName() + " -- " +outH.getValue());
       
}

        soapMessage
.saveChanges();

       
/* Print the request message */
       
if (Boolean.FALSE) {
           
System.out.print("Request SOAP Message = ");
            soapMessage
.writeTo(System.out);
           
System.out.println();
       
}

       
return soapMessage;
   
}


It's pretty simple.  You might need to tweak the return value handling, because that part wasn't very important to my usage.

(Grrr -- the GG message editor gets funky when I put in a code block, at least in this browser.)


Good luck.

/dps




Dave S

unread,
Nov 25, 2014, 2:32:20 PM11/25/14
to web...@googlegroups.com


On Tuesday, November 25, 2014 11:26:47 AM UTC-8, Dave S wrote stuff.

Obviously I didn't get everything generic-ized.  The logger calls reference the class holding the main() function.

/dps



Pengfei Yu

unread,
Dec 2, 2014, 9:45:04 AM12/2/14
to web...@googlegroups.com
Cool! Thanks very much for sharing, Dave!

Dave S

unread,
Dec 2, 2014, 1:21:58 PM12/2/14
to web...@googlegroups.com


On Tuesday, December 2, 2014 6:45:04 AM UTC-8, Pengfei Yu wrote:
Cool! Thanks very much for sharing, Dave!

My pay-forward.  Glad it helped.

/dps
 
Reply all
Reply to author
Forward
0 new messages