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{
|
||||
|
|
@ -100,14 +100,14 @@ public interface WSInterface {
|
|||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface WSIgnore { }
|
||||
@interface WSIgnore {}
|
||||
|
||||
/**
|
||||
* Method or Parameter comments for the WSDL.
|
||||
* These comments are put in the message part of the WSDL
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface WSDocumentation{
|
||||
@interface WSDocumentation {
|
||||
String value();
|
||||
}
|
||||
|
||||
|
|
@ -116,7 +116,7 @@ public interface WSInterface {
|
|||
* These comments are put in the message part of the WSDL
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface WSParamDocumentation{
|
||||
@interface WSParamDocumentation {
|
||||
String value();
|
||||
}
|
||||
|
||||
|
|
@ -125,7 +125,7 @@ public interface WSInterface {
|
|||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@interface WSHeader { }
|
||||
@interface WSHeader {}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
@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,141 +43,215 @@ 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 me is a method in a class that implements WSInterface
|
||||
* @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) {
|
||||
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();
|
||||
this.method = me;
|
||||
this.inputs = new ArrayList<>();
|
||||
this.outputs = new ArrayList<>();
|
||||
this.exceptions = new ArrayList<>();
|
||||
this.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();
|
||||
// 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;
|
||||
namespace = wsDef.getNamespace() + "?#" + name;
|
||||
|
||||
// Hnadle Exceptions
|
||||
|
||||
//***** Exceptions
|
||||
Collections.addAll(exceptions, method.getExceptionTypes());
|
||||
|
||||
//********* Get the input parameter names **********
|
||||
// Handle input parameter names
|
||||
|
||||
Annotation[][] paramAnnotation = method.getParameterAnnotations();
|
||||
Class<?>[] inputTypes = method.getParameterTypes();
|
||||
|
||||
for (int i=0; i<paramAnnotation.length ;i++){
|
||||
WSParameterDef param = new WSParameterDef( this );
|
||||
for (Annotation annotation : paramAnnotation[i]){
|
||||
if (annotation instanceof WSInterface.WSParamName){
|
||||
for (int i = 0; i < paramAnnotation.length; i++) {
|
||||
WSParameterDef param = new WSParameterDef(this);
|
||||
for (Annotation annotation : paramAnnotation[i]) {
|
||||
if (annotation instanceof WSInterface.WSParamName) {
|
||||
WSInterface.WSParamName paramName = (WSInterface.WSParamName) annotation;
|
||||
param.setName( paramName.value() );
|
||||
param.setOptional( paramName.optional() );
|
||||
param.setName(paramName.value());
|
||||
param.setOptional(paramName.optional());
|
||||
}
|
||||
}
|
||||
param.setParamClass( inputTypes[i] );
|
||||
param.setParamClass(inputTypes[i]);
|
||||
// if no name was found then use default
|
||||
if (param.getName() == null)
|
||||
param.setName( "args"+i );
|
||||
param.setName("args" + i);
|
||||
|
||||
inputs.add( param );
|
||||
inputs.add(param);
|
||||
}
|
||||
|
||||
//******** The return parameter name ************
|
||||
WSInterface.WSReturnName returnName = method.getAnnotation(WSInterface.WSReturnName.class);
|
||||
if (WSReturnObject.class.isAssignableFrom( method.getReturnType())){
|
||||
// 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++){
|
||||
WSParameterDef ret_param = new WSParameterDef( this );
|
||||
WSReturnObject.WSValueName retValName = fields[i]
|
||||
.getAnnotation( WSReturnObject.WSValueName.class );
|
||||
if(retValName != null)
|
||||
ret_param.setName( retValName.value() );
|
||||
for (Field field : fields) {
|
||||
WSParameterDef ret_param = new WSParameterDef(this);
|
||||
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() );
|
||||
outputs.add( ret_param );
|
||||
ret_param.setName(field.getName());
|
||||
|
||||
ret_param.setParamClass(field.getType());
|
||||
outputs.add(ret_param);
|
||||
}
|
||||
}
|
||||
else if( method.getReturnType() != void.class ){
|
||||
WSParameterDef ret_param = new WSParameterDef( this );
|
||||
if (returnName != null)
|
||||
ret_param.setName(returnName.value());
|
||||
} else if (method.getReturnType() != void.class) {
|
||||
WSParameterDef ret_param = new WSParameterDef(this);
|
||||
|
||||
if (returnNameAnnotation != null)
|
||||
ret_param.setName(returnNameAnnotation.value());
|
||||
else
|
||||
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
|
||||
*/
|
||||
public String getName(){
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the path to the WS method endpoint
|
||||
*/
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of exceptions this method throws
|
||||
*/
|
||||
public List<Class<?>> getExceptions(){
|
||||
public List<Class<?>> getExceptions() {
|
||||
return exceptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of input parameters
|
||||
*/
|
||||
public List<WSParameterDef> getInputs(){
|
||||
public List<WSParameterDef> getInputs() {
|
||||
return inputs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of input parameters
|
||||
*/
|
||||
public List<WSParameterDef> getOutputs(){
|
||||
public List<WSParameterDef> getOutputs() {
|
||||
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 the type of request needed to execute this method
|
||||
*/
|
||||
public WSInterface.RequestType getRequestType() {
|
||||
return requestType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the namespace or endpoint url of the method
|
||||
*/
|
||||
public String getNamespace(){
|
||||
public String getNamespace() {
|
||||
return namespace;
|
||||
}
|
||||
|
||||
|
|
@ -183,19 +259,21 @@ public class WSMethodDef {
|
|||
/**
|
||||
* 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 {
|
||||
return this.method.invoke(obj, params );
|
||||
return this.method.invoke(obj, params);
|
||||
}
|
||||
|
||||
|
||||
public String toString(){
|
||||
public String toString() {
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
boolean first = true;
|
||||
|
||||
tmp.append(name).append("(");
|
||||
for (WSParameterDef param : inputs){
|
||||
|
||||
for (WSParameterDef param : inputs) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
|
|
@ -205,8 +283,9 @@ public class WSMethodDef {
|
|||
tmp.append(param.getName());
|
||||
}
|
||||
tmp.append(") => ");
|
||||
|
||||
first = true;
|
||||
for (WSParameterDef param : outputs){
|
||||
for (WSParameterDef param : outputs) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public class RESTClientFactory {
|
|||
* @return a client Object
|
||||
*/
|
||||
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)));
|
||||
return obj;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
// Send request
|
||||
HttpClient request = new HttpClient(HttpClient.HttpRequestType.POST);
|
||||
request.setURL(url);
|
||||
HttpHeader response = request.send();
|
||||
String rspJson = IOUtil.readContentAsString(request.getResponseInputStream());
|
||||
request.close();
|
||||
WSMethodDef methodDef = wsDef.getMethod(method.getName());
|
||||
HttpURL url = generateRESTRequest(methodDef, args);
|
||||
|
||||
// DEBUG
|
||||
if (logger.isLoggable(Level.FINEST)) {
|
||||
System.out.println("********** Request");
|
||||
System.out.println(url);
|
||||
System.out.println("********** Response");
|
||||
System.out.println(rspJson);
|
||||
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;
|
||||
}
|
||||
|
||||
return parseRESTResponse(rspJson);
|
||||
// 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();
|
||||
logger.fine("Received response(" + response.getResponseStatusCode() + ")");
|
||||
|
||||
// Parse response
|
||||
|
||||
String rspStr = IOUtil.readContentAsString(request.getResponseInputStream());
|
||||
request.close();
|
||||
|
||||
//if (logger.isLoggable(Level.FINEST)) {
|
||||
System.out.println("********** Response: " + url);
|
||||
System.out.println(rspStr);
|
||||
//}
|
||||
|
||||
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;
|
||||
|
|
@ -1,52 +1,52 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
|
||||
/**
|
||||
* This interface defines a WSDL Service type.
|
||||
*
|
||||
* User: Ziver
|
||||
*/
|
||||
public abstract class WSDLService {
|
||||
/** The URL of this service **/
|
||||
private String url;
|
||||
|
||||
public WSDLService(String url){
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getServiceAddress(){
|
||||
return url;
|
||||
}
|
||||
|
||||
public abstract String getServiceType();
|
||||
|
||||
public abstract void generateBinding(Element definitions);
|
||||
|
||||
public abstract void generateOperation(Element definitions, WSMethodDef method);
|
||||
}
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
|
||||
/**
|
||||
* This interface defines a WSDL Service type.
|
||||
*
|
||||
* User: Ziver
|
||||
*/
|
||||
public abstract class WSDLService {
|
||||
/** The URL of this service **/
|
||||
private String url;
|
||||
|
||||
public WSDLService(String url){
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getServiceAddress(){
|
||||
return url;
|
||||
}
|
||||
|
||||
public abstract String getServiceType();
|
||||
|
||||
public abstract void generateBinding(Element definitions);
|
||||
|
||||
public abstract void generateOperation(Element definitions, WSMethodDef method);
|
||||
}
|
||||
|
|
@ -1,78 +1,78 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
|
||||
/**
|
||||
* User: Ziver
|
||||
*/
|
||||
public class WSDLServiceSOAP extends WSDLService{
|
||||
|
||||
public WSDLServiceSOAP(String url){
|
||||
super(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() { return "soap"; }
|
||||
|
||||
@Override
|
||||
public void generateBinding(Element definitions) {
|
||||
// definitions -> binding -> soap:binding
|
||||
Element soap_binding = definitions.addElement("soap:binding");
|
||||
soap_binding.addAttribute("style", "rpc");
|
||||
soap_binding.addAttribute("transport", "http://schemas.xmlsoap.org/soap/http");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateOperation(Element definitions, WSMethodDef method) {
|
||||
// definitions -> binding -> operation
|
||||
Element operation = definitions.addElement("wsdl:operation");
|
||||
operation.addAttribute("name", method.getName());
|
||||
|
||||
// definitions -> binding -> operation -> soap:operation
|
||||
Element soap_operation = operation.addElement("soap:operation");
|
||||
soap_operation.addAttribute("soapAction", method.getNamespace());
|
||||
|
||||
//*************************** Input
|
||||
// definitions -> binding -> operation -> input
|
||||
Element input = operation.addElement("wsdl:input");
|
||||
// definitions -> binding -> operation -> input -> body
|
||||
Element input_body = input.addElement("soap:body");
|
||||
input_body.addAttribute("use", "literal");
|
||||
input_body.addAttribute("namespace", method.getNamespace());
|
||||
|
||||
//*************************** output
|
||||
if(!method.getOutputs().isEmpty()){
|
||||
// definitions -> binding -> operation -> output
|
||||
Element output = operation.addElement("wsdl:output");
|
||||
// definitions -> binding -> operation -> input -> body
|
||||
Element output_body = output.addElement("soap:body");
|
||||
output_body.addAttribute("use", "literal");
|
||||
output_body.addAttribute("namespace", method.getNamespace());
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import org.dom4j.Element;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
|
||||
/**
|
||||
* User: Ziver
|
||||
*/
|
||||
public class WSDLServiceSOAP extends WSDLService{
|
||||
|
||||
public WSDLServiceSOAP(String url){
|
||||
super(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceType() { return "soap"; }
|
||||
|
||||
@Override
|
||||
public void generateBinding(Element definitions) {
|
||||
// definitions -> binding -> soap:binding
|
||||
Element soap_binding = definitions.addElement("soap:binding");
|
||||
soap_binding.addAttribute("style", "rpc");
|
||||
soap_binding.addAttribute("transport", "http://schemas.xmlsoap.org/soap/http");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateOperation(Element definitions, WSMethodDef method) {
|
||||
// definitions -> binding -> operation
|
||||
Element operation = definitions.addElement("wsdl:operation");
|
||||
operation.addAttribute("name", method.getName());
|
||||
|
||||
// definitions -> binding -> operation -> soap:operation
|
||||
Element soap_operation = operation.addElement("soap:operation");
|
||||
soap_operation.addAttribute("soapAction", method.getNamespace());
|
||||
|
||||
//*************************** Input
|
||||
// definitions -> binding -> operation -> input
|
||||
Element input = operation.addElement("wsdl:input");
|
||||
// definitions -> binding -> operation -> input -> body
|
||||
Element input_body = input.addElement("soap:body");
|
||||
input_body.addAttribute("use", "literal");
|
||||
input_body.addAttribute("namespace", method.getNamespace());
|
||||
|
||||
//*************************** output
|
||||
if(!method.getOutputs().isEmpty()){
|
||||
// definitions -> binding -> operation -> output
|
||||
Element output = operation.addElement("wsdl:output");
|
||||
// definitions -> binding -> operation -> input -> body
|
||||
Element output_body = output.addElement("soap:body");
|
||||
output_body.addAttribute("use", "literal");
|
||||
output_body.addAttribute("namespace", method.getNamespace());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,392 +1,392 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser.wsdl;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import zutil.io.StringOutputStream;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
import zutil.net.ws.WSParameterDef;
|
||||
import zutil.net.ws.WSReturnObject;
|
||||
import zutil.net.ws.WSReturnObject.WSValueName;
|
||||
import zutil.net.ws.WebServiceDef;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class WSDLWriter{
|
||||
/** Current Web service definition ***/
|
||||
private WebServiceDef ws;
|
||||
/** Cache of generated WSDL **/
|
||||
private String cache;
|
||||
/** A list of services **/
|
||||
private ArrayList<WSDLService> services;
|
||||
|
||||
public WSDLWriter( WebServiceDef ws ){
|
||||
this.services = new ArrayList<>();
|
||||
this.ws = ws;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a service to be published with the WSDL
|
||||
*/
|
||||
public void addService(WSDLService serv){
|
||||
cache = null;
|
||||
services.add(serv);
|
||||
}
|
||||
|
||||
|
||||
public void write( Writer out ) throws IOException {
|
||||
out.write(generate());
|
||||
}
|
||||
public void write( PrintStream out ) {
|
||||
out.print(generate());
|
||||
}
|
||||
public void write( OutputStream out ) throws IOException {
|
||||
out.write(generate().getBytes() );
|
||||
}
|
||||
|
||||
|
||||
private String generate(){
|
||||
if(cache == null){
|
||||
try {
|
||||
OutputFormat outformat = OutputFormat.createPrettyPrint();
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
XMLWriter writer = new XMLWriter(out, outformat);
|
||||
|
||||
Document docroot = generateDefinition();
|
||||
writer.write(docroot);
|
||||
|
||||
writer.flush();
|
||||
this.cache = out.toString();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
private Document generateDefinition(){
|
||||
Document wsdl = DocumentHelper.createDocument();
|
||||
Element definitions = wsdl.addElement("wsdl:definitions");
|
||||
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
|
||||
definitions.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
|
||||
definitions.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/");
|
||||
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
|
||||
definitions.addNamespace("soap-enc", "http://schemas.xmlsoap.org/soap/encoding/");
|
||||
definitions.addNamespace("tns", ws.getNamespace()+"?type");
|
||||
definitions.addAttribute("targetNamespace", ws.getNamespace());
|
||||
|
||||
generateType(definitions);
|
||||
generateMessages(definitions);
|
||||
generatePortType(definitions);
|
||||
generateBinding(definitions);
|
||||
generateService(definitions);
|
||||
|
||||
return wsdl;
|
||||
}
|
||||
|
||||
private void generateMessages(Element definitions){
|
||||
for( WSMethodDef method : ws.getMethods() ){
|
||||
generateMessage(definitions, method);
|
||||
}
|
||||
|
||||
// Default message used for functions without input parameters
|
||||
// definitions -> message: empty
|
||||
Element empty = definitions.addElement("wsdl:message");
|
||||
empty.addAttribute("name", "empty");
|
||||
// definitions -> message: empty -> part
|
||||
Element empty_part = empty.addElement("wsdl:part");
|
||||
empty_part.addAttribute("name", "empty");
|
||||
empty_part.addAttribute("type", "td:empty");
|
||||
|
||||
// Exception message
|
||||
// definitions -> message: exception
|
||||
Element exception = definitions.addElement("wsdl:message");
|
||||
exception.addAttribute("name", "exception");
|
||||
// definitions -> message: exception -> part
|
||||
Element exc_part = exception.addElement("wsdl:part");
|
||||
exc_part.addAttribute("name", "exception");
|
||||
exc_part.addAttribute("type", "td:string");
|
||||
}
|
||||
|
||||
private void generateMessage(Element parent, WSMethodDef method){
|
||||
//*************************** Input
|
||||
if(!method.getInputs().isEmpty()){
|
||||
// definitions -> message
|
||||
Element input = parent.addElement("wsdl:message");
|
||||
input.addAttribute("name", method.getName()+"Request");
|
||||
|
||||
// Parameters
|
||||
for( WSParameterDef param : method.getInputs() ){
|
||||
// definitions -> message -> part
|
||||
Element part = input.addElement("wsdl:part");
|
||||
part.addAttribute("name", param.getName());
|
||||
part.addAttribute("type", "xsd:"+getClassName( param.getParamClass()));
|
||||
|
||||
if( param.isOptional() )
|
||||
part.addAttribute("minOccurs", "0");
|
||||
}
|
||||
}
|
||||
//*************************** Output
|
||||
if(!method.getOutputs().isEmpty()){
|
||||
// definitions -> message
|
||||
Element output = parent.addElement("wsdl:message");
|
||||
output.addAttribute("name", method.getName()+"Response");
|
||||
|
||||
// Parameters
|
||||
for( WSParameterDef param : method.getOutputs() ){
|
||||
// definitions -> message -> part
|
||||
Element part = output.addElement("wsdl:part");
|
||||
part.addAttribute("name", param.getName());
|
||||
|
||||
Class<?> paramClass = param.getParamClass();
|
||||
Class<?> valueClass = getClass( paramClass );
|
||||
// is an binary array
|
||||
if(byte[].class.isAssignableFrom( paramClass )){
|
||||
part.addAttribute("type", "xsd:base64Binary");
|
||||
}
|
||||
// is an array?
|
||||
else if( paramClass.isArray()){
|
||||
part.addAttribute("type", "td:" +getArrayClassName(paramClass));
|
||||
}
|
||||
else if( WSReturnObject.class.isAssignableFrom(valueClass) ){
|
||||
// its an SOAPObject
|
||||
part.addAttribute("type", "td:"+getClassName( paramClass ));
|
||||
}
|
||||
else{// its an Object
|
||||
part.addAttribute("type", "xsd:"+getClassName( paramClass ));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void generatePortType(Element definitions){
|
||||
// definitions -> portType
|
||||
Element portType = definitions.addElement("wsdl:portType");
|
||||
portType.addAttribute("name", ws.getName()+"PortType");
|
||||
|
||||
for( WSMethodDef method : ws.getMethods() ){
|
||||
// definitions -> portType -> operation
|
||||
Element operation = portType.addElement("wsdl:operation");
|
||||
operation.addAttribute("name", method.getName());
|
||||
|
||||
// Documentation
|
||||
if(method.getDocumentation() != null){
|
||||
Element doc = operation.addElement("wsdl:documentation");
|
||||
doc.setText(method.getDocumentation());
|
||||
}
|
||||
|
||||
//*************************** Input
|
||||
if( method.getInputs().size() > 0 ){
|
||||
// definitions -> message
|
||||
Element input = operation.addElement("wsdl:input");
|
||||
input.addAttribute("message", "tns:"+method.getName()+"Request");
|
||||
}
|
||||
//*************************** Output
|
||||
if( method.getOutputs().size() > 0 ){
|
||||
// definitions -> message
|
||||
Element output = operation.addElement("wsdl:output");
|
||||
output.addAttribute("message", "tns:"+method.getName()+"Response");
|
||||
}
|
||||
//*************************** Fault
|
||||
if( method.getOutputs().size() > 0 ){
|
||||
// definitions -> message
|
||||
Element fault = operation.addElement("wsdl:fault");
|
||||
fault.addAttribute("message", "tns:exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void generateBinding(Element definitions){
|
||||
// definitions -> binding
|
||||
Element binding = definitions.addElement("wsdl:binding");
|
||||
binding.addAttribute("name", ws.getName()+"Binding");
|
||||
binding.addAttribute("type", "tns:"+ws.getName()+"PortType");
|
||||
|
||||
for(WSDLService serv : services){
|
||||
serv.generateBinding(binding);
|
||||
|
||||
for(WSMethodDef method : ws.getMethods()){
|
||||
serv.generateOperation(binding, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void generateService(Element parent){
|
||||
// definitions -> service
|
||||
Element root = parent.addElement("wsdl:service");
|
||||
root.addAttribute("name", ws.getName()+"Service");
|
||||
|
||||
// definitions -> service -> port
|
||||
Element port = root.addElement("wsdl:port");
|
||||
port.addAttribute("name", ws.getName()+"Port");
|
||||
port.addAttribute("binding", "tns:"+ws.getName()+"Binding");
|
||||
|
||||
for(WSDLService serv : services){
|
||||
// definitions -> service-> port -> address
|
||||
Element address = port.addElement(serv.getServiceType()+":address");
|
||||
address.addAttribute("location", serv.getServiceAddress());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function generates the Type section of the WSDL.
|
||||
* <b><pre>
|
||||
* -wsdl:definitions
|
||||
* -wsdl:type
|
||||
* </pre></b>
|
||||
*/
|
||||
private void generateType(Element definitions){
|
||||
ArrayList<Class<?>> types = new ArrayList<>();
|
||||
// Find types
|
||||
for( WSMethodDef method : ws.getMethods() ){
|
||||
if(!method.getOutputs().isEmpty()){
|
||||
for( WSParameterDef param : method.getOutputs() ){
|
||||
Class<?> paramClass = param.getParamClass();
|
||||
Class<?> valueClass = getClass(paramClass);
|
||||
// is an array? or special class
|
||||
if( paramClass.isArray() || WSReturnObject.class.isAssignableFrom(valueClass)){
|
||||
// add to type generation list
|
||||
if(!types.contains( paramClass ))
|
||||
types.add( paramClass );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// definitions -> types
|
||||
Element typeE = definitions.addElement("wsdl:types");
|
||||
Element schema = typeE.addElement("xsd:schema");
|
||||
schema.addAttribute("targetNamespace", ws.getNamespace()+"?type");
|
||||
|
||||
// empty type
|
||||
Element empty = schema.addElement("xsd:complexType");
|
||||
empty.addAttribute("name", "empty");
|
||||
empty.addElement("xsd:sequence");
|
||||
|
||||
for(int n=0; n<types.size() ;n++){
|
||||
Class<?> c = types.get(n);
|
||||
// Generate Array type
|
||||
if(c.isArray()){
|
||||
Class<?> ctmp = getClass(c);
|
||||
|
||||
Element type = schema.addElement("xsd:complexType");
|
||||
type.addAttribute("name", getArrayClassName(c));
|
||||
|
||||
Element sequence = type.addElement("xsd:sequence");
|
||||
|
||||
Element element = sequence.addElement("xsd:element");
|
||||
element.addAttribute("minOccurs", "0");
|
||||
element.addAttribute("maxOccurs", "unbounded");
|
||||
element.addAttribute("name", "element");
|
||||
element.addAttribute("nillable", "true");
|
||||
if( WSReturnObject.class.isAssignableFrom(ctmp) )
|
||||
element.addAttribute("type", "tns:"+getClassName(c).replace("[]", ""));
|
||||
else
|
||||
element.addAttribute("type", "xsd:"+getClassName(c).replace("[]", ""));
|
||||
|
||||
if(!types.contains(ctmp))
|
||||
types.add(ctmp);
|
||||
}
|
||||
// Generate SOAPObject type
|
||||
else if(WSReturnObject.class.isAssignableFrom(c)){
|
||||
Element type = schema.addElement("xsd:complexType");
|
||||
type.addAttribute("name", getClassName(c));
|
||||
|
||||
Element sequence = type.addElement("xsd:sequence");
|
||||
|
||||
Field[] fields = c.getFields();
|
||||
for(int i=0; i<fields.length ;i++){
|
||||
WSValueName tmp = fields[i].getAnnotation( WSValueName.class );
|
||||
String name;
|
||||
if(tmp != null) name = tmp.value();
|
||||
else name = "field"+i;
|
||||
|
||||
Element element = sequence.addElement("xsd:element");
|
||||
element.addAttribute("name", name);
|
||||
|
||||
// Check if the object is an SOAPObject
|
||||
Class<?> cTmp = getClass(fields[i].getType());
|
||||
if( WSReturnObject.class.isAssignableFrom(cTmp) ){
|
||||
element.addAttribute("type", "tns:"+getClassName(cTmp));
|
||||
if(!types.contains(cTmp))
|
||||
types.add(cTmp);
|
||||
}
|
||||
else{
|
||||
element.addAttribute("type", "xsd:"+getClassName(fields[i].getType()));
|
||||
}
|
||||
// Is the Field optional
|
||||
if(tmp != null && tmp.optional())
|
||||
element.addAttribute("minOccurs", "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TODO: FIX THESE ARE DUPLICATES FROM SOAPHttpPage
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private Class<?> getClass(Class<?> c){
|
||||
if(c!=null && c.isArray()){
|
||||
return getClass(c.getComponentType());
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private String getArrayClassName(Class<?> c){
|
||||
return "ArrayOf"+getClassName(c).replaceAll("[\\[\\]]", "");
|
||||
}
|
||||
|
||||
private String getClassName(Class<?> c){
|
||||
Class<?> cTmp = getClass(c);
|
||||
if( byte[].class.isAssignableFrom(c) ){
|
||||
return "base64Binary";
|
||||
}
|
||||
else if( WSReturnObject.class.isAssignableFrom(cTmp) ){
|
||||
return c.getSimpleName();
|
||||
}
|
||||
else{
|
||||
String ret = c.getSimpleName().toLowerCase();
|
||||
|
||||
if( cTmp == Integer.class ) ret = ret.replaceAll("integer", "int");
|
||||
else if( cTmp == Character.class ) ret = ret.replaceAll("character", "char");
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {}
|
||||
|
||||
}
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2020 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.net.ws.wsdl;
|
||||
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import zutil.io.StringOutputStream;
|
||||
import zutil.net.ws.WSMethodDef;
|
||||
import zutil.net.ws.WSParameterDef;
|
||||
import zutil.net.ws.WSReturnObject;
|
||||
import zutil.net.ws.WSReturnObject.WSValueName;
|
||||
import zutil.net.ws.WebServiceDef;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class WSDLWriter{
|
||||
/** Current Web service definition ***/
|
||||
private WebServiceDef ws;
|
||||
/** Cache of generated WSDL **/
|
||||
private String cache;
|
||||
/** A list of services **/
|
||||
private ArrayList<WSDLService> services;
|
||||
|
||||
public WSDLWriter( WebServiceDef ws ){
|
||||
this.services = new ArrayList<>();
|
||||
this.ws = ws;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a service to be published with the WSDL
|
||||
*/
|
||||
public void addService(WSDLService serv){
|
||||
cache = null;
|
||||
services.add(serv);
|
||||
}
|
||||
|
||||
|
||||
public void write( Writer out ) throws IOException {
|
||||
out.write(generate());
|
||||
}
|
||||
public void write( PrintStream out ) {
|
||||
out.print(generate());
|
||||
}
|
||||
public void write( OutputStream out ) throws IOException {
|
||||
out.write(generate().getBytes() );
|
||||
}
|
||||
|
||||
|
||||
private String generate(){
|
||||
if(cache == null){
|
||||
try {
|
||||
OutputFormat outformat = OutputFormat.createPrettyPrint();
|
||||
StringOutputStream out = new StringOutputStream();
|
||||
XMLWriter writer = new XMLWriter(out, outformat);
|
||||
|
||||
Document docroot = generateDefinition();
|
||||
writer.write(docroot);
|
||||
|
||||
writer.flush();
|
||||
this.cache = out.toString();
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
private Document generateDefinition(){
|
||||
Document wsdl = DocumentHelper.createDocument();
|
||||
Element definitions = wsdl.addElement("wsdl:definitions");
|
||||
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
|
||||
definitions.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
|
||||
definitions.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/");
|
||||
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
|
||||
definitions.addNamespace("soap-enc", "http://schemas.xmlsoap.org/soap/encoding/");
|
||||
definitions.addNamespace("tns", ws.getNamespace()+"?type");
|
||||
definitions.addAttribute("targetNamespace", ws.getNamespace());
|
||||
|
||||
generateType(definitions);
|
||||
generateMessages(definitions);
|
||||
generatePortType(definitions);
|
||||
generateBinding(definitions);
|
||||
generateService(definitions);
|
||||
|
||||
return wsdl;
|
||||
}
|
||||
|
||||
private void generateMessages(Element definitions){
|
||||
for( WSMethodDef method : ws.getMethods() ){
|
||||
generateMessage(definitions, method);
|
||||
}
|
||||
|
||||
// Default message used for functions without input parameters
|
||||
// definitions -> message: empty
|
||||
Element empty = definitions.addElement("wsdl:message");
|
||||
empty.addAttribute("name", "empty");
|
||||
// definitions -> message: empty -> part
|
||||
Element empty_part = empty.addElement("wsdl:part");
|
||||
empty_part.addAttribute("name", "empty");
|
||||
empty_part.addAttribute("type", "td:empty");
|
||||
|
||||
// Exception message
|
||||
// definitions -> message: exception
|
||||
Element exception = definitions.addElement("wsdl:message");
|
||||
exception.addAttribute("name", "exception");
|
||||
// definitions -> message: exception -> part
|
||||
Element exc_part = exception.addElement("wsdl:part");
|
||||
exc_part.addAttribute("name", "exception");
|
||||
exc_part.addAttribute("type", "td:string");
|
||||
}
|
||||
|
||||
private void generateMessage(Element parent, WSMethodDef method){
|
||||
//*************************** Input
|
||||
if(!method.getInputs().isEmpty()){
|
||||
// definitions -> message
|
||||
Element input = parent.addElement("wsdl:message");
|
||||
input.addAttribute("name", method.getName()+"Request");
|
||||
|
||||
// Parameters
|
||||
for( WSParameterDef param : method.getInputs() ){
|
||||
// definitions -> message -> part
|
||||
Element part = input.addElement("wsdl:part");
|
||||
part.addAttribute("name", param.getName());
|
||||
part.addAttribute("type", "xsd:"+getClassName( param.getParamClass()));
|
||||
|
||||
if( param.isOptional() )
|
||||
part.addAttribute("minOccurs", "0");
|
||||
}
|
||||
}
|
||||
//*************************** Output
|
||||
if(!method.getOutputs().isEmpty()){
|
||||
// definitions -> message
|
||||
Element output = parent.addElement("wsdl:message");
|
||||
output.addAttribute("name", method.getName()+"Response");
|
||||
|
||||
// Parameters
|
||||
for( WSParameterDef param : method.getOutputs() ){
|
||||
// definitions -> message -> part
|
||||
Element part = output.addElement("wsdl:part");
|
||||
part.addAttribute("name", param.getName());
|
||||
|
||||
Class<?> paramClass = param.getParamClass();
|
||||
Class<?> valueClass = getClass( paramClass );
|
||||
// is an binary array
|
||||
if(byte[].class.isAssignableFrom( paramClass )){
|
||||
part.addAttribute("type", "xsd:base64Binary");
|
||||
}
|
||||
// is an array?
|
||||
else if( paramClass.isArray()){
|
||||
part.addAttribute("type", "td:" +getArrayClassName(paramClass));
|
||||
}
|
||||
else if( WSReturnObject.class.isAssignableFrom(valueClass) ){
|
||||
// its an SOAPObject
|
||||
part.addAttribute("type", "td:"+getClassName( paramClass ));
|
||||
}
|
||||
else{// its an Object
|
||||
part.addAttribute("type", "xsd:"+getClassName( paramClass ));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void generatePortType(Element definitions){
|
||||
// definitions -> portType
|
||||
Element portType = definitions.addElement("wsdl:portType");
|
||||
portType.addAttribute("name", ws.getName()+"PortType");
|
||||
|
||||
for( WSMethodDef method : ws.getMethods() ){
|
||||
// definitions -> portType -> operation
|
||||
Element operation = portType.addElement("wsdl:operation");
|
||||
operation.addAttribute("name", method.getName());
|
||||
|
||||
// Documentation
|
||||
if(method.getDocumentation() != null){
|
||||
Element doc = operation.addElement("wsdl:documentation");
|
||||
doc.setText(method.getDocumentation());
|
||||
}
|
||||
|
||||
//*************************** Input
|
||||
if( method.getInputs().size() > 0 ){
|
||||
// definitions -> message
|
||||
Element input = operation.addElement("wsdl:input");
|
||||
input.addAttribute("message", "tns:"+method.getName()+"Request");
|
||||
}
|
||||
//*************************** Output
|
||||
if( method.getOutputs().size() > 0 ){
|
||||
// definitions -> message
|
||||
Element output = operation.addElement("wsdl:output");
|
||||
output.addAttribute("message", "tns:"+method.getName()+"Response");
|
||||
}
|
||||
//*************************** Fault
|
||||
if( method.getOutputs().size() > 0 ){
|
||||
// definitions -> message
|
||||
Element fault = operation.addElement("wsdl:fault");
|
||||
fault.addAttribute("message", "tns:exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void generateBinding(Element definitions){
|
||||
// definitions -> binding
|
||||
Element binding = definitions.addElement("wsdl:binding");
|
||||
binding.addAttribute("name", ws.getName()+"Binding");
|
||||
binding.addAttribute("type", "tns:"+ws.getName()+"PortType");
|
||||
|
||||
for(WSDLService serv : services){
|
||||
serv.generateBinding(binding);
|
||||
|
||||
for(WSMethodDef method : ws.getMethods()){
|
||||
serv.generateOperation(binding, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void generateService(Element parent){
|
||||
// definitions -> service
|
||||
Element root = parent.addElement("wsdl:service");
|
||||
root.addAttribute("name", ws.getName()+"Service");
|
||||
|
||||
// definitions -> service -> port
|
||||
Element port = root.addElement("wsdl:port");
|
||||
port.addAttribute("name", ws.getName()+"Port");
|
||||
port.addAttribute("binding", "tns:"+ws.getName()+"Binding");
|
||||
|
||||
for(WSDLService serv : services){
|
||||
// definitions -> service-> port -> address
|
||||
Element address = port.addElement(serv.getServiceType()+":address");
|
||||
address.addAttribute("location", serv.getServiceAddress());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function generates the Type section of the WSDL.
|
||||
* <b><pre>
|
||||
* -wsdl:definitions
|
||||
* -wsdl:type
|
||||
* </pre></b>
|
||||
*/
|
||||
private void generateType(Element definitions){
|
||||
ArrayList<Class<?>> types = new ArrayList<>();
|
||||
// Find types
|
||||
for( WSMethodDef method : ws.getMethods() ){
|
||||
if(!method.getOutputs().isEmpty()){
|
||||
for( WSParameterDef param : method.getOutputs() ){
|
||||
Class<?> paramClass = param.getParamClass();
|
||||
Class<?> valueClass = getClass(paramClass);
|
||||
// is an array? or special class
|
||||
if( paramClass.isArray() || WSReturnObject.class.isAssignableFrom(valueClass)){
|
||||
// add to type generation list
|
||||
if(!types.contains( paramClass ))
|
||||
types.add( paramClass );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// definitions -> types
|
||||
Element typeE = definitions.addElement("wsdl:types");
|
||||
Element schema = typeE.addElement("xsd:schema");
|
||||
schema.addAttribute("targetNamespace", ws.getNamespace()+"?type");
|
||||
|
||||
// empty type
|
||||
Element empty = schema.addElement("xsd:complexType");
|
||||
empty.addAttribute("name", "empty");
|
||||
empty.addElement("xsd:sequence");
|
||||
|
||||
for(int n=0; n<types.size() ;n++){
|
||||
Class<?> c = types.get(n);
|
||||
// Generate Array type
|
||||
if(c.isArray()){
|
||||
Class<?> ctmp = getClass(c);
|
||||
|
||||
Element type = schema.addElement("xsd:complexType");
|
||||
type.addAttribute("name", getArrayClassName(c));
|
||||
|
||||
Element sequence = type.addElement("xsd:sequence");
|
||||
|
||||
Element element = sequence.addElement("xsd:element");
|
||||
element.addAttribute("minOccurs", "0");
|
||||
element.addAttribute("maxOccurs", "unbounded");
|
||||
element.addAttribute("name", "element");
|
||||
element.addAttribute("nillable", "true");
|
||||
if( WSReturnObject.class.isAssignableFrom(ctmp) )
|
||||
element.addAttribute("type", "tns:"+getClassName(c).replace("[]", ""));
|
||||
else
|
||||
element.addAttribute("type", "xsd:"+getClassName(c).replace("[]", ""));
|
||||
|
||||
if(!types.contains(ctmp))
|
||||
types.add(ctmp);
|
||||
}
|
||||
// Generate SOAPObject type
|
||||
else if(WSReturnObject.class.isAssignableFrom(c)){
|
||||
Element type = schema.addElement("xsd:complexType");
|
||||
type.addAttribute("name", getClassName(c));
|
||||
|
||||
Element sequence = type.addElement("xsd:sequence");
|
||||
|
||||
Field[] fields = c.getFields();
|
||||
for(int i=0; i<fields.length ;i++){
|
||||
WSValueName tmp = fields[i].getAnnotation( WSValueName.class );
|
||||
String name;
|
||||
if(tmp != null) name = tmp.value();
|
||||
else name = "field"+i;
|
||||
|
||||
Element element = sequence.addElement("xsd:element");
|
||||
element.addAttribute("name", name);
|
||||
|
||||
// Check if the object is an SOAPObject
|
||||
Class<?> cTmp = getClass(fields[i].getType());
|
||||
if( WSReturnObject.class.isAssignableFrom(cTmp) ){
|
||||
element.addAttribute("type", "tns:"+getClassName(cTmp));
|
||||
if(!types.contains(cTmp))
|
||||
types.add(cTmp);
|
||||
}
|
||||
else{
|
||||
element.addAttribute("type", "xsd:"+getClassName(fields[i].getType()));
|
||||
}
|
||||
// Is the Field optional
|
||||
if(tmp != null && tmp.optional())
|
||||
element.addAttribute("minOccurs", "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// TODO: FIX THESE ARE DUPLICATES FROM SOAPHttpPage
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private Class<?> getClass(Class<?> c){
|
||||
if(c!=null && c.isArray()){
|
||||
return getClass(c.getComponentType());
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private String getArrayClassName(Class<?> c){
|
||||
return "ArrayOf"+getClassName(c).replaceAll("[\\[\\]]", "");
|
||||
}
|
||||
|
||||
private String getClassName(Class<?> c){
|
||||
Class<?> cTmp = getClass(c);
|
||||
if( byte[].class.isAssignableFrom(c) ){
|
||||
return "base64Binary";
|
||||
}
|
||||
else if( WSReturnObject.class.isAssignableFrom(cTmp) ){
|
||||
return c.getSimpleName();
|
||||
}
|
||||
else{
|
||||
String ret = c.getSimpleName().toLowerCase();
|
||||
|
||||
if( cTmp == Integer.class ) ret = ret.replaceAll("integer", "int");
|
||||
else if( cTmp == Character.class ) ret = ret.replaceAll("character", "char");
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {}
|
||||
|
||||
}
|
||||
|
|
@ -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