Some progress on SOAPClient

This commit is contained in:
Ziver Koc 2013-02-05 17:15:53 +00:00
parent cc4cef5f4e
commit a64464593a
5 changed files with 144 additions and 60 deletions

View file

@ -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 <T> is the class of the web service definition
* @param intf is the class of the web service definition
* @param <T> 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 <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
* @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
* @return a client Object
*/
@SuppressWarnings("unchecked")
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!");
@ -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<T> 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<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");
}
}

View file

@ -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<m.getOutputCount() ;i++){
WSParameterDef param = m.getOutput( i );

View file

@ -125,7 +125,7 @@ public class WSMethodDef {
outputs.add( ret_param );
}
}
else{
else if( method.getReturnType() != void.class ){
WSParameterDef ret_param = new WSParameterDef( this );
if(returnName != null)
ret_param.setName(returnName.value());

View file

@ -0,0 +1,44 @@
/*******************************************************************************
* 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.test;
import javassist.CannotCompileException;
import javassist.NotFoundException;
import javax.wsdl.WSDLException;
import zutil.net.http.soap.SOAPClientFactory;
import zutil.net.ws.WSInterface;
public class SOAPClientTest {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException, WSDLException{
TestClient intf = SOAPClientFactory.getClient(TestClient.class);
System.out.println("GenClass: "+intf.getClass().getName());
intf.m();
}
public interface TestClient extends WSInterface{
public void m();
}
}

View file

@ -43,7 +43,7 @@ public class SOAPTest {
}
public SOAPTest(){
WebServiceDef wsDef = new WebServiceDef( SOAPTestClass.class );
WebServiceDef wsDef = new WebServiceDef( MainSOAPClass.class );
SOAPHttpPage soap = new SOAPHttpPage( wsDef );
WSDLWriterOld wsdl = new WSDLWriterOld( wsDef );
@ -53,23 +53,22 @@ public class SOAPTest {
wsdl2.write(System.out);
// Response
try {
try {
System.out.println( "****************** LOG *********************" );
Document document = soap.genSOAPResponse(
"<?xml version=\"1.0\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body xmlns:m=\"http://www.example.org/stock\">\n" +
//" <m:pubA>\n" +
//" <m:Ztring>IBM</m:Ztring>\n" +
//" </m:pubA>\n" +
//" <m:pubZ>\n" +
//" <m:olle>66</m:olle>\n" +
//" </m:pubZ>\n" +
" <m:pubB>\n" +
" <m:stringArrayMethod>\n" +
" <m:StringName>IBM</m:StringName>\n" +
" </m:stringArrayMethod>\n" +
" <m:simpleReturnClassMethod>\n" +
" <m:byte>IBM</m:byte>\n" +
" </m:pubB>\n" +
" </m:simpleReturnClassMethod>\n" +
" </soap:Body>\n" +
"</soap:Envelope>");
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(){ }
}
}