Bugfix and continued implementation of soap client

This commit is contained in:
Ziver Koc 2013-02-06 17:44:56 +00:00
parent a64464593a
commit 7bbec55626
6 changed files with 112 additions and 18 deletions

View file

@ -51,10 +51,8 @@ public class LogUtil {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
for(int i=1; i<stackTraceElements.length ;++i){ for(int i=1; i<stackTraceElements.length ;++i){
String name = stackTraceElements[i].getClassName(); String name = stackTraceElements[i].getClassName();
//name = name.substring( name.lastIndexOf('.')+1 );
if( !name.equals( LogUtil.class.getName() ) ){ if( !name.equals( LogUtil.class.getName() ) ){
//System.out.println("\""+name+"\""); logger.fine("Calling class: \""+name+"\"");
logger.fine("Caling class: \""+name+"\"");
return name; return name;
} }
} }

View file

@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2013 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.http.soap;
import java.util.HashMap;
/**
* This is an abstract client that will do generic requests to a
* SOAP Web service
*
* @author Ziver
*/
public class SOAPAbstractClient {
/** Web address of the web service */
protected String url;
/**
* Makes a request to the target web service
* @return
*/
public static Object request(String methodName, HashMap<String,Object> input){
return null;
}
}

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2011 Ziver Koc * Copyright (c) 2013 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -21,16 +21,20 @@
******************************************************************************/ ******************************************************************************/
package zutil.net.http.soap; package zutil.net.http.soap;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
import javax.wsdl.WSDLException; import javax.wsdl.WSDLException;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.ClassPool; import javassist.ClassPool;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod; import javassist.CtMethod;
import javassist.CtNewMethod; import javassist.CtNewMethod;
import javassist.NotFoundException; import javassist.NotFoundException;
import zutil.log.LogUtil;
import zutil.net.ws.WSInterface; import zutil.net.ws.WSInterface;
import zutil.net.ws.WSMethodDef; import zutil.net.ws.WSMethodDef;
import zutil.net.ws.WSParameterDef; import zutil.net.ws.WSParameterDef;
@ -40,9 +44,9 @@ import zutil.net.ws.WebServiceDef;
* This is an factory that generates clients for web services * This is an factory that generates clients for web services
* *
* @author Ziver * @author Ziver
* TODO: Incomplete
*/ */
public class SOAPClientFactory { public class SOAPClientFactory {
private static Logger logger = LogUtil.getLogger();
/** /**
* Generates a Client Object for the web service. * Generates a Client Object for the web service.
@ -74,9 +78,8 @@ public class SOAPClientFactory {
// Generate the class // Generate the class
ClassPool pool = ClassPool.getDefault(); ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(intf.getName()+"Impl_"+Math.random());
CtClass intfClass = pool.get( intf.getName() ); CtClass intfClass = pool.get( intf.getName() );
CtClass cc = pool.makeClass(intf.getName()+"Impl_"+ (int)(Math.random()*10000));
// Is intf an interface // Is intf an interface
if( intf.isInterface() ){ if( intf.isInterface() ){
@ -87,15 +90,22 @@ public class SOAPClientFactory {
cc.setSuperclass( intfClass ); cc.setSuperclass( intfClass );
} }
// Add the logger class
CtField logger = CtField.make(
"private static "+Logger.class.getName()+" logger = "+LogUtil.class.getName()+".getLogger();",
cc);
cc.addField(logger);
// Generate the methods // Generate the methods
for(WSMethodDef methodDef : wsDef.getMethods()){ for(WSMethodDef methodDef : wsDef.getMethods()){
// Create method
CtMethod method = CtNewMethod.make( CtMethod method = CtNewMethod.make(
getOutputClass(methodDef.getOutputs()), // Return type getOutputClass(methodDef.getOutputs()), // Return type
methodDef.getName(), // Method name methodDef.getName(), // Method name
getParameterClasses(methodDef.getInputs()), // Parameters getParameterClasses(methodDef.getInputs()), // Parameters
new CtClass[]{pool.get("zutil.net.http.soap.SOAPException")}, // Exceptions new CtClass[]{pool.get( SOAPException.class.getName() )}, // Exceptions
"System.out.println(\"Hello.say():\");", generateCodeBody(methodDef), // Code Body
cc); // Class cc); // Class
cc.addMethod(method); cc.addMethod(method);
} }
@ -107,6 +117,34 @@ public class SOAPClientFactory {
return obj; return obj;
} }
/**
* 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();
}
private static CtClass getParameterClass(WSParameterDef param) throws NotFoundException{ private static CtClass getParameterClass(WSParameterDef param) throws NotFoundException{
return ClassPool.getDefault().get( param.getClass().getName() ); return ClassPool.getDefault().get( param.getClass().getName() );
} }

View file

@ -283,11 +283,15 @@ public class SOAPHttpPage implements HttpPage{
response.addNamespace("m", m.getNamespace() ); response.addNamespace("m", m.getNamespace() );
response.setName("m:"+m.getName()+"Response"); response.setName("m:"+m.getName()+"Response");
// TODO: problem does not work for other returns than WSReturnObject if( ret instanceof WSReturnObject ){
Field[] f = ret.getClass().getFields(); Field[] f = ret.getClass().getFields();
for(int i=0; i<m.getOutputCount() ;i++){ for(int i=0; i<m.getOutputCount() ;i++){
WSParameterDef param = m.getOutput( i ); WSParameterDef param = m.getOutput( i );
generateReturnXML(response,((WSReturnObject)ret).getValue(f[i]) , param.getName()); generateReturnXML(response,((WSReturnObject)ret).getValue(f[i]) , param.getName());
}
}
else{
generateReturnXML(response, ret, m.getOutput(0).getName());
} }
} }
} }

View file

@ -21,24 +21,34 @@
******************************************************************************/ ******************************************************************************/
package zutil.test; package zutil.test;
import java.util.logging.Level;
import javassist.CannotCompileException; import javassist.CannotCompileException;
import javassist.NotFoundException; import javassist.NotFoundException;
import javax.wsdl.WSDLException; import javax.wsdl.WSDLException;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
import zutil.net.http.soap.SOAPClientFactory; import zutil.net.http.soap.SOAPClientFactory;
import zutil.net.ws.WSInterface; import zutil.net.ws.WSInterface;
public class SOAPClientTest { public class SOAPClientTest {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{ public static void main(String[] args) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{
LogUtil.setGlobalLevel(Level.ALL);
LogUtil.setFormatter("", new CompactLogFormatter());
TestClient intf = SOAPClientFactory.getClient(TestClient.class); TestClient intf = SOAPClientFactory.getClient(TestClient.class);
System.out.println("GenClass: "+intf.getClass().getName());
intf.m(); intf.m();
intf.c();
} }
public interface TestClient extends WSInterface{ public interface TestClient extends WSInterface{
public void m(); public void m();
public void c();
} }
} }

View file

@ -110,7 +110,7 @@ public class SOAPTest {
@WSParamName(value="otherParam1", optional=true) int param1, @WSParamName(value="otherParam1", optional=true) int param1,
@WSParamName(value="otherParam2", optional=true) int param2) throws Exception{ @WSParamName(value="otherParam2", optional=true) int param2) throws Exception{
System.out.println("Executing method: exceptionMethod()"); System.out.println("Executing method: exceptionMethod()");
throw new Exception("Ziver is the fizle"); throw new Exception("This is an Exception");
} }
@WSReturnName("stringArray") @WSReturnName("stringArray")