From a64464593a30dc07135abcd58cc51595ebdd2191 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 5 Feb 2013 17:15:53 +0000 Subject: [PATCH] Some progress on SOAPClient --- .../net/http/soap/SOAPClientFactory.java | 51 +++++++-- src/zutil/net/http/soap/SOAPHttpPage.java | 1 + src/zutil/net/ws/WSMethodDef.java | 2 +- src/zutil/test/SOAPClientTest.java | 44 ++++++++ src/zutil/test/SOAPTest.java | 106 +++++++++--------- 5 files changed, 144 insertions(+), 60 deletions(-) create mode 100644 src/zutil/test/SOAPClientTest.java diff --git a/src/zutil/net/http/soap/SOAPClientFactory.java b/src/zutil/net/http/soap/SOAPClientFactory.java index cf7401e..0aa1e22 100644 --- a/src/zutil/net/http/soap/SOAPClientFactory.java +++ b/src/zutil/net/http/soap/SOAPClientFactory.java @@ -21,6 +21,8 @@ ******************************************************************************/ package zutil.net.http.soap; +import java.util.List; + import javax.wsdl.WSDLException; import javassist.CannotCompileException; @@ -31,6 +33,7 @@ import javassist.CtNewMethod; import javassist.NotFoundException; import zutil.net.ws.WSInterface; import zutil.net.ws.WSMethodDef; +import zutil.net.ws.WSParameterDef; import zutil.net.ws.WebServiceDef; /** @@ -44,8 +47,8 @@ public class SOAPClientFactory { /** * Generates a Client Object for the web service. * - * @param is the class of the web service definition - * @param intf is the class of the web service definition + * @param is the class of the web service definition + * @param intf is the class of the web service definition * @return a client Object */ @SuppressWarnings("unchecked") @@ -59,12 +62,11 @@ public class SOAPClientFactory { /** * Generates a Client Object for the web service. * - * @param 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 + * @param 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 * @return a client Object */ - @SuppressWarnings("unchecked") public static T getClient(Class 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!"); @@ -86,16 +88,47 @@ public class SOAPClientFactory { } // Generate the methods - // TODO: for(WSMethodDef methodDef : wsDef.getMethods()){ - CtMethod method = CtNewMethod.make("public int m(int i){}", cc); - method.insertBefore("System.out.println(\"Hello.say():\");"); + CtMethod method = CtNewMethod.make( + getOutputClass(methodDef.getOutputs()), // Return type + methodDef.getName(), // Method name + getParameterClasses(methodDef.getInputs()), // Parameters + new CtClass[]{pool.get("zutil.net.http.soap.SOAPException")}, // Exceptions + "System.out.println(\"Hello.say():\");", + cc); // Class + cc.addMethod(method); } // Initiate the class + @SuppressWarnings("unchecked") Class c = cc.toClass(); T obj = c.newInstance(); return obj; } + + private static CtClass getParameterClass(WSParameterDef param) throws NotFoundException{ + return ClassPool.getDefault().get( param.getClass().getName() ); + } + + private static CtClass[] getParameterClasses(List 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 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"); + } } diff --git a/src/zutil/net/http/soap/SOAPHttpPage.java b/src/zutil/net/http/soap/SOAPHttpPage.java index cc378b0..f6430a7 100644 --- a/src/zutil/net/http/soap/SOAPHttpPage.java +++ b/src/zutil/net/http/soap/SOAPHttpPage.java @@ -283,6 +283,7 @@ public class SOAPHttpPage implements HttpPage{ response.addNamespace("m", m.getNamespace() ); response.setName("m:"+m.getName()+"Response"); + // TODO: problem does not work for other returns than WSReturnObject Field[] f = ret.getClass().getFields(); for(int i=0; i" + "\n" + " \n" + - //" \n" + - //" IBM\n" + - //" \n" + - //" \n" + - //" 66\n" + - //" \n" + - " \n" + + " \n" + + " IBM\n" + + " \n" + + + " \n" + " IBM\n" + - " \n" + + " \n" + " \n" + ""); - System.out.println( "****************** RESPONSE *********************" ); + System.out.println( "****************** RESPONSE *********************" ); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter( System.out, format ); @@ -81,66 +80,73 @@ public class SOAPTest { } } - public static class SOAPTestClass3 extends WSReturnObject{ - public String lol = "lol11"; - public String lol2 = "lol22"; - } - - public static class SOAPTestClass2 extends WSReturnObject{ - @WSValueName(value="lolz") - public String lol = "lol1"; - @WSValueName("lolx") - public String lol2 = "lol2"; + public static class SpecialReturnClass extends WSReturnObject{ + @WSValueName(value="otherValue1") + public String param1 = "otherValue1"; + @WSValueName("otherValue2") + public String param2 = "otherValue2"; public byte[] b = new byte[]{0x12, 0x23}; - public SOAPTestClass3 l = new SOAPTestClass3(); + public InnerClass inner = new InnerClass(); + } + + public static class InnerClass extends WSReturnObject{ + public String innerClassParam1 = "innerClass1"; + public String innerClassParam2 = "innerClass2"; } - public static class SOAPTestRetClass extends WSReturnObject{ - @WSValueName("retTest") - public String lol = "lol1"; - public String lol2 = "lol2"; + public static class SimpleReturnClass extends WSReturnObject{ + @WSValueName("otherParam1") + public String param1 = "param1"; + public String param2 = "param2"; } @WSNamespace("http://test.se:8080/") - public static class SOAPTestClass implements WSInterface{ - public SOAPTestClass(){} + public static class MainSOAPClass implements WSInterface{ + public MainSOAPClass(){} @WSHeader() - @WSDocumentation("hello") - public void pubZ( - @WSParamName(value="olle", optional=true) int lol, - @WSParamName(value="olle2", optional=true) int lol2) throws Exception{ - //System.out.println("Param: "+lol); + @WSDocumentation("Documentation of method exceptionMethod()") + public void exceptionMethod( + @WSParamName(value="otherParam1", optional=true) int param1, + @WSParamName(value="otherParam2", optional=true) int param2) throws Exception{ + System.out.println("Executing method: exceptionMethod()"); throw new Exception("Ziver is the fizle"); } - @WSReturnName("param") - @WSParamDocumentation("null is the shizzle") - public String[][] pubA ( - @WSParamName("Ztring") String lol) throws Exception{ - //System.out.println("ParamZ: "+lol); + @WSReturnName("stringArray") + @WSParamDocumentation("Documentation of stringArrayMethod()") + public String[][] stringArrayMethod ( + @WSParamName("StringName") String str) throws Exception{ + System.out.println("Executing method: stringArrayMethod()"); return new String[][]{{"test","test2"},{"test3","test4"}}; } - @WSReturnName("zivarray") - @WSParamDocumentation("null is the bla") - public SOAPTestClass2[] pubX ( - @WSParamName("Ztring") String lol) throws Exception{ - return new SOAPTestClass2[]{new SOAPTestClass2(), new SOAPTestClass2()}; + @WSReturnName("specialReturnClass") + @WSParamDocumentation("Documentation of specialReturnMethod()") + public SpecialReturnClass[] specialReturnMethod ( + @WSParamName("StringName2") String str) throws Exception{ + System.out.println("Executing method: specialReturnMethod()"); + return new SpecialReturnClass[]{new SpecialReturnClass(), new SpecialReturnClass()}; } - @WSReturnName("zivarray") + @WSReturnName("SimpleReturnClass") @WSParamDocumentation("null is the kala") - public SOAPTestRetClass pubB ( + public SimpleReturnClass simpleReturnClassMethod ( @WSParamName("byte") String lol) throws Exception{ - SOAPTestRetClass tmp = new SOAPTestRetClass(); - tmp.lol = "test"; - tmp.lol2 = "test2"; + System.out.println("Executing method: simpleReturnClassMethod()"); + SimpleReturnClass tmp = new SimpleReturnClass(); + tmp.param1 = "newParam1"; + tmp.param2 = "newParam2"; return tmp; } + @WSParamDocumentation("void method documentation") + public void voidMethod (){ } + @WSDisabled() - public void privaZ(){ } - protected void protZ(){ } + public void disabledMethod(){ } + protected void protectedMethod(){ } + @SuppressWarnings("unused") + private void privateMethod(){ } } }