2ronwalf// The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalfpackage org.mindswap.wsdl;
2ronwalf
2ronwalf
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.Vector;
2ronwalf
2ronwalfimport javax.xml.namespace.QName;
2ronwalfimport javax.xml.soap.SOAPElement;
2ronwalfimport javax.xml.soap.SOAPException;
2ronwalf
2ronwalfimport org.apache.axis.client.Call;
6daenzeroramaimport org.apache.axis.constants.Style;
2ronwalfimport org.apache.axis.message.SOAPBodyElement;
2ronwalfimport org.apache.axis.message.SOAPEnvelope;
2ronwalfimport org.mindswap.utils.URIUtils;
2ronwalfimport org.mindswap.utils.Utils;
2ronwalfimport org.w3c.dom.Node;
2ronwalfimport org.w3c.dom.NodeList;
2ronwalf
2ronwalfpublic class WSDLOperation {
2ronwalf public static boolean DEBUG = false;
2ronwalf
2ronwalf private WSDLService service = null;
2ronwalf private Call call = null;
2ronwalf private Vector inputs = new Vector();
2ronwalf private Vector outputs = new Vector();
2ronwalf private String operationName;
2ronwalf private String inputMessageName;
2ronwalf private String outputMessageName;
2ronwalf private String portName;
2ronwalf private String documentation;
2ronwalf
2ronwalf public WSDLOperation(Call c) {
2ronwalf this.call = c;
2ronwalf }
2ronwalf
2ronwalf public WSDLParameter getInput(int i) {
2ronwalf return (WSDLParameter) getInputs().elementAt(i);
2ronwalf }
2ronwalf public WSDLParameter getInput(String name) {
2ronwalf for(int i = 0; i < inputs.size(); i++) {
2ronwalf WSDLParameter in = (WSDLParameter) inputs.get(i);
2ronwalf String paramName = in.getName();
2ronwalf if(paramName.equals(name) || URIUtils.getLocalName(paramName).equals(name))
2ronwalf return in;
2ronwalf }
2ronwalf
2ronwalf return null;
2ronwalf }
2ronwalf public WSDLParameter getOutput(int i) {
2ronwalf return (WSDLParameter) getOutputs().elementAt(i);
2ronwalf }
2ronwalf public WSDLParameter getOutput(String name) {
2ronwalf for(int i = 0; i < outputs.size(); i++) {
2ronwalf WSDLParameter out = (WSDLParameter) outputs.get(i);
2ronwalf String paramName = out.getName();
2ronwalf if( URIUtils.relaxedMatch(paramName, name) )
2ronwalf return out;
2ronwalf }
2ronwalf
2ronwalf return null;
2ronwalf }
2ronwalf public Vector getInputs() { return inputs; }
2ronwalf public Vector getOutputs() { return outputs; }
2ronwalf void addInput(String name, QName type) { inputs.add(new WSDLParameter(name, type)); }
2ronwalf void addOutput(String name, QName type) { outputs.add(new WSDLParameter(name, type)); }
2ronwalf
2ronwalf public String getName() { return call.getOperationName().getLocalPart(); }
2ronwalf
2ronwalf public String getOperationName() { return operationName; }
2ronwalf public void setOperationName(String s) { operationName = s; }
2ronwalf public String getPortName() { return portName; }
2ronwalf public void setPortName(String s) { portName = s; }
2ronwalf public String getInputMessageName() { return inputMessageName; }
2ronwalf public void setInputMessageName(String s) { inputMessageName = s; }
2ronwalf public String getOutputMessageName() { return outputMessageName; }
2ronwalf public void setOutputMessageName(String s) { outputMessageName = s; }
2ronwalf public WSDLService getService() { return service; }
2ronwalf public void setService(WSDLService s) { service = s; }
2ronwalf public String getDocumentation() { return documentation; }
2ronwalf public void setDocumentation(String s) { documentation = s; }
2ronwalf
2ronwalf public String toString() {
2ronwalf return getName();
2ronwalf }
2ronwalf
2ronwalf public String getDescription() {
2ronwalf String s = getName() + "(";
2ronwalf
2ronwalf for(Iterator i = inputs./*values().*/iterator(); i.hasNext(); ) {
2ronwalf WSDLParameter param = (WSDLParameter) i.next();
2ronwalf s += param.getName() + ":" + param.getType().getLocalPart();
2ronwalf if(i.hasNext()) s += ", ";
2ronwalf }
2ronwalf s += ") -> (";
2ronwalf
2ronwalf for(Iterator i = outputs./*values().*/iterator(); i.hasNext(); ) {
2ronwalf WSDLParameter param = (WSDLParameter) i.next();
2ronwalf s += param.getName() + ":" + param.getType().getLocalPart();
2ronwalf if(i.hasNext()) s += ", ";
2ronwalf }
2ronwalf s += ")";
2ronwalf
2ronwalf return s;
2ronwalf }
2ronwalf
2ronwalf
2ronwalf public void invoke() throws Exception {
2ronwalf SOAPEnvelope request = createRequest();
2ronwalf
2ronwalf if(DEBUG) {
2ronwalf System.out.println("Invoke operation " + getDescription());
2ronwalf System.out.println(request);
2ronwalf }
2ronwalf
2ronwalf SOAPEnvelope reply = call.invoke(request);
2ronwalf
2ronwalf if(DEBUG)
2ronwalf System.out.println(Utils.formatXML(reply));
2ronwalf
2ronwalf // TODO Handle SOAPFault message
2ronwalf
2ronwalf
2ronwalf processResult(reply);
2ronwalf }
2ronwalf
2ronwalf private void processResult(SOAPEnvelope reply) throws SOAPException {
2ronwalf SOAPElement soapBody = reply.getBody();
2ronwalf SOAPElement response = (SOAPElement) soapBody.getChildElements().next();
2ronwalf Iterator messageParts = response.getChildElements();
2ronwalf
2ronwalf for(int i = 0; i < outputs.size(); i++) {
2ronwalf WSDLParameter output = (WSDLParameter) outputs.elementAt(i);
2ronwalf
2ronwalf SOAPElement e = (SOAPElement) messageParts.next();
2ronwalf output.setTextValue(e.toString());
2ronwalf
2ronwalf if(DEBUG) {
2ronwalf System.out.println("processResult " + e);
2ronwalf System.out.println("getValue " + e.getValue());
2ronwalf System.out.println("getType " + (e.getNodeType() == Node.ELEMENT_NODE));
2ronwalf System.out.println("getValue is null? " + (e.getValue() == null));
2ronwalf System.out.println("result has children? " + e.getChildElements().hasNext());
2ronwalf if(e.getChildElements().hasNext()) {
2ronwalf Node child = (Node) e.getChildElements().next();
2ronwalf System.out.println("result first child " + child);
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf Iterator children = e.getChildElements();
2ronwalf if( children.hasNext() ) {
2ronwalf Node child = (Node) e.getChildElements().next();
2ronwalf if( child.getNodeType() == Node.TEXT_NODE )
2ronwalf output.setValue( child.toString() );
2ronwalf else
2ronwalf output.setValue( e.toString() );
2ronwalf }
2ronwalf else
2ronwalf output.setValue( e.toString() );
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf
2ronwalf private SOAPEnvelope createRequest() throws SOAPException {
2ronwalf String targetNamespace = call.getOperationName().getNamespaceURI();
2ronwalf String opName = call.getOperationName().getLocalPart();
2ronwalf SOAPEnvelope envelope = new SOAPEnvelope();
2ronwalf
2ronwalf if(DEBUG) {
2ronwalf System.out.println("SOAP Action = " +call.getSOAPActionURI());
2ronwalf System.out.println("SOAP Action used = " + call.useSOAPAction());
2ronwalf }
2ronwalf
2ronwalf envelope.addNamespaceDeclaration("xsi", WSDLConsts.xsiURI);
2ronwalf envelope.addNamespaceDeclaration("xsd",WSDLConsts. xsdURI);
2ronwalf //envelope.addNamespaceDeclaration("ns0", targetNamespace);
2ronwalf
2ronwalf String inputEncodingStyle = "http://schemas.xmlsoap.org/soap/encoding/";
2ronwalf if(inputEncodingStyle != null) {
2ronwalf envelope.setEncodingStyle(inputEncodingStyle);
2ronwalf envelope.addAttribute(WSDLConsts.soapURI, "encodingStyle", inputEncodingStyle);
2ronwalf }
2ronwalf
2ronwalf // FIXME test this feature
2ronwalf String nsOp = call.getOperationStyle().equals(Style.RPC) ? "u" : "";
2ronwalf SOAPBodyElement soapBody = new SOAPBodyElement(
2ronwalf envelope.createName(opName, nsOp, targetNamespace));
2ronwalf envelope.addBodyElement(soapBody);
2ronwalf
2ronwalf for(Iterator i = inputs.iterator(); i.hasNext(); ) {
2ronwalf WSDLParameter param = (WSDLParameter) i.next();
2ronwalf Object paramValue = param.getValue();
2ronwalf
2ronwalf if(paramValue == null) continue;
2ronwalf
2ronwalf SOAPElement soapElement = soapBody.addChildElement(URIUtils.getLocalName(param.getName()), "");
2ronwalf
2ronwalf // TODO treat string, int, float differently
2ronwalf if(paramValue instanceof Node) {
2ronwalf if(DEBUG)
2ronwalf System.out.println("Case 1");
2ronwalf createSOAPElement(soapElement, (Node) paramValue);
2ronwalf if(soapElement.getAttributeValue(WSDLConsts.xsiType) == null) {
2ronwalf if(DEBUG) System.out.println("Case 1a");
2ronwalf soapElement.addAttribute(WSDLConsts.xsiType,
2ronwalf "u:" + param.getType().getLocalPart());
2ronwalf }
2ronwalf }
2ronwalf else {
2ronwalf if(DEBUG)
2ronwalf System.out.println("Case 2 " + param.getType());
2ronwalf soapElement.addAttribute(WSDLConsts.xsiType, "xsd:" + param.getType().getLocalPart());
2ronwalf soapElement.addTextNode(paramValue.toString());
2ronwalf }
2ronwalf
2ronwalf }
2ronwalf
2ronwalf return envelope;
2ronwalf }
2ronwalf
2ronwalf public void createSOAPElement(SOAPElement parent, Node node) throws SOAPException {
2ronwalf int type = node.getNodeType();
2ronwalf
2ronwalf if(type == Node.TEXT_NODE) {
2ronwalf if(DEBUG)
2ronwalf System.out.println("Case 3");
2ronwalf
2ronwalf parent.addAttribute(WSDLConsts.xsiType, "xsd:string");
2ronwalf parent.addTextNode(node.getNodeValue());
2ronwalf }
2ronwalf else if(type == Node.ELEMENT_NODE) {
2ronwalf SOAPElement soapElement;
2ronwalf
2ronwalf if(!(node.getParentNode() instanceof org.w3c.dom.Document)) {
2ronwalf if(DEBUG)
2ronwalf System.out.println("Case 4");
2ronwalf
2ronwalf soapElement = parent.addChildElement(node.getNodeName());
2ronwalf }
2ronwalf else {
2ronwalf if(DEBUG)
2ronwalf System.out.println("Case 5");
2ronwalf
2ronwalf soapElement = parent;
2ronwalf }
2ronwalf
2ronwalf //NamedNodeMap attrs = node.getAttributes();
2ronwalf
2ronwalf NodeList children = node.getChildNodes();
2ronwalf if (children != null) {
2ronwalf int len = children.getLength();
2ronwalf for (int i = 0; i < len; i++) {
2ronwalf createSOAPElement(soapElement, children.item(i));
2ronwalf }
2ronwalf }
2ronwalf }
2ronwalf }
2ronwalf}