Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good Java library for dynamic SOAP client operations?

I've been searching for SOAP client libraries for Java and have found a plethora of libraries based on the idea of building stub and proxy classes based on a WSDL. I'm interested in allowing the user to enter a WSDL at runtime, parsing the WSDL, then allowing the user to perform operations on the Web Service.

Does anyone know of a good SOAP client library which will allow this runtime use? Or is there a way I can use the axis2 wsdl2java functionality to build stubs into the classloader and use them at runtime?

like image 754
rharter Avatar asked Jul 27 '11 19:07

rharter


1 Answers

Later than never. :)

You should achieve that in two steps:

  • 1) parse the WSDL informed by the user to retrieve the available operations. Refer to this question to know how to do this in a simple way.

  • 2) Create a dynamic client to send a request using the selected operations. It can be done by using the Dispatch API from Apache CXF.

Build the Dispatch object for the dynamic client (It can be created on the fly by informing web service endpoint, port name, etc):

package com.mycompany.demo;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

public class Client {
  public static void main(String args[]) {
    QName serviceName = new QName("http://org.apache.cxf", "stockQuoteReporter");
    Service s = Service.create(serviceName);

    QName portName = new QName("http://org.apache.cxf", "stockQuoteReporterPort");
    Dispatch<DOMSource> dispatch = s.createDispatch(portName,
                                                  DOMSource.class,
                                                  Service.Mode.PAYLOAD);
    ...
  }
}

Construct the request message (In the example below we are using DOMSource):

// Creating a DOMSource Object for the request
DocumentBuilder db = DocumentBuilderFactory.newDocumentBuilder();
Document requestDoc = db.newDocument();
Element root = requestDoc.createElementNS("http://org.apache.cxf/stockExample", "getStockPrice");
root.setNodeValue("DOW");
DOMSource request = new DOMSource(requestDoc);

Invoke the web service

// Dispatch disp created previously
DOMSource response = dispatch.invoke(request);

Recommendations:

  • Use ((BindingProvider)dispatch).getRequestContext().put("thread.local.request.context", "true"); if you want to make the Dispatch object thread safe.
  • Cache the Dispatch object for using later, if it is the case. The process o building the object is not for free.

Other Methods

There are other methods for creating dynamic clients, like using CXF dynamic-clients API. You can read in project's index page:

CXF supports several alternatives to allow an application to communicate with a service without the SEI and data classes

I haven't tried that myself but should worth giving it a try.

like image 144
Ben-Hur Langoni Junior Avatar answered Sep 28 '22 00:09

Ben-Hur Langoni Junior