From 52fee71c30339bf2cb80d877f251358b1d4b895d Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sun, 23 Feb 2014 14:28:40 +0000 Subject: [PATCH] Refactoring of WSDLWriter --- src/zutil/net/ws/soap/SOAPHttpPage.java | 113 ++++++++---------- .../ws/zutil/net/ws/rest/RestHttpPage.java | 42 +++++++ src/zutil/parser/wsdl/WSDLHttpPage.java | 50 ++++++++ src/zutil/parser/wsdl/WSDLService.java | 50 ++++++++ src/zutil/parser/wsdl/WSDLServiceSOAP.java | 76 ++++++++++++ src/zutil/parser/wsdl/WSDLWriter.java | 85 +++++-------- 6 files changed, 301 insertions(+), 115 deletions(-) create mode 100644 src/zutil/net/ws/zutil/net/ws/rest/RestHttpPage.java create mode 100644 src/zutil/parser/wsdl/WSDLHttpPage.java create mode 100644 src/zutil/parser/wsdl/WSDLService.java create mode 100644 src/zutil/parser/wsdl/WSDLServiceSOAP.java diff --git a/src/zutil/net/ws/soap/SOAPHttpPage.java b/src/zutil/net/ws/soap/SOAPHttpPage.java index a90604d..faf82b9 100644 --- a/src/zutil/net/ws/soap/SOAPHttpPage.java +++ b/src/zutil/net/ws/soap/SOAPHttpPage.java @@ -35,7 +35,6 @@ import zutil.net.http.HttpPage; import zutil.net.http.HttpPrintStream; import zutil.net.ws.*; import zutil.net.ws.WSReturnObject.WSValueName; -import zutil.parser.wsdl.WSDLWriter; import java.lang.reflect.Array; import java.lang.reflect.Field; @@ -82,16 +81,12 @@ public class SOAPHttpPage implements HttpPage{ private WebServiceDef wsDef; /** This instance of the web service class is used if session is disabled **/ private WSInterface ws; - /** The WSDL document **/ - private WSDLWriter wsdl; /** Session enabled **/ private boolean session_enabled; public SOAPHttpPage( WebServiceDef wsDef ){ this.wsDef = wsDef; this.session_enabled = false; - - wsdl = new WSDLWriter( wsDef ); } /** @@ -124,43 +119,37 @@ public class SOAPHttpPage implements HttpPage{ out.setHeader("Content-Type", "text/xml"); out.flush(); - if(request.containsKey("wsdl")){ - wsdl.write( out ); - } - else{ - WSInterface obj = null; - if(session_enabled){ - if( session.containsKey("SOAPInterface")) - obj = (WSInterface)session.get("SOAPInterface"); - else{ - obj = wsDef.newInstance(); - session.put("SOAPInterface", obj); - } - } - else{ - if( ws == null ) - ws = wsDef.newInstance(); - obj = ws; - } - - Document document = genSOAPResponse( request.get(""), obj); - - OutputFormat format = OutputFormat.createCompactFormat(); - XMLWriter writer = new XMLWriter( out, format ); - writer.write( document ); - - - // DEBUG - if( logger.isLoggable(Level.FINEST) ){ - OutputFormat format2 = OutputFormat.createPrettyPrint(); - System.err.println("********** Request"); - System.err.println(request); - System.out.println("********** Response"); - writer = new XMLWriter( System.out, format2 ); - writer.write( document ); - } - - } + WSInterface obj = null; + if(session_enabled){ + if( session.containsKey("SOAPInterface")) + obj = (WSInterface)session.get("SOAPInterface"); + else{ + obj = wsDef.newInstance(); + session.put("SOAPInterface", obj); + } + } + else{ + if( ws == null ) + ws = wsDef.newInstance(); + obj = ws; + } + + Document document = genSOAPResponse( request.get(""), obj); + + OutputFormat format = OutputFormat.createCompactFormat(); + XMLWriter writer = new XMLWriter( out, format ); + writer.write( document ); + + + // DEBUG + if( logger.isLoggable(Level.FINEST) ){ + OutputFormat format2 = OutputFormat.createPrettyPrint(); + System.err.println("********** Request"); + System.err.println(request); + System.out.println("********** Response"); + writer = new XMLWriter( System.out, format2 ); + writer.write( document ); + } } catch (Exception e) { logger.log(Level.WARNING, "Unhandled request", e); } @@ -299,57 +288,57 @@ public class SOAPHttpPage implements HttpPage{ /** * Generates an return XML Element. This function can - * handle return values as XML Elements, WSReturnObject and the + * handle return values as XML Elements, WSReturnObject and * Java basic data types. * * @param root is the parent Element - * @param ret is the object that is the return value + * @param retObj is the object that is the return value * @param ename is the name of the parent Element */ - private void generateReturnXML(Element root, Object ret, String ename) throws IllegalArgumentException, IllegalAccessException{ - if(ret == null) return; - if(byte[].class.isAssignableFrom(ret.getClass())){ + private void generateReturnXML(Element root, Object retObj, String ename) throws IllegalArgumentException, IllegalAccessException{ + if(retObj == null) return; + if(byte[].class.isAssignableFrom(retObj.getClass())){ Element valueE = root.addElement( ename ); - valueE.addAttribute("type", "xsd:"+getClassSOAPName(ret.getClass())); - String tmp = new sun.misc.BASE64Encoder().encode((byte[])ret); + valueE.addAttribute("type", "xsd:"+ getSOAPClassName(retObj.getClass())); + String tmp = new sun.misc.BASE64Encoder().encode((byte[])retObj); tmp = tmp.replaceAll("\\s", ""); valueE.setText(tmp); } // return an array - else if(ret.getClass().isArray()){ + else if(retObj.getClass().isArray()){ Element array = root.addElement( (ename.equals("element") ? "Array" : ename) ); - String arrayType = "xsd:"+getClassSOAPName(ret.getClass()); - arrayType = arrayType.replaceFirst("\\[\\]", "["+Array.getLength(ret)+"]"); + String arrayType = "xsd:"+ getSOAPClassName(retObj.getClass()); + arrayType = arrayType.replaceFirst("\\[\\]", "["+Array.getLength(retObj)+"]"); array.addAttribute("type", "soap:Array"); array.addAttribute("soap:arrayType", arrayType); - for(int i=0; i c){ + public static String getSOAPClassName(Class c){ Class cTmp = getClass(c); if(byte[].class.isAssignableFrom(c)){ return "base64Binary"; diff --git a/src/zutil/net/ws/zutil/net/ws/rest/RestHttpPage.java b/src/zutil/net/ws/zutil/net/ws/rest/RestHttpPage.java new file mode 100644 index 0000000..250cdfd --- /dev/null +++ b/src/zutil/net/ws/zutil/net/ws/rest/RestHttpPage.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 Ziver + * + * 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.net.ws.zutil.net.ws.rest; + +import zutil.net.http.HttpPage; +import zutil.net.http.HttpPrintStream; + +import java.util.Map; + +/** + * User: Ziver + */ +public class RestHttpPage implements HttpPage { + @Override + public void respond(HttpPrintStream out, + Map client_info, + Map session, + Map cookie, + Map request) { + + } +} diff --git a/src/zutil/parser/wsdl/WSDLHttpPage.java b/src/zutil/parser/wsdl/WSDLHttpPage.java new file mode 100644 index 0000000..b70322d --- /dev/null +++ b/src/zutil/parser/wsdl/WSDLHttpPage.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014 Ziver + * + * 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.parser.wsdl; + +import zutil.net.http.HttpPage; +import zutil.net.http.HttpPrintStream; +import zutil.net.ws.WebServiceDef; + +import java.util.Map; + +/** + * User: Ziver + */ +public class WSDLHttpPage implements HttpPage { + /** The WSDL document **/ + private WSDLWriter wsdl; + + public WSDLHttpPage( WebServiceDef wsDef ){ + wsdl = new WSDLWriter( wsDef ); + } + + public void respond(HttpPrintStream out, + Map client_info, + Map session, + Map cookie, + Map request) { + out.setHeader("Content-Type", "text/xml"); + wsdl.write( out ); + } +} diff --git a/src/zutil/parser/wsdl/WSDLService.java b/src/zutil/parser/wsdl/WSDLService.java new file mode 100644 index 0000000..4664478 --- /dev/null +++ b/src/zutil/parser/wsdl/WSDLService.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014 Ziver + * + * 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.parser.wsdl; + +import org.dom4j.Element; +import zutil.net.ws.WSMethodDef; + +/** + * This interface defines a WSDL Service type. + * + * User: Ziver + */ +public abstract class WSDLService { + /** The URL of this service **/ + private String url; + + public WSDLService(String url){ + this.url = url; + } + + public String getServiceAddress(){ + return url; + } + + public abstract String getServiceType(); + + public abstract void generateBinding(Element definitions); + + public abstract void generateOperation(Element definitions, WSMethodDef method); +} diff --git a/src/zutil/parser/wsdl/WSDLServiceSOAP.java b/src/zutil/parser/wsdl/WSDLServiceSOAP.java new file mode 100644 index 0000000..8825921 --- /dev/null +++ b/src/zutil/parser/wsdl/WSDLServiceSOAP.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014 Ziver + * + * 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.parser.wsdl; + +import org.dom4j.Element; +import zutil.net.ws.WSMethodDef; + +/** + * User: Ziver + */ +public class WSDLServiceSOAP extends WSDLService{ + + public WSDLServiceSOAP(String url){ + super(url); + } + + @Override + public String getServiceType() { return "soap"; } + + @Override + public void generateBinding(Element definitions) { + // definitions -> binding -> soap:binding + Element soap_binding = definitions.addElement("soap:binding"); + soap_binding.addAttribute("style", "rpc"); + soap_binding.addAttribute("transport", "http://schemas.xmlsoap.org/soap/http"); + } + + @Override + public void generateOperation(Element definitions, WSMethodDef method) { + // definitions -> binding -> operation + Element operation = definitions.addElement("wsdl:operation"); + operation.addAttribute("name", method.getName()); + + // definitions -> binding -> operation -> soap:operation + Element soap_operation = operation.addElement("soap:operation"); + soap_operation.addAttribute("soapAction", method.getNamespace()); + + //*************************** Input + // definitions -> binding -> operation -> input + Element input = operation.addElement("wsdl:input"); + // definitions -> binding -> operation -> input -> body + Element input_body = input.addElement("soap:body"); + input_body.addAttribute("use", "literal"); + input_body.addAttribute("namespace", method.getNamespace()); + + //*************************** output + if( method.getOutputCount() > 0 ){ + // definitions -> binding -> operation -> output + Element output = operation.addElement("wsdl:output"); + // definitions -> binding -> operation -> input -> body + Element output_body = output.addElement("soap:body"); + output_body.addAttribute("use", "literal"); + output_body.addAttribute("namespace", method.getNamespace()); + } + } +} diff --git a/src/zutil/parser/wsdl/WSDLWriter.java b/src/zutil/parser/wsdl/WSDLWriter.java index ecb1a4f..8e2e65b 100644 --- a/src/zutil/parser/wsdl/WSDLWriter.java +++ b/src/zutil/parser/wsdl/WSDLWriter.java @@ -22,19 +22,11 @@ package zutil.parser.wsdl; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.io.UnsupportedEncodingException; -import java.lang.reflect.Field; -import java.util.ArrayList; - import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; - import zutil.io.StringOutputStream; import zutil.net.ws.WSMethodDef; import zutil.net.ws.WSParameterDef; @@ -42,16 +34,33 @@ import zutil.net.ws.WSReturnObject; import zutil.net.ws.WSReturnObject.WSValueName; import zutil.net.ws.WebServiceDef; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Field; +import java.util.ArrayList; + public class WSDLWriter{ - - private WebServiceDef ws; - - private String cache; + /** Current Web service definition ***/ + private WebServiceDef ws; + /** Cache of generated WSDL **/ + private String cache; + /** A list of services **/ + private ArrayList services; public WSDLWriter( WebServiceDef ws ){ + this.services = new ArrayList(); this.ws = ws; } + /** + * Add a service to be published with the WSDL + */ + public void addService(WSDLService serv){ + cache = null; + services.add(serv); + } public void write( PrintStream out ) { out.print(generate()); @@ -223,47 +232,15 @@ public class WSDLWriter{ binding.addAttribute("name", ws.getName()+"Binding"); binding.addAttribute("type", "tns:"+ws.getName()+"PortType"); - generateSOAPOBinding(binding); + for(WSDLService serv : services){ + serv.generateBinding(binding); - for(WSMethodDef method : ws.getMethods()){ - generateSOAPOperation(binding, method); - } + for(WSMethodDef method : ws.getMethods()){ + serv.generateOperation(binding, method); + } + } } - private void generateSOAPOBinding(Element definitions){ - // definitions -> binding -> soap:binding - Element soap_binding = definitions.addElement("soap:binding"); - soap_binding.addAttribute("style", "rpc"); - soap_binding.addAttribute("transport", "http://schemas.xmlsoap.org/soap/http"); - } - - private void generateSOAPOperation(Element definitions, WSMethodDef method){ - // definitions -> binding -> operation - Element operation = definitions.addElement("wsdl:operation"); - operation.addAttribute("name", method.getName()); - - // definitions -> binding -> operation -> soap:operation - Element soap_operation = operation.addElement("soap:operation"); - soap_operation.addAttribute("soapAction", method.getNamespace()); - - //*************************** Input - // definitions -> binding -> operation -> input - Element input = operation.addElement("wsdl:input"); - // definitions -> binding -> operation -> input -> body - Element input_body = input.addElement("soap:body"); - input_body.addAttribute("use", "literal"); - input_body.addAttribute("namespace", method.getNamespace()); - - //*************************** output - if( method.getOutputCount() > 0 ){ - // definitions -> binding -> operation -> output - Element output = operation.addElement("wsdl:output"); - // definitions -> binding -> operation -> input -> body - Element output_body = output.addElement("soap:body"); - output_body.addAttribute("use", "literal"); - output_body.addAttribute("namespace", method.getNamespace()); - } - } private void generateService(Element parent){ // definitions -> service @@ -275,9 +252,11 @@ public class WSDLWriter{ port.addAttribute("name", ws.getName()+"Port"); port.addAttribute("binding", "tns:"+ws.getName()+"Binding"); - // definitions -> service-> port -> address - Element address = port.addElement("soap:address"); - address.addAttribute("location", null); + for(WSDLService serv : services){ + // definitions -> service-> port -> address + Element address = port.addElement(serv.getServiceType()+":address"); + address.addAttribute("location", serv.getServiceAddress()); + } } /**