View Javadoc

1   /*
2    * $Id: WSDLBindingFactory.java,v 1.1 2004/12/15 14:18:16 patforna Exp $
3    *
4    * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
5    * Berne University of Applied Sciences
6    * School of Engineering and Information Technology
7    * All rights reserved.
8    */
9   package bexee.wsdl;
10  
11  import java.util.Iterator;
12  import java.util.List;
13  import java.util.Map;
14  
15  import javax.wsdl.Binding;
16  import javax.wsdl.BindingFault;
17  import javax.wsdl.BindingInput;
18  import javax.wsdl.BindingOperation;
19  import javax.wsdl.BindingOutput;
20  import javax.wsdl.Definition;
21  import javax.wsdl.Fault;
22  import javax.wsdl.Input;
23  import javax.wsdl.Operation;
24  import javax.wsdl.Output;
25  import javax.wsdl.Port;
26  import javax.wsdl.PortType;
27  import javax.wsdl.Service;
28  import javax.wsdl.WSDLException;
29  import javax.wsdl.extensions.ExtensibilityElement;
30  import javax.wsdl.extensions.ExtensionRegistry;
31  import javax.wsdl.extensions.soap.SOAPAddress;
32  import javax.wsdl.extensions.soap.SOAPBinding;
33  import javax.wsdl.extensions.soap.SOAPBody;
34  import javax.wsdl.extensions.soap.SOAPFault;
35  import javax.wsdl.extensions.soap.SOAPOperation;
36  import javax.xml.namespace.QName;
37  
38  import bexee.util.Constants;
39  
40  /***
41   * This class is used as a WSDL:Binding and WSDL:Service factory. It will be
42   * used for the generation of concrete bindings and services for abstract web
43   * services.
44   * 
45   * @version $Revision: 1.1 $, $Date: 2004/12/15 14:18:16 $
46   * @author Patric Fornasier
47   * @author Pawel Kowalski
48   */
49  public class WSDLBindingFactory {
50  
51      /***
52       * Add a binding information to the Definitions instance. Uses the given URI
53       * String as the service location.
54       * 
55       * @param definition
56       * @param uri
57       * @return @throws
58       *         WSDLException
59       */
60      public Definition addBinding(Definition definition, String uri)
61              throws WSDLException {
62  
63          ExtensionRegistry extReg = definition.getExtensionRegistry();
64  
65          // create and add operationbindings
66          Map portTypes = definition.getPortTypes();
67          for (Iterator iter = portTypes.values().iterator(); iter.hasNext();) {
68              PortType portType = (PortType) iter.next();
69  
70              // create and add a binding
71              Binding binding = getBinding(portType, definition);
72              definition.addBinding(binding);
73  
74              // create and add a service
75              definition
76                      .addService(getService(binding, portType, definition, uri));
77          }
78  
79          // add the soap address to the definition
80          definition.addNamespace(Constants.WSDL_SOAP_PPREFIX,
81                  Constants.URI_WSDL_SOAP);
82          definition.addNamespace(Constants.WSDL_PRFIX, Constants.URI_WSDL);
83  
84          return definition;
85      }
86  
87      private Service getService(Binding binding, PortType portType,
88              Definition def, String uri) throws WSDLException {
89  
90          ExtensionRegistry extReg = def.getExtensionRegistry();
91  
92          // create a service for the portType
93          Service service = def.createService();
94          service.setQName(new QName(portType.getQName().getNamespaceURI(),
95                  portType.getQName().getLocalPart() + Constants.SERVICE_SUFFIX));
96  
97          // create a port and add it to the service
98          Port port = def.createPort();
99          port.setBinding(binding);
100         port.setName(portType.getQName().getLocalPart());
101         service.addPort(port);
102 
103         // create a soapaddress and add it to the port
104         SOAPAddress soapAddress = (SOAPAddress) extReg.createExtension(
105                 Port.class, Constants.SOAP_ADDRESS_QNAME);
106         soapAddress.setLocationURI(uri);
107         port.addExtensibilityElement(soapAddress);
108 
109         return service;
110     }
111 
112     private Binding getBinding(PortType portType, Definition def)
113             throws WSDLException {
114 
115         ExtensionRegistry extReg = def.getExtensionRegistry();
116 
117         // Create a binding
118         Binding binding = def.createBinding();
119         binding.setUndefined(false);
120         binding.setQName(new QName(portType.getQName().getNamespaceURI(),
121                 portType.getQName().getLocalPart()
122                         + Constants.SOAP_BINDING_SUFFIX));
123         binding.setPortType(portType);
124 
125         // create a soapbinding
126         SOAPBinding soapBinding = (SOAPBinding) extReg.createExtension(
127                 Binding.class, Constants.SOAP_BINDING_QNAME);
128         soapBinding.setStyle(bexee.util.Constants.DEFAULT_STYLE);
129         soapBinding.setTransportURI(bexee.util.Constants.URI_SOAP_HTTP);
130 
131         // add the soapbinding
132         binding.addExtensibilityElement(soapBinding);
133 
134         // create and add operationbindings
135         List operations = portType.getOperations();
136         for (Iterator iterator = operations.iterator(); iterator.hasNext();) {
137             Operation operation = (Operation) iterator.next();
138             binding.addBindingOperation(getBindingOperation(operation, def));
139         }
140 
141         return binding;
142     }
143 
144     private BindingOperation getBindingOperation(Operation operation,
145             Definition definition) throws WSDLException {
146 
147         // get the extension registry for element creation
148         ExtensionRegistry extReg = definition.getExtensionRegistry();
149 
150         // create the parts of the BindingOperation
151         BindingOperation bindingOper = definition.createBindingOperation();
152         BindingInput bindingInput = definition.createBindingInput();
153         BindingOutput bindingOutput = definition.createBindingOutput();
154 
155         // set properties of the BindingOperation
156         bindingOper.setName(operation.getName());
157         bindingOper.setOperation(operation);
158 
159         // create a soapoperation and set it
160         SOAPOperation soapOper = (SOAPOperation) extReg.createExtension(
161                 BindingOperation.class, Constants.SOAP_OPERATION_QNAME);
162         soapOper.setSoapActionURI("");
163         bindingOper.addExtensibilityElement(soapOper);
164 
165         // Input clause
166         bindingOper.setBindingInput(getBindingInput(operation.getInput(),
167                 definition));
168 
169         // Output clause
170         bindingOper.setBindingOutput(getBindingOutput(operation.getOutput(),
171                 definition));
172 
173         // Faults clause
174         Map faults = operation.getFaults();
175         for (Iterator iter = faults.values().iterator(); iter.hasNext();) {
176             Fault fault = (Fault) iter.next();
177             bindingOper.addBindingFault(getBindingFault(fault, definition));
178         }
179 
180         return bindingOper;
181     }
182 
183     private BindingInput getBindingInput(Input input, Definition definition)
184             throws WSDLException {
185 
186         // create an input and set properties
187         BindingInput bindInput = definition.createBindingInput();
188         bindInput.setName(input.getMessage().getQName().getLocalPart());
189 
190         bindInput.addExtensibilityElement(getSOAPBody(bindInput.getClass(),
191                 definition));
192 
193         return bindInput;
194     }
195 
196     private BindingOutput getBindingOutput(Output output, Definition definition)
197             throws WSDLException {
198 
199         // create an input and set properties
200         BindingOutput bindOutput = definition.createBindingOutput();
201         bindOutput.setName(output.getMessage().getQName().getLocalPart());
202 
203         bindOutput.addExtensibilityElement(getSOAPBody(bindOutput.getClass(),
204                 definition));
205         return bindOutput;
206     }
207 
208     private BindingFault getBindingFault(Fault fault, Definition definition)
209             throws WSDLException {
210 
211         BindingFault bindingFault = definition.createBindingFault();
212         bindingFault.setName(fault.getName());
213         bindingFault.addExtensibilityElement(getSOAPFault(bindingFault,
214                 definition));
215 
216         return bindingFault;
217 
218     }
219 
220     private ExtensibilityElement getSOAPBody(Class clazz, Definition definition)
221             throws WSDLException {
222 
223         // get the extension registry for element creation
224         ExtensionRegistry extReg = definition.getExtensionRegistry();
225 
226         // create a soapbody
227         SOAPBody soapBody = (SOAPBody) extReg.createExtension(clazz
228                 .getInterfaces()[0], Constants.SOAP_BODY_QNAME);
229 
230         // set properties
231         soapBody.setUse(Constants.DEFAULT_USE);
232         soapBody.setEncodingStyles(Constants.ENCODING_STYLES);
233         soapBody.setNamespaceURI(definition.getTargetNamespace());
234 
235         return soapBody;
236     }
237 
238     private SOAPFault getSOAPFault(BindingFault fault, Definition definition)
239             throws WSDLException {
240 
241         // get the extension registry for element creation
242         ExtensionRegistry extReg = definition.getExtensionRegistry();
243 
244         SOAPFault soapFault = (SOAPFault) extReg.createExtension(
245                 BindingFault.class, Constants.SOAP_FAULT_QNAME);
246 
247         soapFault.setUse(Constants.DEFAULT_USE);
248         soapFault.setEncodingStyles(Constants.ENCODING_STYLES);
249 
250         return soapFault;
251     }
252 
253 }