Improved the Web service classes, needs to be tested

RESOLVED - #92 
http://bugs.koc.se/view.php?id=92
This commit is contained in:
Ziver Koc 2011-09-14 20:30:06 +00:00
parent 09b671bda7
commit a2b6be1f35
13 changed files with 401 additions and 817 deletions

View file

@ -55,6 +55,17 @@ public class StringOutputStream extends OutputStream{
buffer.append( new String(b, off, len) ); buffer.append( new String(b, off, len) );
} }
/**
* Same as {@link clear()}
*/
@Override
public void close() {
clear();
}
/**
* Clears the String buffer
*/
public void clear(){ public void clear(){
buffer = new StringBuilder(); buffer = new StringBuilder();
} }

View file

@ -21,44 +21,13 @@
******************************************************************************/ ******************************************************************************/
package zutil.net.http.soap; package zutil.net.http.soap;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.wsdl.Binding;
import javax.wsdl.BindingInput;
import javax.wsdl.BindingOperation;
import javax.wsdl.BindingOutput;
import javax.wsdl.Definition;
import javax.wsdl.Fault;
import javax.wsdl.Import;
import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.Output;
import javax.wsdl.Part;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.wsdl.extensions.soap.SOAPBinding;
import javax.wsdl.extensions.soap.SOAPBody;
import javax.wsdl.extensions.soap.SOAPHeader;
import javax.wsdl.extensions.soap.SOAPOperation;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLWriter;
import javax.xml.namespace.QName;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper; import org.dom4j.DocumentHelper;
@ -68,20 +37,16 @@ import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import zutil.converters.Converter; import zutil.converters.Converter;
import zutil.io.StringOutputStream;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.http.HttpPage; import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.ws.WSInterface; import zutil.net.ws.WSInterface;
import zutil.net.ws.WSObject; import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSReturnValueList; import zutil.net.ws.WSParameterDef;
import zutil.net.ws.WSInterface.WSDocumentation; import zutil.net.ws.WSReturnObject;
import zutil.net.ws.WSInterface.WSParamDocumentation; import zutil.net.ws.WSReturnObject.WSValueName;
import zutil.net.ws.WSObject.WSFieldName; import zutil.net.ws.WebServiceDef;
import zutil.parser.wsdl.WSDLWriter;
import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
import com.ibm.wsdl.extensions.soap.SOAPConstants;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
/** /**
* This is an HTTPPage for the HTTPServer that * This is an HTTPPage for the HTTPServer that
@ -118,135 +83,20 @@ import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
public class SOAPHttpPage implements HttpPage{ public class SOAPHttpPage implements HttpPage{
public static final Logger logger = LogUtil.getLogger(); public static final Logger logger = LogUtil.getLogger();
// valid methods for this soap page /** The object that the functions will be invoked from **/
private HashMap<String, MethodCache> methods; private WebServiceDef wsDef;
// contains an method and the names for the parameters /** This instance of the web service class is used if session is disabled **/
private class MethodCache{ private WSInterface ws;
String[] paramName; /** The WSDL document **/
boolean[] paramOptional; private WSDLWriter wsdl;
String[] returnName; /** Session enabled **/
Class<?>[] returnClass;
Method method;
boolean header;
MethodCache(Method m){
method = m;
paramName = new String[method.getParameterTypes().length];
paramOptional = new boolean[method.getParameterTypes().length];
header = false;
Class<?> tmp = m.getReturnType();
if( WSReturnValueList.class.isAssignableFrom( tmp )){
returnName = new String[ tmp.getFields().length ];
returnClass = new Class<?>[ tmp.getFields().length ];
}
else if( !tmp.isAssignableFrom( void.class )){
returnName = new String[1];
returnClass = new Class<?>[1];
}
else{
returnName = new String[0];
returnClass = new Class<?>[0];
}
}
}
// The object that the functions will be invoked from
private WSInterface interf;
// The WSDL document
private Definition wsdl;
// The WSDL Type part
private Document wsdlType;
// the URL to this soap page
private String url;
// Session enabled
private boolean session_enabled; private boolean session_enabled;
public SOAPHttpPage(String url, WSInterface interf) throws WSDLException{ public SOAPHttpPage( WebServiceDef wsDef ){
//if(!SOAPInterface.class.isAssignableFrom(interf) ) this.wsDef = wsDef;
// throw new ClassCastException("Class does not implement SOAPInterface!");
this.url = url;
this.interf = interf;
this.session_enabled = false; this.session_enabled = false;
methods = new HashMap<String, MethodCache>();
for(Method m : interf.getClass().getDeclaredMethods()){ wsdl = new WSDLWriter( wsDef );
// check for public methods
if((m.getModifiers() & Modifier.PUBLIC) > 0 &&
!m.isAnnotationPresent(WSInterface.WSDisabled.class)){
MethodCache chasch = new MethodCache(m);
StringBuffer tmp = new StringBuffer(m.getName()+"(");
// Get the parameter names
Annotation[][] paramAnnotation = m.getParameterAnnotations();
for(int i=0; i<paramAnnotation.length ;i++){
for(Annotation annotation : paramAnnotation[i]){
if(annotation instanceof WSInterface.WSParamName){
WSInterface.WSParamName paramName = (WSInterface.WSParamName) annotation;
chasch.paramName[i] = paramName.value();
chasch.paramOptional[i] = paramName.optional();
}
}
// if no name was found then use default
if(chasch.paramName[i] == null)
chasch.paramName[i] = "args"+i;
tmp.append(m.getParameterTypes()[i].getSimpleName()+" "+chasch.paramName[i]);
if( i<paramAnnotation.length-1 ) tmp.append(", ");
}
tmp.append(") => ");
// the return parameter name
WSInterface.WSReturnName returnName = m.getAnnotation(WSInterface.WSReturnName.class);
if( WSReturnValueList.class.isAssignableFrom( m.getReturnType() ) ){
Class<?> retClass = m.getReturnType();
for(int i=0; i<retClass.getFields().length ;i++){
if(i!=0) tmp.append(", ");
WSReturnValueList.WSValueName retValName = retClass.getFields()[i]
.getAnnotation( WSReturnValueList.WSValueName.class );
if(retValName != null) chasch.returnName[i] = retValName.value();
else chasch.returnName[i] = retClass.getFields()[i].getName();
chasch.returnClass[i] = retClass.getFields()[i].getType();
tmp.append(chasch.returnClass[i].getSimpleName()+" "+chasch.returnName[i]);
}
}
else if( chasch.returnName.length>0 ){
if(returnName != null) chasch.returnName[0] = returnName.value();
else chasch.returnName[0] = "return";
chasch.returnClass[0] = m.getReturnType();
tmp.append(chasch.returnClass[0].getSimpleName()+" "+chasch.returnName[0]);
}
// SOAP header?
if(m.getAnnotation(WSInterface.WSHeader.class) != null)
chasch.header = true;
// save in HashMap
logger.info("New SOAP Method Registered: "+tmp);
methods.put(m.getName(), chasch);
}
}
generateWSDL();
if(logger.isLoggable(Level.INFO)){
try {
// WSDL
StringOutputStream out = new StringOutputStream();
WSDLFactory factory = WSDLFactory.newInstance();
WSDLWriter writer = factory.newWSDLWriter();
writer.writeWSDL(wsdl, out);
logger.info(out.toString());
// WSDL Type
out.clear();
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter xmlWriter = new XMLWriter( out, format );
xmlWriter.write( wsdlType );
logger.info(out.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
} }
/** /**
@ -260,6 +110,14 @@ public class SOAPHttpPage implements HttpPage{
this.session_enabled = enabled; this.session_enabled = enabled;
} }
/**
* Sets the web service object to the specified one.
* Only used when session is disabled
*/
public void setObject(WSInterface obj) {
this.ws = obj;
}
public void respond(HttpPrintStream out, public void respond(HttpPrintStream out,
Map<String, String> client_info, Map<String, String> client_info,
@ -272,13 +130,7 @@ public class SOAPHttpPage implements HttpPage{
out.flush(); out.flush();
if(request.containsKey("wsdl")){ if(request.containsKey("wsdl")){
WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter(); wsdl.write( out );
writer.writeWSDL(wsdl, out);
}
else if(request.containsKey("type")){
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( out, format );
writer.write( wsdlType );
} }
else{ else{
WSInterface obj = null; WSInterface obj = null;
@ -286,12 +138,14 @@ public class SOAPHttpPage implements HttpPage{
if( session.containsKey("SOAPInterface")) if( session.containsKey("SOAPInterface"))
obj = (WSInterface)session.get("SOAPInterface"); obj = (WSInterface)session.get("SOAPInterface");
else{ else{
obj = interf.getClass().newInstance(); obj = wsDef.newInstance();
session.put("SOAPInterface", obj); session.put("SOAPInterface", obj);
} }
} }
else{ else{
obj = interf; if( ws == null )
ws = wsDef.newInstance();
obj = ws;
} }
Document document = genSOAPResponse( request.get(""), obj); Document document = genSOAPResponse( request.get(""), obj);
@ -302,12 +156,14 @@ public class SOAPHttpPage implements HttpPage{
// DEBUG // DEBUG
OutputFormat format2 = OutputFormat.createPrettyPrint(); if( logger.isLoggable(Level.FINEST) ){
System.err.println("********** Request"); OutputFormat format2 = OutputFormat.createPrettyPrint();
System.err.println(request); System.err.println("********** Request");
System.out.println("********** Response"); System.err.println(request);
writer = new XMLWriter( System.out, format2 ); System.out.println("********** Response");
writer.write( document ); writer = new XMLWriter( System.out, format2 );
writer.write( document );
}
} }
} catch (Exception e) { } catch (Exception e) {
@ -317,16 +173,18 @@ public class SOAPHttpPage implements HttpPage{
/** /**
* Generates a soap response for the given XML * Generates a soap response for the given XML
* @param xml is the XML request *
* @return a Document with the response * @param xml is the XML request
* @return a Document with the response
*/ */
public Document genSOAPResponse(String xml){ public Document genSOAPResponse(String xml){
try { try {
WSInterface o = null; WSInterface obj = null;
if(session_enabled) o = interf.getClass().newInstance(); if( ws == null )
else o = interf; ws = wsDef.newInstance();
obj = ws;
return genSOAPResponse(xml, o ); 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);
} }
@ -380,8 +238,8 @@ public class SOAPHttpPage implements HttpPage{
/** /**
* Converts an String XML to an Element * Converts an String XML to an Element
* *
* @param msg is the string XML * @param msg is the string XML
* @return the XML root Element * @return the XML root Element
*/ */
private Element getXMLRoot(String xml) throws Exception { private Element getXMLRoot(String xml) throws Exception {
if(xml != null && !xml.isEmpty()){ if(xml != null && !xml.isEmpty()){
@ -392,50 +250,44 @@ public class SOAPHttpPage implements HttpPage{
} }
/** /**
* Takes an XML Element and invokes all the * Takes an XML Element and invokes all the it's child elements as methods.
* Child Elements as methods.
* *
* @param obj is the object that the methods will be called from * @param obj is the object that the methods will be called from
* @param requestRoot is the Element where the children lies * @param requestRoot is the Element where the children lies
* @param responseRoot is the root element of the response * @param responseRoot is the root element of the response
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void prepareInvoke(WSInterface obj, Element requestRoot, Element responseRoot) throws Throwable{ private void prepareInvoke(WSInterface obj, Element requestRoot, Element responseRoot) throws Throwable{
Iterator<Element> it = requestRoot.elementIterator(); Iterator<Element> it = requestRoot.elementIterator();
while( it.hasNext() ){ while( it.hasNext() ){
Element e = it.next(); Element e = it.next();
if(methods.containsKey(e.getQName().getName())){ if( wsDef.hasMethod( e.getQName().getName()) ){
MethodCache m = methods.get(e.getQName().getName()); WSMethodDef m = wsDef.getMethod( e.getQName().getName() );
Object[] params = new Object[m.paramName.length]; Object[] params = new Object[ m.getInputCount() ];
// Get the parameter values // Get the parameter values
for(int i=0; i<m.paramName.length ;i++){ for(int i=0; i<m.getInputCount() ;i++){
if(e.element(m.paramName[i]) != null) WSParameterDef param = m.getInput( i );
if( e.element(param.getName()) != null )
params[i] = Converter.fromString( params[i] = Converter.fromString(
e.element(m.paramName[i]).getTextTrim(), e.element(param.getName()).getTextTrim(),
m.method.getParameterTypes()[i]); param.getParamClass());
i++;
} }
// MultiPrintStream.out.println("invoking: "+m.method.getName()+" "+MultiPrintStream.out.dumpToString(params));
// Invoke // Invoke
Object ret = invoke(obj, m.method, params); Object ret = m.invoke(obj, params);
// generate response XML // generate response XML
if( m.returnClass.length>0 ){ if( m.getOutputCount()>0 ){
WSInterface.WSNamespace namespace = m.method.getAnnotation(WSInterface.WSNamespace.class);
Element response = responseRoot.addElement(""); Element response = responseRoot.addElement("");
if( namespace != null ) response.addNamespace("m", m.getNamespace() );
response.addNamespace("m", namespace.value()); response.setName("m:"+m.getName()+"Response");
else
response.addNamespace("m", url+""+m.method.getName()); Field[] f = ret.getClass().getFields();
response.setName("m:"+m.method.getName()+"Response"); for(int i=0; i<m.getOutputCount() ;i++){
if( ret instanceof WSReturnValueList ){ WSParameterDef param = m.getOutput( i );
Field[] f = ret.getClass().getFields(); generateReturnXML(response,((WSReturnObject)ret).getValue(f[i]) , param.getName());
for(int i=0; i<m.returnName.length ;i++ ){
generateReturnXML(response,((WSReturnValueList)ret).getValue(f[i]) , m.returnName[i], m);
}
}
else{
generateReturnXML(response, ret, m.returnName[0], m);
} }
} }
} }
@ -445,36 +297,16 @@ public class SOAPHttpPage implements HttpPage{
} }
} }
/**
* Invokes a specified method
*
* @param m is the function
* @param params a vector with arguments
* @throws Throwable
*/
protected Object invoke(Object obj, Method m, Object[] params) throws Throwable{
try {
return m.invoke(obj, params );
} catch (IllegalArgumentException e) {
throw new SOAPException("Arguments missing for "+m.getName()+"!");
} catch (IllegalAccessException e) {
throw e;
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
/** /**
* Generates an return XML Element. This function can * Generates an return XML Element. This function can
* handle return values as XML Elements, SOAPObject and the * handle return values as XML Elements, SOAPObject and the
* Java basic data types. * Java basic data types.
* *
* @param root is the parent Element * @param root is the parent Element
* @param ret is the object that is the return value * @param ret is the object that is the return value
* @param ename is the name of the parent Element * @param ename is the name of the parent Element
* @param m is the method that returned the ret value
*/ */
private void generateReturnXML(Element root, Object ret, String ename, MethodCache m) throws IllegalArgumentException, IllegalAccessException{ private void generateReturnXML(Element root, Object ret, String ename) throws IllegalArgumentException, IllegalAccessException{
if(ret == null) return; if(ret == null) return;
if(byte[].class.isAssignableFrom(ret.getClass())){ if(byte[].class.isAssignableFrom(ret.getClass())){
Element valueE = root.addElement( ename ); Element valueE = root.addElement( ename );
@ -492,21 +324,21 @@ public class SOAPHttpPage implements HttpPage{
array.addAttribute("type", "soap:Array"); array.addAttribute("type", "soap:Array");
array.addAttribute("soap:arrayType", arrayType); array.addAttribute("soap:arrayType", arrayType);
for(int i=0; i<Array.getLength(ret) ;i++){ for(int i=0; i<Array.getLength(ret) ;i++){
generateReturnXML(array, Array.get(ret, i), "element", m); generateReturnXML(array, Array.get(ret, i), "element");
} }
} }
else{ else{
Element objectE = root.addElement( ename ); //getClassSOAPName(ret.getClass()) Element objectE = root.addElement( ename );
if(ret instanceof Element) if(ret instanceof Element)
objectE.add( (Element)ret ); objectE.add( (Element)ret );
else if(ret instanceof WSObject){ else if(ret instanceof WSReturnObject){
Field[] fields = ret.getClass().getFields(); Field[] fields = ret.getClass().getFields();
for(int i=0; i<fields.length ;i++){ for(int i=0; i<fields.length ;i++){
WSFieldName tmp = fields[i].getAnnotation(WSObject.WSFieldName.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;
generateReturnXML(objectE, fields[i].get(ret), name, m); generateReturnXML(objectE, fields[i].get(ret), name);
} }
} }
else { else {
@ -517,12 +349,12 @@ public class SOAPHttpPage implements HttpPage{
} }
private String getClassSOAPName(Class<?> c){ public static String getClassSOAPName(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( WSObject.class.isAssignableFrom(cTmp) ){ else if( WSReturnObject.class.isAssignableFrom(cTmp) ){
return c.getSimpleName(); return c.getSimpleName();
} }
else{ else{
@ -535,343 +367,7 @@ public class SOAPHttpPage implements HttpPage{
} }
} }
//******************************************************** public static Class<?> getClass(Class<?> c){
//********* WSDL Generation *************************
/**
* Generates an WSDL document for the class
*
* @throws WSDLException
*/
private void generateWSDL() throws WSDLException{
ArrayList<Class<?>> types = new ArrayList<Class<?>>();
String tns = url+"?wsdl";
String xsd = "http://www.w3.org/2001/XMLSchema";
String soap = "http://schemas.xmlsoap.org/wsdl/soap/";
String wsdln = "http://schemas.xmlsoap.org/wsdl/";
String td = url+"?type";
PopulatedExtensionRegistry extReg = new PopulatedExtensionRegistry();
WSDLFactory factory = WSDLFactory.newInstance();
String portTypeName = this.interf.getClass().getSimpleName()+"PortType";
wsdl = factory.newDefinition();
wsdl.setQName(new QName(tns, this.interf.getClass().getSimpleName()));
wsdl.setTargetNamespace(tns);
wsdl.addNamespace("tns", tns);
wsdl.addNamespace("xsd", xsd);
wsdl.addNamespace("soap", soap);
wsdl.addNamespace("wsdl", wsdln);
wsdl.addNamespace("td", td);
Message exception = wsdl.createMessage();
exception.setQName(new QName(tns, "exception"));
exception.setUndefined(true);
Part epart = wsdl.createPart();
epart.setName("message");
epart.setTypeName(new QName(xsd, "string"));
exception.addPart(epart);
wsdl.addMessage(exception);
Message empty = wsdl.createMessage();
empty.setQName(new QName(tns, "empty"));
empty.setUndefined(false);
epart = wsdl.createPart();
epart.setName("empty");
epart.setTypeName(new QName(td, "empty"));
empty.addPart(epart);
wsdl.addMessage(empty);
// Types import
Import imp = wsdl.createImport();
imp.setNamespaceURI(td);
imp.setLocationURI(td);
wsdl.addImport(imp);
// PortType
PortType portType = wsdl.createPortType();
portType.setQName(new QName(tns, portTypeName));
portType.setUndefined(false);
for(MethodCache m : methods.values()){
Operation operation = wsdl.createOperation();
//********* Request Messages
if(m.paramName.length > 0){
Message msgIn = wsdl.createMessage();
msgIn.setQName(new QName(tns, m.method.getName()+"Request"));
msgIn.setUndefined(false);
//***** Documentation
WSParamDocumentation tmpParamDoc = m.method.getAnnotation(WSInterface.WSParamDocumentation.class);
if(tmpParamDoc != null){
org.w3c.dom.Document xmldoc= new DocumentImpl();
org.w3c.dom.Element paramDoc = xmldoc.createElement("wsdl:documentation");
paramDoc.setTextContent(tmpParamDoc.value());
msgIn.setDocumentationElement(paramDoc);
}
// Parameters
for(int i=0; i<m.paramName.length ;i++){
// Parts
Part part = wsdl.createPart();
part.setName(m.paramName[i]);
part.setTypeName(new QName( xsd,
getClassSOAPName(m.method.getParameterTypes()[i])));
if(m.paramOptional[i])
part.getExtensionAttribute(new QName("minOccurs", "0"));
msgIn.addPart(part);
}
wsdl.addMessage(msgIn);
Input input = wsdl.createInput();
input.setMessage(msgIn);
operation.setInput(input);
}
else{
Input input = wsdl.createInput();
input.setMessage(empty);
operation.setInput(input);
}
//********** Response Message
if( m.returnName.length>0 ){
Message msgOut = wsdl.createMessage();
msgOut.setQName(new QName(tns, m.method.getName()+"Response"));
msgOut.setUndefined(false);
for( int i=0; i<m.returnName.length ;i++ ){
Class<?> retClass = m.returnClass[i];
//MultiPrintStream.out.println(m.method.getName()+"=>"+m.returnName[i]+"="+retClass);
// Parts
Part part = wsdl.createPart();
part.setName( m.returnName[i] );
msgOut.addPart(part);
Class<?> cTmp = getClass( retClass );
// is an binary array
if(byte[].class.isAssignableFrom( retClass )){
part.setTypeName(new QName(xsd, "base64Binary"));
}
// is an array?
else if( retClass.isArray()){
part.setTypeName(new QName(td,
"ArrayOf"+getClassSOAPName( retClass ).replaceAll("[\\[\\]]", "")));
// add to type generation list
if(!types.contains( retClass ))
types.add( retClass );
}
else if( WSObject.class.isAssignableFrom(cTmp) ){
// its an SOAPObject
part.setTypeName(new QName(td, getClassSOAPName( retClass )));
// add to type generation list
if(!types.contains(cTmp))
types.add(cTmp);
}
else{// its an Object
part.setTypeName(new QName(xsd, getClassSOAPName( retClass )));
}
}
wsdl.addMessage(msgOut);
Output output = wsdl.createOutput();
output.setMessage(msgOut);
operation.setOutput(output);
}
//************* Exceptions
if(m.method.getExceptionTypes().length <= 0){
Fault fault = wsdl.createFault();
fault.setMessage(exception);
operation.addFault(fault);
}
//************* Operations
operation.setName(m.method.getName());
operation.setUndefined(false);
//***** Documentation
WSDocumentation tmpDoc = m.method.getAnnotation(WSInterface.WSDocumentation.class);
if(tmpDoc != null){
// <!-- example -->
org.w3c.dom.Document xmldoc= new DocumentImpl();
org.w3c.dom.Element doc = xmldoc.createElement("wsdl:documentation");
doc.setTextContent(tmpDoc.value());
operation.setDocumentationElement(doc);
}
portType.addOperation(operation);
}
wsdl.addPortType(portType);
// Binding
Binding binding = wsdl.createBinding();
binding.setQName(new QName(tns, interf.getClass().getSimpleName()+"Binding"));
binding.setPortType(portType);
binding.setUndefined(false);
SOAPBinding soapBinding = (SOAPBinding)extReg.createExtension(Binding.class, SOAPConstants.Q_ELEM_SOAP_BINDING);
soapBinding.setStyle("rpc");
//soapBinding.setRequired(true);
soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
binding.addExtensibilityElement(soapBinding);
for(MethodCache m : methods.values()){
BindingOperation operation = wsdl.createBindingOperation();
operation.setName(m.method.getName());
SOAPOperation soapOperation = (SOAPOperation)extReg.createExtension(BindingOperation.class, SOAPConstants.Q_ELEM_SOAP_OPERATION);
soapOperation.setSoapActionURI(url+""+m.method.getName());
operation.addExtensibilityElement(soapOperation);
// input
BindingInput input = wsdl.createBindingInput();
// Header
if(m.header){
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
soapHeader.setUse("literal");
soapHeader.setNamespaceURI(url+""+m.method.getName());
input.addExtensibilityElement(soapHeader);
}// Body
else{
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
soapBody.setUse("literal");
soapBody.setNamespaceURI(url+""+m.method.getName());
input.addExtensibilityElement(soapBody);
}
operation.setBindingInput(input);
// output
if(!m.method.getReturnType().equals( void.class )){
BindingOutput output = wsdl.createBindingOutput();
// Header
if(m.header){
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
soapHeader.setUse("literal");
soapHeader.setNamespaceURI(url+""+m.method.getName());
output.addExtensibilityElement(soapHeader);
}// Body
else{
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
soapBody.setUse("literal");
soapBody.setNamespaceURI(url+""+m.method.getName());
output.addExtensibilityElement(soapBody);
}
operation.setBindingOutput(output);
}
binding.addBindingOperation(operation);
}
wsdl.addBinding(binding);
// Service
Port port = wsdl.createPort();
port.setName( interf.getClass().getSimpleName()+"Port" );
port.setBinding(binding);
SOAPAddress addr = (SOAPAddress)extReg.createExtension(Port.class, SOAPConstants.Q_ELEM_SOAP_ADDRESS);
addr.setLocationURI(url);
port.addExtensibilityElement(addr);
Service ser = wsdl.createService();
ser.setQName(new QName(tns, interf.getClass().getSimpleName()+"Service"));
ser.addPort(port);
wsdl.addService(ser);
// generate the complexTypes
generateWSDLType(types);
}
/**
* This function generates the Type part of the WSDL.
* Should be cabled after generateWSDL has finished.
*
*/
private void generateWSDLType(ArrayList<Class<?>> types){
wsdlType = DocumentHelper.createDocument();
Element definitions = wsdlType.addElement( "wsdl:definitions" );
definitions.addAttribute("targetNamespace", url);
definitions.addNamespace("tns", url+"?type");
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
definitions.addNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
Element typeE = definitions.addElement("wsdl:types");
Element schema = typeE.addElement("xsd:schema");
schema.addAttribute("targetNamespace", url+"?type");
// empty type
Element empty = schema.addElement("xsd:complexType");
empty.addAttribute("name", "empty");
empty.addElement("xsd:sequence");
for(int n=0; n<types.size() ;n++){
Class<?> c = types.get(n);
// Generate Array type
if(c.isArray()){
Class<?> ctmp = getClass(c);
Element type = schema.addElement("xsd:complexType");
type.addAttribute("name",
"ArrayOf"+getClassSOAPName(c).replaceAll("[\\[\\]]", ""));
/*// .Net can't handle this code
Element complexContent = type.addElement("complexContent");
Element restriction = complexContent.addElement("restriction");
restriction.addAttribute("base", "SOAP-ENC:Array");
Element attribute = restriction.addElement("attribute");
attribute.addAttribute("ref", "SOAP-ENC:arrayType");
attribute.addAttribute("wsdl:arrayType", "tns:"+getClassSOAPName(c));
*/
Element sequence = type.addElement("xsd:sequence");
Element element = sequence.addElement("xsd:element");
element.addAttribute("minOccurs", "0");
element.addAttribute("maxOccurs", "unbounded");
element.addAttribute("name", "element");
element.addAttribute("nillable", "true");
if(WSObject.class.isAssignableFrom(ctmp))
element.addAttribute("type", "tns:"+getClassSOAPName(c).replace("[]", ""));
else
element.addAttribute("type", "xsd:"+getClassSOAPName(c).replace("[]", ""));
if(!types.contains(ctmp))
types.add(ctmp);
}
// Generate SOAPObject type
else if(WSObject.class.isAssignableFrom(c)){
Element type = schema.addElement("xsd:complexType");
type.addAttribute("name", getClassSOAPName(c));
Element sequence = type.addElement("xsd:sequence");
Field[] fields = c.getFields();
for(int i=0; i<fields.length ;i++){
WSFieldName tmp = fields[i].getAnnotation(WSObject.WSFieldName.class);
String name;
if(tmp != null) name = tmp.value();
else name = "field"+i;
Element element = sequence.addElement("xsd:element");
element.addAttribute("name", name);
// Check if the object is an SOAPObject
Class<?> cTmp = getClass(fields[i].getType());
if(WSObject.class.isAssignableFrom(cTmp)){
element.addAttribute("type", "tns:"+getClassSOAPName(cTmp));
if(!types.contains(cTmp))
types.add(cTmp);
}
else{
element.addAttribute("type", "xsd:"+getClassSOAPName(fields[i].getType()));
}
// Is the Field optional
if(tmp != null && tmp.optional())
element.addAttribute("minOccurs", "0");
}
}
}
}
private Class<?> getClass(Class<?> c){
if(c!=null && c.isArray()){ if(c!=null && c.isArray()){
return getClass(c.getComponentType()); return getClass(c.getComponentType());
} }

View file

@ -21,9 +21,10 @@
******************************************************************************/ ******************************************************************************/
package zutil.net.upnp.services; package zutil.net.upnp.services;
import zutil.net.ws.WSReturnValueList; import zutil.net.ws.WSReturnObject;
public class BrowseRetObj extends WSReturnValueList{
public class BrowseRetObj extends WSReturnObject{
@WSValueName("Result") @WSValueName("Result")
public String Result; public String Result;
@WSValueName("NumberReturned") @WSValueName("NumberReturned")

View file

@ -33,7 +33,7 @@ import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream; import zutil.net.http.HttpPrintStream;
import zutil.net.upnp.UPnPService; import zutil.net.upnp.UPnPService;
import zutil.net.ws.WSInterface; import zutil.net.ws.WSInterface;
import zutil.net.ws.WSReturnValueList; import zutil.net.ws.WSReturnObject;
/** /**
* Information about a UPNP Service * Information about a UPNP Service
@ -137,7 +137,7 @@ public class UPnPContentDirectory implements UPnPService, HttpPage, WSInterface
} }
return ret; return ret;
} }
public class BrowseRetObj extends WSReturnValueList{ public class BrowseRetObj extends WSReturnObject{
@WSValueName("Result") @WSValueName("Result")
public String Result; public String Result;
@WSValueName("NumberReturned") @WSValueName("NumberReturned")

View file

@ -121,12 +121,12 @@ public interface WSInterface {
public @interface WSHeader { } public @interface WSHeader { }
/** /**
* Specifies the name space for the method. * Specifies the name space for method.
* *
* @author Ziver * @author Ziver
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //@Target(ElementType.TYPE)
public @interface WSNamespace { public @interface WSNamespace {
String value(); String value();
} }

View file

@ -23,14 +23,23 @@ package zutil.net.ws;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import zutil.net.ws.WSInterface.WSDocumentation; import zutil.net.ws.WSInterface.WSDocumentation;
import zutil.net.ws.WSInterface.WSNamespace;
/**
* This is a web service method definition class
*
* @author Ziver
*/
// TODO: Header parameters // TODO: Header parameters
public class WSMethodDef { public class WSMethodDef {
/** The parent web service definition **/
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 **/
@ -41,6 +50,8 @@ public class WSMethodDef {
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 **/
private String namespace;
/** The published name of the method **/ /** The published name of the method **/
private String name; private String name;
@ -49,63 +60,78 @@ public class WSMethodDef {
* *
* @param me is a method in a class that implements WSInterface * @param me is a method in a class that implements WSInterface
*/ */
public WSMethodDef(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;
method = me; method = me;
inputs = new ArrayList<WSParameterDef>(); inputs = new ArrayList<WSParameterDef>();
outputs = new ArrayList<WSParameterDef>(); outputs = new ArrayList<WSParameterDef>();
exceptions = new ArrayList<Class<?>>(); exceptions = new ArrayList<Class<?>>();
name = method.getName(); name = method.getName();
//***** Documentation //***** Documentation & Namespace
WSDocumentation tmpDoc = method.getAnnotation(WSInterface.WSDocumentation.class); WSDocumentation tmpDoc = method.getAnnotation( WSDocumentation.class );
if(tmpDoc != null){ if(tmpDoc != null){
doc = tmpDoc.value(); doc = tmpDoc.value();
} }
WSNamespace tmpSpace = method.getAnnotation( WSNamespace.class );
if( tmpSpace != null )
namespace = tmpSpace.value();
else
namespace = wsDef.getNamespace()+"?#"+name;
//***** Exceptions //***** Exceptions
for( Class<?> exc : method.getExceptionTypes() ){ for( Class<?> exc : method.getExceptionTypes() ){
exceptions.add( exc ); exceptions.add( exc );
} }
//********* Get the input parameter names ********** //********* Get the input parameter names **********
Annotation[][] paramAnnotation = method.getParameterAnnotations(); Annotation[][] paramAnnotation = method.getParameterAnnotations();
Class<?>[] inputTypes = method.getParameterTypes();
for(int i=0; i<paramAnnotation.length ;i++){ for(int i=0; i<paramAnnotation.length ;i++){
WSParameterDef param = new WSParameterDef(); 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.name = paramName.value(); param.setName( paramName.value() );
param.optional = paramName.optional(); param.setOptional( paramName.optional() );
} }
} }
param.setParamClass( inputTypes[i] );
// if no name was found then use default // if no name was found then use default
if(param.name == null) if(param.getName() == null)
param.name = "args"+i; param.setName( "args"+i );
inputs.add( param ); inputs.add( param );
} }
//******** The return parameter name ************ //******** The return parameter name ************
WSInterface.WSReturnName returnName = method.getAnnotation(WSInterface.WSReturnName.class); WSInterface.WSReturnName returnName = method.getAnnotation(WSInterface.WSReturnName.class);
if( WSReturnValueList.class.isAssignableFrom( method.getReturnType() ) ){ 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(int i=0; i<fields.length ;i++){
WSParameterDef ret_param = new WSParameterDef(); WSParameterDef ret_param = new WSParameterDef( this );
WSReturnValueList.WSValueName retValName = fields[i] WSReturnObject.WSValueName retValName = fields[i]
.getAnnotation( WSReturnValueList.WSValueName.class ); .getAnnotation( WSReturnObject.WSValueName.class );
if(retValName != null) ret_param.name = retValName.value(); if(retValName != null)
else ret_param.name = fields[i].getName(); ret_param.setName( retValName.value() );
ret_param.paramClass = fields[i].getType(); else
ret_param.setName( fields[i].getName() );
ret_param.setParamClass( fields[i].getType() );
outputs.add( ret_param ); outputs.add( ret_param );
} }
} }
else{ else{
WSParameterDef ret_param = new WSParameterDef(); WSParameterDef ret_param = new WSParameterDef( this );
if(returnName != null) ret_param.name = returnName.value(); if(returnName != null)
else ret_param.name = "return"; ret_param.setName(returnName.value());
ret_param.paramClass = method.getReturnType(); else
ret_param.setName("return");
ret_param.setParamClass( method.getReturnType() );
outputs.add( ret_param ); outputs.add( ret_param );
} }
} }
@ -134,7 +160,7 @@ public class WSMethodDef {
/** /**
* @return the number of parameters for this method * @return the number of parameters for this method
*/ */
public int inputCount(){ public int getInputCount(){
return inputs.size(); return inputs.size();
} }
@ -145,10 +171,18 @@ public class WSMethodDef {
return inputs; return inputs;
} }
/**
* @param index is a index
* @return a {@link WSParameterDef} object in the given index
*/
public WSParameterDef getInput( int index ){
return inputs.get( index );
}
/** /**
* @return the number of parameters for this method * @return the number of parameters for this method
*/ */
public int outputCount(){ public int getOutputCount(){
return outputs.size(); return outputs.size();
} }
@ -159,6 +193,14 @@ public class WSMethodDef {
return outputs; return outputs;
} }
/**
* @param index is a index
* @return a {@link WSParameterDef} object in the given index
*/
public WSParameterDef getOutput( int index ){
return outputs.get( index );
}
/** /**
* @return Documentation of the method if one exists or else null * @return Documentation of the method if one exists or else null
*/ */
@ -166,6 +208,35 @@ public class WSMethodDef {
return doc; return doc;
} }
/**
* @return the namespace of the method
*/
public String getNamespace(){
return namespace;
}
public WebServiceDef getWebService(){
return wsDef;
}
/**
* Invokes a specified method
*
* @param obj the object the method will called on
* @param params a vector with arguments
*/
public Object invoke(Object obj, Object[] params) throws Throwable{
try {
return this.method.invoke(obj, params );
} catch (IllegalArgumentException e) {
throw e;
} catch (IllegalAccessException e) {
throw e;
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
public String toString(){ public String toString(){
StringBuilder tmp = new StringBuilder(); StringBuilder tmp = new StringBuilder();
@ -176,9 +247,9 @@ public class WSMethodDef {
first = false; first = false;
else else
tmp.append(" ,"); tmp.append(" ,");
tmp.append(param.paramClass.getSimpleName()); tmp.append(param.getParamClass().getSimpleName());
tmp.append(" "); tmp.append(" ");
tmp.append(param.name); tmp.append(param.getName());
} }
tmp.append(") => "); tmp.append(") => ");
first = true; first = true;
@ -187,9 +258,9 @@ public class WSMethodDef {
first = false; first = false;
else else
tmp.append(" ,"); tmp.append(" ,");
tmp.append(param.paramClass.getSimpleName()); tmp.append(param.getParamClass().getSimpleName());
tmp.append(" "); tmp.append(" ");
tmp.append(param.name); tmp.append(param.getName());
} }
return tmp.toString(); return tmp.toString();
} }

View file

@ -1,77 +0,0 @@
/*******************************************************************************
* Copyright (c) 2011 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;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This class is used as an return Object for a web service.
* If an class implements this interface then it can return
* multiple values through the SOAPInterface. Example:
* <pre>
* private static class TestObject implements WSObject{
* @SOAPFieldName("name")
* public String name;
* @SOAPFieldName("lastname")
* public String lastname;
*
* public TestObject(String n, String l){
* name = n;
* lastname = l;
* }
* }
* </pre>
*
* @author Ziver
*
*/
public interface WSObject{
/**
* Specifies the name of a field.
* The fields that are available in the service should
* be declared public.
*
* @author Ziver
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface WSFieldName {
String value();
boolean optional() default false;
}
/**
* This generates an documentation tag in the
* WSDL for the object type
*
* @author Ziver
*/
/*
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface WSDLDocumentation {
String value();
}*/
}

View file

@ -21,18 +21,30 @@
******************************************************************************/ ******************************************************************************/
package zutil.net.ws; package zutil.net.ws;
/**
* This is a web service parameter definition class
*
* @author Ziver
*/
public class WSParameterDef{ public class WSParameterDef{
/** The parent method **/
private WSMethodDef mDef;
/** The class type of the parameter **/ /** The class type of the parameter **/
protected Class<?> paramClass; private Class<?> paramClass;
/** The web service name of the parameter **/ /** The web service name of the parameter **/
protected String name; private String name;
/** Developer documentation **/ /** Developer documentation **/
protected String doc; private String doc;
/** If this parameter is optional **/ /** If this parameter is optional **/
protected boolean optional; private boolean optional;
/** Is it an header parameter **/ /** Is it an header parameter **/
//boolean header; //boolean header;
protected WSParameterDef( WSMethodDef mDef ){
this.mDef = mDef;
this.optional = false;
}
public Class<?> getParamClass() { public Class<?> getParamClass() {
return paramClass; return paramClass;
@ -61,4 +73,8 @@ public class WSParameterDef{
protected void setOptional(boolean optional) { protected void setOptional(boolean optional) {
this.optional = optional; this.optional = optional;
} }
public WSMethodDef getMethod(){
return mDef;
}
} }

View file

@ -28,16 +28,29 @@ import java.lang.annotation.Target;
import java.lang.reflect.Field; import java.lang.reflect.Field;
/** /**
* This interface is for returning multiple object. * This class is used as an return Object for a web service.
* All the public fields in the class that implements * If an class implements this interface then it can return
* this class will be set as return values. And the * multiple values through the WSInterface. And the
* implementing class will be transparent. * implementing class will be transparent. Example:
*
* <pre>
* private static class TestObject implements WSReturnObject{
* @WSValueName("name")
* public String name;
* @WSValueName("lastname")
* public String lastname;
*
* public TestObject(String n, String l){
* name = n;
* lastname = l;
* }
* }
* </pre>
* *
* @author Ziver * @author Ziver
* *
*/ */
public class WSReturnValueList { public class WSReturnObject{
/** /**
* Method comments for the WSDL. * Method comments for the WSDL.
* These comments are put in the operation part of the WSDL * These comments are put in the operation part of the WSDL
@ -49,15 +62,6 @@ public class WSReturnValueList {
String value(); String value();
} }
/**
* Disables publication of the given field.
*
* @author Ziver
*/
/*@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SOAPDisabledValue { }*/
/** /**
* Annotation that assigns a name to the return value * Annotation that assigns a name to the return value
* to the field. * to the field.
@ -68,6 +72,7 @@ public class WSReturnValueList {
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
public @interface WSValueName { public @interface WSValueName {
String value(); String value();
boolean optional() default false;
} }
@ -75,4 +80,3 @@ public class WSReturnValueList {
return field.get(this); return field.get(this);
} }
} }

View file

@ -27,31 +27,35 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import javax.wsdl.WSDLException; /**
* Defines a web service from a class implementing the {@link zutil.net.ws.WSInterface}
*
* @author Ziver
*/
public class WebServiceDef { public class WebServiceDef {
/** A map of methods in this Service **/ /** A map of methods in this Service **/
private HashMap<String,WSMethodDef> methods; private HashMap<String,WSMethodDef> methods;
/** URL of the Service **/
//private String url;
/** 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 **/ /** This is the WSInterface class **/
private Class<? extends WSInterface> intf; private Class<? extends WSInterface> intf;
public WebServiceDef(Class<? extends WSInterface> intf) throws WSDLException{ public WebServiceDef(Class<? extends WSInterface> intf){
this.intf = intf; this.intf = intf;
methods = new HashMap<String,WSMethodDef>(); methods = new HashMap<String,WSMethodDef>();
name = intf.getSimpleName(); name = intf.getSimpleName();
if( intf.getAnnotation( WSInterface.WSNamespace.class ) != null )
this.namespace = intf.getAnnotation( WSInterface.WSNamespace.class ).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.WSDisabled.class)){ !m.isAnnotationPresent(WSInterface.WSDisabled.class)){
WSMethodDef method = new WSMethodDef(m); WSMethodDef method = new WSMethodDef(this, m);
methods.put(method.getName(), method); methods.put(method.getName(), method);
} }
} }
@ -71,6 +75,22 @@ public class WebServiceDef {
return name; return name;
} }
/**
* @param name is the name of the method
* @return if there is a method by the given name
*/
public boolean hasMethod( String name ){
return methods.containsKey( name );
}
/**
* @param name is the name of the method
* @return the method or null if there is no such method
*/
public WSMethodDef getMethod( String name ){
return methods.get( name );
}
/** /**
* @return a Set of all the method names * @return a Set of all the method names
*/ */
@ -84,4 +104,15 @@ public class WebServiceDef {
public Collection<WSMethodDef> getMethods(){ public Collection<WSMethodDef> getMethods(){
return methods.values(); return methods.values();
} }
/**
* @return the namespace of this web service ( usually the URL of the service )
*/
public String getNamespace(){
return namespace;
}
public WSInterface newInstance() throws InstantiationException, IllegalAccessException {
return intf.newInstance();
}
} }

View file

@ -21,10 +21,11 @@
******************************************************************************/ ******************************************************************************/
package zutil.parser.wsdl; package zutil.parser.wsdl;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.logging.Logger;
import javax.wsdl.Binding; import javax.wsdl.Binding;
import javax.wsdl.BindingInput; import javax.wsdl.BindingInput;
@ -57,39 +58,71 @@ import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
import com.ibm.wsdl.extensions.soap.SOAPConstants; import com.ibm.wsdl.extensions.soap.SOAPConstants;
import com.sun.org.apache.xerces.internal.dom.DocumentImpl; import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
import zutil.log.LogUtil; import zutil.io.StringOutputStream;
import zutil.net.ws.WSMethodDef; import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSObject;
import zutil.net.ws.WSParameterDef; import zutil.net.ws.WSParameterDef;
import zutil.net.ws.WSReturnObject;
import zutil.net.ws.WSReturnObject.WSValueName;
import zutil.net.ws.WebServiceDef; import zutil.net.ws.WebServiceDef;
import zutil.net.ws.WSObject.WSFieldName;
public class WSDLWriter{ public class WSDLWriter{
private static final Logger logger = LogUtil.getLogger();
private WebServiceDef ws; private WebServiceDef ws;
private StringBuilder cache; private String cache;
private OutputStream out; private String soapURL;
public void write(WebServiceDef object) { public WSDLWriter( WebServiceDef ws ){
// TODO Auto-generated method stub this.ws = ws;
try {
Definition wsdl = generateWSDL( );
StringOutputStream out = new StringOutputStream();
javax.wsdl.xml.WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
writer.writeWSDL(wsdl, out);
//OutputFormat format = OutputFormat.createPrettyPrint();
//XMLWriter writer = new XMLWriter( out, format );
//writer.write( wsdlType );
this.cache = out.toString();
out.close();
} catch (WSDLException e) {
e.printStackTrace();
}
} }
/**
* @param binding adds this binding to the WSDL generation
*/
public void setSOAPAddress( String url ){
this.soapURL = url;
}
public void write( PrintStream out ) {
out.print( cache );
}
public void write( OutputStream out ) throws IOException {
out.write( cache.getBytes() );
}
/** /**
* Generates an WSDL document for the class * Generates an WSDL document for the class
* *
* @throws WSDLException * @throws WSDLException
*/ */
private void generateWSDL(String url) throws WSDLException{ private Definition generateWSDL( ) throws WSDLException {
ArrayList<Class<?>> types = new ArrayList<Class<?>>(); ArrayList<Class<?>> types = new ArrayList<Class<?>>();
String tns = url+"?wsdl"; String tns = ws.getNamespace()+"?wsdl";
String xsd = "http://www.w3.org/2001/XMLSchema"; String xsd = "http://www.w3.org/2001/XMLSchema";
String soap = "http://schemas.xmlsoap.org/wsdl/soap/"; String soap = "http://schemas.xmlsoap.org/wsdl/soap/";
String wsdln = "http://schemas.xmlsoap.org/wsdl/"; String wsdln = "http://schemas.xmlsoap.org/wsdl/";
String td = url+"?type"; String td = ws.getNamespace()+"?type";
PopulatedExtensionRegistry extReg = new PopulatedExtensionRegistry(); PopulatedExtensionRegistry extReg = new PopulatedExtensionRegistry();
WSDLFactory factory = WSDLFactory.newInstance(); WSDLFactory factory = WSDLFactory.newInstance();
@ -132,10 +165,10 @@ public class WSDLWriter{
PortType portType = wsdl.createPortType(); PortType portType = wsdl.createPortType();
portType.setQName(new QName(tns, portTypeName)); portType.setQName(new QName(tns, portTypeName));
portType.setUndefined(false); portType.setUndefined(false);
for(WSMethodDef method : ws.getMethods()){ for( WSMethodDef method : ws.getMethods() ){
Operation operation = wsdl.createOperation(); Operation operation = wsdl.createOperation();
//********* Request Messages //********* Request Messages
if(method.inputCount() > 0){ if( method.getInputCount() > 0 ){
Message msgIn = wsdl.createMessage(); Message msgIn = wsdl.createMessage();
msgIn.setQName(new QName(tns, method.getName()+"Request")); msgIn.setQName(new QName(tns, method.getName()+"Request"));
msgIn.setUndefined(false); msgIn.setUndefined(false);
@ -149,10 +182,10 @@ public class WSDLWriter{
} }
// Parameters // Parameters
for(WSParameterDef param : method.getInputs()){ for( WSParameterDef param : method.getInputs() ){
// Parts // Parts
Part part = wsdl.createPart(); Part part = wsdl.createPart();
part.setName(param.getName()); part.setName( param.getName() );
part.setTypeName(new QName( xsd, part.setTypeName(new QName( xsd,
getClassSOAPName( param.getParamClass() ))); getClassSOAPName( param.getParamClass() )));
if( param.isOptional() ) if( param.isOptional() )
@ -170,7 +203,7 @@ public class WSDLWriter{
operation.setInput(input); operation.setInput(input);
} }
//********** Response Message //********** Response Message
if( method.outputCount() > 0 ){ if( method.getOutputCount() > 0 ){
Message msgOut = wsdl.createMessage(); Message msgOut = wsdl.createMessage();
msgOut.setQName(new QName(tns, method.getName()+"Response")); msgOut.setQName(new QName(tns, method.getName()+"Response"));
msgOut.setUndefined(false); msgOut.setUndefined(false);
@ -195,7 +228,7 @@ public class WSDLWriter{
if(!types.contains( paramClass )) if(!types.contains( paramClass ))
types.add( paramClass ); types.add( paramClass );
} }
else if( WSObject.class.isAssignableFrom(valueClass) ){ else if( WSReturnObject.class.isAssignableFrom(valueClass) ){
// its an SOAPObject // its an SOAPObject
part.setTypeName(new QName(td, getClassSOAPName( paramClass ))); part.setTypeName(new QName(td, getClassSOAPName( paramClass )));
// add to type generation list // add to type generation list
@ -252,42 +285,30 @@ public class WSDLWriter{
operation.setName(method.getName()); operation.setName(method.getName());
SOAPOperation soapOperation = (SOAPOperation)extReg.createExtension(BindingOperation.class, SOAPConstants.Q_ELEM_SOAP_OPERATION); SOAPOperation soapOperation = (SOAPOperation)extReg.createExtension(BindingOperation.class, SOAPConstants.Q_ELEM_SOAP_OPERATION);
soapOperation.setSoapActionURI(url+""+method.getName()); soapOperation.setSoapActionURI(method.getNamespace());
operation.addExtensibilityElement(soapOperation); operation.addExtensibilityElement(soapOperation);
// input //********************************* input
BindingInput input = wsdl.createBindingInput(); BindingInput input = wsdl.createBindingInput();
// TODO: Header // TODO: Header
/*if(m.header){
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
soapHeader.setUse("literal");
soapHeader.setNamespaceURI(url+""+method.getName());
input.addExtensibilityElement(soapHeader);
}*/
// Body // Body
//else{ //else{
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY); SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
soapBody.setUse("literal"); soapBody.setUse("literal");
soapBody.setNamespaceURI(url+""+method.getName()); soapBody.setNamespaceURI(method.getNamespace());
input.addExtensibilityElement(soapBody); input.addExtensibilityElement(soapBody);
//} //}
operation.setBindingInput(input); operation.setBindingInput(input);
// output //********************************** output
if( method.outputCount() > 0 ){ if( method.getOutputCount() > 0 ){
BindingOutput output = wsdl.createBindingOutput(); BindingOutput output = wsdl.createBindingOutput();
// TODO: Header // TODO: Header
/*if(m.header){
SOAPHeader soapHeaderBind = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
soapHeaderBind("literal");
soapHeaderBind(url+""+method.getName());
output.addExtensibilityElement(soapHeaderBind);
}*/
// Body // Body
//else{ //else{
SOAPBody soapBodyBind = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY); SOAPBody soapBodyBind = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
soapBodyBind.setUse("literal"); soapBodyBind.setUse("literal");
soapBodyBind.setNamespaceURI(url+""+method.getName()); soapBodyBind.setNamespaceURI(method.getNamespace());
output.addExtensibilityElement(soapBodyBind); output.addExtensibilityElement(soapBodyBind);
//} //}
operation.setBindingOutput(output); operation.setBindingOutput(output);
@ -297,12 +318,12 @@ public class WSDLWriter{
} }
wsdl.addBinding(binding); wsdl.addBinding(binding);
// Service //******************* Service
Port port = wsdl.createPort(); Port port = wsdl.createPort();
port.setName( ws.getName()+"Port" ); port.setName( ws.getName()+"Port" );
port.setBinding(binding); port.setBinding(binding);
SOAPAddress addr = (SOAPAddress)extReg.createExtension(Port.class, SOAPConstants.Q_ELEM_SOAP_ADDRESS); SOAPAddress addr = (SOAPAddress)extReg.createExtension(Port.class, SOAPConstants.Q_ELEM_SOAP_ADDRESS);
addr.setLocationURI(url); addr.setLocationURI( soapURL );
port.addExtensibilityElement(addr); port.addExtensibilityElement(addr);
Service ser = wsdl.createService(); Service ser = wsdl.createService();
@ -311,7 +332,8 @@ public class WSDLWriter{
wsdl.addService(ser); wsdl.addService(ser);
// generate the complexTypes // generate the complexTypes
generateWSDLType(url, types); generateWSDLType(types);
return wsdl;
} }
/** /**
@ -319,18 +341,18 @@ public class WSDLWriter{
* Should be cabled after generateWSDL has finished. * Should be cabled after generateWSDL has finished.
* *
*/ */
private void generateWSDLType(String url, ArrayList<Class<?>> types){ private void generateWSDLType(ArrayList<Class<?>> types){
Document wsdlType = DocumentHelper.createDocument(); Document wsdlType = DocumentHelper.createDocument();
Element definitions = wsdlType.addElement( "wsdl:definitions" ); Element definitions = wsdlType.addElement( "wsdl:definitions" );
definitions.addAttribute("targetNamespace", url); definitions.addAttribute("targetNamespace", ws.getNamespace());
definitions.addNamespace("tns", url+"?type"); definitions.addNamespace("tns", ws.getNamespace()+"?type");
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/"); definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
definitions.addNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/"); definitions.addNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
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", url+"?type"); schema.addAttribute("targetNamespace", ws.getNamespace()+"?type");
// empty type // empty type
Element empty = schema.addElement("xsd:complexType"); Element empty = schema.addElement("xsd:complexType");
@ -354,7 +376,7 @@ 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(WSObject.class.isAssignableFrom(ctmp)) if( WSReturnObject.class.isAssignableFrom(ctmp) )
element.addAttribute("type", "tns:"+getClassSOAPName(c).replace("[]", "")); element.addAttribute("type", "tns:"+getClassSOAPName(c).replace("[]", ""));
else else
element.addAttribute("type", "xsd:"+getClassSOAPName(c).replace("[]", "")); element.addAttribute("type", "xsd:"+getClassSOAPName(c).replace("[]", ""));
@ -363,7 +385,7 @@ public class WSDLWriter{
types.add(ctmp); types.add(ctmp);
} }
// Generate SOAPObject type // Generate SOAPObject type
else if(WSObject.class.isAssignableFrom(c)){ else if(WSReturnObject.class.isAssignableFrom(c)){
Element type = schema.addElement("xsd:complexType"); Element type = schema.addElement("xsd:complexType");
type.addAttribute("name", getClassSOAPName(c)); type.addAttribute("name", getClassSOAPName(c));
@ -371,7 +393,7 @@ public class WSDLWriter{
Field[] fields = c.getFields(); Field[] fields = c.getFields();
for(int i=0; i<fields.length ;i++){ for(int i=0; i<fields.length ;i++){
WSFieldName tmp = fields[i].getAnnotation(WSObject.WSFieldName.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;
@ -381,7 +403,7 @@ public class WSDLWriter{
// 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(WSObject.class.isAssignableFrom(cTmp)){ if( WSReturnObject.class.isAssignableFrom(cTmp) ){
element.addAttribute("type", "tns:"+getClassSOAPName(cTmp)); element.addAttribute("type", "tns:"+getClassSOAPName(cTmp));
if(!types.contains(cTmp)) if(!types.contains(cTmp))
types.add(cTmp); types.add(cTmp);
@ -397,6 +419,11 @@ public class WSDLWriter{
} }
} }
///////////////////////////////////////////////////////////////////////////////////////////////
// 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());
@ -406,17 +433,17 @@ public class WSDLWriter{
private String getClassSOAPName(Class<?> c){ private String getClassSOAPName(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( WSObject.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;
} }

View file

@ -21,16 +21,16 @@
******************************************************************************/ ******************************************************************************/
package zutil.test; package zutil.test;
import javax.wsdl.WSDLException;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.io.OutputFormat; import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter; import org.dom4j.io.XMLWriter;
import zutil.net.http.soap.SOAPHttpPage; import zutil.net.http.soap.SOAPHttpPage;
import zutil.net.ws.WSInterface; import zutil.net.ws.WSInterface;
import zutil.net.ws.WSObject; import zutil.net.ws.WSInterface.*;
import zutil.net.ws.WSReturnValueList; import zutil.net.ws.WSReturnObject;
import zutil.net.ws.WebServiceDef;
import zutil.parser.wsdl.WSDLWriter;
public class SOAPTest { public class SOAPTest {
@ -42,64 +42,65 @@ public class SOAPTest {
} }
public SOAPTest(){ public SOAPTest(){
try { WebServiceDef wsDef = new WebServiceDef( SOAPTestClass.class );
SOAPHttpPage soap = new SOAPHttpPage("http://test.se:8080/", new SOAPTestClass()); SOAPHttpPage soap = new SOAPHttpPage( wsDef );
// Response WSDLWriter wsdl = new WSDLWriter( wsDef );
try { wsdl.write(System.out);
Document document = soap.genSOAPResponse(
"<?xml version=\"1.0\"?>" + // Response
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" + try {
" <soap:Body xmlns:m=\"http://www.example.org/stock\">\n" + Document document = soap.genSOAPResponse(
//" <m:pubA>\n" + "<?xml version=\"1.0\"?>" +
//" <m:Ztring>IBM</m:Ztring>\n" + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
//" </m:pubA>\n" + " <soap:Body xmlns:m=\"http://www.example.org/stock\">\n" +
//" <m:pubZ>\n" + //" <m:pubA>\n" +
//" <m:olle>66</m:olle>\n" + //" <m:Ztring>IBM</m:Ztring>\n" +
//" </m:pubZ>\n" + //" </m:pubA>\n" +
" <m:pubB>\n" + //" <m:pubZ>\n" +
" <m:byte>IBM</m:byte>\n" + //" <m:olle>66</m:olle>\n" +
" </m:pubB>\n" + //" </m:pubZ>\n" +
" </soap:Body>\n" + " <m:pubB>\n" +
"</soap:Envelope>"); " <m:byte>IBM</m:byte>\n" +
System.out.println( "****************** RESPONSE *********************" ); " </m:pubB>\n" +
" </soap:Body>\n" +
"</soap:Envelope>");
System.out.println( "****************** RESPONSE *********************" );
OutputFormat format = OutputFormat.createPrettyPrint(); OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter( System.out, format ); XMLWriter writer = new XMLWriter( System.out, format );
writer.write( document ); writer.write( document );
System.out.println(); System.out.println();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
}
} catch (WSDLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public class SOAPTestClass3 implements WSObject{ public static class SOAPTestClass3 extends WSReturnObject{
public String lol = "lol11"; public String lol = "lol11";
public String lol2 = "lol22"; public String lol2 = "lol22";
} }
public class SOAPTestClass2 implements WSObject{ public static class SOAPTestClass2 extends WSReturnObject{
@WSFieldName(value="lolz", optional=true) @WSValueName(value="lolz")
public String lol = "lol1"; public String lol = "lol1";
@WSFieldName("lolx") @WSValueName("lolx")
public String lol2 = "lol2"; public String lol2 = "lol2";
public byte[] b = new byte[]{0x12, 0x23}; public byte[] b = new byte[]{0x12, 0x23};
public SOAPTestClass3 l = new SOAPTestClass3(); public SOAPTestClass3 l = new SOAPTestClass3();
} }
public class SOAPTestRetClass extends WSReturnValueList{ public static class SOAPTestRetClass extends WSReturnObject{
@WSValueName("retTest") @WSValueName("retTest")
public String lol = "lol1"; public String lol = "lol1";
public String lol2 = "lol2"; public String lol2 = "lol2";
} }
public class SOAPTestClass implements WSInterface{ @WSNamespace("http://test.se:8080/")
public static class SOAPTestClass implements WSInterface{
public SOAPTestClass(){} public SOAPTestClass(){}
@WSHeader() @WSHeader()
@WSDocumentation("hello") @WSDocumentation("hello")
public void pubZ( public void pubZ(

View file

@ -32,6 +32,7 @@ import zutil.net.http.soap.SOAPHttpPage;
import zutil.net.ssdp.SSDPServer; import zutil.net.ssdp.SSDPServer;
import zutil.net.upnp.UPnPMediaServer; import zutil.net.upnp.UPnPMediaServer;
import zutil.net.upnp.services.UPnPContentDirectory; import zutil.net.upnp.services.UPnPContentDirectory;
import zutil.net.ws.WebServiceDef;
public class UPnPServerTest { public class UPnPServerTest {
@ -40,13 +41,15 @@ public class UPnPServerTest {
MultiPrintStream.out.println("UPNP Server running"); MultiPrintStream.out.println("UPNP Server running");
UPnPContentDirectory cds = new UPnPContentDirectory(new File("C:\\Users\\Ziver\\Desktop\\lan")); UPnPContentDirectory cds = new UPnPContentDirectory(new File("C:\\Users\\Ziver\\Desktop\\lan"));
WebServiceDef ws = new WebServiceDef( UPnPContentDirectory.class );
HttpServer http = new HttpServer("http://192.168.0.60/", 8080); HttpServer http = new HttpServer("http://192.168.0.60/", 8080);
//http.setDefaultPage(upnp); //http.setDefaultPage(upnp);
http.setPage("/RootDesc", upnp ); http.setPage("/RootDesc", upnp );
http.setPage("/SCP/ContentDir", cds ); http.setPage("/SCP/ContentDir", cds );
SOAPHttpPage soap = new SOAPHttpPage("Action/ContentDir", cds); SOAPHttpPage soap = new SOAPHttpPage(ws);
soap.enableSession(false); soap.setObject( cds );
soap.enableSession( false );
http.setPage("/Action/ContentDir", soap ); http.setPage("/Action/ContentDir", soap );
http.start(); http.start();
MultiPrintStream.out.println("HTTP Server running"); MultiPrintStream.out.println("HTTP Server running");