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);
// Send request WSMethodDef methodDef = wsDef.getMethod(method.getName());
HttpClient request = new HttpClient(HttpClient.HttpRequestType.POST); HttpURL url = generateRESTRequest(methodDef, args);
request.setURL(url);
HttpHeader response = request.send();
String rspJson = IOUtil.readContentAsString(request.getResponseInputStream());
request.close();
// DEBUG String requestType = "GET";
if (logger.isLoggable(Level.FINEST)) { switch (methodDef.getRequestType()) {
System.out.println("********** Request"); case HTTP_GET: requestType = "GET"; break;
System.out.println(url); case HTTP_PUT: requestType = "PUT"; break;
System.out.println("********** Response"); case HTTP_POST: requestType = "POST"; break;
System.out.println(rspJson); 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) { 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

@ -1,52 +1,52 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Ziver Koc * Copyright (c) 2020 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * 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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* 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;
/** /**
* This interface defines a WSDL Service type. * This interface defines a WSDL Service type.
* *
* User: Ziver * User: Ziver
*/ */
public abstract class WSDLService { public abstract class WSDLService {
/** The URL of this service **/ /** The URL of this service **/
private String url; private String url;
public WSDLService(String url){ public WSDLService(String url){
this.url = url; this.url = url;
} }
public String getServiceAddress(){ public String getServiceAddress(){
return url; return url;
} }
public abstract String getServiceType(); public abstract String getServiceType();
public abstract void generateBinding(Element definitions); public abstract void generateBinding(Element definitions);
public abstract void generateOperation(Element definitions, WSMethodDef method); public abstract void generateOperation(Element definitions, WSMethodDef method);
} }

View file

@ -1,78 +1,78 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Ziver Koc * Copyright (c) 2020 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * 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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* 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;
/** /**
* User: Ziver * User: Ziver
*/ */
public class WSDLServiceSOAP extends WSDLService{ public class WSDLServiceSOAP extends WSDLService{
public WSDLServiceSOAP(String url){ public WSDLServiceSOAP(String url){
super(url); super(url);
} }
@Override @Override
public String getServiceType() { return "soap"; } public String getServiceType() { return "soap"; }
@Override @Override
public void generateBinding(Element definitions) { public void generateBinding(Element definitions) {
// definitions -> binding -> soap:binding // definitions -> binding -> soap:binding
Element soap_binding = definitions.addElement("soap:binding"); Element soap_binding = definitions.addElement("soap:binding");
soap_binding.addAttribute("style", "rpc"); soap_binding.addAttribute("style", "rpc");
soap_binding.addAttribute("transport", "http://schemas.xmlsoap.org/soap/http"); soap_binding.addAttribute("transport", "http://schemas.xmlsoap.org/soap/http");
} }
@Override @Override
public void generateOperation(Element definitions, WSMethodDef method) { public void generateOperation(Element definitions, WSMethodDef method) {
// definitions -> binding -> operation // definitions -> binding -> operation
Element operation = definitions.addElement("wsdl:operation"); Element operation = definitions.addElement("wsdl:operation");
operation.addAttribute("name", method.getName()); operation.addAttribute("name", method.getName());
// definitions -> binding -> operation -> soap:operation // definitions -> binding -> operation -> soap:operation
Element soap_operation = operation.addElement("soap:operation"); Element soap_operation = operation.addElement("soap:operation");
soap_operation.addAttribute("soapAction", method.getNamespace()); soap_operation.addAttribute("soapAction", method.getNamespace());
//*************************** Input //*************************** Input
// definitions -> binding -> operation -> input // definitions -> binding -> operation -> input
Element input = operation.addElement("wsdl:input"); Element input = operation.addElement("wsdl:input");
// definitions -> binding -> operation -> input -> body // definitions -> binding -> operation -> input -> body
Element input_body = input.addElement("soap:body"); Element input_body = input.addElement("soap:body");
input_body.addAttribute("use", "literal"); input_body.addAttribute("use", "literal");
input_body.addAttribute("namespace", method.getNamespace()); input_body.addAttribute("namespace", method.getNamespace());
//*************************** output //*************************** output
if(!method.getOutputs().isEmpty()){ if(!method.getOutputs().isEmpty()){
// definitions -> binding -> operation -> output // definitions -> binding -> operation -> output
Element output = operation.addElement("wsdl:output"); Element output = operation.addElement("wsdl:output");
// definitions -> binding -> operation -> input -> body // definitions -> binding -> operation -> input -> body
Element output_body = output.addElement("soap:body"); Element output_body = output.addElement("soap:body");
output_body.addAttribute("use", "literal"); output_body.addAttribute("use", "literal");
output_body.addAttribute("namespace", method.getNamespace()); output_body.addAttribute("namespace", method.getNamespace());
} }
} }
} }

View file

@ -1,392 +1,392 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Ziver Koc * Copyright (c) 2020 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * 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 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* 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;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.io.OutputFormat; import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter; import org.dom4j.io.XMLWriter;
import zutil.io.StringOutputStream; import zutil.io.StringOutputStream;
import zutil.net.ws.WSMethodDef; import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSParameterDef; import zutil.net.ws.WSParameterDef;
import zutil.net.ws.WSReturnObject; import zutil.net.ws.WSReturnObject;
import zutil.net.ws.WSReturnObject.WSValueName; import zutil.net.ws.WSReturnObject.WSValueName;
import zutil.net.ws.WebServiceDef; import zutil.net.ws.WebServiceDef;
import java.io.*; import java.io.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
public class WSDLWriter{ public class WSDLWriter{
/** Current Web service definition ***/ /** Current Web service definition ***/
private WebServiceDef ws; private WebServiceDef ws;
/** Cache of generated WSDL **/ /** Cache of generated WSDL **/
private String cache; private String cache;
/** A list of services **/ /** A list of services **/
private ArrayList<WSDLService> services; private ArrayList<WSDLService> services;
public WSDLWriter( WebServiceDef ws ){ public WSDLWriter( WebServiceDef ws ){
this.services = new ArrayList<>(); this.services = new ArrayList<>();
this.ws = ws; this.ws = ws;
} }
/** /**
* Add a service to be published with the WSDL * Add a service to be published with the WSDL
*/ */
public void addService(WSDLService serv){ public void addService(WSDLService serv){
cache = null; cache = null;
services.add(serv); services.add(serv);
} }
public void write( Writer out ) throws IOException { public void write( Writer out ) throws IOException {
out.write(generate()); out.write(generate());
} }
public void write( PrintStream out ) { public void write( PrintStream out ) {
out.print(generate()); out.print(generate());
} }
public void write( OutputStream out ) throws IOException { public void write( OutputStream out ) throws IOException {
out.write(generate().getBytes() ); out.write(generate().getBytes() );
} }
private String generate(){ private String generate(){
if(cache == null){ if(cache == null){
try { try {
OutputFormat outformat = OutputFormat.createPrettyPrint(); OutputFormat outformat = OutputFormat.createPrettyPrint();
StringOutputStream out = new StringOutputStream(); StringOutputStream out = new StringOutputStream();
XMLWriter writer = new XMLWriter(out, outformat); XMLWriter writer = new XMLWriter(out, outformat);
Document docroot = generateDefinition(); Document docroot = generateDefinition();
writer.write(docroot); writer.write(docroot);
writer.flush(); writer.flush();
this.cache = out.toString(); this.cache = out.toString();
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return cache; return cache;
} }
private Document generateDefinition(){ private Document generateDefinition(){
Document wsdl = DocumentHelper.createDocument(); Document wsdl = DocumentHelper.createDocument();
Element definitions = wsdl.addElement("wsdl:definitions"); Element definitions = wsdl.addElement("wsdl:definitions");
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/"); definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
definitions.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/"); definitions.addNamespace("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
definitions.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/"); definitions.addNamespace("http", "http://schemas.xmlsoap.org/wsdl/http/");
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
definitions.addNamespace("soap-enc", "http://schemas.xmlsoap.org/soap/encoding/"); definitions.addNamespace("soap-enc", "http://schemas.xmlsoap.org/soap/encoding/");
definitions.addNamespace("tns", ws.getNamespace()+"?type"); definitions.addNamespace("tns", ws.getNamespace()+"?type");
definitions.addAttribute("targetNamespace", ws.getNamespace()); definitions.addAttribute("targetNamespace", ws.getNamespace());
generateType(definitions); generateType(definitions);
generateMessages(definitions); generateMessages(definitions);
generatePortType(definitions); generatePortType(definitions);
generateBinding(definitions); generateBinding(definitions);
generateService(definitions); generateService(definitions);
return wsdl; return wsdl;
} }
private void generateMessages(Element definitions){ private void generateMessages(Element definitions){
for( WSMethodDef method : ws.getMethods() ){ for( WSMethodDef method : ws.getMethods() ){
generateMessage(definitions, method); generateMessage(definitions, method);
} }
// Default message used for functions without input parameters // Default message used for functions without input parameters
// definitions -> message: empty // definitions -> message: empty
Element empty = definitions.addElement("wsdl:message"); Element empty = definitions.addElement("wsdl:message");
empty.addAttribute("name", "empty"); empty.addAttribute("name", "empty");
// definitions -> message: empty -> part // definitions -> message: empty -> part
Element empty_part = empty.addElement("wsdl:part"); Element empty_part = empty.addElement("wsdl:part");
empty_part.addAttribute("name", "empty"); empty_part.addAttribute("name", "empty");
empty_part.addAttribute("type", "td:empty"); empty_part.addAttribute("type", "td:empty");
// Exception message // Exception message
// definitions -> message: exception // definitions -> message: exception
Element exception = definitions.addElement("wsdl:message"); Element exception = definitions.addElement("wsdl:message");
exception.addAttribute("name", "exception"); exception.addAttribute("name", "exception");
// definitions -> message: exception -> part // definitions -> message: exception -> part
Element exc_part = exception.addElement("wsdl:part"); Element exc_part = exception.addElement("wsdl:part");
exc_part.addAttribute("name", "exception"); exc_part.addAttribute("name", "exception");
exc_part.addAttribute("type", "td:string"); exc_part.addAttribute("type", "td:string");
} }
private void generateMessage(Element parent, WSMethodDef method){ private void generateMessage(Element parent, WSMethodDef method){
//*************************** Input //*************************** Input
if(!method.getInputs().isEmpty()){ if(!method.getInputs().isEmpty()){
// definitions -> message // definitions -> message
Element input = parent.addElement("wsdl:message"); Element input = parent.addElement("wsdl:message");
input.addAttribute("name", method.getName()+"Request"); input.addAttribute("name", method.getName()+"Request");
// Parameters // Parameters
for( WSParameterDef param : method.getInputs() ){ for( WSParameterDef param : method.getInputs() ){
// definitions -> message -> part // definitions -> message -> part
Element part = input.addElement("wsdl:part"); Element part = input.addElement("wsdl:part");
part.addAttribute("name", param.getName()); part.addAttribute("name", param.getName());
part.addAttribute("type", "xsd:"+getClassName( param.getParamClass())); part.addAttribute("type", "xsd:"+getClassName( param.getParamClass()));
if( param.isOptional() ) if( param.isOptional() )
part.addAttribute("minOccurs", "0"); part.addAttribute("minOccurs", "0");
} }
} }
//*************************** Output //*************************** Output
if(!method.getOutputs().isEmpty()){ if(!method.getOutputs().isEmpty()){
// definitions -> message // definitions -> message
Element output = parent.addElement("wsdl:message"); Element output = parent.addElement("wsdl:message");
output.addAttribute("name", method.getName()+"Response"); output.addAttribute("name", method.getName()+"Response");
// Parameters // Parameters
for( WSParameterDef param : method.getOutputs() ){ for( WSParameterDef param : method.getOutputs() ){
// definitions -> message -> part // definitions -> message -> part
Element part = output.addElement("wsdl:part"); Element part = output.addElement("wsdl:part");
part.addAttribute("name", param.getName()); part.addAttribute("name", param.getName());
Class<?> paramClass = param.getParamClass(); Class<?> paramClass = param.getParamClass();
Class<?> valueClass = getClass( paramClass ); Class<?> valueClass = getClass( paramClass );
// is an binary array // is an binary array
if(byte[].class.isAssignableFrom( paramClass )){ if(byte[].class.isAssignableFrom( paramClass )){
part.addAttribute("type", "xsd:base64Binary"); part.addAttribute("type", "xsd:base64Binary");
} }
// is an array? // is an array?
else if( paramClass.isArray()){ else if( paramClass.isArray()){
part.addAttribute("type", "td:" +getArrayClassName(paramClass)); part.addAttribute("type", "td:" +getArrayClassName(paramClass));
} }
else if( WSReturnObject.class.isAssignableFrom(valueClass) ){ else if( WSReturnObject.class.isAssignableFrom(valueClass) ){
// its an SOAPObject // its an SOAPObject
part.addAttribute("type", "td:"+getClassName( paramClass )); part.addAttribute("type", "td:"+getClassName( paramClass ));
} }
else{// its an Object else{// its an Object
part.addAttribute("type", "xsd:"+getClassName( paramClass )); part.addAttribute("type", "xsd:"+getClassName( paramClass ));
} }
} }
} }
} }
private void generatePortType(Element definitions){ private void generatePortType(Element definitions){
// definitions -> portType // definitions -> portType
Element portType = definitions.addElement("wsdl:portType"); Element portType = definitions.addElement("wsdl:portType");
portType.addAttribute("name", ws.getName()+"PortType"); portType.addAttribute("name", ws.getName()+"PortType");
for( WSMethodDef method : ws.getMethods() ){ for( WSMethodDef method : ws.getMethods() ){
// definitions -> portType -> operation // definitions -> portType -> operation
Element operation = portType.addElement("wsdl:operation"); Element operation = portType.addElement("wsdl:operation");
operation.addAttribute("name", method.getName()); operation.addAttribute("name", method.getName());
// Documentation // Documentation
if(method.getDocumentation() != null){ if(method.getDocumentation() != null){
Element doc = operation.addElement("wsdl:documentation"); Element doc = operation.addElement("wsdl:documentation");
doc.setText(method.getDocumentation()); doc.setText(method.getDocumentation());
} }
//*************************** Input //*************************** Input
if( method.getInputs().size() > 0 ){ if( method.getInputs().size() > 0 ){
// definitions -> message // definitions -> message
Element input = operation.addElement("wsdl:input"); Element input = operation.addElement("wsdl:input");
input.addAttribute("message", "tns:"+method.getName()+"Request"); input.addAttribute("message", "tns:"+method.getName()+"Request");
} }
//*************************** Output //*************************** Output
if( method.getOutputs().size() > 0 ){ if( method.getOutputs().size() > 0 ){
// definitions -> message // definitions -> message
Element output = operation.addElement("wsdl:output"); Element output = operation.addElement("wsdl:output");
output.addAttribute("message", "tns:"+method.getName()+"Response"); output.addAttribute("message", "tns:"+method.getName()+"Response");
} }
//*************************** Fault //*************************** Fault
if( method.getOutputs().size() > 0 ){ if( method.getOutputs().size() > 0 ){
// definitions -> message // definitions -> message
Element fault = operation.addElement("wsdl:fault"); Element fault = operation.addElement("wsdl:fault");
fault.addAttribute("message", "tns:exception"); fault.addAttribute("message", "tns:exception");
} }
} }
} }
private void generateBinding(Element definitions){ private void generateBinding(Element definitions){
// definitions -> binding // definitions -> binding
Element binding = definitions.addElement("wsdl:binding"); Element binding = definitions.addElement("wsdl:binding");
binding.addAttribute("name", ws.getName()+"Binding"); binding.addAttribute("name", ws.getName()+"Binding");
binding.addAttribute("type", "tns:"+ws.getName()+"PortType"); binding.addAttribute("type", "tns:"+ws.getName()+"PortType");
for(WSDLService serv : services){ for(WSDLService serv : services){
serv.generateBinding(binding); serv.generateBinding(binding);
for(WSMethodDef method : ws.getMethods()){ for(WSMethodDef method : ws.getMethods()){
serv.generateOperation(binding, method); serv.generateOperation(binding, method);
} }
} }
} }
private void generateService(Element parent){ private void generateService(Element parent){
// definitions -> service // definitions -> service
Element root = parent.addElement("wsdl:service"); Element root = parent.addElement("wsdl:service");
root.addAttribute("name", ws.getName()+"Service"); root.addAttribute("name", ws.getName()+"Service");
// definitions -> service -> port // definitions -> service -> port
Element port = root.addElement("wsdl:port"); Element port = root.addElement("wsdl:port");
port.addAttribute("name", ws.getName()+"Port"); port.addAttribute("name", ws.getName()+"Port");
port.addAttribute("binding", "tns:"+ws.getName()+"Binding"); port.addAttribute("binding", "tns:"+ws.getName()+"Binding");
for(WSDLService serv : services){ for(WSDLService serv : services){
// definitions -> service-> port -> address // definitions -> service-> port -> address
Element address = port.addElement(serv.getServiceType()+":address"); Element address = port.addElement(serv.getServiceType()+":address");
address.addAttribute("location", serv.getServiceAddress()); address.addAttribute("location", serv.getServiceAddress());
} }
} }
/** /**
* This function generates the Type section of the WSDL. * This function generates the Type section of the WSDL.
* <b><pre> * <b><pre>
* -wsdl:definitions * -wsdl:definitions
* -wsdl:type * -wsdl:type
* </pre></b> * </pre></b>
*/ */
private void generateType(Element definitions){ private void generateType(Element definitions){
ArrayList<Class<?>> types = new ArrayList<>(); ArrayList<Class<?>> types = new ArrayList<>();
// Find types // Find types
for( WSMethodDef method : ws.getMethods() ){ for( WSMethodDef method : ws.getMethods() ){
if(!method.getOutputs().isEmpty()){ if(!method.getOutputs().isEmpty()){
for( WSParameterDef param : method.getOutputs() ){ for( WSParameterDef param : method.getOutputs() ){
Class<?> paramClass = param.getParamClass(); Class<?> paramClass = param.getParamClass();
Class<?> valueClass = getClass(paramClass); Class<?> valueClass = getClass(paramClass);
// is an array? or special class // is an array? or special class
if( paramClass.isArray() || WSReturnObject.class.isAssignableFrom(valueClass)){ if( paramClass.isArray() || WSReturnObject.class.isAssignableFrom(valueClass)){
// add to type generation list // add to type generation list
if(!types.contains( paramClass )) if(!types.contains( paramClass ))
types.add( paramClass ); types.add( paramClass );
} }
} }
} }
} }
// definitions -> types // definitions -> types
Element typeE = definitions.addElement("wsdl:types"); Element typeE = definitions.addElement("wsdl:types");
Element schema = typeE.addElement("xsd:schema"); Element schema = typeE.addElement("xsd:schema");
schema.addAttribute("targetNamespace", ws.getNamespace()+"?type"); schema.addAttribute("targetNamespace", ws.getNamespace()+"?type");
// empty type // empty type
Element empty = schema.addElement("xsd:complexType"); Element empty = schema.addElement("xsd:complexType");
empty.addAttribute("name", "empty"); empty.addAttribute("name", "empty");
empty.addElement("xsd:sequence"); empty.addElement("xsd:sequence");
for(int n=0; n<types.size() ;n++){ for(int n=0; n<types.size() ;n++){
Class<?> c = types.get(n); Class<?> c = types.get(n);
// Generate Array type // Generate Array type
if(c.isArray()){ if(c.isArray()){
Class<?> ctmp = getClass(c); Class<?> ctmp = getClass(c);
Element type = schema.addElement("xsd:complexType"); Element type = schema.addElement("xsd:complexType");
type.addAttribute("name", getArrayClassName(c)); type.addAttribute("name", getArrayClassName(c));
Element sequence = type.addElement("xsd:sequence"); Element sequence = type.addElement("xsd:sequence");
Element element = sequence.addElement("xsd:element"); Element element = sequence.addElement("xsd:element");
element.addAttribute("minOccurs", "0"); element.addAttribute("minOccurs", "0");
element.addAttribute("maxOccurs", "unbounded"); element.addAttribute("maxOccurs", "unbounded");
element.addAttribute("name", "element"); element.addAttribute("name", "element");
element.addAttribute("nillable", "true"); element.addAttribute("nillable", "true");
if( WSReturnObject.class.isAssignableFrom(ctmp) ) if( WSReturnObject.class.isAssignableFrom(ctmp) )
element.addAttribute("type", "tns:"+getClassName(c).replace("[]", "")); element.addAttribute("type", "tns:"+getClassName(c).replace("[]", ""));
else else
element.addAttribute("type", "xsd:"+getClassName(c).replace("[]", "")); element.addAttribute("type", "xsd:"+getClassName(c).replace("[]", ""));
if(!types.contains(ctmp)) if(!types.contains(ctmp))
types.add(ctmp); types.add(ctmp);
} }
// Generate SOAPObject type // Generate SOAPObject type
else if(WSReturnObject.class.isAssignableFrom(c)){ else if(WSReturnObject.class.isAssignableFrom(c)){
Element type = schema.addElement("xsd:complexType"); Element type = schema.addElement("xsd:complexType");
type.addAttribute("name", getClassName(c)); type.addAttribute("name", getClassName(c));
Element sequence = type.addElement("xsd:sequence"); Element sequence = type.addElement("xsd:sequence");
Field[] fields = c.getFields(); Field[] fields = c.getFields();
for(int i=0; i<fields.length ;i++){ for(int i=0; i<fields.length ;i++){
WSValueName tmp = fields[i].getAnnotation( WSValueName.class ); WSValueName tmp = fields[i].getAnnotation( WSValueName.class );
String name; String name;
if(tmp != null) name = tmp.value(); if(tmp != null) name = tmp.value();
else name = "field"+i; else name = "field"+i;
Element element = sequence.addElement("xsd:element"); Element element = sequence.addElement("xsd:element");
element.addAttribute("name", name); element.addAttribute("name", name);
// Check if the object is an SOAPObject // Check if the object is an SOAPObject
Class<?> cTmp = getClass(fields[i].getType()); Class<?> cTmp = getClass(fields[i].getType());
if( WSReturnObject.class.isAssignableFrom(cTmp) ){ if( WSReturnObject.class.isAssignableFrom(cTmp) ){
element.addAttribute("type", "tns:"+getClassName(cTmp)); element.addAttribute("type", "tns:"+getClassName(cTmp));
if(!types.contains(cTmp)) if(!types.contains(cTmp))
types.add(cTmp); types.add(cTmp);
} }
else{ else{
element.addAttribute("type", "xsd:"+getClassName(fields[i].getType())); element.addAttribute("type", "xsd:"+getClassName(fields[i].getType()));
} }
// Is the Field optional // Is the Field optional
if(tmp != null && tmp.optional()) if(tmp != null && tmp.optional())
element.addAttribute("minOccurs", "0"); element.addAttribute("minOccurs", "0");
} }
} }
} }
} }
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
// TODO: FIX THESE ARE DUPLICATES FROM SOAPHttpPage // TODO: FIX THESE ARE DUPLICATES FROM SOAPHttpPage
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
private Class<?> getClass(Class<?> c){ private Class<?> getClass(Class<?> c){
if(c!=null && c.isArray()){ if(c!=null && c.isArray()){
return getClass(c.getComponentType()); return getClass(c.getComponentType());
} }
return c; return c;
} }
private String getArrayClassName(Class<?> c){ private String getArrayClassName(Class<?> c){
return "ArrayOf"+getClassName(c).replaceAll("[\\[\\]]", ""); return "ArrayOf"+getClassName(c).replaceAll("[\\[\\]]", "");
} }
private String getClassName(Class<?> c){ private String getClassName(Class<?> c){
Class<?> cTmp = getClass(c); Class<?> cTmp = getClass(c);
if( byte[].class.isAssignableFrom(c) ){ if( byte[].class.isAssignableFrom(c) ){
return "base64Binary"; return "base64Binary";
} }
else if( WSReturnObject.class.isAssignableFrom(cTmp) ){ else if( WSReturnObject.class.isAssignableFrom(cTmp) ){
return c.getSimpleName(); return c.getSimpleName();
} }
else{ else{
String ret = c.getSimpleName().toLowerCase(); String ret = c.getSimpleName().toLowerCase();
if( cTmp == Integer.class ) ret = ret.replaceAll("integer", "int"); if( cTmp == Integer.class ) ret = ret.replaceAll("integer", "int");
else if( cTmp == Character.class ) ret = ret.replaceAll("character", "char"); else if( cTmp == Character.class ) ret = ret.replaceAll("character", "char");
return ret; return ret;
} }
} }
public void close() {} public void close() {}
} }

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