001 /****************************************************************************** 002 * Copyright (C) MActor Developers. All rights reserved. * 003 * ---------------------------------------------------------------------------* 004 * This file is part of MActor. * 005 * * 006 * MActor is free software; you can redistribute it and/or modify * 007 * it under the terms of the GNU General Public License as published by * 008 * the Free Software Foundation; either version 2 of the License, or * 009 * (at your option) any later version. * 010 * * 011 * MActor is distributed in the hope that it will be useful, * 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 014 * GNU General Public License for more details. * 015 * * 016 * You should have received a copy of the GNU General Public License * 017 * along with MActor; if not, write to the Free Software * 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * 019 ******************************************************************************/ 020 package org.mactor.brokers.soap.wsdl; 021 022 import java.io.File; 023 import java.io.FileInputStream; 024 import java.net.URL; 025 import java.util.ArrayList; 026 import java.util.Iterator; 027 import java.util.LinkedList; 028 import java.util.List; 029 import java.util.Map; 030 031 import javax.wsdl.Binding; 032 import javax.wsdl.BindingInput; 033 import javax.wsdl.BindingOperation; 034 import javax.wsdl.BindingOutput; 035 import javax.wsdl.Definition; 036 import javax.wsdl.Input; 037 import javax.wsdl.Message; 038 import javax.wsdl.Operation; 039 import javax.wsdl.Output; 040 import javax.wsdl.Port; 041 import javax.wsdl.Service; 042 import javax.wsdl.extensions.ExtensibilityElement; 043 import javax.wsdl.extensions.soap.SOAPAddress; 044 import javax.wsdl.extensions.soap.SOAPBinding; 045 import javax.wsdl.extensions.soap.SOAPBody; 046 import javax.wsdl.extensions.soap.SOAPOperation; 047 import javax.wsdl.factory.WSDLFactory; 048 import javax.wsdl.xml.WSDLReader; 049 import javax.xml.namespace.QName; 050 051 import org.xml.sax.InputSource; 052 053 // This code is based on example provided by Jim Winfield at 054 // http://www2.sys-con.com/ITSG/virtualcd/WebServices/archives/0310/behera/index.htm 055 public class Wsdl { 056 /** The default SOAP encoding to use. */ 057 public final static String DEFAULT_SOAP_ENCODING_STYLE = "http://schemas.xmlsoap.org/soap/encoding/"; 058 List<OperationInfo> operations; 059 Definition def; 060 public Wsdl(URL url) throws Exception { 061 this(new InputSource(url.openStream())); 062 } 063 public Wsdl(File file) throws Exception { 064 this(new InputSource(new FileInputStream(file))); 065 } 066 private Wsdl(InputSource is) throws Exception { 067 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); 068 def = reader.readWSDL(null, is); 069 this.operations = getOperations(def); 070 } 071 private List<OperationInfo> getOperations(Definition def) { 072 List<OperationInfo> operations = new LinkedList<OperationInfo>(); 073 Map services = def.getServices(); 074 if (services != null) { 075 Iterator it = services.values().iterator(); 076 for (int i = 0; it.hasNext(); i++) { 077 Service service = (Service) it.next(); 078 QName qName = service.getQName(); 079 String namespace = qName.getNamespaceURI(); 080 Map ports = service.getPorts(); 081 Iterator portIter = ports.values().iterator(); 082 while (portIter.hasNext()) { 083 Port port = (Port) portIter.next(); 084 Binding binding = port.getBinding(); 085 Iterator operIter = getOperations(binding).iterator(); 086 while (operIter.hasNext()) { 087 OperationInfo operation = (OperationInfo) operIter.next(); 088 operation.setNamespaceURI(namespace); 089 operation.setPort(port.getName()); 090 operation.setService(service.getQName().getLocalPart()); 091 ExtensibilityElement addrElem = findExtensibilityElement(port.getExtensibilityElements(), "address"); 092 if (addrElem != null && addrElem instanceof SOAPAddress) { 093 SOAPAddress soapAddr = (SOAPAddress) addrElem; 094 operation.setTargetURL(soapAddr.getLocationURI()); 095 } 096 operations.add(operation); 097 } 098 } 099 } 100 } 101 return operations; 102 } 103 private List getOperations(Binding binding) { 104 List operationInfos = new ArrayList(); 105 List operations = binding.getBindingOperations(); 106 if (operations != null && !operations.isEmpty()) { 107 ExtensibilityElement soapBindingElem = findExtensibilityElement(binding.getExtensibilityElements(), "binding"); 108 String style = "document"; // default 109 if (soapBindingElem != null && soapBindingElem instanceof SOAPBinding) { 110 SOAPBinding soapBinding = (SOAPBinding) soapBindingElem; 111 style = soapBinding.getStyle(); 112 } 113 Iterator opIter = operations.iterator(); 114 int i = 0; 115 while (opIter.hasNext()) { 116 BindingOperation oper = (BindingOperation) opIter.next(); 117 ExtensibilityElement operElem = findExtensibilityElement(oper.getExtensibilityElements(), "operation"); 118 if (operElem != null && operElem instanceof SOAPOperation) { 119 OperationInfo operationInfo = new OperationInfo(style); 120 buildOperation(operationInfo, oper); 121 // Add to the return list 122 operationInfos.add(operationInfo); 123 } 124 } 125 } 126 return operationInfos; 127 } 128 private OperationInfo buildOperation(OperationInfo operationInfo, BindingOperation bindingOper) { 129 Operation oper = bindingOper.getOperation(); 130 operationInfo.setTargetMethodName(oper.getName()); 131 ExtensibilityElement operElem = findExtensibilityElement(bindingOper.getExtensibilityElements(), "operation"); 132 if (operElem != null && operElem instanceof SOAPOperation) { 133 SOAPOperation soapOperation = (SOAPOperation) operElem; 134 operationInfo.setSoapActionURI(soapOperation.getSoapActionURI()); 135 } 136 BindingInput bindingInput = bindingOper.getBindingInput(); 137 BindingOutput bindingOutput = bindingOper.getBindingOutput(); 138 ExtensibilityElement bodyElem = findExtensibilityElement(bindingInput.getExtensibilityElements(), "body"); 139 if (bodyElem != null && bodyElem instanceof SOAPBody) { 140 SOAPBody soapBody = (SOAPBody) bodyElem; 141 List styles = soapBody.getEncodingStyles(); 142 String encodingStyle = null; 143 if (styles != null) { 144 encodingStyle = styles.get(0).toString(); 145 } 146 if (encodingStyle == null) { 147 encodingStyle = DEFAULT_SOAP_ENCODING_STYLE; 148 } 149 operationInfo.setEncodingStyle(encodingStyle.toString()); 150 operationInfo.setTargetObjectURI(soapBody.getNamespaceURI()); 151 } 152 Input inDef = oper.getInput(); 153 if (inDef != null) { 154 Message inMsg = inDef.getMessage(); 155 if (inMsg != null) { 156 operationInfo.setInputMessageName(inMsg.getQName().getLocalPart()); 157 // Set the body of the operation's input message 158 // TODO:operationInfo.setInputMessageText(buildMessageText(operationInfo, 159 // inMsg)); 160 } 161 } 162 Output outDef = oper.getOutput(); 163 if (outDef != null) { 164 Message outMsg = outDef.getMessage(); 165 if (outMsg != null) { 166 operationInfo.setOutputMessageName(outMsg.getQName().getLocalPart()); 167 // Set the body of the operation's output message 168 // TODO:operationInfo.setOutputMessageText(buildMessageText(operationInfo, 169 // outMsg)); 170 } 171 } 172 return operationInfo; 173 } 174 private ExtensibilityElement findExtensibilityElement(List extensibilityElements, String elementType) { 175 if (extensibilityElements != null) { 176 Iterator iter = extensibilityElements.iterator(); 177 while (iter.hasNext()) { 178 ExtensibilityElement element = (ExtensibilityElement) iter.next(); 179 if (element.getElementType().getLocalPart().equalsIgnoreCase(elementType)) { 180 return element; 181 } 182 } 183 } 184 return null; 185 } 186 public static class OperationInfo { 187 private String operationType = ""; 188 private String encodingStyle = ""; 189 private String targetURL = ""; 190 private String namespaceURI = ""; 191 private String targetObjectURI = ""; 192 private String targetMethodName = ""; 193 private String inputMessageText = ""; 194 private String outputMessageText = ""; 195 private String inputMessageName = ""; 196 private String outputMessageName = ""; 197 private String soapActionURI = ""; 198 private String style = "document"; 199 private String port; 200 private String service; 201 public OperationInfo(String style) { 202 super(); 203 setStyle(style); 204 } 205 public void setEncodingStyle(String value) { 206 encodingStyle = value; 207 } 208 public String getEncodingStyle() { 209 return encodingStyle; 210 } 211 public void setTargetURL(String value) { 212 targetURL = value; 213 } 214 public String getTargetURL() { 215 return targetURL; 216 } 217 public void setNamespaceURI(String value) { 218 namespaceURI = value; 219 } 220 public String getNamespaceURI() { 221 return namespaceURI; 222 } 223 public void setTargetObjectURI(String value) { 224 targetObjectURI = value; 225 } 226 public String getTargetObjectURI() { 227 return targetObjectURI; 228 } 229 public void setTargetMethodName(String value) { 230 targetMethodName = value; 231 } 232 public String getTargetMethodName() { 233 return targetMethodName; 234 } 235 public void setInputMessageName(String value) { 236 inputMessageName = value; 237 } 238 public String getInputMessageName() { 239 return inputMessageName; 240 } 241 public void setOutputMessageName(String value) { 242 outputMessageName = value; 243 } 244 public String getOutputMessageName() { 245 return outputMessageName; 246 } 247 public void setInputMessageText(String value) { 248 inputMessageText = value; 249 } 250 public String getInputMessageText() { 251 return inputMessageText; 252 } 253 public void setOutputMessageText(String value) { 254 outputMessageText = value; 255 } 256 public String getOutputMessageText() { 257 return outputMessageText; 258 } 259 public void setSoapActionURI(String value) { 260 soapActionURI = value; 261 } 262 public String getSoapActionURI() { 263 return soapActionURI; 264 } 265 public void setStyle(String value) { 266 style = value; 267 } 268 public String getStyle() { 269 return style; 270 } 271 public String toString() { 272 return getTargetMethodName(); 273 } 274 public String getOperationType() { 275 return operationType; 276 } 277 public void setOperationType(String operationType) { 278 this.operationType = operationType; 279 } 280 public String getPort() { 281 return port; 282 } 283 public void setPort(String port) { 284 this.port = port; 285 } 286 public String getService() { 287 return service; 288 } 289 public void setService(String service) { 290 this.service = service; 291 } 292 } 293 public List<OperationInfo> getOperations() { 294 return operations; 295 } 296 }