2009-04-22 18:34:11 +00:00
|
|
|
package zutil.network.http.soap;
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
import java.io.IOException;
|
2009-04-22 18:34:11 +00:00
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
|
import java.lang.reflect.Array;
|
|
|
|
|
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;
|
2009-05-18 12:52:16 +00:00
|
|
|
import java.util.Map;
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
import javax.wsdl.Binding;
|
|
|
|
|
import javax.wsdl.BindingInput;
|
|
|
|
|
import javax.wsdl.BindingOperation;
|
|
|
|
|
import javax.wsdl.BindingOutput;
|
|
|
|
|
import javax.wsdl.Definition;
|
|
|
|
|
import javax.wsdl.Fault;
|
2009-05-17 18:46:05 +00:00
|
|
|
import javax.wsdl.Import;
|
2009-04-22 18:34:11 +00:00
|
|
|
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;
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
import zutil.network.http.HttpPage;
|
|
|
|
|
import zutil.network.http.HttpPrintStream;
|
|
|
|
|
import zutil.network.http.soap.SOAPInterface.WSDLDocumentation;
|
|
|
|
|
import zutil.network.http.soap.SOAPInterface.WSDLParamDocumentation;
|
|
|
|
|
import zutil.network.http.soap.SOAPObject.SOAPFieldName;
|
|
|
|
|
import zutil.MultiPrintStream;
|
|
|
|
|
|
2009-04-22 18:34:11 +00:00
|
|
|
import org.dom4j.Document;
|
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
|
import org.dom4j.DocumentHelper;
|
|
|
|
|
import org.dom4j.Element;
|
|
|
|
|
import org.dom4j.Namespace;
|
|
|
|
|
import org.dom4j.io.OutputFormat;
|
|
|
|
|
import org.dom4j.io.XMLWriter;
|
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
|
|
import com.ibm.wsdl.extensions.PopulatedExtensionRegistry;
|
|
|
|
|
import com.ibm.wsdl.extensions.soap.SOAPConstants;
|
|
|
|
|
import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
/**
|
|
|
|
|
* This is an HTTPPage for the HTTPServer that
|
|
|
|
|
* handles soap messages.
|
|
|
|
|
*
|
|
|
|
|
* TODO: Read SOAPObjects as input parameter
|
|
|
|
|
* TODO: Ability to have multiple arrays of same SOAPObject
|
|
|
|
|
*
|
|
|
|
|
* Features:
|
|
|
|
|
* Input:
|
|
|
|
|
* <br>-int
|
|
|
|
|
* <br>-double
|
|
|
|
|
* <br>-float
|
|
|
|
|
* <br>-char
|
|
|
|
|
* <br>-String
|
|
|
|
|
* <br>-byte[]
|
|
|
|
|
* <br>-And the Wrappers except byte
|
|
|
|
|
*
|
|
|
|
|
* Output:
|
|
|
|
|
* <br>-SOAPObjects
|
|
|
|
|
* <br>-byte[]
|
|
|
|
|
* <br>-int
|
|
|
|
|
* <br>-double
|
|
|
|
|
* <br>-float
|
|
|
|
|
* <br>-char
|
|
|
|
|
* <br>-String
|
|
|
|
|
* <br>-Arrays of Output
|
|
|
|
|
* <br>-And the Wrappers except byte
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
2009-04-22 18:34:11 +00:00
|
|
|
public class SOAPHttpPage implements HttpPage{
|
|
|
|
|
// valid methods for this soap page
|
|
|
|
|
private HashMap<String, MethodChasch> methods;
|
|
|
|
|
// contains an method and the names for the parameters
|
|
|
|
|
private class MethodChasch{
|
|
|
|
|
String[] paramName;
|
|
|
|
|
boolean[] paramOptional;
|
|
|
|
|
String returnName;
|
|
|
|
|
Method method;
|
|
|
|
|
boolean header;
|
|
|
|
|
|
|
|
|
|
MethodChasch(Method m){
|
|
|
|
|
method = m;
|
|
|
|
|
paramName = new String[method.getParameterTypes().length];
|
|
|
|
|
paramOptional = new boolean[method.getParameterTypes().length];
|
|
|
|
|
header = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// The object that the functions will be invoked from
|
|
|
|
|
private SOAPInterface interf;
|
|
|
|
|
// The WSDL document
|
|
|
|
|
private Definition wsdl;
|
|
|
|
|
// The WSDL Type part
|
|
|
|
|
private Document wsdlType;
|
|
|
|
|
// the URL to this soap page
|
|
|
|
|
private String url;
|
2009-05-17 18:46:05 +00:00
|
|
|
// Session enabled
|
|
|
|
|
private boolean session_enabled;
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
public SOAPHttpPage(String url, SOAPInterface interf) throws WSDLException{
|
|
|
|
|
//if(!SOAPInterface.class.isAssignableFrom(interf) )
|
|
|
|
|
// throw new ClassCastException("Class does not implement SOAPInterface!");
|
|
|
|
|
this.url = url;
|
|
|
|
|
this.interf = interf;
|
2009-05-17 18:46:05 +00:00
|
|
|
this.session_enabled = false;
|
2009-04-22 18:34:11 +00:00
|
|
|
methods = new HashMap<String, MethodChasch>();
|
|
|
|
|
|
|
|
|
|
for(Method m : interf.getClass().getDeclaredMethods()){
|
|
|
|
|
// check for public methods
|
2009-05-17 18:46:05 +00:00
|
|
|
if((m.getModifiers() & Modifier.PUBLIC) > 0 &&
|
2009-04-22 18:34:11 +00:00
|
|
|
!m.isAnnotationPresent(SOAPInterface.SOAPDisabled.class)){
|
|
|
|
|
MethodChasch chasch = new MethodChasch(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 SOAPInterface.SOAPParamName){
|
|
|
|
|
SOAPInterface.SOAPParamName paramName = (SOAPInterface.SOAPParamName) 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 param name
|
|
|
|
|
SOAPInterface.SOAPReturnName returnName = m.getAnnotation(SOAPInterface.SOAPReturnName.class);
|
|
|
|
|
if(returnName != null) chasch.returnName = returnName.value();
|
|
|
|
|
else chasch.returnName = "return";
|
|
|
|
|
|
|
|
|
|
// SOAP header?
|
|
|
|
|
if(m.getAnnotation(SOAPInterface.SOAPHeader.class) != null)
|
|
|
|
|
chasch.header = true;
|
|
|
|
|
|
|
|
|
|
// save in HashMap
|
|
|
|
|
MultiPrintStream.out.println("New SOAP Method Registered: "+tmp);
|
|
|
|
|
methods.put(m.getName(), chasch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateWSDL();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// WSDL
|
|
|
|
|
MultiPrintStream.out.println();
|
|
|
|
|
WSDLFactory factory = WSDLFactory.newInstance();
|
|
|
|
|
WSDLWriter writer = factory.newWSDLWriter();
|
|
|
|
|
writer.writeWSDL(wsdl, MultiPrintStream.out);
|
|
|
|
|
MultiPrintStream.out.println();
|
|
|
|
|
// WSDL Type
|
|
|
|
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
|
|
|
XMLWriter xmlWriter = new XMLWriter( MultiPrintStream.out, format );
|
|
|
|
|
xmlWriter.write( wsdlType );
|
|
|
|
|
MultiPrintStream.out.println();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
public void enableSession(boolean enabled){
|
|
|
|
|
this.session_enabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-22 18:34:11 +00:00
|
|
|
public void respond(HttpPrintStream out,
|
2009-05-18 12:52:16 +00:00
|
|
|
Map<String, String> client_info,
|
|
|
|
|
Map<String, Object> session,
|
|
|
|
|
Map<String, String> cookie,
|
|
|
|
|
Map<String, String> request) {
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
try {
|
2009-05-17 18:46:05 +00:00
|
|
|
out.setHeader("Content-Type", "text/xml");
|
|
|
|
|
out.flush();
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
if(request.containsKey("wsdl")){
|
|
|
|
|
WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
|
|
|
|
|
writer.writeWSDL(wsdl, out);
|
|
|
|
|
}
|
|
|
|
|
else if(request.containsKey("type")){
|
|
|
|
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
|
|
|
XMLWriter writer = new XMLWriter( out, format );
|
|
|
|
|
writer.write( wsdlType );
|
|
|
|
|
}
|
|
|
|
|
else{
|
2009-05-17 18:46:05 +00:00
|
|
|
SOAPInterface obj = null;
|
|
|
|
|
if(session_enabled && session.containsKey("SOAPInterface"))
|
|
|
|
|
obj = (SOAPInterface)session.get("SOAPInterface");
|
|
|
|
|
else{
|
|
|
|
|
obj = interf.getClass().newInstance();
|
|
|
|
|
if(session_enabled) session.put("SOAPInterface", obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Document document = soapResponse( request.get(""), obj);
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
|
|
|
XMLWriter writer = new XMLWriter( out, format );
|
|
|
|
|
writer.write( document );
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2009-05-17 18:46:05 +00:00
|
|
|
e.printStackTrace(MultiPrintStream.out);
|
2009-04-22 18:34:11 +00:00
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a soap response for the given
|
|
|
|
|
* @param xml is the XML request
|
|
|
|
|
* @return a Document with the response
|
|
|
|
|
*/
|
|
|
|
|
public Document soapResponse(String xml){
|
|
|
|
|
try {
|
|
|
|
|
return soapResponse(xml, interf.getClass().newInstance());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2009-04-22 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
protected Document soapResponse(String xml, SOAPInterface obj){
|
2009-04-22 18:34:11 +00:00
|
|
|
try {
|
|
|
|
|
Document document = DocumentHelper.createDocument();
|
|
|
|
|
Element envelope = document.addElement("soap:Envelope");
|
|
|
|
|
envelope.add(new Namespace("soap", "http://www.w3.org/2001/12/soap-envelope"));
|
|
|
|
|
envelope.addAttribute("soap:encodingStyle", "http://www.w3.org/2001/12/soap-encoding");
|
|
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
Element header = envelope.addElement( "soap:Header" );
|
2009-04-22 18:34:11 +00:00
|
|
|
Element body = envelope.addElement( "soap:Body" );
|
|
|
|
|
try{
|
|
|
|
|
Element request = getXMLRoot(xml);
|
|
|
|
|
// Header
|
|
|
|
|
if( request.element("Header") != null){
|
|
|
|
|
prepareInvoke( obj, request.element("Header"), header );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Body
|
|
|
|
|
if( request.element("Body") != null){
|
|
|
|
|
prepareInvoke( obj, request.element("Body"), body );
|
|
|
|
|
}
|
|
|
|
|
}catch(Throwable e){
|
|
|
|
|
body.clearContent();
|
|
|
|
|
Element fault = body.addElement("soap:Fault");
|
|
|
|
|
// The fault source
|
|
|
|
|
if(e instanceof SOAPClientException || e instanceof SAXException || e instanceof DocumentException)
|
|
|
|
|
fault.addElement("faultcode").setText( "soap:Client" );
|
|
|
|
|
else
|
|
|
|
|
fault.addElement("faultcode").setText( "soap:Server" );
|
|
|
|
|
// The fault message
|
|
|
|
|
if( e.getMessage() == null || e.getMessage().isEmpty())
|
|
|
|
|
fault.addElement("faultstring").setText( ""+e.getClass().getSimpleName() );
|
|
|
|
|
else
|
|
|
|
|
fault.addElement("faultstring").setText( ""+e.getMessage() );
|
|
|
|
|
e.printStackTrace(MultiPrintStream.out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return document;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace(MultiPrintStream.out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Takes an XML Element and invokes all the
|
|
|
|
|
* chide Elements as methods.
|
|
|
|
|
*
|
|
|
|
|
* @param obj is the object that the methods will be called from
|
|
|
|
|
* @param requestRoot is the Element where the children lies
|
|
|
|
|
* @param responseRoot is the root element of the response
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private void prepareInvoke(Object obj, Element requestRoot, Element responseRoot) throws Throwable{
|
|
|
|
|
Iterator<Element> it = requestRoot.elementIterator();
|
|
|
|
|
while( it.hasNext() ){
|
|
|
|
|
Element e = it.next();
|
|
|
|
|
if(methods.containsKey(e.getQName().getName())){
|
|
|
|
|
MethodChasch m = methods.get(e.getQName().getName());
|
|
|
|
|
Object[] params = new Object[m.paramName.length];
|
|
|
|
|
|
|
|
|
|
// Get the param values
|
|
|
|
|
for(int i=0; i<m.paramName.length ;i++){
|
|
|
|
|
if(e.element(m.paramName[i]) != null)
|
|
|
|
|
params[i] = convertToClass(
|
|
|
|
|
e.element(m.paramName[i]).getTextTrim(),
|
|
|
|
|
m.method.getParameterTypes()[i]);
|
|
|
|
|
}
|
|
|
|
|
// MultiPrintStream.out.println("invoking: "+m.method.getName()+" "+MultiPrintStream.out.dumpToString(params));
|
|
|
|
|
// Invoke
|
|
|
|
|
Object ret = invoke(obj, m.method, params);
|
|
|
|
|
|
|
|
|
|
// generate response xml
|
|
|
|
|
if(m.method.getReturnType() != void.class){
|
|
|
|
|
Element response = responseRoot.addElement(m.method.getName()+"Response");
|
|
|
|
|
createReturnXML(response, ret, m.returnName, m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
throw new Exception("No such method: "+e.getQName().getName()+"!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates an return XML Element. This function can
|
|
|
|
|
* handle return values as XML Elements, SOAPObject and the
|
|
|
|
|
* Java basic data types.
|
|
|
|
|
*
|
|
|
|
|
* @param root is the parent Element
|
|
|
|
|
* @param ret is the object that is the return value
|
|
|
|
|
* @param ename is the name of the parent Element
|
|
|
|
|
* @param m is the method that returned the ret
|
|
|
|
|
*/
|
|
|
|
|
private void createReturnXML(Element root, Object ret, String ename, MethodChasch m) throws IllegalArgumentException, IllegalAccessException{
|
2009-05-17 18:46:05 +00:00
|
|
|
if(ret == null) return;
|
|
|
|
|
if(byte[].class.isAssignableFrom(ret.getClass())){
|
|
|
|
|
Element valueE = root.addElement( ename );
|
|
|
|
|
valueE.addAttribute("type", "xsd:"+getClassSOAPName(ret.getClass()));
|
|
|
|
|
String tmp = new sun.misc.BASE64Encoder().encode((byte[])ret);
|
|
|
|
|
tmp = tmp.replaceAll("\\s", "");
|
|
|
|
|
valueE.setText(tmp);
|
|
|
|
|
}
|
2009-04-22 18:34:11 +00:00
|
|
|
// return an array
|
2009-05-17 18:46:05 +00:00
|
|
|
else if(ret.getClass().isArray()){
|
2009-04-22 18:34:11 +00:00
|
|
|
Element array = root.addElement( (ename.equals("element") ? "Array" : ename) );
|
2009-05-17 18:46:05 +00:00
|
|
|
String arrayType = "xsd:"+getClassSOAPName(ret.getClass());
|
2009-04-22 18:34:11 +00:00
|
|
|
arrayType = arrayType.replaceFirst("\\[\\]", "["+Array.getLength(ret)+"]");
|
|
|
|
|
|
|
|
|
|
array.addAttribute("type", "soap:Array");
|
|
|
|
|
array.addAttribute("soap:arrayType", arrayType);
|
|
|
|
|
for(int i=0; i<Array.getLength(ret) ;i++){
|
|
|
|
|
createReturnXML(array, Array.get(ret, i), "element", m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
if(ret instanceof Element)
|
|
|
|
|
root.add( (Element)ret );
|
|
|
|
|
if(ret instanceof SOAPObject){
|
2009-05-17 18:46:05 +00:00
|
|
|
Element objectE = root.addElement( getClassSOAPName(ret.getClass()) );
|
2009-04-22 18:34:11 +00:00
|
|
|
Field[] fields = ret.getClass().getFields();
|
|
|
|
|
for(int i=0; i<fields.length ;i++){
|
|
|
|
|
SOAPFieldName tmp = fields[i].getAnnotation(SOAPObject.SOAPFieldName.class);
|
|
|
|
|
String name;
|
|
|
|
|
if(tmp != null) name = tmp.value();
|
|
|
|
|
else name = "field"+i;
|
|
|
|
|
createReturnXML(objectE, fields[i].get(ret), name, m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Element valueE = root.addElement( ename );
|
2009-05-17 18:46:05 +00:00
|
|
|
valueE.addAttribute("type", "xsd:"+getClassSOAPName(ret.getClass()));
|
2009-04-22 18:34:11 +00:00
|
|
|
valueE.addText( ""+ret );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts an given String to a specified class
|
|
|
|
|
*/
|
2009-05-17 18:46:05 +00:00
|
|
|
protected Object convertToClass(String data, Class<?> c) throws IOException{
|
2009-04-22 18:34:11 +00:00
|
|
|
if(data == null || data.isEmpty())
|
|
|
|
|
return null;
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2009-04-22 18:34:11 +00:00
|
|
|
if( c == String.class) return data;
|
|
|
|
|
else if(c == Integer.class) return Integer.parseInt(data);
|
|
|
|
|
else if(c == int.class) return Integer.parseInt(data);
|
|
|
|
|
else if(c == Long.class) return Long.parseLong(data);
|
|
|
|
|
else if(c == long.class) return Long.parseLong(data);
|
|
|
|
|
else if(c == Float.class) return Float.parseFloat(data);
|
|
|
|
|
else if(c == float.class) return Float.parseFloat(data);
|
|
|
|
|
else if(c == Double.class) return Double.parseDouble(data);
|
|
|
|
|
else if(c == double.class) return Double.parseDouble(data);
|
|
|
|
|
else if(c == Boolean.class) return Boolean.parseBoolean(data);
|
|
|
|
|
else if(c == boolean.class) return Boolean.parseBoolean(data);
|
|
|
|
|
else if(c == Byte.class) return Byte.parseByte(data);
|
|
|
|
|
else if(c == byte.class) return Byte.parseByte(data);
|
2009-05-17 18:46:05 +00:00
|
|
|
else if(byte[].class.isAssignableFrom(c))
|
|
|
|
|
return new sun.misc.BASE64Decoder().decodeBuffer(data);
|
2009-04-22 18:34:11 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invokes a specified method
|
|
|
|
|
*
|
|
|
|
|
* @param m is the function
|
|
|
|
|
* @param params a vector with arguments
|
|
|
|
|
* @throws Throwable
|
|
|
|
|
*/
|
2009-05-17 18:46:05 +00:00
|
|
|
protected Object invoke(Object obj, Method m, Object[] params) throws Throwable{
|
2009-04-22 18:34:11 +00:00
|
|
|
try {
|
|
|
|
|
return m.invoke(obj, params );
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
throw new SOAPClientException("Arguments missing for "+m.getName()+"!");
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
|
throw e.getCause();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts an String XML to an Element
|
|
|
|
|
*
|
|
|
|
|
* @param msg is the string XML
|
|
|
|
|
* @return the XML root Element
|
|
|
|
|
*/
|
2009-05-17 18:46:05 +00:00
|
|
|
private Element getXMLRoot(String xml) throws Exception {
|
2009-04-22 18:34:11 +00:00
|
|
|
if(xml != null && !xml.isEmpty()){
|
|
|
|
|
Document document = DocumentHelper.parseText(xml);
|
|
|
|
|
return document.getRootElement();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
|
|
|
|
|
private String getClassSOAPName(Class<?> c){
|
|
|
|
|
Class<?> cTmp = getClass(c);
|
|
|
|
|
if(byte[].class.isAssignableFrom(c)){
|
|
|
|
|
return "base64Binary";
|
|
|
|
|
}
|
|
|
|
|
else if( SOAPObject.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates an WSDL document for the class
|
|
|
|
|
*
|
|
|
|
|
* @throws WSDLException
|
|
|
|
|
*/
|
|
|
|
|
private void generateWSDL() throws WSDLException{
|
|
|
|
|
ArrayList<Class<?>> types = new ArrayList<Class<?>>();
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2009-04-22 18:34:11 +00:00
|
|
|
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";
|
2009-05-17 18:46:05 +00:00
|
|
|
|
|
|
|
|
PopulatedExtensionRegistry extReg = new PopulatedExtensionRegistry();
|
|
|
|
|
WSDLFactory factory = WSDLFactory.newInstance();
|
2009-04-22 18:34:11 +00:00
|
|
|
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(false);
|
|
|
|
|
Part epart = wsdl.createPart();
|
|
|
|
|
epart.setName("message");
|
|
|
|
|
epart.setTypeName(new QName(xsd, "string"));
|
|
|
|
|
exception.addPart(epart);
|
|
|
|
|
wsdl.addMessage(exception);
|
|
|
|
|
|
|
|
|
|
// Types import
|
|
|
|
|
Import imp = wsdl.createImport();
|
|
|
|
|
imp.setNamespaceURI(td);
|
|
|
|
|
imp.setLocationURI(td);
|
|
|
|
|
wsdl.addImport(imp);
|
2009-05-17 18:46:05 +00:00
|
|
|
|
2009-04-22 18:34:11 +00:00
|
|
|
// PrtType
|
|
|
|
|
PortType portType = wsdl.createPortType();
|
|
|
|
|
portType.setQName(new QName(tns, portTypeName));
|
|
|
|
|
portType.setUndefined(false);
|
|
|
|
|
for(MethodChasch 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
|
|
|
|
|
WSDLParamDocumentation tmpParamDoc = m.method.getAnnotation(SOAPInterface.WSDLParamDocumentation.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]);
|
2009-05-17 18:46:05 +00:00
|
|
|
part.setTypeName(new QName( xsd,
|
|
|
|
|
getClassSOAPName(m.method.getParameterTypes()[i])));
|
2009-04-22 18:34:11 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
//********** Response Message
|
|
|
|
|
if(!m.method.getReturnType().equals( void.class )){
|
|
|
|
|
Message msgOut = wsdl.createMessage();
|
|
|
|
|
msgOut.setQName(new QName(tns, m.method.getName()+"Response"));
|
|
|
|
|
msgOut.setUndefined(false);
|
|
|
|
|
|
|
|
|
|
// Parts
|
|
|
|
|
Part part = wsdl.createPart();
|
|
|
|
|
part.setName(m.returnName);
|
|
|
|
|
msgOut.addPart(part);
|
|
|
|
|
|
|
|
|
|
// Generate new type if the object is an SOAPObject
|
|
|
|
|
Class<?> cTmp = getClass(m.method.getReturnType());
|
2009-05-17 18:46:05 +00:00
|
|
|
if(byte[].class.isAssignableFrom(m.method.getReturnType())){
|
|
|
|
|
part.setTypeName(new QName(xsd, "base64Binary"));
|
|
|
|
|
}
|
|
|
|
|
// is an array?
|
|
|
|
|
else if(m.method.getReturnType().isArray()){
|
|
|
|
|
part.setTypeName(new QName(td,
|
|
|
|
|
"ArrayOf"+getClassSOAPName(m.method.getReturnType()).replaceAll("[\\[\\]]", "")));
|
|
|
|
|
// add to type generation list
|
|
|
|
|
if(!types.contains(m.method.getReturnType()))
|
|
|
|
|
types.add(m.method.getReturnType());
|
|
|
|
|
}
|
|
|
|
|
else if( SOAPObject.class.isAssignableFrom(cTmp) ){
|
|
|
|
|
// its an SOAPObject
|
|
|
|
|
part.setTypeName(new QName(td, getClassSOAPName(m.method.getReturnType())));
|
2009-04-22 18:34:11 +00:00
|
|
|
// add to type generation list
|
|
|
|
|
if(!types.contains(cTmp))
|
|
|
|
|
types.add(cTmp);
|
|
|
|
|
}
|
2009-05-17 18:46:05 +00:00
|
|
|
else{// its an Object
|
|
|
|
|
part.setTypeName(new QName(xsd, getClassSOAPName(m.method.getReturnType())));
|
2009-04-22 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
WSDLDocumentation tmpDoc = m.method.getAnnotation(SOAPInterface.WSDLDocumentation.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");
|
2009-05-17 18:46:05 +00:00
|
|
|
//soapBinding.setRequired(true);
|
2009-04-22 18:34:11 +00:00
|
|
|
soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
|
|
|
|
|
binding.addExtensibilityElement(soapBinding);
|
|
|
|
|
|
|
|
|
|
for(MethodChasch 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("");
|
|
|
|
|
operation.addExtensibilityElement(soapOperation);
|
|
|
|
|
|
|
|
|
|
// input
|
|
|
|
|
if(m.paramName.length > 0){
|
|
|
|
|
BindingInput input = wsdl.createBindingInput();
|
|
|
|
|
// Header
|
|
|
|
|
if(m.header){
|
|
|
|
|
SOAPHeader soapHeader = (SOAPHeader)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_HEADER);
|
|
|
|
|
soapHeader.setUse("literal");
|
|
|
|
|
input.addExtensibilityElement(soapHeader);
|
|
|
|
|
}// Body
|
|
|
|
|
else{
|
|
|
|
|
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
|
|
|
|
|
soapBody.setUse("literal");
|
|
|
|
|
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");
|
|
|
|
|
output.addExtensibilityElement(soapHeader);
|
|
|
|
|
}// Body
|
|
|
|
|
else{
|
|
|
|
|
SOAPBody soapBody = (SOAPBody)extReg.createExtension(BindingInput.class, SOAPConstants.Q_ELEM_SOAP_BODY);
|
|
|
|
|
soapBody.setUse("literal");
|
|
|
|
|
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();
|
2009-05-17 18:46:05 +00:00
|
|
|
ser.setQName(new QName(tns, interf.getClass().getSimpleName()+"Service"));
|
2009-04-22 18:34:11 +00:00
|
|
|
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" );
|
2009-05-17 18:46:05 +00:00
|
|
|
definitions.addAttribute("targetNamespace", url+"?type");
|
2009-04-22 18:34:11 +00:00
|
|
|
definitions.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
|
2009-05-17 18:46:05 +00:00
|
|
|
definitions.addNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
|
|
|
|
|
definitions.addNamespace("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/");
|
2009-04-22 18:34:11 +00:00
|
|
|
|
|
|
|
|
Element typeE = definitions.addElement("wsdl:types");
|
|
|
|
|
Element schema = typeE.addElement("xsd:schema");
|
|
|
|
|
|
|
|
|
|
for(int n=0; n<types.size() ;n++){
|
|
|
|
|
Class<?> c = types.get(n);
|
2009-05-17 18:46:05 +00:00
|
|
|
// Generate Array type
|
|
|
|
|
if(c.isArray()){
|
|
|
|
|
Class<?> ctmp = getClass(c);
|
|
|
|
|
|
|
|
|
|
Element type = schema.addElement("xsd:complexType");
|
|
|
|
|
type.addAttribute("name",
|
|
|
|
|
"ArrayOf"+getClassSOAPName(c).replaceAll("[\\[\\]]", ""));
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
if(!types.contains(ctmp))
|
|
|
|
|
types.add(ctmp);
|
|
|
|
|
}
|
|
|
|
|
// Generate SOAPObject type
|
|
|
|
|
else if(SOAPObject.class.isAssignableFrom(c)){
|
|
|
|
|
Element type = schema.addElement("xsd:complexType");
|
|
|
|
|
type.addAttribute("name", getClassSOAPName(c));
|
2009-04-22 18:34:11 +00:00
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
Element sequence = type.addElement("xsd:sequence");
|
2009-04-22 18:34:11 +00:00
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
Field[] fields = c.getFields();
|
|
|
|
|
for(int i=0; i<fields.length ;i++){
|
|
|
|
|
SOAPFieldName tmp = fields[i].getAnnotation(SOAPObject.SOAPFieldName.class);
|
|
|
|
|
String name;
|
|
|
|
|
if(tmp != null) name = tmp.value();
|
|
|
|
|
else name = "field"+i;
|
2009-04-22 18:34:11 +00:00
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
Element element = sequence.addElement("xsd:element");
|
|
|
|
|
element.addAttribute("name", name);
|
2009-04-22 18:34:11 +00:00
|
|
|
|
2009-05-17 18:46:05 +00:00
|
|
|
// Check if the object is an SOAPObject
|
|
|
|
|
Class<?> cTmp = getClass(fields[i].getType());
|
|
|
|
|
if(SOAPObject.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");
|
2009-04-22 18:34:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Class<?> getClass(Class<?> c){
|
|
|
|
|
if(c.isArray()){
|
|
|
|
|
return getClass(c.getComponentType());
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|