Some SOAP Client progress

This commit is contained in:
Ziver Koc 2015-12-19 13:32:34 +01:00
parent 973dfdec35
commit 3508b7fdaf
10 changed files with 120 additions and 44 deletions

View file

@ -32,6 +32,7 @@ import zutil.net.ws.WebServiceDef;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -47,24 +48,28 @@ public class SOAPClientFactory {
* Generates a Client Object for the web service.
*
* @param <T> is the class of the web service definition
* @param url is the target service url
* @param intf is the class of the web service definition
* @return a client Object
*/
@SuppressWarnings("unchecked")
public static <T> T createClient(Class<T> intf){
return createClient( intf, new WebServiceDef((Class<? extends WSInterface>)intf) );
public static <T> T createClient(URL url, Class<T> intf){
return createClient(url, intf,
new WebServiceDef((Class<? extends WSInterface>)intf) );
}
/**
* Generates a Client Object for the web service.
*
* @param <T> is the class of the web service definition
* @param url is the target service url
* @param intf is the class of the web service definition
* @param wsDef is the web service definition of the intf parameter
* @return a client Object
*/
public static <T> T createClient(Class<T> intf, WebServiceDef wsDef){
T obj = WSClientFactory.createClient(intf, new SOAPClientInvocationHandler());
public static <T> T createClient(URL url, Class<T> intf, WebServiceDef wsDef){
T obj = WSClientFactory.createClient( intf,
new SOAPClientInvocationHandler(url, wsDef));
return obj;
}

View file

@ -28,7 +28,7 @@ import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.SAXException;
import zutil.io.IOUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpClient;
import zutil.net.http.HttpHeaderParser;
@ -39,6 +39,7 @@ import zutil.net.ws.WebServiceDef;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -53,22 +54,46 @@ public class SOAPClientInvocationHandler implements InvocationHandler {
private WebServiceDef wsDef;
/** Web address of the web service */
protected String url;
protected URL url;
public SOAPClientInvocationHandler(URL url, WebServiceDef wsDef){
this.url = url;
this.wsDef = wsDef;
}
/**
* Makes a request to the target web service
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
HttpClient request = HttpClient.POST();
// Generate XML
Document document = genSOAPRequest((WSInterface)proxy, method.getName(), args);
String reqXml = document.asXML();
// Send request
HttpClient request = new HttpClient(HttpClient.HttpRequestType.POST);
request.setURL(url);
request.setData(reqXml);
HttpHeaderParser response = request.send();
String rspXml = IOUtil.getContentAsString( request.getResponseReader());
return null;
// DEBUG
if( logger.isLoggable(Level.FINEST) ){
System.out.println("********** Request");
System.out.println(reqXml);
System.out.println("********** Response");
System.out.println(rspXml);
}
return parseSOAPResponse(rspXml);
}
private Document genSOAPRequest(WSInterface obj, String targetMethod, Object[] args){
logger.fine("Sending request for "+targetMethod);
Document document = DocumentHelper.createDocument();
Element envelope = document.addElement("soap:Envelope");
WSMethodDef methodDef = wsDef.getMethod( targetMethod );

View file

@ -139,18 +139,17 @@ public class SOAPHttpPage implements HttpPage{
Document document = genSOAPResponse( request.get(""), obj);
OutputFormat format = OutputFormat.createCompactFormat();
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( out, format );
writer.write( document );
// DEBUG
if( logger.isLoggable(Level.FINEST) ){
OutputFormat format2 = OutputFormat.createPrettyPrint();
System.err.println("********** Request");
System.err.println(request);
System.out.println("********** Request");
System.out.println(request);
System.out.println("********** Response");
writer = new XMLWriter( System.out, format2 );
writer = new XMLWriter( System.out, format );
writer.write( document );
}
} catch (Exception e) {