Moved WSDL under WS and some fixes for REST service

This commit is contained in:
Ziver Koc 2021-02-27 00:01:02 +01:00
parent bbcb62b913
commit 77e4bce99b
10 changed files with 730 additions and 630 deletions

View file

@ -45,18 +45,15 @@ public class WSClientFactory {
* *
* @param <T> is the class of the web service definition * @param <T> is the class of the web service definition
* @param intf is the class of the web service definition * @param intf is the class of the web service definition
* @param handler is the handler that will execute the calls to the web service
* @return a client Object * @return a client Object
*/ */
public static <T> T createClient(Class<T> intf, InvocationHandler handler){ public static <T extends WSInterface> T createClient(Class<T> intf, InvocationHandler handler){
if( !WSInterface.class.isAssignableFrom( intf )){
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
}
try { try {
Class proxyClass = Proxy.getProxyClass( T obj = (T) Proxy.newProxyInstance(
WSClientFactory.class.getClassLoader(), intf); WSClientFactory.class.getClassLoader(),
Constructor<T> constructor = proxyClass.getConstructor(InvocationHandler.class); new Class[] { intf },
T obj = constructor.newInstance(handler); handler);
return obj; return obj;
} catch (Exception e){ } catch (Exception e){

View file

@ -41,7 +41,7 @@ import java.lang.annotation.Target;
* public Test(){} * public Test(){}
* *
* &#64;WSDocumentation("This is a description of the method") * &#64;WSDocumentation("This is a description of the method")
* &#64;WSDLParamDocumentation("arg1 = variable description?") * &#64;WSParamDocumentation("arg1 = variable description?")
* public void pubZ( * public void pubZ(
* &#64;WSParamName("arg1") int randomName) * &#64;WSParamName("arg1") int randomName)
* throws Exception{ * throws Exception{
@ -100,14 +100,14 @@ public interface WSInterface {
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@interface WSIgnore { } @interface WSIgnore {}
/** /**
* Method or Parameter comments for the WSDL. * Method or Parameter comments for the WSDL.
* These comments are put in the message part of the WSDL * These comments are put in the message part of the WSDL
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface WSDocumentation{ @interface WSDocumentation {
String value(); String value();
} }
@ -116,7 +116,7 @@ public interface WSInterface {
* These comments are put in the message part of the WSDL * These comments are put in the message part of the WSDL
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface WSParamDocumentation{ @interface WSParamDocumentation {
String value(); String value();
} }
@ -125,7 +125,7 @@ public interface WSInterface {
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@interface WSHeader { } @interface WSHeader {}
/** /**
* Specifies the name space for the method. * Specifies the name space for the method.
@ -145,7 +145,7 @@ public interface WSInterface {
} }
/** /**
* Specifies the specific path for the method overriding the auto generated path. * Sets a specific path for the method overriding the auto generated path.
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) @Target(ElementType.METHOD)

View file

@ -26,6 +26,8 @@ package zutil.net.ws;
import zutil.net.ws.WSInterface.WSDocumentation; import zutil.net.ws.WSInterface.WSDocumentation;
import zutil.net.ws.WSInterface.WSNamespace; import zutil.net.ws.WSInterface.WSNamespace;
import zutil.net.ws.WSInterface.WSPath;
import zutil.net.ws.WSInterface.WSRequestType;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -41,141 +43,215 @@ import java.util.List;
*/ */
// TODO: Header parameters // TODO: Header parameters
public class WSMethodDef { public class WSMethodDef {
/** The parent web service definition **/ /**
* The parent web service definition
**/
private WebServiceDef wsDef; private WebServiceDef wsDef;
/** A list of input parameters **/ /**
* A list of input parameters
**/
private ArrayList<WSParameterDef> inputs; private ArrayList<WSParameterDef> inputs;
/** A List of return parameters of the method **/ /**
* A List of return parameters of the method
**/
private ArrayList<WSParameterDef> outputs; private ArrayList<WSParameterDef> outputs;
/** A List of exceptions that this method throws **/ /**
* A List of exceptions that this method throws
**/
private ArrayList<Class<?>> exceptions; private ArrayList<Class<?>> exceptions;
/** The real method that this class represent, can be null if its a remote method **/ /**
* The real method that this class represent, can be null if its a remote method
**/
private Method method; private Method method;
/** Documentation of the method **/ /**
* Documentation of the method
**/
private String doc; private String doc;
/** This is the namespace of the method **/ /**
* The namespace of the method
**/
private String namespace; private String namespace;
/** The published name of the method **/ /**
* The type of request required to execute the method
**/
private WSInterface.RequestType requestType;
/**
* The published name of the method
**/
private String name; private String name;
/**
* The endpoint location
**/
private String path;
/** /**
* * @param wsDef is the parent web service defining interface
* @param me is a method in a class that implements WSInterface * @param me is a method in a class that implements WSInterface
*/ */
protected WSMethodDef( WebServiceDef wsDef, Method me) { protected WSMethodDef(WebServiceDef wsDef, Method me) {
if (!WSInterface.class.isAssignableFrom(me.getDeclaringClass())) if (!WSInterface.class.isAssignableFrom(me.getDeclaringClass()))
throw new ClassCastException("Declaring class does not implement WSInterface!"); throw new ClassCastException("Declaring class does not implement WSInterface!");
this.wsDef = wsDef; this.wsDef = wsDef;
method = me; this.method = me;
inputs = new ArrayList<>(); this.inputs = new ArrayList<>();
outputs = new ArrayList<>(); this.outputs = new ArrayList<>();
exceptions = new ArrayList<>(); this.exceptions = new ArrayList<>();
name = method.getName(); this.name = method.getName();
//***** Documentation & Namespace // Handle documentation and namespace
WSDocumentation tmpDoc = method.getAnnotation(WSDocumentation.class);
if (tmpDoc != null){ WSDocumentation docAnnotation = method.getAnnotation(WSDocumentation.class);
doc = tmpDoc.value(); if (docAnnotation != null)
} doc = docAnnotation.value();
WSNamespace tmpSpace = method.getAnnotation(WSNamespace.class);
if ( tmpSpace != null ) WSNamespace namespaceAnnotation = method.getAnnotation(WSNamespace.class);
namespace = tmpSpace.value(); if (namespaceAnnotation != null)
namespace = namespaceAnnotation.value();
else else
namespace = wsDef.getNamespace()+"?#"+name; namespace = wsDef.getNamespace() + "?#" + name;
// Hnadle Exceptions
//***** Exceptions
Collections.addAll(exceptions, method.getExceptionTypes()); Collections.addAll(exceptions, method.getExceptionTypes());
//********* Get the input parameter names ********** // Handle input parameter names
Annotation[][] paramAnnotation = method.getParameterAnnotations(); Annotation[][] paramAnnotation = method.getParameterAnnotations();
Class<?>[] inputTypes = method.getParameterTypes(); Class<?>[] inputTypes = method.getParameterTypes();
for (int i=0; i<paramAnnotation.length ;i++){ for (int i = 0; i < paramAnnotation.length; i++) {
WSParameterDef param = new WSParameterDef( this ); WSParameterDef param = new WSParameterDef(this);
for (Annotation annotation : paramAnnotation[i]){ for (Annotation annotation : paramAnnotation[i]) {
if (annotation instanceof WSInterface.WSParamName){ if (annotation instanceof WSInterface.WSParamName) {
WSInterface.WSParamName paramName = (WSInterface.WSParamName) annotation; WSInterface.WSParamName paramName = (WSInterface.WSParamName) annotation;
param.setName( paramName.value() ); param.setName(paramName.value());
param.setOptional( paramName.optional() ); param.setOptional(paramName.optional());
} }
} }
param.setParamClass( inputTypes[i] ); param.setParamClass(inputTypes[i]);
// if no name was found then use default // if no name was found then use default
if (param.getName() == null) if (param.getName() == null)
param.setName( "args"+i ); param.setName("args" + i);
inputs.add( param ); inputs.add(param);
} }
//******** The return parameter name ************ // Handle return parameter names
WSInterface.WSReturnName returnName = method.getAnnotation(WSInterface.WSReturnName.class);
if (WSReturnObject.class.isAssignableFrom( method.getReturnType())){ WSInterface.WSReturnName returnNameAnnotation = method.getAnnotation(WSInterface.WSReturnName.class);
if (WSReturnObject.class.isAssignableFrom(method.getReturnType())) {
Class<?> retClass = method.getReturnType(); Class<?> retClass = method.getReturnType();
Field[] fields = retClass.getFields(); Field[] fields = retClass.getFields();
for (int i=0; i<fields.length ;i++){ for (Field field : fields) {
WSParameterDef ret_param = new WSParameterDef( this ); WSParameterDef ret_param = new WSParameterDef(this);
WSReturnObject.WSValueName retValName = fields[i] WSReturnObject.WSValueName retValName = field.getAnnotation(WSReturnObject.WSValueName.class);
.getAnnotation( WSReturnObject.WSValueName.class );
if(retValName != null) if (retValName != null)
ret_param.setName( retValName.value() ); ret_param.setName(retValName.value());
else else
ret_param.setName( fields[i].getName() ); ret_param.setName(field.getName());
ret_param.setParamClass( fields[i].getType() );
outputs.add( ret_param ); ret_param.setParamClass(field.getType());
outputs.add(ret_param);
} }
} } else if (method.getReturnType() != void.class) {
else if( method.getReturnType() != void.class ){ WSParameterDef ret_param = new WSParameterDef(this);
WSParameterDef ret_param = new WSParameterDef( this );
if (returnName != null) if (returnNameAnnotation != null)
ret_param.setName(returnName.value()); ret_param.setName(returnNameAnnotation.value());
else else
ret_param.setName("return"); ret_param.setName("return");
ret_param.setParamClass( method.getReturnType() );
outputs.add( ret_param ); ret_param.setParamClass(method.getReturnType());
outputs.add(ret_param);
} }
// Handle the request type
WSRequestType requestTypeAnnotation = method.getAnnotation(WSRequestType.class);
if (requestTypeAnnotation != null) {
this.requestType = requestTypeAnnotation.value();
} else {
// Specific request type was not provided, try to figure it out by the method name
if (name.startsWith("get"))
this.requestType = WSInterface.RequestType.HTTP_GET;
if (name.startsWith("post"))
this.requestType = WSInterface.RequestType.HTTP_POST;
if (name.startsWith("put"))
this.requestType = WSInterface.RequestType.HTTP_PUT;
if (name.startsWith("delete"))
this.requestType = WSInterface.RequestType.HTTP_DELETE;
}
// Handle endpoint path
WSPath pathAnnotation = method.getAnnotation(WSPath.class);
if (pathAnnotation != null)
path = pathAnnotation.value();
else
path = this.name;
if (path.startsWith("/"))
path = path.substring(1);
} }
/** /**
* @return the published name of the method * @return the published name of the method
*/ */
public String getName(){ public String getName() {
return name; return name;
} }
/**
* @return the path to the WS method endpoint
*/
public String getPath() {
return path;
}
/** /**
* @return a list of exceptions this method throws * @return a list of exceptions this method throws
*/ */
public List<Class<?>> getExceptions(){ public List<Class<?>> getExceptions() {
return exceptions; return exceptions;
} }
/** /**
* @return a list of input parameters * @return a list of input parameters
*/ */
public List<WSParameterDef> getInputs(){ public List<WSParameterDef> getInputs() {
return inputs; return inputs;
} }
/** /**
* @return a list of input parameters * @return a list of input parameters
*/ */
public List<WSParameterDef> getOutputs(){ public List<WSParameterDef> getOutputs() {
return outputs; return outputs;
} }
/** /**
* @return Documentation of the method if one exists or else null * @return documentation of the method if one exists or else null
*/ */
public String getDocumentation(){ public String getDocumentation() {
return doc; return doc;
} }
/**
* @return the type of request needed to execute this method
*/
public WSInterface.RequestType getRequestType() {
return requestType;
}
/** /**
* @return the namespace or endpoint url of the method * @return the namespace or endpoint url of the method
*/ */
public String getNamespace(){ public String getNamespace() {
return namespace; return namespace;
} }
@ -183,19 +259,21 @@ public class WSMethodDef {
/** /**
* Invokes a specified method * Invokes a specified method
* *
* @param obj the object the method will called on
* @param params a vector with arguments * @param params a vector with arguments
* @param obj the object the method will called on
*/ */
public Object invoke(Object obj, Object[] params) throws Exception { public Object invoke(Object obj, Object[] params) throws Exception {
return this.method.invoke(obj, params ); return this.method.invoke(obj, params);
} }
public String toString(){ public String toString() {
StringBuilder tmp = new StringBuilder(); StringBuilder tmp = new StringBuilder();
boolean first = true; boolean first = true;
tmp.append(name).append("("); tmp.append(name).append("(");
for (WSParameterDef param : inputs){
for (WSParameterDef param : inputs) {
if (first) if (first)
first = false; first = false;
else else
@ -205,8 +283,9 @@ public class WSMethodDef {
tmp.append(param.getName()); tmp.append(param.getName());
} }
tmp.append(") => "); tmp.append(") => ");
first = true; first = true;
for (WSParameterDef param : outputs){ for (WSParameterDef param : outputs) {
if (first) if (first)
first = false; first = false;
else else

View file

@ -51,7 +51,7 @@ public class RESTClientFactory {
* @return a client Object * @return a client Object
*/ */
public static <T extends WSInterface> T createClient(URL url, Class<T> intf){ public static <T extends WSInterface> T createClient(URL url, Class<T> intf){
T obj = WSClientFactory.createClient( intf, T obj = WSClientFactory.createClient(intf,
new RESTClientInvocationHandler(url, new WebServiceDef(intf))); new RESTClientInvocationHandler(url, new WebServiceDef(intf)));
return obj; return obj;
} }

View file

@ -35,7 +35,10 @@ import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSParameterDef; import zutil.net.ws.WSParameterDef;
import zutil.net.ws.WebServiceDef; import zutil.net.ws.WebServiceDef;
import zutil.net.ws.soap.SOAPHttpPage; import zutil.net.ws.soap.SOAPHttpPage;
import zutil.parser.DataNode;
import zutil.parser.json.JSONParser;
import javax.naming.OperationNotSupportedException;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -70,38 +73,52 @@ public class RESTClientInvocationHandler implements InvocationHandler {
*/ */
@Override @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Generate XML // Generate request
HttpURL url = generateRESTRequest(method.getName(), args);
WSMethodDef methodDef = wsDef.getMethod(method.getName());
HttpURL url = generateRESTRequest(methodDef, args);
String requestType = "GET";
switch (methodDef.getRequestType()) {
case HTTP_GET: requestType = "GET"; break;
case HTTP_PUT: requestType = "PUT"; break;
case HTTP_POST: requestType = "POST"; break;
case HTTP_DELETE: requestType = "DELETE"; break;
}
// Send request // Send request
HttpClient request = new HttpClient(HttpClient.HttpRequestType.POST); HttpClient request = new HttpClient(HttpClient.HttpRequestType.POST);
request.setURL(url); request.setURL(url);
request.setType(requestType);
logger.fine("Sending request for: " + url);
HttpHeader response = request.send(); HttpHeader response = request.send();
String rspJson = IOUtil.readContentAsString(request.getResponseInputStream()); logger.fine("Received response(" + response.getResponseStatusCode() + ")");
// Parse response
String rspStr = IOUtil.readContentAsString(request.getResponseInputStream());
request.close(); request.close();
// DEBUG //if (logger.isLoggable(Level.FINEST)) {
if (logger.isLoggable(Level.FINEST)) { System.out.println("********** Response: " + url);
System.out.println("********** Request"); System.out.println(rspStr);
System.out.println(url); //}
System.out.println("********** Response");
System.out.println(rspJson);
}
return parseRESTResponse(rspJson); Object rspObj = parseRESTResponse(methodDef, rspStr);
return rspObj;
} }
private HttpURL generateRESTRequest(String targetMethod, Object[] args) { private HttpURL generateRESTRequest(WSMethodDef methodDef, Object[] args) {
logger.fine("Sending request for " + targetMethod);
HttpURL url = new HttpURL(serviceUrl); HttpURL url = new HttpURL(serviceUrl);
WSMethodDef methodDef = wsDef.getMethod(targetMethod);
url.setPath(serviceUrl.getPath() url.setPath(serviceUrl.getPath()
+ (serviceUrl.getPath().endsWith("/") ? "" : "/") + (serviceUrl.getPath().endsWith("/") ? "" : "/")
+ methodDef.getName()); + methodDef.getPath());
List<WSParameterDef> params = methodDef.getOutputs(); List<WSParameterDef> params = methodDef.getInputs();
for (int i = 0; i < params.size(); i++) { for (int i = 0; i < params.size(); i++) {
WSParameterDef param = params.get(i); WSParameterDef param = params.get(i);
url.setParameter(param.getName(), args[i].toString()); url.setParameter(param.getName(), args[i].toString());
@ -110,8 +127,15 @@ public class RESTClientInvocationHandler implements InvocationHandler {
return url; return url;
} }
private Object parseRESTResponse(String json) { private Object parseRESTResponse(WSMethodDef methodDef, String str) {
DataNode json = JSONParser.read(str);
List<WSParameterDef> outputs = methodDef.getOutputs();
return null; if (outputs.size() == 1) {
if (outputs.get(0).getParamClass().isAssignableFrom(DataNode.class))
return json;
}
throw new RuntimeException("WS JSON return type currently not supported: " + methodDef);
} }
} }

View file

@ -22,7 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.parser.wsdl; package zutil.net.ws.wsdl;
import zutil.net.http.HttpHeader; import zutil.net.http.HttpHeader;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;

View file

@ -22,7 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.parser.wsdl; package zutil.net.ws.wsdl;
import org.dom4j.Element; import org.dom4j.Element;
import zutil.net.ws.WSMethodDef; import zutil.net.ws.WSMethodDef;

View file

@ -22,7 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.parser.wsdl; package zutil.net.ws.wsdl;
import org.dom4j.Element; import org.dom4j.Element;
import zutil.net.ws.WSMethodDef; import zutil.net.ws.WSMethodDef;

View file

@ -22,7 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.parser.wsdl; package zutil.net.ws.wsdl;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;

View file

@ -31,7 +31,7 @@ import zutil.net.ws.WSInterface;
import zutil.net.ws.WSInterface.WSNamespace; import zutil.net.ws.WSInterface.WSNamespace;
import zutil.net.ws.WSReturnObject; import zutil.net.ws.WSReturnObject;
import zutil.net.ws.WebServiceDef; import zutil.net.ws.WebServiceDef;
import zutil.parser.wsdl.WSDLWriter; import zutil.net.ws.wsdl.WSDLWriter;
// TODO: Convert to JUnit // TODO: Convert to JUnit