001: /*
002: * $Id: WSDLBindingFactory.java,v 1.9 2004/12/09 12:34:49 kowap Exp $
003: *
004: * Copyright (c) 2004 Patric Fornasier, Pawel Kowalski
005: * Berne University of Applied Sciences
006: * School of Engineering and Information Technology
007: * All rights reserved.
008: */
009: package bexee.wsdl;
010:
011: import java.util.Iterator;
012: import java.util.List;
013: import java.util.Map;
014:
015: import javax.wsdl.Binding;
016: import javax.wsdl.BindingFault;
017: import javax.wsdl.BindingInput;
018: import javax.wsdl.BindingOperation;
019: import javax.wsdl.BindingOutput;
020: import javax.wsdl.Definition;
021: import javax.wsdl.Fault;
022: import javax.wsdl.Input;
023: import javax.wsdl.Operation;
024: import javax.wsdl.Output;
025: import javax.wsdl.Port;
026: import javax.wsdl.PortType;
027: import javax.wsdl.Service;
028: import javax.wsdl.WSDLException;
029: import javax.wsdl.extensions.ExtensibilityElement;
030: import javax.wsdl.extensions.ExtensionRegistry;
031: import javax.wsdl.extensions.soap.SOAPAddress;
032: import javax.wsdl.extensions.soap.SOAPBinding;
033: import javax.wsdl.extensions.soap.SOAPBody;
034: import javax.wsdl.extensions.soap.SOAPFault;
035: import javax.wsdl.extensions.soap.SOAPOperation;
036: import javax.xml.namespace.QName;
037:
038: import bexee.util.Constants;
039:
040: /**
041: * This class is used as a WSDL:Binding and WSDL:Service factory. It will be
042: * used for the generation of concrete bindings and services for abstract web
043: * services.
044: *
045: * @version $Revision: 1.9 $, $Date: 2004/12/09 12:34:49 $
046: * @author Patric Fornasier
047: * @author Pawel Kowalski
048: */
049: public class WSDLBindingFactory {
050:
051: /**
052: * Add a binding information to the Definitions instance. Uses the given URI
053: * String as the service location.
054: *
055: * @param definition
056: * @param uri
057: * @return @throws
058: * WSDLException
059: */
060: public Definition addBinding(Definition definition, String uri)
061: throws WSDLException {
062:
063: ExtensionRegistry extReg = definition.getExtensionRegistry();
064:
065: // create and add operationbindings
066: Map portTypes = definition.getPortTypes();
067: for (Iterator iter = portTypes.values().iterator(); iter
068: .hasNext();) {
069: PortType portType = (PortType) iter.next();
070:
071: // create and add a binding
072: Binding binding = getBinding(portType, definition);
073: definition.addBinding(binding);
074:
075: // create and add a service
076: definition.addService(getService(binding, portType,
077: definition, uri));
078: }
079:
080: // add the soap address to the definition
081: definition.addNamespace(Constants.WSDL_SOAP_PPREFIX,
082: Constants.URI_WSDL_SOAP);
083: definition.addNamespace(Constants.WSDL_PRFIX,
084: Constants.URI_WSDL);
085:
086: return definition;
087: }
088:
089: private Service getService(Binding binding, PortType portType,
090: Definition def, String uri) throws WSDLException {
091:
092: ExtensionRegistry extReg = def.getExtensionRegistry();
093:
094: // create a service for the portType
095: Service service = def.createService();
096: service.setQName(new QName(portType.getQName()
097: .getNamespaceURI(), portType.getQName().getLocalPart()
098: + Constants.SERVICE_SUFFIX));
099:
100: // create a port and add it to the service
101: Port port = def.createPort();
102: port.setBinding(binding);
103: port.setName(portType.getQName().getLocalPart());
104: service.addPort(port);
105:
106: // create a soapaddress and add it to the port
107: SOAPAddress soapAddress = (SOAPAddress) extReg.createExtension(
108: Port.class, Constants.SOAP_ADDRESS_QNAME);
109: soapAddress.setLocationURI(uri);
110: port.addExtensibilityElement(soapAddress);
111:
112: return service;
113: }
114:
115: private Binding getBinding(PortType portType, Definition def)
116: throws WSDLException {
117:
118: ExtensionRegistry extReg = def.getExtensionRegistry();
119:
120: // Create a binding
121: Binding binding = def.createBinding();
122: binding.setUndefined(false);
123: binding.setQName(new QName(portType.getQName()
124: .getNamespaceURI(), portType.getQName().getLocalPart()
125: + Constants.SOAP_BINDING_SUFFIX));
126: binding.setPortType(portType);
127:
128: // create a soapbinding
129: SOAPBinding soapBinding = (SOAPBinding) extReg.createExtension(
130: Binding.class, Constants.SOAP_BINDING_QNAME);
131: soapBinding.setStyle(bexee.util.Constants.DEFAULT_STYLE);
132: soapBinding.setTransportURI(bexee.util.Constants.URI_SOAP_HTTP);
133:
134: // add the soapbinding
135: binding.addExtensibilityElement(soapBinding);
136:
137: // create and add operationbindings
138: List operations = portType.getOperations();
139: for (Iterator iterator = operations.iterator(); iterator
140: .hasNext();) {
141: Operation operation = (Operation) iterator.next();
142: binding.addBindingOperation(getBindingOperation(operation,
143: def));
144: }
145:
146: return binding;
147: }
148:
149: private BindingOperation getBindingOperation(Operation operation,
150: Definition definition) throws WSDLException {
151:
152: // get the extension registry for element creation
153: ExtensionRegistry extReg = definition.getExtensionRegistry();
154:
155: // create the parts of the BindingOperation
156: BindingOperation bindingOper = definition
157: .createBindingOperation();
158: BindingInput bindingInput = definition.createBindingInput();
159: BindingOutput bindingOutput = definition.createBindingOutput();
160:
161: // set properties of the BindingOperation
162: bindingOper.setName(operation.getName());
163: bindingOper.setOperation(operation);
164:
165: // create a soapoperation and set it
166: SOAPOperation soapOper = (SOAPOperation) extReg
167: .createExtension(BindingOperation.class,
168: Constants.SOAP_OPERATION_QNAME);
169: soapOper.setSoapActionURI("");
170: bindingOper.addExtensibilityElement(soapOper);
171:
172: // Input clause
173: bindingOper.setBindingInput(getBindingInput(operation
174: .getInput(), definition));
175:
176: // Output clause
177: bindingOper.setBindingOutput(getBindingOutput(operation
178: .getOutput(), definition));
179:
180: // Faults clause
181: Map faults = operation.getFaults();
182: for (Iterator iter = faults.values().iterator(); iter.hasNext();) {
183: Fault fault = (Fault) iter.next();
184: bindingOper.addBindingFault(getBindingFault(fault,
185: definition));
186: }
187:
188: return bindingOper;
189: }
190:
191: private BindingInput getBindingInput(Input input,
192: Definition definition) throws WSDLException {
193:
194: // create an input and set properties
195: BindingInput bindInput = definition.createBindingInput();
196: bindInput.setName(input.getMessage().getQName().getLocalPart());
197:
198: bindInput.addExtensibilityElement(getSOAPBody(bindInput
199: .getClass(), definition));
200:
201: return bindInput;
202: }
203:
204: private BindingOutput getBindingOutput(Output output,
205: Definition definition) throws WSDLException {
206:
207: // create an input and set properties
208: BindingOutput bindOutput = definition.createBindingOutput();
209: bindOutput.setName(output.getMessage().getQName()
210: .getLocalPart());
211:
212: bindOutput.addExtensibilityElement(getSOAPBody(bindOutput
213: .getClass(), definition));
214: return bindOutput;
215: }
216:
217: private BindingFault getBindingFault(Fault fault,
218: Definition definition) throws WSDLException {
219:
220: BindingFault bindingFault = definition.createBindingFault();
221: bindingFault.setName(fault.getName());
222: bindingFault.addExtensibilityElement(getSOAPFault(bindingFault,
223: definition));
224:
225: return bindingFault;
226:
227: }
228:
229: private ExtensibilityElement getSOAPBody(Class clazz,
230: Definition definition) throws WSDLException {
231:
232: // get the extension registry for element creation
233: ExtensionRegistry extReg = definition.getExtensionRegistry();
234:
235: // create a soapbody
236: SOAPBody soapBody = (SOAPBody) extReg.createExtension(clazz
237: .getInterfaces()[0], Constants.SOAP_BODY_QNAME);
238:
239: // set properties
240: soapBody.setUse(Constants.DEFAULT_USE);
241: soapBody.setEncodingStyles(Constants.ENCODING_STYLES);
242: soapBody.setNamespaceURI(definition.getTargetNamespace());
243:
244: return soapBody;
245: }
246:
247: private SOAPFault getSOAPFault(BindingFault fault,
248: Definition definition) throws WSDLException {
249:
250: // get the extension registry for element creation
251: ExtensionRegistry extReg = definition.getExtensionRegistry();
252:
253: SOAPFault soapFault = (SOAPFault) extReg.createExtension(
254: BindingFault.class, Constants.SOAP_FAULT_QNAME);
255:
256: soapFault.setUse(Constants.DEFAULT_USE);
257: soapFault.setEncodingStyles(Constants.ENCODING_STYLES);
258:
259: return soapFault;
260: }
261:
262: }
|