2011-07-13 17:53:17 +00:00
|
|
|
/*******************************************************************************
|
2013-02-06 17:44:56 +00:00
|
|
|
* Copyright (c) 2013 Ziver Koc
|
2011-07-13 17:53:17 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
******************************************************************************/
|
2011-02-15 19:37:35 +00:00
|
|
|
package zutil.net.http.soap;
|
2010-08-13 22:36:08 +00:00
|
|
|
|
2013-02-06 17:44:56 +00:00
|
|
|
import java.util.HashMap;
|
2013-02-05 17:15:53 +00:00
|
|
|
import java.util.List;
|
2013-02-06 17:44:56 +00:00
|
|
|
import java.util.logging.Logger;
|
2013-02-05 17:15:53 +00:00
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
import javax.wsdl.WSDLException;
|
|
|
|
|
|
|
|
|
|
import javassist.CannotCompileException;
|
|
|
|
|
import javassist.ClassPool;
|
|
|
|
|
import javassist.CtClass;
|
2013-02-06 17:44:56 +00:00
|
|
|
import javassist.CtField;
|
2010-08-13 22:36:08 +00:00
|
|
|
import javassist.CtMethod;
|
|
|
|
|
import javassist.CtNewMethod;
|
|
|
|
|
import javassist.NotFoundException;
|
2013-02-06 17:44:56 +00:00
|
|
|
import zutil.log.LogUtil;
|
2011-02-15 19:37:35 +00:00
|
|
|
import zutil.net.ws.WSInterface;
|
|
|
|
|
import zutil.net.ws.WSMethodDef;
|
2013-02-05 17:15:53 +00:00
|
|
|
import zutil.net.ws.WSParameterDef;
|
2011-02-15 19:37:35 +00:00
|
|
|
import zutil.net.ws.WebServiceDef;
|
2010-08-13 22:36:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is an factory that generates clients for web services
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
|
|
|
|
public class SOAPClientFactory {
|
2013-02-06 17:44:56 +00:00
|
|
|
private static Logger logger = LogUtil.getLogger();
|
2010-08-13 22:36:08 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a Client Object for the web service.
|
|
|
|
|
*
|
2013-02-05 17:15:53 +00:00
|
|
|
* @param <T> is the class of the web service definition
|
|
|
|
|
* @param intf is the class of the web service definition
|
2010-08-13 22:36:08 +00:00
|
|
|
* @return a client Object
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public static <T> T getClient(Class<T> intf) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{
|
|
|
|
|
if( !WSInterface.class.isAssignableFrom( intf )){
|
|
|
|
|
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
|
|
|
|
|
}
|
|
|
|
|
return getClient( intf, new WebServiceDef((Class<? extends WSInterface>)intf) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates a Client Object for the web service.
|
|
|
|
|
*
|
2013-02-05 17:15:53 +00:00
|
|
|
* @param <T> is the class of the web service definition
|
|
|
|
|
* @param intf is the class of the web service definition
|
|
|
|
|
* @param wsDef is the web service definition of the intf parameter
|
2010-08-13 22:36:08 +00:00
|
|
|
* @return a client Object
|
|
|
|
|
*/
|
|
|
|
|
public static <T> T getClient(Class<T> intf, WebServiceDef wsDef) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException{
|
|
|
|
|
if( !WSInterface.class.isAssignableFrom( intf )){
|
|
|
|
|
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate the class
|
|
|
|
|
ClassPool pool = ClassPool.getDefault();
|
|
|
|
|
CtClass intfClass = pool.get( intf.getName() );
|
2013-02-06 17:44:56 +00:00
|
|
|
CtClass cc = pool.makeClass(intf.getName()+"Impl_"+ (int)(Math.random()*10000));
|
2010-08-13 22:36:08 +00:00
|
|
|
|
|
|
|
|
// Is intf an interface
|
|
|
|
|
if( intf.isInterface() ){
|
|
|
|
|
cc.addInterface( intfClass );
|
|
|
|
|
}
|
|
|
|
|
// or a class
|
|
|
|
|
else{
|
|
|
|
|
cc.setSuperclass( intfClass );
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-06 17:44:56 +00:00
|
|
|
// Add the logger class
|
|
|
|
|
CtField logger = CtField.make(
|
|
|
|
|
"private static "+Logger.class.getName()+" logger = "+LogUtil.class.getName()+".getLogger();",
|
|
|
|
|
cc);
|
|
|
|
|
cc.addField(logger);
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
// Generate the methods
|
2013-02-06 17:44:56 +00:00
|
|
|
for(WSMethodDef methodDef : wsDef.getMethods()){
|
|
|
|
|
// Create method
|
2013-02-05 17:15:53 +00:00
|
|
|
CtMethod method = CtNewMethod.make(
|
|
|
|
|
getOutputClass(methodDef.getOutputs()), // Return type
|
|
|
|
|
methodDef.getName(), // Method name
|
|
|
|
|
getParameterClasses(methodDef.getInputs()), // Parameters
|
2013-02-06 17:44:56 +00:00
|
|
|
new CtClass[]{pool.get( SOAPException.class.getName() )}, // Exceptions
|
|
|
|
|
generateCodeBody(methodDef), // Code Body
|
|
|
|
|
cc); // Class
|
2013-02-05 17:15:53 +00:00
|
|
|
cc.addMethod(method);
|
2010-08-13 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initiate the class
|
2013-02-05 17:15:53 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2010-08-13 22:36:08 +00:00
|
|
|
Class<T> c = cc.toClass();
|
|
|
|
|
T obj = c.newInstance();
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2013-02-05 17:15:53 +00:00
|
|
|
|
2013-02-06 17:44:56 +00:00
|
|
|
/**
|
|
|
|
|
* Generates a generic method code body that calls the SOAPAbstractClient class
|
|
|
|
|
*/
|
|
|
|
|
private static String generateCodeBody(WSMethodDef m) {
|
|
|
|
|
logger.finer("Generating method "+m.getName()+"(...)");
|
|
|
|
|
|
|
|
|
|
StringBuilder body = new StringBuilder("{\n");
|
|
|
|
|
// Logging
|
|
|
|
|
body.append( "logger.fine(\"Executing method: "+m.getName()+"(...)\");\n" );
|
|
|
|
|
|
|
|
|
|
// Generate parameter list
|
|
|
|
|
body.append( HashMap.class.getName()+"<String,Object> params = new "+HashMap.class.getName()+"<String,Object>();\n" );
|
|
|
|
|
for(WSParameterDef param : m.getInputs()){
|
|
|
|
|
body.append( "params.put(\""+param.getName()+"\", "+param.getName()+");\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Call SOAPAbstractClient class
|
|
|
|
|
if(m.getOutputCount() > 0) // non void function
|
|
|
|
|
body.append( "return " );
|
|
|
|
|
body.append( SOAPAbstractClient.class.getName()+".request(\""+m.getName()+"\", params);\n" );
|
|
|
|
|
|
|
|
|
|
body.append("}");
|
|
|
|
|
logger.finest("###################### BODY #########################");
|
|
|
|
|
logger.finest(body.toString());
|
|
|
|
|
logger.finest("#######################################################");
|
|
|
|
|
return body.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 17:15:53 +00:00
|
|
|
private static CtClass getParameterClass(WSParameterDef param) throws NotFoundException{
|
|
|
|
|
return ClassPool.getDefault().get( param.getClass().getName() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CtClass[] getParameterClasses(List<WSParameterDef> params) throws NotFoundException{
|
|
|
|
|
CtClass[] c = new CtClass[params.size()];
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
for(WSParameterDef param : params){
|
|
|
|
|
c[i++] = getParameterClass(param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CtClass getOutputClass(List<WSParameterDef> params) throws NotFoundException{
|
|
|
|
|
if(params.isEmpty()){
|
|
|
|
|
return CtClass.voidType;
|
|
|
|
|
}
|
|
|
|
|
else if(params.size() == 1){
|
|
|
|
|
return ClassPool.getDefault().get( params.get(0).getClass().getName() );
|
|
|
|
|
}
|
|
|
|
|
throw new IllegalArgumentException("Unknown return type");
|
|
|
|
|
}
|
2010-08-13 22:36:08 +00:00
|
|
|
}
|