Some progress on SOAPClient
This commit is contained in:
parent
cc4cef5f4e
commit
a64464593a
5 changed files with 144 additions and 60 deletions
|
|
@ -21,6 +21,8 @@
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package zutil.net.http.soap;
|
package zutil.net.http.soap;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.wsdl.WSDLException;
|
import javax.wsdl.WSDLException;
|
||||||
|
|
||||||
import javassist.CannotCompileException;
|
import javassist.CannotCompileException;
|
||||||
|
|
@ -31,6 +33,7 @@ import javassist.CtNewMethod;
|
||||||
import javassist.NotFoundException;
|
import javassist.NotFoundException;
|
||||||
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.WebServiceDef;
|
import zutil.net.ws.WebServiceDef;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -64,7 +67,6 @@ public class SOAPClientFactory {
|
||||||
* @param wsDef is the web service definition of the intf parameter
|
* @param wsDef is the web service definition of the intf parameter
|
||||||
* @return a client Object
|
* @return a client Object
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public static <T> T getClient(Class<T> intf, WebServiceDef wsDef) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException{
|
public static <T> T getClient(Class<T> intf, WebServiceDef wsDef) throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException{
|
||||||
if( !WSInterface.class.isAssignableFrom( intf )){
|
if( !WSInterface.class.isAssignableFrom( intf )){
|
||||||
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
|
throw new ClassCastException("The Web Service class is not a subclass of WSInterface!");
|
||||||
|
|
@ -86,16 +88,47 @@ public class SOAPClientFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the methods
|
// Generate the methods
|
||||||
// TODO:
|
|
||||||
for(WSMethodDef methodDef : wsDef.getMethods()){
|
for(WSMethodDef methodDef : wsDef.getMethods()){
|
||||||
CtMethod method = CtNewMethod.make("public int m(int i){}", cc);
|
CtMethod method = CtNewMethod.make(
|
||||||
method.insertBefore("System.out.println(\"Hello.say():\");");
|
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
|
// Initiate the class
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
Class<T> c = cc.toClass();
|
Class<T> c = cc.toClass();
|
||||||
T obj = c.newInstance();
|
T obj = c.newInstance();
|
||||||
|
|
||||||
return obj;
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -283,6 +283,7 @@ 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
|
||||||
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 );
|
||||||
|
|
|
||||||
|
|
@ -125,7 +125,7 @@ public class WSMethodDef {
|
||||||
outputs.add( ret_param );
|
outputs.add( ret_param );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else if( method.getReturnType() != void.class ){
|
||||||
WSParameterDef ret_param = new WSParameterDef( this );
|
WSParameterDef ret_param = new WSParameterDef( this );
|
||||||
if(returnName != null)
|
if(returnName != null)
|
||||||
ret_param.setName(returnName.value());
|
ret_param.setName(returnName.value());
|
||||||
|
|
|
||||||
44
src/zutil/test/SOAPClientTest.java
Normal file
44
src/zutil/test/SOAPClientTest.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -43,7 +43,7 @@ public class SOAPTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public SOAPTest(){
|
public SOAPTest(){
|
||||||
WebServiceDef wsDef = new WebServiceDef( SOAPTestClass.class );
|
WebServiceDef wsDef = new WebServiceDef( MainSOAPClass.class );
|
||||||
SOAPHttpPage soap = new SOAPHttpPage( wsDef );
|
SOAPHttpPage soap = new SOAPHttpPage( wsDef );
|
||||||
|
|
||||||
WSDLWriterOld wsdl = new WSDLWriterOld( wsDef );
|
WSDLWriterOld wsdl = new WSDLWriterOld( wsDef );
|
||||||
|
|
@ -54,19 +54,18 @@ public class SOAPTest {
|
||||||
|
|
||||||
// Response
|
// Response
|
||||||
try {
|
try {
|
||||||
|
System.out.println( "****************** LOG *********************" );
|
||||||
Document document = soap.genSOAPResponse(
|
Document document = soap.genSOAPResponse(
|
||||||
"<?xml version=\"1.0\"?>" +
|
"<?xml version=\"1.0\"?>" +
|
||||||
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
|
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
|
||||||
" <soap:Body xmlns:m=\"http://www.example.org/stock\">\n" +
|
" <soap:Body xmlns:m=\"http://www.example.org/stock\">\n" +
|
||||||
//" <m:pubA>\n" +
|
" <m:stringArrayMethod>\n" +
|
||||||
//" <m:Ztring>IBM</m:Ztring>\n" +
|
" <m:StringName>IBM</m:StringName>\n" +
|
||||||
//" </m:pubA>\n" +
|
" </m:stringArrayMethod>\n" +
|
||||||
//" <m:pubZ>\n" +
|
|
||||||
//" <m:olle>66</m:olle>\n" +
|
" <m:simpleReturnClassMethod>\n" +
|
||||||
//" </m:pubZ>\n" +
|
|
||||||
" <m:pubB>\n" +
|
|
||||||
" <m:byte>IBM</m:byte>\n" +
|
" <m:byte>IBM</m:byte>\n" +
|
||||||
" </m:pubB>\n" +
|
" </m:simpleReturnClassMethod>\n" +
|
||||||
" </soap:Body>\n" +
|
" </soap:Body>\n" +
|
||||||
"</soap:Envelope>");
|
"</soap:Envelope>");
|
||||||
System.out.println( "****************** RESPONSE *********************" );
|
System.out.println( "****************** RESPONSE *********************" );
|
||||||
|
|
@ -81,66 +80,73 @@ public class SOAPTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class SOAPTestClass3 extends WSReturnObject{
|
public static class SpecialReturnClass extends WSReturnObject{
|
||||||
public String lol = "lol11";
|
@WSValueName(value="otherValue1")
|
||||||
public String lol2 = "lol22";
|
public String param1 = "otherValue1";
|
||||||
}
|
@WSValueName("otherValue2")
|
||||||
|
public String param2 = "otherValue2";
|
||||||
public static class SOAPTestClass2 extends WSReturnObject{
|
|
||||||
@WSValueName(value="lolz")
|
|
||||||
public String lol = "lol1";
|
|
||||||
@WSValueName("lolx")
|
|
||||||
public String lol2 = "lol2";
|
|
||||||
public byte[] b = new byte[]{0x12, 0x23};
|
public byte[] b = new byte[]{0x12, 0x23};
|
||||||
public SOAPTestClass3 l = new SOAPTestClass3();
|
public InnerClass inner = new InnerClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class SOAPTestRetClass extends WSReturnObject{
|
public static class InnerClass extends WSReturnObject{
|
||||||
@WSValueName("retTest")
|
public String innerClassParam1 = "innerClass1";
|
||||||
public String lol = "lol1";
|
public String innerClassParam2 = "innerClass2";
|
||||||
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/")
|
@WSNamespace("http://test.se:8080/")
|
||||||
public static class SOAPTestClass implements WSInterface{
|
public static class MainSOAPClass implements WSInterface{
|
||||||
public SOAPTestClass(){}
|
public MainSOAPClass(){}
|
||||||
|
|
||||||
@WSHeader()
|
@WSHeader()
|
||||||
@WSDocumentation("hello")
|
@WSDocumentation("Documentation of method exceptionMethod()")
|
||||||
public void pubZ(
|
public void exceptionMethod(
|
||||||
@WSParamName(value="olle", optional=true) int lol,
|
@WSParamName(value="otherParam1", optional=true) int param1,
|
||||||
@WSParamName(value="olle2", optional=true) int lol2) throws Exception{
|
@WSParamName(value="otherParam2", optional=true) int param2) throws Exception{
|
||||||
//System.out.println("Param: "+lol);
|
System.out.println("Executing method: exceptionMethod()");
|
||||||
throw new Exception("Ziver is the fizle");
|
throw new Exception("Ziver is the fizle");
|
||||||
}
|
}
|
||||||
|
|
||||||
@WSReturnName("param")
|
@WSReturnName("stringArray")
|
||||||
@WSParamDocumentation("null is the shizzle")
|
@WSParamDocumentation("Documentation of stringArrayMethod()")
|
||||||
public String[][] pubA (
|
public String[][] stringArrayMethod (
|
||||||
@WSParamName("Ztring") String lol) throws Exception{
|
@WSParamName("StringName") String str) throws Exception{
|
||||||
//System.out.println("ParamZ: "+lol);
|
System.out.println("Executing method: stringArrayMethod()");
|
||||||
return new String[][]{{"test","test2"},{"test3","test4"}};
|
return new String[][]{{"test","test2"},{"test3","test4"}};
|
||||||
}
|
}
|
||||||
|
|
||||||
@WSReturnName("zivarray")
|
@WSReturnName("specialReturnClass")
|
||||||
@WSParamDocumentation("null is the bla")
|
@WSParamDocumentation("Documentation of specialReturnMethod()")
|
||||||
public SOAPTestClass2[] pubX (
|
public SpecialReturnClass[] specialReturnMethod (
|
||||||
@WSParamName("Ztring") String lol) throws Exception{
|
@WSParamName("StringName2") String str) throws Exception{
|
||||||
return new SOAPTestClass2[]{new SOAPTestClass2(), new SOAPTestClass2()};
|
System.out.println("Executing method: specialReturnMethod()");
|
||||||
|
return new SpecialReturnClass[]{new SpecialReturnClass(), new SpecialReturnClass()};
|
||||||
}
|
}
|
||||||
|
|
||||||
@WSReturnName("zivarray")
|
@WSReturnName("SimpleReturnClass")
|
||||||
@WSParamDocumentation("null is the kala")
|
@WSParamDocumentation("null is the kala")
|
||||||
public SOAPTestRetClass pubB (
|
public SimpleReturnClass simpleReturnClassMethod (
|
||||||
@WSParamName("byte") String lol) throws Exception{
|
@WSParamName("byte") String lol) throws Exception{
|
||||||
SOAPTestRetClass tmp = new SOAPTestRetClass();
|
System.out.println("Executing method: simpleReturnClassMethod()");
|
||||||
tmp.lol = "test";
|
SimpleReturnClass tmp = new SimpleReturnClass();
|
||||||
tmp.lol2 = "test2";
|
tmp.param1 = "newParam1";
|
||||||
|
tmp.param2 = "newParam2";
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@WSParamDocumentation("void method documentation")
|
||||||
|
public void voidMethod (){ }
|
||||||
|
|
||||||
@WSDisabled()
|
@WSDisabled()
|
||||||
public void privaZ(){ }
|
public void disabledMethod(){ }
|
||||||
protected void protZ(){ }
|
protected void protectedMethod(){ }
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private void privateMethod(){ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue