Introduction of OpenAPI documentation for WebServices, including some refactorings

This commit is contained in:
Ziver Koc 2021-02-28 18:58:01 +01:00
parent 77e4bce99b
commit 5622a3b489
16 changed files with 519 additions and 267 deletions

View file

@ -183,4 +183,15 @@ public class ClassUtil {
} }
return null; return null;
} }
/**
* @param c a array class
* @return the base class the array is based on, if the input is not an array then the input is returned.
*/
public static Class<?> getArrayClass(Class<?> c) {
if (c != null && c.isArray()) {
return getArrayClass(c.getComponentType());
}
return c;
}
} }

View file

@ -24,16 +24,17 @@
package zutil.net.upnp.service; package zutil.net.upnp.service;
import zutil.net.ws.WSInterface.WSParamName;
import zutil.net.ws.WSReturnObject; import zutil.net.ws.WSReturnObject;
public class BrowseRetObj extends WSReturnObject{ public class BrowseRetObj extends WSReturnObject{
@WSValueName("Result") @WSParamName("Result")
public String Result; public String Result;
@WSValueName("NumberReturned") @WSParamName("NumberReturned")
public int NumberReturned; public int NumberReturned;
@WSValueName("TotalMatches") @WSParamName("TotalMatches")
public int TotalMatches; public int TotalMatches;
@WSValueName("UpdateID") @WSParamName("UpdateID")
public int UpdateID; public int UpdateID;
} }

View file

@ -141,13 +141,13 @@ public class UPnPContentDirectory implements UPnPService, HttpPage, WSInterface
return ret; return ret;
} }
public class BrowseRetObj extends WSReturnObject{ public class BrowseRetObj extends WSReturnObject{
@WSValueName("Result") @WSParamName("Result")
public String Result; public String Result;
@WSValueName("NumberReturned") @WSParamName("NumberReturned")
public int NumberReturned; public int NumberReturned;
@WSValueName("TotalMatches") @WSParamName("TotalMatches")
public int TotalMatches; public int TotalMatches;
@WSValueName("UpdateID") @WSParamName("UpdateID")
public int UpdateID; public int UpdateID;
} }

View file

@ -67,10 +67,11 @@ import java.lang.annotation.Target;
public interface WSInterface { public interface WSInterface {
enum RequestType { enum RequestType {
HTTP_GET, GET,
HTTP_POST, POST,
HTTP_PUT, PUT,
HTTP_DELETE DELETE,
PATCH
} }
@ -79,7 +80,7 @@ public interface WSInterface {
* in an method. * in an method.
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER) @Target({ElementType.PARAMETER, ElementType.FIELD})
@interface WSParamName { @interface WSParamName {
String value(); String value();
boolean optional() default false; boolean optional() default false;

View file

@ -147,13 +147,17 @@ public class WSMethodDef {
for (Field field : fields) { for (Field field : fields) {
WSParameterDef ret_param = new WSParameterDef(this); WSParameterDef ret_param = new WSParameterDef(this);
WSReturnObject.WSValueName retValName = field.getAnnotation(WSReturnObject.WSValueName.class);
if (retValName != null) WSInterface.WSParamName paramNameAnnotation = field.getAnnotation(WSInterface.WSParamName.class);
ret_param.setName(retValName.value()); if (paramNameAnnotation != null)
ret_param.setName(paramNameAnnotation.value());
else else
ret_param.setName(field.getName()); ret_param.setName(field.getName());
WSInterface.WSDocumentation documentationAnnotation = field.getAnnotation(WSInterface.WSDocumentation.class);
if (documentationAnnotation != null)
ret_param.setDocumentation(documentationAnnotation.value());
ret_param.setParamClass(field.getType()); ret_param.setParamClass(field.getType());
outputs.add(ret_param); outputs.add(ret_param);
} }
@ -178,13 +182,15 @@ public class WSMethodDef {
// Specific request type was not provided, try to figure it out by the method name // Specific request type was not provided, try to figure it out by the method name
if (name.startsWith("get")) if (name.startsWith("get"))
this.requestType = WSInterface.RequestType.HTTP_GET; this.requestType = WSInterface.RequestType.GET;
if (name.startsWith("post")) if (name.startsWith("post"))
this.requestType = WSInterface.RequestType.HTTP_POST; this.requestType = WSInterface.RequestType.POST;
if (name.startsWith("put")) if (name.startsWith("put"))
this.requestType = WSInterface.RequestType.HTTP_PUT; this.requestType = WSInterface.RequestType.PUT;
if (name.startsWith("delete")) if (name.startsWith("delete"))
this.requestType = WSInterface.RequestType.HTTP_DELETE; this.requestType = WSInterface.RequestType.DELETE;
if (name.startsWith("patch"))
this.requestType = WSInterface.RequestType.PATCH;
} }
// Handle endpoint path // Handle endpoint path

View file

@ -29,7 +29,7 @@ package zutil.net.ws;
* *
* @author Ziver * @author Ziver
*/ */
public class WSParameterDef{ public class WSParameterDef {
/** The parent method **/ /** The parent method **/
private WSMethodDef mDef; private WSMethodDef mDef;
/** The class type of the parameter **/ /** The class type of the parameter **/
@ -37,13 +37,12 @@ public class WSParameterDef{
/** The web service name of the parameter **/ /** The web service name of the parameter **/
private String name; private String name;
/** Developer documentation **/ /** Developer documentation **/
private String doc; private String documentation;
/** If this parameter is optional **/ /** If this parameter is optional **/
private boolean optional; private boolean optional;
/** Is it an header parameter **/
//boolean header;
protected WSParameterDef( WSMethodDef mDef ){
protected WSParameterDef(WSMethodDef mDef){
this.mDef = mDef; this.mDef = mDef;
this.optional = false; this.optional = false;
} }
@ -63,11 +62,11 @@ public class WSParameterDef{
this.name = name; this.name = name;
} }
public String getDoc() { public String getDocumentation() {
return doc; return documentation;
} }
protected void setDoc(String doc) { protected void setDocumentation(String documentation) {
this.doc = doc; this.documentation = documentation;
} }
public boolean isOptional() { public boolean isOptional() {

View file

@ -32,15 +32,16 @@ import java.lang.reflect.Field;
/** /**
* This class is used as an return Object for a web service. * This class is used as an return Object for a web service.
* If an class implements this interface then it can return * If a class implements this interface then it implies that multiple
* multiple values through the WSInterface. And the * parameters can be returned through the WSInterface. And the
* implementing class will be transparent. Example: * implementing class will be transparent to the requester. Example:
* *
* <pre> * <pre>
* private static class TestObject implements WSReturnObject{ * private static class TestObject implements WSReturnObject{
* &#64;WSValueName("name") * &#64;WSParamName("name")
* public String name; * public String name;
* &#64;WSValueName("lastname") * &#64;WSParamName("lastname")
* &#64;WSDocumentation("The users last name")
* public String lastname; * public String lastname;
* *
* public TestObject(String n, String l){ * public TestObject(String n, String l){
@ -53,31 +54,7 @@ import java.lang.reflect.Field;
* @author Ziver * @author Ziver
* *
*/ */
public class WSReturnObject{ public abstract class WSReturnObject{
/**
* Method comments for the WSDL.
* These comments are put in the operation part of the WSDL
*
* @author Ziver
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface WSDLDocumentation{
String value();
}
/**
* Annotation that assigns a name to the return value
* to the field.
*
* @author Ziver
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface WSValueName {
String value();
boolean optional() default false;
}
public Object getValue(Field field) throws IllegalArgumentException, IllegalAccessException{ public Object getValue(Field field) throws IllegalArgumentException, IllegalAccessException{
return field.get(this); return field.get(this);

View file

@ -36,26 +36,33 @@ import java.util.Set;
* @author Ziver * @author Ziver
*/ */
public class WebServiceDef { public class WebServiceDef {
/** A map of methods in this Service **/ /** This is the WSInterface class **/
private HashMap<String,WSMethodDef> methods; private Class<? extends WSInterface> intf;
/** Namespace of the service **/ /** Namespace of the service **/
private String namespace; private String namespace;
/** Name of the web service **/ /** Name of the web service **/
private String name; private String name;
/** This is the WSInterface class **/ /** Human readable description of the service **/
private Class<? extends WSInterface> intf; private String documentation = "";
/** A map of methods in this Service **/
private HashMap<String,WSMethodDef> methods = new HashMap<>();
public WebServiceDef(Class<? extends WSInterface> intf){ public WebServiceDef(Class<? extends WSInterface> intf){
this.intf = intf; this.intf = intf;
methods = new HashMap<>();
name = intf.getSimpleName(); name = intf.getSimpleName();
if (intf.getAnnotation( WSInterface.WSNamespace.class) != null) WSInterface.WSNamespace namespaceAnnotation = intf.getAnnotation(WSInterface.WSNamespace.class);
this.namespace = intf.getAnnotation(WSInterface.WSNamespace.class).value(); if (namespaceAnnotation != null)
this.namespace = namespaceAnnotation.value();
WSInterface.WSDocumentation documentationAnnotation = intf.getAnnotation(WSInterface.WSDocumentation.class);
if (documentationAnnotation != null)
this.documentation = documentationAnnotation.value();
for(Method m : intf.getDeclaredMethods()){ for(Method m : intf.getDeclaredMethods()){
// check for public methods // Check for public methods
if ((m.getModifiers() & Modifier.PUBLIC) > 0 && if ((m.getModifiers() & Modifier.PUBLIC) > 0 &&
!m.isAnnotationPresent(WSInterface.WSIgnore.class)){ !m.isAnnotationPresent(WSInterface.WSIgnore.class)){
WSMethodDef method = new WSMethodDef(this, m); WSMethodDef method = new WSMethodDef(this, m);
@ -78,6 +85,13 @@ public class WebServiceDef {
return name; return name;
} }
/**
* @return a human readable description of the service, or a empty String if no documentation has been provided.
*/
public String getDocumentation(){
return documentation;
}
/** /**
* @param name is the name of the method * @param name is the name of the method
* @return if there is a method by the given name * @return if there is a method by the given name

View file

@ -0,0 +1,90 @@
/*
* 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.openapi;
import zutil.net.http.HttpHeader;
import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream;
import zutil.net.ws.WebServiceDef;
import zutil.net.ws.wsdl.WSDLWriter;
import java.io.IOException;
import java.util.Map;
/**
* User: Ziver
*/
public class OpenAPIHttpPage implements HttpPage {
/**
* The WSDL document
**/
private WSDLWriter wsdl;
public OpenAPIHttpPage(WebServiceDef wsDef) {
wsdl = new WSDLWriter(wsDef);
}
public void respond(HttpPrintStream out,
HttpHeader headers,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
if (request.containsKey("json")) {
out.setHeader(HttpHeader.HEADER_CONTENT_TYPE, "application/json");
wsdl.write(out);
} else {
// Output human readable interface
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println(" <title>OpenAPI Documentation</title>");
out.println();
out.println(" <link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/swagger-ui-dist@3.17.0/swagger-ui.css\">");
out.println(" <script src=\"https://unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js\"></script>");
out.println();
out.println(" <script>");
out.println(" function render() {");
out.println(" var ui = SwaggerUIBundle({");
out.println(" url: '" + headers.getRequestURL() + "?json',");
out.println(" dom_id: '#swagger-ui',");
out.println(" presets: [");
out.println(" SwaggerUIBundle.presets.apis,");
out.println(" SwaggerUIBundle.SwaggerUIStandalonePreset");
out.println(" ]");
out.println(" });");
out.println(" }");
out.println(" </script>");
out.println("</head>");
out.println("<body onload=\"render()\">");
out.println(" <div id=\"swagger-ui\"></div>");
out.println("</body>");
out.println("</html>");
}
}
}

View file

@ -0,0 +1,164 @@
package zutil.net.ws.openapi;
import zutil.log.LogUtil;
import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSParameterDef;
import zutil.net.ws.WebServiceDef;
import zutil.parser.DataNode;
import zutil.parser.json.JSONWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
/**
* A OpenAPI specification generator class.
*
* @see <a href="https://swagger.io/specification/">OpenAPI Specification</a>
*/
public class OpenAPIWriter {
private static final Logger logger = LogUtil.getLogger();
private static final String OPENAPI_VERSION = "3.0.1";
/** Current Web service definition **/
private WebServiceDef ws;
/** Current Web service definition **/
private List<ServerData> servers = new ArrayList<>();
/** Cache of generated WSDL **/
private String cache;
public OpenAPIWriter(WebServiceDef ws) {
this.ws = ws;
}
public void addServer(String url, String description){
servers.add(new ServerData(url, description));
cache = null;
}
public void write(Writer out) throws IOException {
out.write(write());
}
public void write(PrintStream out) {
out.print(write());
}
public void write(OutputStream out) throws IOException {
out.write(write().getBytes());
}
public String write() {
if (cache == null) {
DataNode root = new DataNode(DataNode.DataType.Map);
root.set("openapi", OPENAPI_VERSION);
root.set("info", generateInfo());
root.set("servers", generateServers());
root.set("paths", generatePaths());
root.set("components", generateComponents());
this.cache = JSONWriter.toString(root);
}
return cache;
}
private DataNode generateInfo() {
DataNode infoRoot = new DataNode(DataNode.DataType.Map);
infoRoot.set("title", ws.getName());
infoRoot.set("description", ws.getDocumentation());
// Not implemented properties
// "termsOfService": xxx,
// "contact": {"name": xxx,"url": xxx,"email": xxx},
// "license": {"name": xxx, "url": xxx},
// "version": xxx
return infoRoot;
}
private DataNode generateServers() {
DataNode serversRoot = new DataNode(DataNode.DataType.List);
for (ServerData data : servers) {
DataNode serverNode = new DataNode(DataNode.DataType.Map);
serverNode.set("url", data.url);
serverNode.set("description", data.description);
serversRoot.add(serverNode);
}
return serversRoot;
}
private DataNode generatePaths() {
DataNode pathsRoot = new DataNode(DataNode.DataType.Map);
for (WSMethodDef methodDef : ws.getMethods()) {
DataNode pathNode = new DataNode(DataNode.DataType.Map);
DataNode typeNode = new DataNode(DataNode.DataType.Map);
typeNode.set("description", methodDef.getDocumentation());
pathNode.set(methodDef.getRequestType().toString().toLowerCase(), typeNode);
// --------------------------------------------
// Inputs
// --------------------------------------------
DataNode parameterNode = new DataNode(DataNode.DataType.Map);
for (WSParameterDef parameterDef : methodDef.getInputs()) {
parameterNode.set("name", parameterDef.getName());
parameterNode.set("description", parameterDef.getDocumentation());
parameterNode.set("in", "query");
parameterNode.set("required", parameterDef.isOptional());
parameterNode.set("schema", "");
}
typeNode.set("parameters", parameterNode);
// --------------------------------------------
// Outputs
// --------------------------------------------
DataNode responseNode = new DataNode(DataNode.DataType.Map);
for (WSParameterDef parameterDef : methodDef.getOutputs()) {
parameterNode.set("name", parameterDef.getName());
parameterNode.set("description", parameterDef.getDocumentation());
parameterNode.set("in", "query");
parameterNode.set("required", parameterDef.isOptional());
parameterNode.set("schema", "");
}
typeNode.set("responses", responseNode);
}
return pathsRoot;
}
private DataNode generateComponents() {
DataNode componentsRoot = new DataNode(DataNode.DataType.Map);
DataNode schemasNode = new DataNode(DataNode.DataType.Map);
componentsRoot.set("schemas", schemasNode);
// Generate schemas
return componentsRoot;
}
protected static class ServerData {
String url;
String description;
protected ServerData(String url, String description) {
this.url = url;
this.description = description;
}
}
}

View file

@ -28,23 +28,17 @@ import zutil.io.IOUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.http.HttpClient; import zutil.net.http.HttpClient;
import zutil.net.http.HttpHeader; import zutil.net.http.HttpHeader;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpURL; import zutil.net.http.HttpURL;
import zutil.net.ws.WSInterface;
import zutil.net.ws.WSMethodDef; 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.parser.DataNode; import zutil.parser.DataNode;
import zutil.parser.json.JSONParser; 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.URL; import java.net.URL;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -80,10 +74,10 @@ public class RESTClientInvocationHandler implements InvocationHandler {
String requestType = "GET"; String requestType = "GET";
switch (methodDef.getRequestType()) { switch (methodDef.getRequestType()) {
case HTTP_GET: requestType = "GET"; break; case GET: requestType = "GET"; break;
case HTTP_PUT: requestType = "PUT"; break; case PUT: requestType = "PUT"; break;
case HTTP_POST: requestType = "POST"; break; case POST: requestType = "POST"; break;
case HTTP_DELETE: requestType = "DELETE"; break; case DELETE: requestType = "DELETE"; break;
} }
// Send request // Send request

View file

@ -31,13 +31,13 @@ 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 org.xml.sax.SAXException; import org.xml.sax.SAXException;
import zutil.ClassUtil;
import zutil.converter.Converter; import zutil.converter.Converter;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.http.HttpHeader; import zutil.net.http.HttpHeader;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.ws.*; import zutil.net.ws.*;
import zutil.net.ws.WSReturnObject.WSValueName;
import zutil.parser.Base64Encoder; import zutil.parser.Base64Encoder;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -127,10 +127,12 @@ public class SOAPHttpPage implements HttpPage{
// Read http body // Read http body
StringBuilder data = null; StringBuilder data = null;
String contentType = headers.getHeader("Content-Type"); String contentType = headers.getHeader("Content-Type");
if (contentType != null && if (contentType != null &&
(contentType.contains("application/soap+xml") || (contentType.contains("application/soap+xml") ||
contentType.contains("text/xml") || contentType.contains("text/xml") ||
contentType.contains("text/plain"))) { contentType.contains("text/plain"))) {
int post_data_length = Integer.parseInt(headers.getHeader("Content-Length")); int post_data_length = Integer.parseInt(headers.getHeader("Content-Length"));
BufferedReader in = new BufferedReader(new InputStreamReader(headers.getInputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(headers.getInputStream()));
data = new StringBuilder(post_data_length); data = new StringBuilder(post_data_length);
@ -140,25 +142,24 @@ public class SOAPHttpPage implements HttpPage{
} }
// Response // Response
out.setHeader("Content-Type", "text/xml"); out.setHeader(HttpHeader.HEADER_CONTENT_TYPE, "text/xml");
out.flush(); out.flush();
WSInterface obj; WSInterface obj;
if (session_enabled) { if (session_enabled) {
if ( session.containsKey("SOAPInterface")) if (session.containsKey("SOAPInterface"))
obj = (WSInterface)session.get("SOAPInterface"); obj = (WSInterface)session.get("SOAPInterface");
else { else {
obj = wsDef.newInstance(); obj = wsDef.newInstance();
session.put("SOAPInterface", obj); session.put("SOAPInterface", obj);
} }
} } else {
else {
if (ws == null) if (ws == null)
ws = wsDef.newInstance(); ws = wsDef.newInstance();
obj = ws; obj = ws;
} }
Document document = genSOAPResponse( (data!=null ? data.toString() : ""), obj); Document document = genSOAPResponse((data!=null ? data.toString() : ""), obj);
OutputFormat format = OutputFormat.createPrettyPrint(); OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( out, format ); XMLWriter writer = new XMLWriter( out, format );
@ -187,11 +188,11 @@ public class SOAPHttpPage implements HttpPage{
public Document genSOAPResponse(String xml) { public Document genSOAPResponse(String xml) {
try { try {
WSInterface obj; WSInterface obj;
if ( ws == null ) if (ws == null)
ws = wsDef.newInstance(); ws = wsDef.newInstance();
obj = ws; obj = ws;
return genSOAPResponse(xml, obj ); return genSOAPResponse(xml, obj);
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.WARNING, "Exception in SOAP generation", e); logger.log(Level.WARNING, "Exception in SOAP generation", e);
} }
@ -291,11 +292,11 @@ public class SOAPHttpPage implements HttpPage{
if (outputParamDefs.size() > 0) { if (outputParamDefs.size() > 0) {
Element response = responseRoot.addElement(""); Element response = responseRoot.addElement("");
response.addNamespace("m", methodDef.getNamespace() ); response.addNamespace("m", methodDef.getNamespace() );
response.setName("m:"+methodDef.getName()+"Response"); response.setName("m:" + methodDef.getName() + "Response");
if (outputParams instanceof WSReturnObject) { if (outputParams instanceof WSReturnObject) {
Field[] f = outputParams.getClass().getFields(); Field[] f = outputParams.getClass().getFields();
for(int i=0; i<outputParamDefs.size() ;i++) { for(int i=0; i<outputParamDefs.size(); i++) {
WSParameterDef param = outputParamDefs.get(i); WSParameterDef param = outputParamDefs.get(i);
generateSOAPXMLForObj(response,((WSReturnObject)outputParams).getValue(f[i]) , param.getName()); generateSOAPXMLForObj(response,((WSReturnObject)outputParams).getValue(f[i]) , param.getName());
} }
@ -312,9 +313,6 @@ public class SOAPHttpPage implements HttpPage{
} }
/** /**
* Generates a XML Element for a given Object. This method can * Generates a XML Element for a given Object. This method can
* handle return values as XML Elements, WSReturnObject and * handle return values as XML Elements, WSReturnObject and
@ -337,61 +335,58 @@ public class SOAPHttpPage implements HttpPage{
} }
// Return an array // Return an array
else if (obj.getClass().isArray()) { else if (obj.getClass().isArray()) {
Element array = root.addElement( (elementName.equals("element") ? "Array" : elementName) ); Element array = root.addElement((elementName.equals("element") ? "Array" : elementName));
String arrayType = "xsd:"+ getSOAPClassName(obj.getClass()); String arrayType = "xsd:" + getSOAPClassName(obj.getClass());
arrayType = arrayType.replaceFirst("\\[\\]", "["+Array.getLength(obj)+"]"); arrayType = arrayType.replaceFirst("\\[\\]", "[" + Array.getLength(obj) + "]");
array.addAttribute("type", "soap:Array"); array.addAttribute("type", "soap:Array");
array.addAttribute("soap:arrayType", arrayType); array.addAttribute("soap:arrayType", "xsd:" + arrayType);
for(int i=0; i<Array.getLength(obj) ;i++) { for(int i=0; i<Array.getLength(obj) ;i++) {
generateSOAPXMLForObj(array, Array.get(obj, i), "element"); generateSOAPXMLForObj(array, Array.get(obj, i), "element");
} }
} }
else { else {
Element objectE = root.addElement( elementName ); Element objectE = root.addElement(elementName);
if (obj instanceof Element) if (obj instanceof Element)
objectE.add( (Element)obj ); objectE.add((Element) obj);
else if (obj instanceof WSReturnObject) { else if (obj instanceof WSReturnObject) {
Field[] fields = obj.getClass().getFields(); Field[] fields = obj.getClass().getFields();
for(int i=0; i<fields.length ;i++) { for(int i=0; i<fields.length; i++) {
WSValueName tmp = fields[i].getAnnotation( WSValueName.class ); WSInterface.WSParamName paramNameAnnotation = fields[i].getAnnotation(WSInterface.WSParamName.class);
String name; String name = (paramNameAnnotation != null ? paramNameAnnotation.value() : "field" + i);
if (tmp != null) name = tmp.value();
else name = "field"+i;
generateSOAPXMLForObj(objectE, fields[i].get(obj), name); generateSOAPXMLForObj(objectE, fields[i].get(obj), name);
} }
} }
else { else {
objectE.addAttribute("type", "xsd:"+ getSOAPClassName(obj.getClass())); objectE.addAttribute("type", "xsd:" + getSOAPClassName(obj.getClass()));
objectE.addText("" + obj); objectE.addText("" + obj);
} }
} }
} }
/**
protected static String getSOAPClassName(Class<?> c) { * Will generate a SOAP based class name from a given class.
Class<?> cTmp = getClass(c); *
* @param c
* @return a String name that can be used by a SOAP call.
*/
public static String getSOAPClassName(Class<?> c) {
Class<?> cTmp = ClassUtil.getArrayClass(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)
else if(cTmp == Character.class) ret = ret.replaceAll("character", "char"); ret = ret.replaceAll("integer", "int");
else if(cTmp == Character.class)
ret = ret.replaceAll("character", "char");
return ret; return ret;
} }
} }
protected static Class<?> getClass(Class<?> c) {
if (c!=null && c.isArray()) {
return getClass(c.getComponentType());
}
return c;
}
} }

View file

@ -39,16 +39,19 @@ public class WSDLHttpPage implements HttpPage {
/** The WSDL document **/ /** The WSDL document **/
private WSDLWriter wsdl; private WSDLWriter wsdl;
public WSDLHttpPage( WebServiceDef wsDef ){
wsdl = new WSDLWriter( wsDef ); public WSDLHttpPage(WebServiceDef wsDef) {
wsdl = new WSDLWriter(wsDef);
} }
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
HttpHeader headers, HttpHeader headers,
Map<String, Object> session, Map<String, Object> session,
Map<String, String> cookie, Map<String, String> cookie,
Map<String, String> request) throws IOException{ Map<String, String> request) throws IOException {
out.setHeader("Content-Type", "text/xml");
wsdl.write( out ); out.setHeader(HttpHeader.HEADER_CONTENT_TYPE, "text/xml");
wsdl.write(out);
} }
} }

View file

@ -36,10 +36,12 @@ 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;
} }

View file

@ -36,6 +36,7 @@ public class WSDLServiceSOAP extends WSDLService{
super(url); super(url);
} }
@Override @Override
public String getServiceType() { return "soap"; } public String getServiceType() { return "soap"; }
@ -57,7 +58,10 @@ public class WSDLServiceSOAP extends WSDLService{
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
@ -65,7 +69,10 @@ public class WSDLServiceSOAP extends WSDLService{
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");

View file

@ -29,52 +29,57 @@ 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.ClassUtil;
import zutil.io.StringOutputStream; import zutil.io.StringOutputStream;
import zutil.net.ws.WSMethodDef; import zutil.log.LogUtil;
import zutil.net.ws.WSParameterDef; import zutil.net.ws.*;
import zutil.net.ws.WSReturnObject; import zutil.net.ws.soap.SOAPHttpPage;
import zutil.net.ws.WSReturnObject.WSValueName;
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;
import java.util.logging.Level;
import java.util.logging.Logger;
public class WSDLWriter{ public class WSDLWriter {
/** Current Web service definition ***/ private static final Logger logger = LogUtil.getLogger();
/** Current Web service definition **/
private WebServiceDef ws; private WebServiceDef ws;
/** A list of services **/
private ArrayList<WSDLService> services = new ArrayList<>();
/** Cache of generated WSDL **/ /** Cache of generated WSDL **/
private String cache; private String cache;
/** A list of services **/
private ArrayList<WSDLService> services;
public WSDLWriter( WebServiceDef ws ){
this.services = new ArrayList<>(); public WSDLWriter(WebServiceDef ws) {
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(write());
}
public void write( PrintStream out ) {
out.print(generate());
}
public void write( OutputStream out ) throws IOException {
out.write(generate().getBytes() );
} }
public void write(PrintStream out) {
out.print(write());
}
private String generate(){ public void write(OutputStream out) throws IOException {
if(cache == null){ out.write(write().getBytes());
}
public String write() {
if (cache == null) {
try { try {
OutputFormat outformat = OutputFormat.createPrettyPrint(); OutputFormat outformat = OutputFormat.createPrettyPrint();
StringOutputStream out = new StringOutputStream(); StringOutputStream out = new StringOutputStream();
@ -87,13 +92,14 @@ public class WSDLWriter{
this.cache = out.toString(); this.cache = out.toString();
out.close(); out.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.log(Level.SEVERE, "Unable to generate WSDL specification.", e);
} }
} }
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/");
@ -101,7 +107,7 @@ public class WSDLWriter{
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);
@ -113,12 +119,13 @@ public class WSDLWriter{
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");
@ -128,6 +135,7 @@ public class WSDLWriter{
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");
@ -137,87 +145,98 @@ public class WSDLWriter{
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 // ------------------------------------------------
if(!method.getInputs().isEmpty()){ // Input
// ------------------------------------------------
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:" + SOAPHttpPage.getSOAPClassName(param.getParamClass()));
if( param.isOptional() ) if (param.isOptional())
part.addAttribute("minOccurs", "0"); part.addAttribute("minOccurs", "0");
} }
} }
//*************************** Output
if(!method.getOutputs().isEmpty()){ // ------------------------------------------------
// Output
// ------------------------------------------------
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 = ClassUtil.getArrayClass(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:" + SOAPHttpPage.getSOAPClassName(paramClass));
} } else {// its an Object
else{// its an Object part.addAttribute("type", "xsd:" + SOAPHttpPage.getSOAPClassName(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
if( method.getOutputs().size() > 0 ){ // Output
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
if( method.getOutputs().size() > 0 ){ // Fault
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");
@ -226,35 +245,35 @@ public class WSDLWriter{
} }
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());
} }
} }
@ -266,19 +285,19 @@ public class WSDLWriter{
* -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 = ClassUtil.getArrayClass(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);
} }
} }
} }
@ -287,18 +306,17 @@ public class WSDLWriter{
// 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 (Class<?> c : types) {
Class<?> c = types.get(n);
// Generate Array type // Generate Array type
if(c.isArray()){ if (c.isArray()) {
Class<?> ctmp = getClass(c); Class<?> ctmp = ClassUtil.getArrayClass(c);
Element type = schema.addElement("xsd:complexType"); Element type = schema.addElement("xsd:complexType");
type.addAttribute("name", getArrayClassName(c)); type.addAttribute("name", getArrayClassName(c));
@ -310,83 +328,53 @@ public class WSDLWriter{
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:" + SOAPHttpPage.getSOAPClassName(c).replace("[]", ""));
else else
element.addAttribute("type", "xsd:"+getClassName(c).replace("[]", "")); element.addAttribute("type", "xsd:" + SOAPHttpPage.getSOAPClassName(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", SOAPHttpPage.getSOAPClassName(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 ); WSInterface.WSParamName tmp = fields[i].getAnnotation(WSInterface.WSParamName.class);
String name; String name;
if(tmp != null) name = tmp.value(); if (tmp != null)
else name = "field"+i; name = tmp.value();
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 = ClassUtil.getArrayClass(fields[i].getType());
if( WSReturnObject.class.isAssignableFrom(cTmp) ){ if (WSReturnObject.class.isAssignableFrom(cTmp)) {
element.addAttribute("type", "tns:"+getClassName(cTmp)); element.addAttribute("type", "tns:" + SOAPHttpPage.getSOAPClassName(cTmp));
if(!types.contains(cTmp)) if (!types.contains(cTmp))
types.add(cTmp); types.add(cTmp);
} else {
element.addAttribute("type", "xsd:" + SOAPHttpPage.getSOAPClassName(fields[i].getType()));
} }
else{
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");
} }
} }
} }
} }
private String getArrayClassName(Class<?> c) {
/////////////////////////////////////////////////////////////////////////////////////////////// return "ArrayOf" + SOAPHttpPage.getSOAPClassName(c).replaceAll("[\\[\\]]", "");
// 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() {}
} }