2015-05-27 13:13:19 +00:00
|
|
|
/*
|
2015-10-01 15:23:40 +00:00
|
|
|
* The MIT License (MIT)
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2015 Ziver Koc
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
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:
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-05-28 19:29:24 +00:00
|
|
|
*
|
2011-07-13 17:53:17 +00:00
|
|
|
* 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.
|
2015-05-27 13:13:19 +00:00
|
|
|
*/
|
2013-05-28 19:29:24 +00:00
|
|
|
|
2011-02-15 19:37:35 +00:00
|
|
|
package zutil.net.ws;
|
2010-08-13 22:36:08 +00:00
|
|
|
|
2015-06-02 15:47:44 +00:00
|
|
|
import zutil.net.ws.WSInterface.WSDocumentation;
|
|
|
|
|
import zutil.net.ws.WSInterface.WSNamespace;
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
|
import java.lang.reflect.Field;
|
2011-09-14 20:30:06 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2010-08-13 22:36:08 +00:00
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2011-09-14 20:30:06 +00:00
|
|
|
/**
|
|
|
|
|
* This is a web service method definition class
|
|
|
|
|
*
|
|
|
|
|
* @author Ziver
|
|
|
|
|
*/
|
2010-08-13 22:36:08 +00:00
|
|
|
// TODO: Header parameters
|
|
|
|
|
public class WSMethodDef {
|
2011-09-14 20:30:06 +00:00
|
|
|
/** The parent web service definition **/
|
|
|
|
|
private WebServiceDef wsDef;
|
2010-08-13 22:36:08 +00:00
|
|
|
/** A list of input parameters **/
|
|
|
|
|
private ArrayList<WSParameterDef> inputs;
|
|
|
|
|
/** A List of return parameters of the method **/
|
|
|
|
|
private ArrayList<WSParameterDef> outputs;
|
|
|
|
|
/** A List of exceptions that this method throws **/
|
|
|
|
|
private ArrayList<Class<?>> exceptions;
|
|
|
|
|
/** The real method that this class represent, can be null if its a remote method **/
|
|
|
|
|
private Method method;
|
|
|
|
|
/** Documentation of the method **/
|
|
|
|
|
private String doc;
|
2011-09-14 20:30:06 +00:00
|
|
|
/** This is the namespace of the method **/
|
|
|
|
|
private String namespace;
|
2010-08-13 22:36:08 +00:00
|
|
|
/** The published name of the method **/
|
|
|
|
|
private String name;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param me is a method in a class that implements WSInterface
|
|
|
|
|
*/
|
2011-09-14 20:30:06 +00:00
|
|
|
protected WSMethodDef( WebServiceDef wsDef, Method me) {
|
|
|
|
|
if( !WSInterface.class.isAssignableFrom(me.getDeclaringClass()) )
|
2010-08-13 22:36:08 +00:00
|
|
|
throw new ClassCastException("Declaring class does not implement WSInterface!");
|
2011-09-14 20:30:06 +00:00
|
|
|
this.wsDef = wsDef;
|
2010-08-13 22:36:08 +00:00
|
|
|
method = me;
|
|
|
|
|
inputs = new ArrayList<WSParameterDef>();
|
|
|
|
|
outputs = new ArrayList<WSParameterDef>();
|
|
|
|
|
exceptions = new ArrayList<Class<?>>();
|
|
|
|
|
name = method.getName();
|
|
|
|
|
|
2011-09-14 20:30:06 +00:00
|
|
|
//***** Documentation & Namespace
|
|
|
|
|
WSDocumentation tmpDoc = method.getAnnotation( WSDocumentation.class );
|
2010-08-13 22:36:08 +00:00
|
|
|
if(tmpDoc != null){
|
|
|
|
|
doc = tmpDoc.value();
|
|
|
|
|
}
|
2011-09-14 20:30:06 +00:00
|
|
|
WSNamespace tmpSpace = method.getAnnotation( WSNamespace.class );
|
|
|
|
|
if( tmpSpace != null )
|
|
|
|
|
namespace = tmpSpace.value();
|
|
|
|
|
else
|
|
|
|
|
namespace = wsDef.getNamespace()+"?#"+name;
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
//***** Exceptions
|
|
|
|
|
for( Class<?> exc : method.getExceptionTypes() ){
|
|
|
|
|
exceptions.add( exc );
|
|
|
|
|
}
|
2011-09-14 20:30:06 +00:00
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
//********* Get the input parameter names **********
|
|
|
|
|
Annotation[][] paramAnnotation = method.getParameterAnnotations();
|
2011-09-14 20:30:06 +00:00
|
|
|
Class<?>[] inputTypes = method.getParameterTypes();
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
for(int i=0; i<paramAnnotation.length ;i++){
|
2011-09-14 20:30:06 +00:00
|
|
|
WSParameterDef param = new WSParameterDef( this );
|
2010-08-13 22:36:08 +00:00
|
|
|
for(Annotation annotation : paramAnnotation[i]){
|
|
|
|
|
if(annotation instanceof WSInterface.WSParamName){
|
|
|
|
|
WSInterface.WSParamName paramName = (WSInterface.WSParamName) annotation;
|
2011-09-14 20:30:06 +00:00
|
|
|
param.setName( paramName.value() );
|
|
|
|
|
param.setOptional( paramName.optional() );
|
2010-08-13 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-09-14 20:30:06 +00:00
|
|
|
param.setParamClass( inputTypes[i] );
|
2010-08-13 22:36:08 +00:00
|
|
|
// if no name was found then use default
|
2011-09-14 20:30:06 +00:00
|
|
|
if(param.getName() == null)
|
|
|
|
|
param.setName( "args"+i );
|
2010-08-13 22:36:08 +00:00
|
|
|
|
|
|
|
|
inputs.add( param );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//******** The return parameter name ************
|
|
|
|
|
WSInterface.WSReturnName returnName = method.getAnnotation(WSInterface.WSReturnName.class);
|
2011-09-14 20:30:06 +00:00
|
|
|
if( WSReturnObject.class.isAssignableFrom( method.getReturnType() ) ){
|
2010-08-13 22:36:08 +00:00
|
|
|
Class<?> retClass = method.getReturnType();
|
|
|
|
|
Field[] fields = retClass.getFields();
|
2011-09-14 20:30:06 +00:00
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
for(int i=0; i<fields.length ;i++){
|
2011-09-14 20:30:06 +00:00
|
|
|
WSParameterDef ret_param = new WSParameterDef( this );
|
|
|
|
|
WSReturnObject.WSValueName retValName = fields[i]
|
|
|
|
|
.getAnnotation( WSReturnObject.WSValueName.class );
|
|
|
|
|
if(retValName != null)
|
|
|
|
|
ret_param.setName( retValName.value() );
|
|
|
|
|
else
|
|
|
|
|
ret_param.setName( fields[i].getName() );
|
|
|
|
|
ret_param.setParamClass( fields[i].getType() );
|
2010-08-13 22:36:08 +00:00
|
|
|
outputs.add( ret_param );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-05 17:15:53 +00:00
|
|
|
else if( method.getReturnType() != void.class ){
|
2011-09-14 20:30:06 +00:00
|
|
|
WSParameterDef ret_param = new WSParameterDef( this );
|
|
|
|
|
if(returnName != null)
|
|
|
|
|
ret_param.setName(returnName.value());
|
|
|
|
|
else
|
|
|
|
|
ret_param.setName("return");
|
|
|
|
|
ret_param.setParamClass( method.getReturnType() );
|
2010-08-13 22:36:08 +00:00
|
|
|
outputs.add( ret_param );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return the published name of the method
|
|
|
|
|
*/
|
|
|
|
|
public String getName(){
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return the number of exceptions this method throws
|
|
|
|
|
*/
|
|
|
|
|
public int exceptionCount(){
|
|
|
|
|
return exceptions.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return a list of exceptions this method throws
|
|
|
|
|
*/
|
|
|
|
|
public List<Class<?>> getExceptions(){
|
|
|
|
|
return exceptions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return the number of parameters for this method
|
|
|
|
|
*/
|
2011-09-14 20:30:06 +00:00
|
|
|
public int getInputCount(){
|
2010-08-13 22:36:08 +00:00
|
|
|
return inputs.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return a list of input parameters
|
|
|
|
|
*/
|
|
|
|
|
public List<WSParameterDef> getInputs(){
|
|
|
|
|
return inputs;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-14 20:30:06 +00:00
|
|
|
/**
|
|
|
|
|
* @param index is a index
|
|
|
|
|
* @return a {@link WSParameterDef} object in the given index
|
|
|
|
|
*/
|
|
|
|
|
public WSParameterDef getInput( int index ){
|
|
|
|
|
return inputs.get( index );
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
/**
|
|
|
|
|
* @return the number of parameters for this method
|
|
|
|
|
*/
|
2011-09-14 20:30:06 +00:00
|
|
|
public int getOutputCount(){
|
2010-08-13 22:36:08 +00:00
|
|
|
return outputs.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return a list of input parameters
|
|
|
|
|
*/
|
|
|
|
|
public List<WSParameterDef> getOutputs(){
|
|
|
|
|
return outputs;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-14 20:30:06 +00:00
|
|
|
/**
|
|
|
|
|
* @param index is a index
|
|
|
|
|
* @return a {@link WSParameterDef} object in the given index
|
|
|
|
|
*/
|
|
|
|
|
public WSParameterDef getOutput( int index ){
|
|
|
|
|
return outputs.get( index );
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
/**
|
|
|
|
|
* @return Documentation of the method if one exists or else null
|
|
|
|
|
*/
|
|
|
|
|
public String getDocumentation(){
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-14 20:30:06 +00:00
|
|
|
/**
|
|
|
|
|
* @return the namespace of the method
|
|
|
|
|
*/
|
|
|
|
|
public String getNamespace(){
|
|
|
|
|
return namespace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WebServiceDef getWebService(){
|
|
|
|
|
return wsDef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invokes a specified method
|
|
|
|
|
*
|
|
|
|
|
* @param obj the object the method will called on
|
|
|
|
|
* @param params a vector with arguments
|
|
|
|
|
*/
|
2016-09-27 16:56:51 +02:00
|
|
|
public Object invoke(Object obj, Object[] params) throws Exception {
|
|
|
|
|
return this.method.invoke(obj, params );
|
2011-09-14 20:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
2010-08-13 22:36:08 +00:00
|
|
|
|
|
|
|
|
public String toString(){
|
|
|
|
|
StringBuilder tmp = new StringBuilder();
|
|
|
|
|
boolean first = true;
|
|
|
|
|
tmp.append(name).append("(");
|
|
|
|
|
for(WSParameterDef param : inputs){
|
|
|
|
|
if(first)
|
|
|
|
|
first = false;
|
|
|
|
|
else
|
|
|
|
|
tmp.append(" ,");
|
2011-09-14 20:30:06 +00:00
|
|
|
tmp.append(param.getParamClass().getSimpleName());
|
2010-08-13 22:36:08 +00:00
|
|
|
tmp.append(" ");
|
2011-09-14 20:30:06 +00:00
|
|
|
tmp.append(param.getName());
|
2010-08-13 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
tmp.append(") => ");
|
|
|
|
|
first = true;
|
|
|
|
|
for(WSParameterDef param : outputs){
|
|
|
|
|
if(first)
|
|
|
|
|
first = false;
|
|
|
|
|
else
|
|
|
|
|
tmp.append(" ,");
|
2011-09-14 20:30:06 +00:00
|
|
|
tmp.append(param.getParamClass().getSimpleName());
|
2010-08-13 22:36:08 +00:00
|
|
|
tmp.append(" ");
|
2011-09-14 20:30:06 +00:00
|
|
|
tmp.append(param.getName());
|
2010-08-13 22:36:08 +00:00
|
|
|
}
|
|
|
|
|
return tmp.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|