Cleaned up WS interfaces

This commit is contained in:
Ziver Koc 2018-07-05 16:58:02 +02:00
parent 1f8d5cfe2a
commit beb0ce754e
10 changed files with 121 additions and 146 deletions

View file

@ -32,29 +32,31 @@ import java.lang.annotation.Target;
* *
* Specifies web service definitions. Which methods that will * Specifies web service definitions. Which methods that will
* be published and other related metadata. * be published and other related metadata.
* Only public and non static methods will be published or
* the WSIgnore annotation can be used to ignore the specific method.
* *
* Example: * Example:
* <pre> * <pre>
* private static class Test implements WSInterface{ * private static class Test implements WSInterface{
* public Test(){} * public Test(){}
* *
* &#64;WSDocumentation("blabla") * &#64;WSDocumentation("This is a description of the method")
* &#64;WSDLParamDocumentation("olle = a variable?") * &#64;WSDLParamDocumentation("arg1 = variable description?")
* public void pubZ( * public void pubZ(
* &#64;WSParamName("olle") int lol) * &#64;WSParamName("arg1") int randomName)
* throws Exception{ * throws Exception{
* .... * ....
* } * }
* *
* &#64;WSReturnName("param") * &#64;WSReturnName("param")
* public String pubA( * public String pubA(
* &#64;WSParamName(value="lol", optional=true) String lol) * &#64;WSParamName(value="optArg", optional=true) String optionalParam)
* throws Exception{ * throws Exception{
* .... * ....
* } * }
* *
* &#64;WSIgnore() * &#64;WSIgnore()
* public void privaZ(....){ * public void privatZ(....){
* ... * ...
* } * }
* } * }
@ -129,7 +131,7 @@ public interface WSInterface {
@interface WSHeader { } @interface WSHeader { }
/** /**
* Specifies the name space for method. * Specifies the name space for the method.
* *
* @author Ziver * @author Ziver
*/ */

View file

@ -144,13 +144,6 @@ public class WSMethodDef {
return name; return name;
} }
/**
* @return the number of exceptions this method throws
*/
public int exceptionCount(){
return exceptions.size();
}
/** /**
* @return a list of exceptions this method throws * @return a list of exceptions this method throws
*/ */
@ -158,13 +151,6 @@ public class WSMethodDef {
return exceptions; return exceptions;
} }
/**
* @return the number of parameters for this method
*/
public int getInputCount(){
return inputs.size();
}
/** /**
* @return a list of input parameters * @return a list of input parameters
*/ */
@ -172,21 +158,6 @@ public class WSMethodDef {
return inputs; return inputs;
} }
/**
* @param index is a index
* @return a {@link WSParameterDef} object in the given index
*/
public WSParameterDef getInput( int index ){
return inputs.get( index );
}
/**
* @return the number of parameters for this method
*/
public int getOutputCount(){
return outputs.size();
}
/** /**
* @return a list of input parameters * @return a list of input parameters
*/ */
@ -194,14 +165,6 @@ public class WSMethodDef {
return outputs; return outputs;
} }
/**
* @param index is a index
* @return a {@link WSParameterDef} object in the given index
*/
public WSParameterDef getOutput( int index ){
return outputs.get( index );
}
/** /**
* @return Documentation of the method if one exists or else null * @return Documentation of the method if one exists or else null
*/ */
@ -210,15 +173,12 @@ public class WSMethodDef {
} }
/** /**
* @return the namespace of the method * @return the namespace or endpoint url of the method
*/ */
public String getNamespace(){ public String getNamespace(){
return namespace; return namespace;
} }
public WebServiceDef getWebService(){
return wsDef;
}
/** /**
* Invokes a specified method * Invokes a specified method

View file

@ -39,6 +39,7 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -97,8 +98,9 @@ public class RESTClientInvocationHandler implements InvocationHandler {
+ (serviceUrl.getPath().endsWith("/") ? "" : "/") + (serviceUrl.getPath().endsWith("/") ? "" : "/")
+ methodDef.getName()); + methodDef.getName());
for (int i = 0; i < methodDef.getOutputCount(); i++) { List<WSParameterDef> params = methodDef.getOutputs();
WSParameterDef param = methodDef.getOutput(i); for (int i = 0; i < params.size(); i++) {
WSParameterDef param = params.get(i);
url.setParameter(param.getName(), args[i].toString()); url.setParameter(param.getName(), args[i].toString());
} }

View file

@ -36,6 +36,7 @@ import zutil.net.ws.WebServiceDef;
import zutil.parser.json.JSONObjectOutputStream; import zutil.parser.json.JSONObjectOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -74,28 +75,29 @@ public class RESTHttpPage implements HttpPage {
if (wsDef.hasMethod(targetMethod)) { if (wsDef.hasMethod(targetMethod)) {
// Parse request // Parse request
WSMethodDef m = wsDef.getMethod(targetMethod); WSMethodDef m = wsDef.getMethod(targetMethod);
Object[] params = prepareInputParams(m, input); Object[] inputParams = prepareInputParams(m, input);
// Invoke // Invoke
Object ret = m.invoke(ws, params); Object ret = m.invoke(ws, inputParams);
// Generate Response // Generate Response
StringOutputStream dummyOut = new StringOutputStream(); StringOutputStream strBuffer = new StringOutputStream();
JSONObjectOutputStream out = new JSONObjectOutputStream(dummyOut); JSONObjectOutputStream out = new JSONObjectOutputStream(strBuffer);
out.enableMetaData(false); out.enableMetaData(false);
out.writeObject(ret); out.writeObject(ret);
out.close(); out.close();
return dummyOut.toString(); return strBuffer.toString();
} }
return "{error: \"Unknown target: "+targetMethod+"\"}"; return "{error: \"Unknown target: "+targetMethod+"\"}";
} }
private Object[] prepareInputParams(WSMethodDef method, Map<String, String> input){ private Object[] prepareInputParams(WSMethodDef methodDef, Map<String, String> input){
Object[] inputParams = new Object[method.getInputCount()]; List<WSParameterDef> inputParamDefs = methodDef.getInputs();
Object[] inputParams = new Object[inputParamDefs.size()];
// Get the parameter values // Get the parameter values
for(int i=0; i<method.getInputCount() ;i++){ for (int i=0; i<inputParamDefs.size(); i++){
WSParameterDef param = method.getInput( i ); WSParameterDef param = inputParamDefs.get(i);
if (input.containsKey(param.getName())){ if (input.containsKey(param.getName())){
inputParams[i] = Converter.fromString( inputParams[i] = Converter.fromString(
input.get(param.getName()), input.get(param.getName()),

View file

@ -40,6 +40,7 @@ import zutil.net.ws.WebServiceDef;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -108,8 +109,10 @@ public class SOAPClientInvocationHandler implements InvocationHandler {
Element method = body.addElement(""); Element method = body.addElement("");
method.addNamespace("m", methodDef.getNamespace()); method.addNamespace("m", methodDef.getNamespace());
method.setName("m:" + methodDef.getName() + "Request"); method.setName("m:" + methodDef.getName() + "Request");
for (int i = 0; i < methodDef.getOutputCount(); i++) {
WSParameterDef param = methodDef.getOutput(i); List<WSParameterDef> outputParamDefs = methodDef.getOutputs();
for (int i = 0; i < outputParamDefs.size(); i++) {
WSParameterDef param = outputParamDefs.get(i);
SOAPHttpPage.generateSOAPXMLForObj(method, args[i], param.getName()); SOAPHttpPage.generateSOAPXMLForObj(method, args[i], param.getName());
} }

View file

@ -45,6 +45,7 @@ import java.io.InputStreamReader;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -268,37 +269,39 @@ public class SOAPHttpPage implements HttpPage{
while(it.hasNext()) { while(it.hasNext()) {
Element e = it.next(); Element e = it.next();
if (wsDef.hasMethod( e.getQName().getName())) { if (wsDef.hasMethod( e.getQName().getName())) {
WSMethodDef m = wsDef.getMethod( e.getQName().getName() ); WSMethodDef methodDef = wsDef.getMethod(e.getQName().getName());
Object[] params = new Object[ m.getInputCount() ]; List<WSParameterDef> inputParamDefs = methodDef.getInputs();
Object[] inputParams = new Object[inputParamDefs.size()];
// Get the parameter values // Get the parameter values
for(int i=0; i<m.getInputCount() ;i++){ for(int i=0; i<inputParamDefs.size() ;i++) {
WSParameterDef param = m.getInput( i ); WSParameterDef param = inputParamDefs.get(i);
if ( e.element(param.getName()) != null ) { if ( e.element(param.getName()) != null ) {
params[i] = Converter.fromString( inputParams[i] = Converter.fromString(
e.element(param.getName()).getTextTrim(), e.element(param.getName()).getTextTrim(),
param.getParamClass()); param.getParamClass());
} }
} }
// Invoke // Invoke
Object ret = m.invoke(obj, params); Object outputParams = methodDef.invoke(obj, inputParams);
List<WSParameterDef> outputParamDefs = methodDef.getOutputs();
// generate response XML // generate response XML
if( m.getOutputCount()>0 ){ if (outputParamDefs.size() > 0) {
Element response = responseRoot.addElement(""); Element response = responseRoot.addElement("");
response.addNamespace("m", m.getNamespace() ); response.addNamespace("m", methodDef.getNamespace() );
response.setName("m:"+m.getName()+"Response"); response.setName("m:"+methodDef.getName()+"Response");
if( ret instanceof WSReturnObject ){ if (outputParams instanceof WSReturnObject) {
Field[] f = ret.getClass().getFields(); Field[] f = outputParams.getClass().getFields();
for(int i=0; i<m.getOutputCount() ;i++){ for(int i=0; i<outputParamDefs.size() ;i++) {
WSParameterDef param = m.getOutput( i ); WSParameterDef param = outputParamDefs.get(i);
generateSOAPXMLForObj(response,((WSReturnObject)ret).getValue(f[i]) , param.getName()); generateSOAPXMLForObj(response,((WSReturnObject)outputParams).getValue(f[i]) , param.getName());
} }
} }
else { else {
generateSOAPXMLForObj(response, ret, m.getOutput(0).getName()); generateSOAPXMLForObj(response, outputParams, methodDef.getOutputs().get(0).getName());
} }
} }
} }
@ -323,6 +326,8 @@ public class SOAPHttpPage implements HttpPage{
*/ */
protected static void generateSOAPXMLForObj(Element root, Object obj, String elementName) throws IllegalArgumentException, IllegalAccessException{ protected static void generateSOAPXMLForObj(Element root, Object obj, String elementName) throws IllegalArgumentException, IllegalAccessException{
if (obj == null) return; if (obj == null) return;
// Return binary data
if (byte[].class.isAssignableFrom(obj.getClass())) { if (byte[].class.isAssignableFrom(obj.getClass())) {
Element valueE = root.addElement( elementName ); Element valueE = root.addElement( elementName );
valueE.addAttribute("type", "xsd:"+ getSOAPClassName(obj.getClass())); valueE.addAttribute("type", "xsd:"+ getSOAPClassName(obj.getClass()));
@ -330,7 +335,7 @@ public class SOAPHttpPage implements HttpPage{
tmp = tmp.replaceAll("\\s", ""); tmp = tmp.replaceAll("\\s", "");
valueE.setText(tmp); valueE.setText(tmp);
} }
// return an array // Return an array
else if (obj.getClass().isArray()) { else if (obj.getClass().isArray()) {
Element array = root.addElement( (elementName.equals("element") ? "Array" : elementName) ); Element array = root.addElement( (elementName.equals("element") ? "Array" : elementName) );
String arrayType = "xsd:"+ getSOAPClassName(obj.getClass()); String arrayType = "xsd:"+ getSOAPClassName(obj.getClass());

View file

@ -66,7 +66,7 @@ public class WSDLServiceSOAP extends WSDLService{
input_body.addAttribute("namespace", method.getNamespace()); input_body.addAttribute("namespace", method.getNamespace());
//*************************** output //*************************** output
if( method.getOutputCount() > 0 ){ if(!method.getOutputs().isEmpty()){
// definitions -> binding -> operation -> output // definitions -> binding -> operation -> output
Element output = operation.addElement("wsdl:output"); Element output = operation.addElement("wsdl:output");
// definitions -> binding -> operation -> input -> body // definitions -> binding -> operation -> input -> body

View file

@ -139,7 +139,7 @@ public class WSDLWriter{
private void generateMessage(Element parent, WSMethodDef method){ private void generateMessage(Element parent, WSMethodDef method){
//*************************** Input //*************************** Input
if( method.getInputCount() > 0 ){ if(!method.getInputs().isEmpty()){
// definitions -> message // definitions -> message
Element input = parent.addElement("wsdl:message"); Element input = parent.addElement("wsdl:message");
input.addAttribute("name", method.getName()+"Request"); input.addAttribute("name", method.getName()+"Request");
@ -156,7 +156,7 @@ public class WSDLWriter{
} }
} }
//*************************** Output //*************************** Output
if( method.getOutputCount() > 0 ){ if(!method.getOutputs().isEmpty()){
// definitions -> message // definitions -> message
Element output = parent.addElement("wsdl:message"); Element output = parent.addElement("wsdl:message");
output.addAttribute("name", method.getName()+"Response"); output.addAttribute("name", method.getName()+"Response");
@ -205,19 +205,19 @@ public class WSDLWriter{
} }
//*************************** Input //*************************** Input
if( method.getInputCount() > 0 ){ if( method.getInputs().size() > 0 ){
// definitions -> message // definitions -> message
Element input = operation.addElement("wsdl:input"); Element input = operation.addElement("wsdl:input");
input.addAttribute("message", "tns:"+method.getName()+"Request"); input.addAttribute("message", "tns:"+method.getName()+"Request");
} }
//*************************** Output //*************************** Output
if( method.getOutputCount() > 0 ){ if( method.getOutputs().size() > 0 ){
// definitions -> message // definitions -> message
Element output = operation.addElement("wsdl:output"); Element output = operation.addElement("wsdl:output");
output.addAttribute("message", "tns:"+method.getName()+"Response"); output.addAttribute("message", "tns:"+method.getName()+"Response");
} }
//*************************** Fault //*************************** Fault
if( method.getOutputCount() > 0 ){ if( method.getOutputs().size() > 0 ){
// definitions -> message // definitions -> message
Element fault = operation.addElement("wsdl:fault"); Element fault = operation.addElement("wsdl:fault");
fault.addAttribute("message", "tns:exception"); fault.addAttribute("message", "tns:exception");
@ -270,7 +270,7 @@ public class WSDLWriter{
ArrayList<Class<?>> types = new ArrayList<>(); ArrayList<Class<?>> types = new ArrayList<>();
// Find types // Find types
for( WSMethodDef method : ws.getMethods() ){ for( WSMethodDef method : ws.getMethods() ){
if( method.getOutputCount() > 0 ){ if(!method.getOutputs().isEmpty()){
for( WSParameterDef param : method.getOutputs() ){ for( WSParameterDef param : method.getOutputs() ){
Class<?> paramClass = param.getParamClass(); Class<?> paramClass = param.getParamClass();
Class<?> valueClass = getClass(paramClass); Class<?> valueClass = getClass(paramClass);

View file

@ -18,6 +18,7 @@ public class RESTClientTest {
public interface OpenWeartherMap extends WSInterface { public interface OpenWeartherMap extends WSInterface {
@WSNamespace("")
int weather(@WSParamName("q") String city); int weather(@WSParamName("q") String city);
} }