1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.tools.wsdl;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.ws.soap.SOAPBinding;
import org.apache.ws.policy.Policy;
import org.apache.ws.policy.util.PolicyFactory;
import org.apache.ws.policy.util.PolicyWriter;
import org.jboss.ws.Constants;
import org.jboss.ws.WSException;
import org.jboss.ws.core.soap.Style;
import org.jboss.ws.extensions.policy.PolicyScopeLevel;
import org.jboss.ws.extensions.policy.metadata.PolicyMetaExtension;
import org.jboss.ws.metadata.umdm.EndpointMetaData;
import org.jboss.ws.metadata.umdm.FaultMetaData;
import org.jboss.ws.metadata.umdm.MetaDataExtension;
import org.jboss.ws.metadata.umdm.OperationMetaData;
import org.jboss.ws.metadata.umdm.ParameterMetaData;
import org.jboss.ws.metadata.umdm.ServiceMetaData;
import org.jboss.ws.metadata.wsdl.Extendable;
import org.jboss.ws.metadata.wsdl.WSDLBinding;
import org.jboss.ws.metadata.wsdl.WSDLBindingFault;
import org.jboss.ws.metadata.wsdl.WSDLBindingOperation;
import org.jboss.ws.metadata.wsdl.WSDLBindingOperationInput;
import org.jboss.ws.metadata.wsdl.WSDLBindingOperationOutput;
import org.jboss.ws.metadata.wsdl.WSDLDefinitions;
import org.jboss.ws.metadata.wsdl.WSDLDocumentation;
import org.jboss.ws.metadata.wsdl.WSDLEndpoint;
import org.jboss.ws.metadata.wsdl.WSDLExtensibilityElement;
import org.jboss.ws.metadata.wsdl.WSDLImport;
import org.jboss.ws.metadata.wsdl.WSDLInterface;
import org.jboss.ws.metadata.wsdl.WSDLInterfaceFault;
import org.jboss.ws.metadata.wsdl.WSDLInterfaceOperation;
import org.jboss.ws.metadata.wsdl.WSDLInterfaceOperationInput;
import org.jboss.ws.metadata.wsdl.WSDLInterfaceOperationOutfault;
import org.jboss.ws.metadata.wsdl.WSDLInterfaceOperationOutput;
import org.jboss.ws.metadata.wsdl.WSDLProperty;
import org.jboss.ws.metadata.wsdl.WSDLRPCPart;
import org.jboss.ws.metadata.wsdl.WSDLRPCSignatureItem;
import org.jboss.ws.metadata.wsdl.WSDLSOAPHeader;
import org.jboss.ws.metadata.wsdl.WSDLService;
import org.jboss.ws.metadata.wsdl.WSDLRPCSignatureItem.Direction;
import org.jboss.wsf.common.DOMUtils;
import org.w3c.dom.Element;
/**
* Generates a WSDL object model.
*
* @author <a href="mailto:jason.greene@jboss.com">Jason T. Greene</a>
* @version $Revision: 5471 $
*/
public abstract class WSDLGenerator
{
protected WSDLDefinitions wsdl;
protected abstract void processTypes();
protected void processEndpoint(WSDLService service, EndpointMetaData endpoint)
{
WSDLEndpoint wsdlEndpoint = new WSDLEndpoint(service, endpoint.getPortName());
String address = endpoint.getEndpointAddress();
wsdlEndpoint.setAddress(address == null ? "REPLACE_WITH_ACTUAL_URL" : address);
service.addEndpoint(wsdlEndpoint);
QName interfaceQName = endpoint.getPortTypeName();
WSDLInterface wsdlInterface = new WSDLInterface(wsdl, interfaceQName);
wsdl.addInterface(wsdlInterface);
// Add imports
if (!interfaceQName.getNamespaceURI().equals(endpoint.getServiceMetaData().getServiceName().getNamespaceURI()))
{
WSDLImport wsdlImport = new WSDLImport(wsdl);
wsdlImport.setLocation(interfaceQName.getLocalPart() + "_PortType");
wsdlImport.setNamespace(interfaceQName.getNamespaceURI());
wsdl.addImport(wsdlImport);
wsdl.registerNamespaceURI(interfaceQName.getNamespaceURI(), null);
}
QName bindingQName = new QName(interfaceQName.getNamespaceURI(), interfaceQName.getLocalPart() + "Binding");
WSDLBinding wsdlBinding = new WSDLBinding(wsdl, bindingQName);
wsdlBinding.setInterfaceName(interfaceQName);
wsdlBinding.setType(endpoint.getBindingId());
wsdl.addBinding(wsdlBinding);
wsdlEndpoint.setBinding(bindingQName);
if (endpoint.getDocumentation() != null)
{
wsdlInterface.setDocumentationElement(new WSDLDocumentation(endpoint.getDocumentation()));
}
for (OperationMetaData operation : endpoint.getOperations())
{
processOperation(wsdlInterface, wsdlBinding, operation);
}
//Policies
MetaDataExtension ext = endpoint.getExtension(Constants.URI_WS_POLICY);
if (ext != null)
{
PolicyMetaExtension policyExt = (PolicyMetaExtension)ext;
for (Policy policy : policyExt.getPolicies(PolicyScopeLevel.WSDL_PORT))
{
addPolicyDefinition(policy);
addPolicyReference(policy, wsdlEndpoint);
}
for (Policy policy : policyExt.getPolicies(PolicyScopeLevel.WSDL_PORT_TYPE))
{
addPolicyDefinition(policy);
addPolicyURIAttribute(policy, wsdlInterface);
}
for (Policy policy : policyExt.getPolicies(PolicyScopeLevel.WSDL_BINDING))
{
addPolicyDefinition(policy);
addPolicyReference(policy, wsdlBinding);
}
}
}
protected void addPolicyDefinition(Policy policy)
{
try
{
PolicyWriter writer = PolicyFactory.getPolicyWriter(PolicyFactory.StAX_POLICY_WRITER);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writer.writePolicy(policy, outputStream);
Element element = DOMUtils.parse(outputStream.toString(Constants.DEFAULT_XML_CHARSET));
WSDLExtensibilityElement ext = new WSDLExtensibilityElement(Constants.WSDL_ELEMENT_POLICY, element);
wsdl.addExtensibilityElement(ext);
//optional: to obtain a better looking wsdl, register ws-policy
//prefix in wsdl:definitions if it is not defined there yet
if (wsdl.getPrefix(element.getNamespaceURI())==null)
{
wsdl.registerNamespaceURI(element.getNamespaceURI(),element.getPrefix());
}
}
catch (IOException ioe)
{
throw new WSException("Error while converting policy to element!");
}
}
protected void addPolicyReference(Policy policy, Extendable extendable)
{
QName policyRefQName = Constants.WSDL_ELEMENT_WSP_POLICYREFERENCE;
String prefix = wsdl.getPrefix(policyRefQName.getNamespaceURI());
if (prefix == null)
{
prefix = "wsp";
wsdl.registerNamespaceURI(policyRefQName.getNamespaceURI(), prefix);
}
Element element = DOMUtils.createElement(policyRefQName.getLocalPart(), prefix);
element.setAttribute("URI", policy.getPolicyURI());
//TODO!! we need to understand if the policy is local or not...
WSDLExtensibilityElement ext = new WSDLExtensibilityElement(Constants.WSDL_ELEMENT_POLICYREFERENCE, element);
extendable.addExtensibilityElement(ext);
}
protected void addPolicyURIAttribute(Policy policy, Extendable extendable)
{
//TODO!! we need to understand if the policy is local or not...
WSDLProperty prop = extendable.getProperty(Constants.WSDL_PROPERTY_POLICYURIS);
if (prop == null)
{
extendable.addProperty(new WSDLProperty(Constants.WSDL_PROPERTY_POLICYURIS, policy.getPolicyURI()));
}
else
{
//PolicyURIs ships a comma separated list of URIs...
prop.setValue(prop.getValue() + "," + policy.getPolicyURI());
}
}
protected void processOperation(WSDLInterface wsdlInterface, WSDLBinding wsdlBinding, OperationMetaData operation)
{
WSDLInterfaceOperation interfaceOperation = new WSDLInterfaceOperation(wsdlInterface, operation.getQName());
WSDLBindingOperation bindingOperation = new WSDLBindingOperation(wsdlBinding);
interfaceOperation.setPattern(operation.isOneWay() ? Constants.WSDL20_PATTERN_IN_ONLY
: Constants.WSDL20_PATTERN_IN_OUT);
bindingOperation.setRef(operation.getQName());
bindingOperation.setSOAPAction(operation.getSOAPAction());
if (operation.getStyle() == Style.DOCUMENT)
processOperationDOC(interfaceOperation, bindingOperation, operation);
else
processOperationRPC(interfaceOperation, bindingOperation, operation);
for (FaultMetaData fault : operation.getFaults())
{
QName faultName = new QName(operation.getQName().getNamespaceURI(), fault.getXmlName().getLocalPart());
WSDLInterfaceFault interfaceFault = new WSDLInterfaceFault(wsdlInterface, faultName);
interfaceFault.setElement(fault.getXmlName());
wsdlInterface.addFault(interfaceFault);
WSDLInterfaceOperationOutfault outfault = new WSDLInterfaceOperationOutfault(interfaceOperation);
outfault.setRef(faultName);
interfaceOperation.addOutfault(outfault);
WSDLBindingFault bindingFault = new WSDLBindingFault(wsdlBinding);
bindingFault.setRef(faultName);
wsdlBinding.addFault(bindingFault);
}
// process optional documentation
if (operation.getDocumentation() != null)
{
interfaceOperation.setDocumentationElement(new WSDLDocumentation(operation.getDocumentation()));
}
wsdlInterface.addOperation(interfaceOperation);
wsdlBinding.addOperation(bindingOperation);
}
protected void addSignatureItem(WSDLInterfaceOperation operation, ParameterMetaData param, boolean isReturn)
{
Direction direction;
if (isReturn)
{
direction = Direction.RETURN;
}
else if (param.getMode() == ParameterMode.INOUT)
{
direction = Direction.INOUT;
}
else if (param.getMode() == ParameterMode.OUT)
{
direction = Direction.OUT;
}
else
{
direction = Direction.IN;
}
operation.addRpcSignatureItem(new WSDLRPCSignatureItem(param.getPartName(), direction));
}
protected void processOperationDOC(WSDLInterfaceOperation interfaceOperation, WSDLBindingOperation bindingOperation, OperationMetaData operation)
{
interfaceOperation.setStyle(Constants.URI_STYLE_DOCUMENT);
WSDLInterfaceOperationInput input = new WSDLInterfaceOperationInput(interfaceOperation);
WSDLBindingOperationInput bindingInput = new WSDLBindingOperationInput(bindingOperation);
WSDLInterfaceOperationOutput output = null;
WSDLBindingOperationOutput bindingOutput = null;
boolean twoWay = !operation.isOneWay();
if (twoWay)
{
output = new WSDLInterfaceOperationOutput(interfaceOperation);
bindingOutput = new WSDLBindingOperationOutput(bindingOperation);
ParameterMetaData returnParameter = operation.getReturnParameter();
if (returnParameter != null)
{
QName xmlName = returnParameter.getXmlName();
String partName = returnParameter.getPartName();
if (returnParameter.isInHeader())
{
WSDLSOAPHeader header = new WSDLSOAPHeader(xmlName, partName);
header.setIncludeInSignature(true);
bindingOutput.addSoapHeader(header);
}
else
{
output.setElement(xmlName);
output.setPartName(partName);
}
addSignatureItem(interfaceOperation, returnParameter, true);
}
// If there is no return parameter, it will most likely be set later with an INOUT or OUT parameter.
// Otherwise, a null element means there is a 0 body element part, which is allowed by BP 1.0
interfaceOperation.addOutput(output);
bindingOperation.addOutput(bindingOutput);
}
for (ParameterMetaData param : operation.getParameters())
{
if (param.isInHeader())
{
WSDLSOAPHeader header = new WSDLSOAPHeader(param.getXmlName(), param.getPartName());
header.setIncludeInSignature(true);
if (param.getMode() != ParameterMode.OUT)
bindingInput.addSoapHeader(header);
if (twoWay && param.getMode() != ParameterMode.IN)
bindingOutput.addSoapHeader(header);
}
else
{
if (param.getMode() != ParameterMode.OUT)
{
input.setElement(param.getXmlName());
input.setPartName(param.getPartName());
}
if (twoWay && param.getMode() != ParameterMode.IN)
{
output.setElement(param.getXmlName());
output.setPartName(param.getPartName());
}
}
addSignatureItem(interfaceOperation, param, false);
}
interfaceOperation.addInput(input);
bindingOperation.addInput(bindingInput);
}
protected void processOperationRPC(WSDLInterfaceOperation interfaceOperation, WSDLBindingOperation bindingOperation, OperationMetaData operation)
{
interfaceOperation.setStyle(Constants.URI_STYLE_RPC);
WSDLInterfaceOperationInput input = new WSDLInterfaceOperationInput(interfaceOperation);
WSDLBindingOperationInput bindingInput = new WSDLBindingOperationInput(bindingOperation);
QName operationName = operation.getQName();
input.setElement(operationName);
WSDLInterfaceOperationOutput output = null;
WSDLBindingOperationOutput bindingOutput = null;
boolean twoWay = !operation.isOneWay();
if (twoWay)
{
output = new WSDLInterfaceOperationOutput(interfaceOperation);
bindingOutput = new WSDLBindingOperationOutput(bindingOperation);
output.setElement(new QName(operationName.getNamespaceURI(), operationName.getLocalPart() + "Response"));
ParameterMetaData returnParameter = operation.getReturnParameter();
if (returnParameter != null)
{
QName xmlName = returnParameter.getXmlName();
String partName = returnParameter.getPartName();
if (returnParameter.isInHeader())
{
WSDLSOAPHeader header = new WSDLSOAPHeader(xmlName, partName);
header.setIncludeInSignature(true);
bindingOutput.addSoapHeader(header);
}
else
{
WSDLRPCPart part = new WSDLRPCPart(returnParameter.getPartName(), returnParameter.getXmlType());
output.addChildPart(part);
}
addSignatureItem(interfaceOperation, returnParameter, true);
}
interfaceOperation.addOutput(output);
bindingOperation.addOutput(bindingOutput);
}
for (ParameterMetaData param : operation.getParameters())
{
if (param.isInHeader())
{
WSDLSOAPHeader header = new WSDLSOAPHeader(param.getXmlName(), param.getPartName());
header.setIncludeInSignature(true);
if (param.getMode() != ParameterMode.OUT)
bindingInput.addSoapHeader(header);
if (twoWay && param.getMode() != ParameterMode.IN)
bindingOutput.addSoapHeader(header);
}
else
{
WSDLRPCPart part = new WSDLRPCPart(param.getPartName(), param.getXmlType());
if (param.getMode() != ParameterMode.OUT)
input.addChildPart(part);
if (twoWay && param.getMode() != ParameterMode.IN)
output.addChildPart(part);
}
addSignatureItem(interfaceOperation, param, false);
}
interfaceOperation.addInput(input);
bindingOperation.addInput(bindingInput);
}
protected void processService(ServiceMetaData service)
{
WSDLService wsdlService = new WSDLService(wsdl, service.getServiceName());
wsdl.addService(wsdlService);
EndpointMetaData endpoint = null;
for (Iterator<EndpointMetaData> iter = service.getEndpoints().iterator(); iter.hasNext();)
{
endpoint = iter.next();
processEndpoint(wsdlService, endpoint);
}
if (endpoint == null)
throw new IllegalStateException("A service must have an endpoint");
wsdlService.setInterfaceName(endpoint.getPortName());
}
/**
* Generate a WSDL object model from the passed in ServiceMetaData.
*
* @param service the service
* @return the WSDL object model
*/
public WSDLDefinitions generate(ServiceMetaData service)
{
// For now only WSDL 1.1
wsdl = new WSDLDefinitions();
wsdl.setWsdlNamespace(Constants.NS_WSDL11);
// One WSDL per service
String ns = service.getServiceName().getNamespaceURI();
wsdl.setTargetNamespace(ns);
wsdl.registerNamespaceURI(ns, "tns");
wsdl.registerNamespaceURI(Constants.NS_SCHEMA_XSD, "xsd");
String soapURI = null;
String soapPrefix = null;
for (EndpointMetaData ep : service.getEndpoints())
{
String bindingId = ep.getBindingId();
if (bindingId.startsWith(SOAPBinding.SOAP11HTTP_BINDING))
{
soapPrefix = "soap";
soapURI = Constants.NS_SOAP11;
}
else if (bindingId.startsWith(SOAPBinding.SOAP12HTTP_BINDING))
{
soapPrefix = "soap12";
soapURI = Constants.NS_SOAP12;
}
}
if (soapURI != null && soapPrefix != null)
wsdl.registerNamespaceURI(soapURI, soapPrefix);
processTypes();
processService(service);
return wsdl;
}
}
|