001: package org.objectweb.celtix.tools.processors.wsdl2;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.wsdl.Binding;
010: import javax.wsdl.BindingFault;
011: import javax.wsdl.BindingInput;
012: import javax.wsdl.BindingOperation;
013: import javax.wsdl.BindingOutput;
014: import javax.wsdl.Fault;
015: import javax.wsdl.Input;
016: import javax.wsdl.Operation;
017: import javax.wsdl.Output;
018: import javax.wsdl.Part;
019: import javax.wsdl.PortType;
020: import javax.wsdl.WSDLException;
021: import javax.wsdl.extensions.ExtensionRegistry;
022: import javax.wsdl.extensions.soap.SOAPBinding;
023: import javax.wsdl.extensions.soap.SOAPBody;
024: import javax.wsdl.extensions.soap.SOAPFault;
025: import javax.wsdl.extensions.soap.SOAPOperation;
026: import javax.wsdl.xml.WSDLWriter;
027: import javax.xml.namespace.QName;
028:
029: import org.objectweb.celtix.common.i18n.Message;
030: import org.objectweb.celtix.tools.common.ToolConstants;
031: import org.objectweb.celtix.tools.common.ToolException;
032: import org.objectweb.celtix.tools.common.WSDLConstants;
033:
034: public class WSDLToSoapProcessor extends WSDLToProcessor {
035:
036: private static final String NEW_FILE_NAME_MODIFIER = "-soapbinding";
037:
038: private ExtensionRegistry extReg;
039:
040: private Map portTypes;
041: private PortType portType;
042: private Binding binding;
043:
044: public void process() throws ToolException {
045: init();
046: validate();
047: extReg = this .wsdlReader.getExtensionRegistry();
048: doAppendBinding();
049: }
050:
051: private void validate() throws ToolException {
052: if (isBindingExisted()) {
053: Message msg = new Message("BINDING_ALREADY_EXIST", LOG);
054: throw new ToolException(msg);
055: }
056: if (!isPortTypeExisted()) {
057: Message msg = new Message("PORTTYPE_NOT_EXIST", LOG);
058: throw new ToolException(msg);
059: }
060: if (!nameSpaceCheck()) {
061: Message msg = new Message(
062: "SOAPBINDING_STYLE_NOT_PROVIEDED", LOG);
063: throw new ToolException(msg);
064: }
065: if (WSDLConstants.RPC.equalsIgnoreCase((String) env
066: .get(ToolConstants.CFG_STYLE))) {
067: Iterator it = portType.getOperations().iterator();
068: while (it.hasNext()) {
069: Operation op = (Operation) it.next();
070: Input input = op.getInput();
071: if (input != null && input.getMessage() != null) {
072: Iterator itParts = input.getMessage().getParts()
073: .values().iterator();
074: while (itParts.hasNext()) {
075: Part part = (Part) itParts.next();
076: if (part.getTypeName() == null
077: || "".equals(part.getTypeName()
078: .toString())) {
079: Message msg = new Message(
080: "RPC_PART_ILLEGAL", LOG,
081: new Object[] { part.getName() });
082: throw new ToolException(msg);
083: }
084: }
085: }
086: Output output = op.getOutput();
087: if (output != null && output.getMessage() != null) {
088: Iterator itParts = output.getMessage().getParts()
089: .values().iterator();
090: while (itParts.hasNext()) {
091: Part part = (Part) itParts.next();
092: if (part.getTypeName() == null
093: || "".equals(part.getTypeName()
094: .toString())) {
095: Message msg = new Message(
096: "RPC_PART_ILLEGAL", LOG,
097: new Object[] { part.getName() });
098: throw new ToolException(msg);
099: }
100: }
101: }
102: }
103: }
104: }
105:
106: private boolean isPortTypeExisted() {
107: portTypes = wsdlDefinition.getPortTypes();
108: if (portTypes == null) {
109: return false;
110: }
111: Iterator it = portTypes.keySet().iterator();
112: while (it.hasNext()) {
113: QName existPortQName = (QName) it.next();
114: String existPortName = existPortQName.getLocalPart();
115: if (existPortName.equals(env
116: .get(ToolConstants.CFG_PORTTYPE))) {
117: portType = (PortType) portTypes.get(existPortQName);
118: break;
119: }
120: }
121: return (portType == null) ? false : true;
122: }
123:
124: private boolean isBindingExisted() {
125: Map bindings = wsdlDefinition.getBindings();
126: if (bindings == null) {
127: return false;
128: }
129: Iterator it = bindings.keySet().iterator();
130: while (it.hasNext()) {
131: QName existBindingQName = (QName) it.next();
132: String existBindingName = existBindingQName.getLocalPart();
133: String bindingName = (String) env
134: .get(ToolConstants.CFG_BINDING);
135: if (bindingName.equals(existBindingName)) {
136: binding = (Binding) bindings.get(existBindingQName);
137: }
138: }
139: return (binding == null) ? false : true;
140: }
141:
142: private boolean nameSpaceCheck() {
143: if (WSDLConstants.RPC.equalsIgnoreCase((String) env
144: .get(ToolConstants.CFG_STYLE))
145: && !env.optionSet(ToolConstants.CFG_NAMESPACE)) {
146: return false;
147: }
148: return true;
149: }
150:
151: protected void init() throws ToolException {
152: parseWSDL((String) env.get(ToolConstants.CFG_WSDLURL));
153: }
154:
155: private void doAppendBinding() throws ToolException {
156: if (binding == null) {
157: binding = wsdlDefinition.createBinding();
158: binding.setQName(new QName(wsdlDefinition
159: .getTargetNamespace(), (String) env
160: .get(ToolConstants.CFG_BINDING)));
161: binding.setUndefined(false);
162: binding.setPortType(portType);
163: }
164: setSoapBindingExtElement();
165: addBindingOperation();
166: wsdlDefinition.addBinding(binding);
167:
168: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
169: Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
170: try {
171: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
172: } catch (WSDLException wse) {
173: Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
174: throw new ToolException(msg);
175:
176: }
177: try {
178: outputWriter.close();
179: } catch (IOException ioe) {
180: Message msg = new Message("PORTTYPE_NOT_EXIST", LOG);
181: throw new ToolException(msg);
182: }
183: }
184:
185: private void setSoapBindingExtElement() throws ToolException {
186: if (extReg == null) {
187: extReg = wsdlFactory.newPopulatedExtensionRegistry();
188: }
189: SOAPBinding soapBinding = null;
190: try {
191: soapBinding = (SOAPBinding) extReg.createExtension(
192: Binding.class, WSDLConstants.NS_SOAP_BINDING);
193: } catch (WSDLException wse) {
194: Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
195: throw new ToolException(msg, wse);
196: }
197: soapBinding.setStyle((String) env.get(ToolConstants.CFG_STYLE));
198: soapBinding
199: .setTransportURI(WSDLConstants.NS_SOAP11_HTTP_BINDING);
200: binding.addExtensibilityElement(soapBinding);
201: }
202:
203: @SuppressWarnings("unchecked")
204: private void addBindingOperation() throws ToolException {
205: /**
206: * This method won't do unique operation name checking on portType The
207: * WS-I Basic Profile[17] R2304 requires that operations within a
208: * wsdl:portType have unique values for their name attribute so mapping
209: * of WS-I compliant WSDLdescriptions will not generate Java interfaces
210: * with overloaded methods. However, for backwards compatibility, JAX-WS
211: * supports operation name overloading provided the overloading does not
212: * cause conflicts (as specified in the Java Language Specification[25])
213: * in the mapped Java service endpoint interface declaration.
214: */
215: List<Operation> ops = portType.getOperations();
216: for (Operation op : ops) {
217: BindingOperation bindingOperation = wsdlDefinition
218: .createBindingOperation();
219: setSoapOperationExtElement(bindingOperation);
220: bindingOperation.setName(op.getName());
221: if (op.getInput() != null) {
222: bindingOperation.setBindingInput(getBindingInput(op
223: .getInput()));
224: }
225: if (op.getOutput() != null) {
226: bindingOperation.setBindingOutput(getBindingOutput(op
227: .getOutput()));
228: }
229: if (op.getFaults() != null && op.getFaults().size() > 0) {
230: addSoapFaults(op, bindingOperation);
231: }
232: bindingOperation.setOperation(op);
233: binding.addBindingOperation(bindingOperation);
234: }
235: }
236:
237: private void setSoapOperationExtElement(BindingOperation bo)
238: throws ToolException {
239: if (extReg == null) {
240: extReg = wsdlFactory.newPopulatedExtensionRegistry();
241: }
242: SOAPOperation soapOperation = null;
243: try {
244: soapOperation = (SOAPOperation) extReg.createExtension(
245: BindingOperation.class,
246: WSDLConstants.NS_SOAP_OPERATION);
247: } catch (WSDLException wse) {
248: Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
249: throw new ToolException(msg, wse);
250: }
251: soapOperation.setStyle((String) env
252: .get(ToolConstants.CFG_STYLE));
253: soapOperation.setSoapActionURI("");
254: bo.addExtensibilityElement(soapOperation);
255: }
256:
257: private BindingInput getBindingInput(Input input)
258: throws ToolException {
259: BindingInput bi = wsdlDefinition.createBindingInput();
260: bi.setName(input.getName());
261: // As command line won't specify the details of body/header for message
262: // parts
263: // All input message's parts will be added into one soap body element
264: bi.addExtensibilityElement(getSoapBody(BindingInput.class));
265: return bi;
266: }
267:
268: private BindingOutput getBindingOutput(Output output)
269: throws ToolException {
270: BindingOutput bo = wsdlDefinition.createBindingOutput();
271: bo.setName(output.getName());
272: // As command line won't specify the details of body/header for message
273: // parts
274: // All output message's parts will be added into one soap body element
275: bo.addExtensibilityElement(getSoapBody(BindingOutput.class));
276: return bo;
277: }
278:
279: private SOAPBody getSoapBody(Class parent) throws ToolException {
280: if (extReg == null) {
281: extReg = wsdlFactory.newPopulatedExtensionRegistry();
282: }
283: SOAPBody soapBody = null;
284: try {
285: soapBody = (SOAPBody) extReg.createExtension(parent,
286: WSDLConstants.NS_SOAP_BODY);
287: } catch (WSDLException wse) {
288: Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
289: throw new ToolException(msg, wse);
290: }
291: soapBody.setUse((String) env.get(ToolConstants.CFG_USE));
292: if (WSDLConstants.RPC.equalsIgnoreCase((String) env
293: .get(ToolConstants.CFG_STYLE))
294: && env.optionSet(ToolConstants.CFG_NAMESPACE)) {
295: soapBody.setNamespaceURI((String) env
296: .get(ToolConstants.CFG_NAMESPACE));
297: }
298: return soapBody;
299: }
300:
301: private void addSoapFaults(Operation op,
302: BindingOperation bindingOperation) throws ToolException {
303: Map faults = op.getFaults();
304: Iterator it = faults.keySet().iterator();
305: while (it.hasNext()) {
306: String key = (String) it.next();
307: Fault fault = (Fault) faults.get(key);
308: BindingFault bf = wsdlDefinition.createBindingFault();
309: bf.setName(fault.getName());
310: setSoapFaultExtElement(bf);
311: bindingOperation.addBindingFault(bf);
312: }
313: }
314:
315: private void setSoapFaultExtElement(BindingFault bf)
316: throws ToolException {
317: if (extReg == null) {
318: extReg = wsdlFactory.newPopulatedExtensionRegistry();
319: }
320: SOAPFault soapFault = null;
321: try {
322: soapFault = (SOAPFault) extReg.createExtension(
323: BindingFault.class, WSDLConstants.NS_SOAP_FAULT);
324: } catch (WSDLException wse) {
325: Message msg = new Message("FAIL_TO_CREATE_SOAPBINDING", LOG);
326: throw new ToolException(msg, wse);
327: }
328: soapFault.setName(bf.getName());
329: soapFault.setUse((String) env.get(ToolConstants.CFG_USE));
330: if (WSDLConstants.RPC.equalsIgnoreCase((String) env
331: .get(ToolConstants.CFG_STYLE))
332: && env.optionSet(ToolConstants.CFG_NAMESPACE)) {
333: soapFault.setNamespaceURI((String) env
334: .get(ToolConstants.CFG_NAMESPACE));
335: }
336: bf.addExtensibilityElement(soapFault);
337: }
338:
339: }
|