Moved WSDL under WS and some fixes for REST service
This commit is contained in:
parent
bbcb62b913
commit
77e4bce99b
10 changed files with 730 additions and 630 deletions
|
|
@ -45,18 +45,15 @@ public class WSClientFactory {
|
|||
*
|
||||
* @param <T> 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
|
||||
*/
|
||||
public static <T> 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!");
|
||||
}
|
||||
|
||||
public static <T extends WSInterface> T createClient(Class<T> intf, InvocationHandler handler){
|
||||
try {
|
||||
Class proxyClass = Proxy.getProxyClass(
|
||||
WSClientFactory.class.getClassLoader(), intf);
|
||||
Constructor<T> constructor = proxyClass.getConstructor(InvocationHandler.class);
|
||||
T obj = constructor.newInstance(handler);
|
||||
T obj = (T) Proxy.newProxyInstance(
|
||||
WSClientFactory.class.getClassLoader(),
|
||||
new Class[] { intf },
|
||||
handler);
|
||||
|
||||
return obj;
|
||||
} catch (Exception e){
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ import java.lang.annotation.Target;
|
|||
* public Test(){}
|
||||
*
|
||||
* @WSDocumentation("This is a description of the method")
|
||||
* @WSDLParamDocumentation("arg1 = variable description?")
|
||||
* @WSParamDocumentation("arg1 = variable description?")
|
||||
* public void pubZ(
|
||||
* @WSParamName("arg1") int randomName)
|
||||
* throws Exception{
|
||||
|
|
@ -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)
|
||||
@Target(ElementType.METHOD)
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ package zutil.net.ws;
|
|||
|
||||
import zutil.net.ws.WSInterface.WSDocumentation;
|
||||
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.reflect.Field;
|
||||
|
|
@ -41,53 +43,81 @@ import java.util.List;
|
|||
*/
|
||||
// TODO: Header parameters
|
||||
public class WSMethodDef {
|
||||
/** The parent web service definition **/
|
||||
/**
|
||||
* The parent web service definition
|
||||
**/
|
||||
private WebServiceDef wsDef;
|
||||
/** A list of input parameters **/
|
||||
/**
|
||||
* A list of input parameters
|
||||
**/
|
||||
private ArrayList<WSParameterDef> inputs;
|
||||
/** A List of return parameters of the method **/
|
||||
/**
|
||||
* A List of return parameters of the method
|
||||
**/
|
||||
private ArrayList<WSParameterDef> outputs;
|
||||
/** A List of exceptions that this method throws **/
|
||||
/**
|
||||
* A List of exceptions that this method throws
|
||||
**/
|
||||
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;
|
||||
/** Documentation of the method **/
|
||||
/**
|
||||
* Documentation of the method
|
||||
**/
|
||||
private String doc;
|
||||
/** This is the namespace of the method **/
|
||||
/**
|
||||
* The namespace of the method
|
||||
**/
|
||||
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;
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected WSMethodDef(WebServiceDef wsDef, Method me) {
|
||||
if (!WSInterface.class.isAssignableFrom(me.getDeclaringClass()))
|
||||
throw new ClassCastException("Declaring class does not implement WSInterface!");
|
||||
this.wsDef = wsDef;
|
||||
method = me;
|
||||
inputs = new ArrayList<>();
|
||||
outputs = new ArrayList<>();
|
||||
exceptions = new ArrayList<>();
|
||||
name = method.getName();
|
||||
|
||||
//***** Documentation & Namespace
|
||||
WSDocumentation tmpDoc = method.getAnnotation(WSDocumentation.class);
|
||||
if (tmpDoc != null){
|
||||
doc = tmpDoc.value();
|
||||
}
|
||||
WSNamespace tmpSpace = method.getAnnotation(WSNamespace.class);
|
||||
if ( tmpSpace != null )
|
||||
namespace = tmpSpace.value();
|
||||
this.wsDef = wsDef;
|
||||
this.method = me;
|
||||
this.inputs = new ArrayList<>();
|
||||
this.outputs = new ArrayList<>();
|
||||
this.exceptions = new ArrayList<>();
|
||||
this.name = method.getName();
|
||||
|
||||
// Handle documentation and namespace
|
||||
|
||||
WSDocumentation docAnnotation = method.getAnnotation(WSDocumentation.class);
|
||||
if (docAnnotation != null)
|
||||
doc = docAnnotation.value();
|
||||
|
||||
WSNamespace namespaceAnnotation = method.getAnnotation(WSNamespace.class);
|
||||
if (namespaceAnnotation != null)
|
||||
namespace = namespaceAnnotation.value();
|
||||
else
|
||||
namespace = wsDef.getNamespace() + "?#" + name;
|
||||
|
||||
//***** Exceptions
|
||||
// Hnadle Exceptions
|
||||
|
||||
Collections.addAll(exceptions, method.getExceptionTypes());
|
||||
|
||||
//********* Get the input parameter names **********
|
||||
// Handle input parameter names
|
||||
|
||||
Annotation[][] paramAnnotation = method.getParameterAnnotations();
|
||||
Class<?>[] inputTypes = method.getParameterTypes();
|
||||
|
||||
|
|
@ -108,33 +138,65 @@ public class WSMethodDef {
|
|||
inputs.add(param);
|
||||
}
|
||||
|
||||
//******** The return parameter name ************
|
||||
WSInterface.WSReturnName returnName = method.getAnnotation(WSInterface.WSReturnName.class);
|
||||
// Handle return parameter names
|
||||
|
||||
WSInterface.WSReturnName returnNameAnnotation = method.getAnnotation(WSInterface.WSReturnName.class);
|
||||
if (WSReturnObject.class.isAssignableFrom(method.getReturnType())) {
|
||||
Class<?> retClass = method.getReturnType();
|
||||
Field[] fields = retClass.getFields();
|
||||
|
||||
for (int i=0; i<fields.length ;i++){
|
||||
for (Field field : fields) {
|
||||
WSParameterDef ret_param = new WSParameterDef(this);
|
||||
WSReturnObject.WSValueName retValName = fields[i]
|
||||
.getAnnotation( WSReturnObject.WSValueName.class );
|
||||
WSReturnObject.WSValueName retValName = field.getAnnotation(WSReturnObject.WSValueName.class);
|
||||
|
||||
if (retValName != null)
|
||||
ret_param.setName(retValName.value());
|
||||
else
|
||||
ret_param.setName( fields[i].getName() );
|
||||
ret_param.setParamClass( fields[i].getType() );
|
||||
ret_param.setName(field.getName());
|
||||
|
||||
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);
|
||||
if (returnName != null)
|
||||
ret_param.setName(returnName.value());
|
||||
|
||||
if (returnNameAnnotation != null)
|
||||
ret_param.setName(returnNameAnnotation.value());
|
||||
else
|
||||
ret_param.setName("return");
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -144,6 +206,13 @@ public class WSMethodDef {
|
|||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the path to the WS method endpoint
|
||||
*/
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of exceptions this method throws
|
||||
*/
|
||||
|
|
@ -166,12 +235,19 @@ public class WSMethodDef {
|
|||
}
|
||||
|
||||
/**
|
||||
* @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() {
|
||||
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
|
||||
*/
|
||||
|
|
@ -183,8 +259,8 @@ public class WSMethodDef {
|
|||
/**
|
||||
* Invokes a specified method
|
||||
*
|
||||
* @param obj the object the method will called on
|
||||
* @param params a vector with arguments
|
||||
* @param obj the object the method will called on
|
||||
*/
|
||||
public Object invoke(Object obj, Object[] params) throws Exception {
|
||||
return this.method.invoke(obj, params);
|
||||
|
|
@ -194,7 +270,9 @@ public class WSMethodDef {
|
|||
public String toString() {
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
boolean first = true;
|
||||
|
||||
tmp.append(name).append("(");
|
||||
|
||||
for (WSParameterDef param : inputs) {
|
||||
if (first)
|
||||
first = false;
|
||||
|
|
@ -205,6 +283,7 @@ public class WSMethodDef {
|
|||
tmp.append(param.getName());
|
||||
}
|
||||
tmp.append(") => ");
|
||||
|
||||
first = true;
|
||||
for (WSParameterDef param : outputs) {
|
||||
if (first)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,10 @@ import zutil.net.ws.WSMethodDef;
|
|||
import zutil.net.ws.WSParameterDef;
|
||||
import zutil.net.ws.WebServiceDef;
|
||||
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.Method;
|
||||
import java.net.MalformedURLException;
|
||||
|
|
@ -70,38 +73,52 @@ public class RESTClientInvocationHandler implements InvocationHandler {
|
|||
*/
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
// Generate XML
|
||||
HttpURL url = generateRESTRequest(method.getName(), args);
|
||||
// Generate request
|
||||
|
||||
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
|
||||
|
||||
HttpClient request = new HttpClient(HttpClient.HttpRequestType.POST);
|
||||
request.setURL(url);
|
||||
request.setType(requestType);
|
||||
|
||||
logger.fine("Sending request for: " + url);
|
||||
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();
|
||||
|
||||
// DEBUG
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
System.out.println("********** Request");
|
||||
System.out.println(url);
|
||||
System.out.println("********** Response");
|
||||
System.out.println(rspJson);
|
||||
}
|
||||
//if (logger.isLoggable(Level.FINEST)) {
|
||||
System.out.println("********** Response: " + url);
|
||||
System.out.println(rspStr);
|
||||
//}
|
||||
|
||||
return parseRESTResponse(rspJson);
|
||||
Object rspObj = parseRESTResponse(methodDef, rspStr);
|
||||
return rspObj;
|
||||
}
|
||||
|
||||
|
||||
private HttpURL generateRESTRequest(String targetMethod, Object[] args) {
|
||||
logger.fine("Sending request for " + targetMethod);
|
||||
private HttpURL generateRESTRequest(WSMethodDef methodDef, Object[] args) {
|
||||
HttpURL url = new HttpURL(serviceUrl);
|
||||
|
||||
WSMethodDef methodDef = wsDef.getMethod(targetMethod);
|
||||
url.setPath(serviceUrl.getPath()
|
||||
+ (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++) {
|
||||
WSParameterDef param = params.get(i);
|
||||
url.setParameter(param.getName(), args[i].toString());
|
||||
|
|
@ -110,8 +127,15 @@ public class RESTClientInvocationHandler implements InvocationHandler {
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
src/zutil/parser/wsdl/WSDLHttpPage.java → src/zutil/net/ws/wsdl/WSDLHttpPage.java
Executable file → Normal file
2
src/zutil/parser/wsdl/WSDLHttpPage.java → src/zutil/net/ws/wsdl/WSDLHttpPage.java
Executable file → Normal file
|
|
@ -22,7 +22,7 @@
|
|||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import zutil.net.http.HttpHeader;
|
||||
import zutil.net.http.HttpPage;
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
|
|
@ -22,7 +22,7 @@
|
|||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
|
|
@ -31,7 +31,7 @@ import zutil.net.ws.WSInterface;
|
|||
import zutil.net.ws.WSInterface.WSNamespace;
|
||||
import zutil.net.ws.WSReturnObject;
|
||||
import zutil.net.ws.WebServiceDef;
|
||||
import zutil.parser.wsdl.WSDLWriter;
|
||||
import zutil.net.ws.wsdl.WSDLWriter;
|
||||
|
||||
|
||||
// TODO: Convert to JUnit
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue