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
* 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:
* <pre>
* private static class Test implements WSInterface{
* public Test(){}
*
* &#64;WSDocumentation("blabla")
* &#64;WSDLParamDocumentation("olle = a variable?")
* &#64;WSDocumentation("This is a description of the method")
* &#64;WSDLParamDocumentation("arg1 = variable description?")
* public void pubZ(
* &#64;WSParamName("olle") int lol)
* &#64;WSParamName("arg1") int randomName)
* throws Exception{
* ....
* }
*
* &#64;WSReturnName("param")
* public String pubA(
* &#64;WSParamName(value="lol", optional=true) String lol)
* &#64;WSParamName(value="optArg", optional=true) String optionalParam)
* throws Exception{
* ....
* }
*
* &#64;WSIgnore()
* public void privaZ(....){
* public void privatZ(....){
* ...
* }
* }
@ -129,7 +131,7 @@ public interface WSInterface {
@interface WSHeader { }
/**
* Specifies the name space for method.
* Specifies the name space for the method.
*
* @author Ziver
*/

View file

@ -144,13 +144,6 @@ public class WSMethodDef {
return name;
}
/**
* @return the number of exceptions this method throws
*/
public int exceptionCount(){
return exceptions.size();
}
/**
* @return a list of exceptions this method throws
*/
@ -158,13 +151,6 @@ public class WSMethodDef {
return exceptions;
}
/**
* @return the number of parameters for this method
*/
public int getInputCount(){
return inputs.size();
}
/**
* @return a list of input parameters
*/
@ -172,21 +158,6 @@ public class WSMethodDef {
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
*/
@ -194,14 +165,6 @@ public class WSMethodDef {
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
*/
@ -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(){
return namespace;
}
public WebServiceDef getWebService(){
return wsDef;
}
/**
* Invokes a specified method

View file

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

View file

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

View file

@ -40,6 +40,7 @@ import zutil.net.ws.WebServiceDef;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -108,8 +109,10 @@ public class SOAPClientInvocationHandler implements InvocationHandler {
Element method = body.addElement("");
method.addNamespace("m", methodDef.getNamespace());
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());
}

View file

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

View file

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

View file

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

View file

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