001: /**
002: *
003: */package com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.wizards.httpExtra.io;
004:
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.io.IOException;
008: import java.net.URL;
009: import java.util.Map.Entry;
010:
011: import javax.wsdl.Binding;
012: import javax.wsdl.BindingInput;
013: import javax.wsdl.BindingOperation;
014: import javax.wsdl.BindingOutput;
015: import javax.wsdl.Definition;
016: import javax.wsdl.Input;
017: import javax.wsdl.Message;
018: import javax.wsdl.Operation;
019: import javax.wsdl.Output;
020: import javax.wsdl.Part;
021: import javax.wsdl.Port;
022: import javax.wsdl.PortType;
023: import javax.wsdl.Service;
024: import javax.wsdl.Types;
025: import javax.wsdl.WSDLException;
026: import javax.wsdl.extensions.schema.Schema;
027: import javax.wsdl.extensions.soap.SOAPAddress;
028: import javax.wsdl.extensions.soap.SOAPBinding;
029: import javax.wsdl.extensions.soap.SOAPBody;
030: import javax.wsdl.extensions.soap.SOAPOperation;
031: import javax.wsdl.factory.WSDLFactory;
032: import javax.wsdl.xml.WSDLWriter;
033: import javax.xml.namespace.QName;
034: import javax.xml.parsers.ParserConfigurationException;
035:
036: import org.apache.xmlbeans.XmlException;
037: import org.eclipse.core.runtime.Path;
038: import org.jdom.JDOMException;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.xml.sax.SAXException;
042:
043: import com.bostechcorp.cbesb.common.util.generators.wsdl.generators.HTTPPublicWsdlUpdater;
044: import com.bostechcorp.cbesb.common.util.xfm.XmlFormatModelFactory;
045: import com.bostechcorp.cbesb.common.util.xfm.impl.XmlFormatModelXSDImpl;
046: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.wizards.httpExtra.NewWSDLWizard;
047: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.wizards.httpExtra.models.SchematicWSDLmodel;
048: import com.bostechcorp.cbesb.ui.componentflow.componentFlowEditor.diagram.wizards.httpExtra.models.ServiceOperation;
049: import com.ibm.wsdl.BindingInputImpl;
050: import com.ibm.wsdl.BindingOutputImpl;
051: import com.ibm.wsdl.InputImpl;
052: import com.ibm.wsdl.OperationImpl;
053: import com.ibm.wsdl.OutputImpl;
054: import com.ibm.wsdl.extensions.schema.SchemaConstants;
055: import com.ibm.wsdl.extensions.schema.SchemaImpl;
056: import com.ibm.wsdl.extensions.soap.SOAPAddressImpl;
057: import com.ibm.wsdl.extensions.soap.SOAPBindingImpl;
058: import com.ibm.wsdl.extensions.soap.SOAPBodyImpl;
059: import com.ibm.wsdl.extensions.soap.SOAPOperationImpl;
060: import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl;
061:
062: /**
063: * this class will generate the WSDL file from configuration file.
064: *
065: * @author LPS
066: *
067: */
068: public class WSDLFileGenerator {
069: private static final String SUFFIX = "_Server.wsdl";
070:
071: /**
072: * path to the src/wsdl/SUNAme
073: */
074: private String path = "";
075:
076: private String suName = "";
077:
078: private String wsdlFileName = "";
079:
080: private String configFileName = NewWSDLWizard.fileName;
081:
082: private SchematicWSDLmodel model;
083:
084: private Definition definition = null;
085:
086: private URL soapAddressLocation;
087:
088: /**
089: * @param path -
090: * path to the SUWsdl folder containing .customWSDL
091: * @param suName -
092: * service unit name, identical to path's last segment
093: * @throws IOException
094: * @throws XmlException
095: */
096: public WSDLFileGenerator(String path, String suName, URL soapUrl)
097: throws XmlException, IOException {
098: super ();
099: this .path = path;
100: this .suName = suName;
101: this .soapAddressLocation = soapUrl;
102: setConfigFileName(path);
103: setWsdlFileName(path, suName);
104: loadModel();
105: }
106:
107: private void loadModel() throws XmlException, IOException {
108: CustomWSDLReader reader = new CustomWSDLReader(configFileName);
109: model = reader.readModel();
110: }
111:
112: /**
113: * @param path -
114: * path to the SUWsdl folder containing .customWSDL
115: * @param suName -
116: * service unit name, identical to path's last segment
117: * @param model -
118: * a model
119: */
120: public WSDLFileGenerator(String path, String suName, URL url,
121: SchematicWSDLmodel model) {
122: super ();
123: this .path = path;
124: this .suName = suName;
125: this .model = model;
126: this .soapAddressLocation = url;
127: setConfigFileName(path);
128: setWsdlFileName(path, suName);
129: }
130:
131: /**
132: * @return the model
133: */
134: public SchematicWSDLmodel getModel() {
135: return model;
136: }
137:
138: /**
139: * @param model
140: * the model to set
141: */
142: public void setModel(SchematicWSDLmodel model) {
143: this .model = model;
144: }
145:
146: /**
147: * @return the configFileName
148: */
149: public String getConfigFileName() {
150: return configFileName;
151: }
152:
153: /**
154: * @return the path
155: */
156: public String getPath() {
157: return path;
158: }
159:
160: /**
161: * @return the suName
162: */
163: public String getSuName() {
164: return suName;
165: }
166:
167: /**
168: * @return the wsdlFileName
169: */
170: public String getWsdlFileName() {
171: return wsdlFileName;
172: }
173:
174: /**
175: * @param configFileName
176: * the configFileName to set
177: * @param suName2
178: */
179: protected void setConfigFileName(String path) {
180: if (path.endsWith(File.separator))
181: this .configFileName = path + configFileName;
182: else {
183: this .path += File.separator;
184: this .configFileName = path + configFileName;
185: }
186: }
187:
188: /**
189: * @param wsdlFileName
190: * the wsdlFileName to set
191: * @param suName2
192: */
193: protected void setWsdlFileName(String path, String suName2) {
194: if (path.endsWith(File.separator))
195: this .wsdlFileName = path + suName2 + SUFFIX;
196: else
197: this .wsdlFileName = path + File.separator + suName2
198: + SUFFIX;
199: }
200:
201: public boolean buildDefinition() throws Exception {
202: if (model == null)
203: return false;
204: definition = WSDLFactory.newInstance().newDefinition();
205: setNamespaces();
206: setTypes();
207: setMessages();
208: setPortTypes();
209: setBindings();
210: setService();
211: return true;
212: }
213:
214: private void setService() {
215: Service service = definition.createService();
216: service.setQName(new QName(definition.getNamespace("tns"),
217: model.getWsName(), "tns"));
218: Port port = definition.createPort();
219: port.setName(model.getWsName() + "SOAPBinding_"
220: + soapAddressLocation.getProtocol());
221: port.setBinding(definition.getBinding(new QName(definition
222: .getNamespace("tns"),
223: model.getWsName() + "SOAPBinding", "tns")));
224: service.addPort(port);
225: // adding soap binding
226: SOAPAddress address = new SOAPAddressImpl();
227: port.addExtensibilityElement(address);
228: address.setLocationURI(soapAddressLocation.toExternalForm());
229: definition.addService(service);
230: }
231:
232: private void setBindings() {
233: Binding binding = definition.createBinding();
234: binding.setUndefined(false);
235: binding.setPortType(definition.getPortType(new QName(definition
236: .getNamespace("tns"), model.getWsName() + "Interface",
237: "tns")));
238: binding.setQName(new QName(definition.getNamespace("tns"),
239: model.getWsName() + "SOAPBinding", "tns"));
240: // setting soapBindigExtension
241: SOAPBinding soapBinding = new SOAPBindingImpl();
242: soapBinding.setStyle("document");
243: soapBinding
244: .setTransportURI("http://schemas.xmlsoap.org/soap/http");
245: binding.addExtensibilityElement(soapBinding);
246: // soap body for all input/outputs
247: SOAPBody soapBody = new SOAPBodyImpl();
248: soapBody.setUse("literal");
249:
250: for (ServiceOperation operation : getModel().getOperations()) {
251: BindingOperation bindingOperation = definition
252: .createBindingOperation();
253: bindingOperation.setName(operation.getOperationName());
254: SOAPOperation extSoapOperation = new SOAPOperationImpl();
255: extSoapOperation.setStyle("document");
256: extSoapOperation.setSoapActionURI("urn:"
257: + operation.getOperationName());
258: bindingOperation.addExtensibilityElement(extSoapOperation);
259: // setting input
260: BindingInput bindingInput = new BindingInputImpl();
261: bindingOperation.setBindingInput(bindingInput);
262: bindingInput.addExtensibilityElement(soapBody);
263: // setting output if any
264: if (operation.hasOutMessage()) {
265: BindingOutput bindingOutput = new BindingOutputImpl();
266: bindingOutput.addExtensibilityElement(soapBody);
267: bindingOperation.setBindingOutput(bindingOutput);
268: }
269: binding.addBindingOperation(bindingOperation);
270: }
271: definition.addBinding(binding);
272:
273: }
274:
275: private void setPortTypes() {
276: PortType portType = definition.createPortType();
277: portType.setUndefined(false);
278: portType.setQName(new QName(definition.getNamespace("tns"),
279: model.getWsName() + "Interface", "tns"));
280: for (ServiceOperation operation : getModel().getOperations()) {
281: Operation wsOperation = new OperationImpl();
282: wsOperation.setUndefined(false);
283: wsOperation.setName(operation.getOperationName());
284: Input input = new InputImpl();
285: wsOperation.setInput(input);
286: input.setMessage(findMessage(operation.getOperationName()
287: + "Input"));
288: if (operation.hasOutMessage()) {
289: Output output = new OutputImpl();
290: wsOperation.setOutput(output);
291: output.setMessage(findMessage(operation
292: .getOperationName()
293: + "Output"));
294: }
295: portType.addOperation(wsOperation);
296: }
297: definition.addPortType(portType);
298: }
299:
300: private Message findMessage(String inputMessageName) {
301: Message m = definition.getMessage(new QName(definition
302: .getNamespace("tns"), inputMessageName, "tns"));
303: return m;
304: }
305:
306: private void setMessages() {
307: for (ServiceOperation operation : getModel().getOperations()) {
308: Message message = definition.createMessage();
309: message.setUndefined(false);
310: message.setQName(new QName(definition.getNamespace("tns"),
311: operation.getOperationName() + "Input", "tns")); // XXX:
312: // what
313: // about
314: // tns
315: // prefix
316: Part mpart = definition.createPart();
317: mpart.setName(operation.getInputMessageName());
318:
319: /*
320: * Axis does not like the type at all. We only are going to support the element.
321: */
322:
323: // if (!operation.isInputTypeElement()) {
324: // mpart.setTypeName(new QName(definition.getNamespace(operation
325: // .getInputMessageTypeNamespace()), operation
326: // .getInputMessageType()));
327: // } else
328: {
329: mpart.setElementName(new QName(definition
330: .getNamespace(operation
331: .getInputMessageTypeNamespace()),
332: operation.getInputMessageType()));
333: }
334:
335: message.addPart(mpart);
336: definition.addMessage(message);
337: // output
338: if (operation.hasOutMessage()) {
339: Message omessage = definition.createMessage();
340: omessage.setUndefined(false);
341: omessage.setQName(new QName(definition
342: .getNamespace("tns"), operation
343: .getOperationName()
344: + "Output", "tns")); // XXX what about tns prefix
345: Part ompart = definition.createPart();
346: ompart.setName(operation.getOutputMessageName());
347:
348: // if (!operation.isOutputTypeElement()) {
349: // ompart.setTypeName(new QName(definition
350: // .getNamespace(operation
351: // .getOutputMessageTypeNamespace()),
352: // operation.getOutputMessageType()));
353: // } else
354: {
355: ompart.setElementName(new QName(definition
356: .getNamespace(operation
357: .getOutputMessageTypeNamespace()),
358: operation.getOutputMessageType()));
359: }
360:
361: omessage.addPart(ompart);
362: definition.addMessage(omessage);
363: }
364: }
365: }
366:
367: private void setTypes() throws WSDLException {
368: Types types = definition.createTypes();
369:
370: Schema element = new SchemaImpl();
371: element.setElementType(SchemaConstants.Q_ELEM_XSD_2001);
372: element.setRequired(true);
373: types.addExtensibilityElement(element);
374: Document schemaDoc = DOMImplementationImpl
375: .getDOMImplementation().createDocument(
376: "http://www.w3.org/2001/XMLSchema", "schema",
377: null);
378: Element schema = schemaDoc.createElementNS(
379: "http://www.w3.org/2001/XMLSchema", "schema");
380: schema.setPrefix("xsd");
381: schema.setAttribute("attributeFormDefault", "qualified");
382: schema.setAttribute("elementFormDefault", "qualified");
383: // setting tns
384: schema.setAttribute("targetNamespace", definition
385: .getTargetNamespace());
386: // setting imports & ns's
387: for (Entry<String, String> entry : model.getXsdFileNames()
388: .entrySet()) {
389: // setting ns
390: String tns = getSchemaTns(entry.getValue());
391: schema.setAttributeNS("http://www.w3.org/2000/xmlns/",
392: "xmlns:" + entry.getKey(), tns);
393: // creatting import element;
394: Element importElement = schemaDoc.createElementNS(
395: "http://www.w3.org/2001/XMLSchema", "xsd:import");
396: importElement.setAttribute("namespace", tns);
397: importElement.setAttribute("schemaLocation", entry
398: .getValue());
399: schema.appendChild(importElement);
400: }
401: element.setElement(schema);
402: definition.setTypes(types);
403: }
404:
405: /**
406: * Parse the schema file and extract the targetNamespace
407: * @param value
408: * @return
409: */
410: private String getSchemaTns(String value) {
411: String filename = path + value;
412: try {
413: return NamespaceFetcher.getTargetNamespace(filename);
414: } catch (SAXException e) {
415: e.printStackTrace();
416: } catch (IOException e) {
417: e.printStackTrace();
418: } catch (ParserConfigurationException e) {
419: e.printStackTrace();
420: }
421: return "NO_Target_Namespace_Available";
422: }
423:
424: /**
425: * Setting wsdl standard namespaces and the ones from the SXD schemas
426: * xmlns:http='http://schemas.xmlsoap.org/wsdl/http/'
427: * xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
428: * xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
429: * xmlns:xsd='http://www.w3.org/2001/XMLSchema' <br/>
430: * targetNamespace:http://cbesb.bostechcorp.com/ServiceAssemblyName/ServiceUnitName
431: * <br/> Note: ServiceAssemblyName will be extracted from the Path, <br/>
432: * where path will alway have the following structure: <br\>
433: * <b>pathToProject/saName/src/wsdl/suname</b>
434: *
435: * @throws Exception
436: */
437: private void setNamespaces() throws Exception {
438: // standard Namespaces
439: definition.addNamespace("http",
440: "http://schemas.xmlsoap.org/wsdl/http/");
441: definition.addNamespace("soap",
442: "http://schemas.xmlsoap.org/wsdl/soap/");
443: definition.addNamespace("wsdl",
444: "http://schemas.xmlsoap.org/wsdl/");
445: definition.addNamespace("xsd",
446: "http://www.w3.org/2001/XMLSchema");
447:
448: // setting xsd namespaces
449: for (Entry<String, String> entry : model.getXsdFileNames()
450: .entrySet()) {
451: XmlFormatModelXSDImpl file = (XmlFormatModelXSDImpl) XmlFormatModelFactory
452: .newInstance(path + entry.getValue(), "XSD");
453: definition.addNamespace(entry.getKey(), file
454: .getTargetNamespace());
455: }
456: // target namespace
457: // saName/src/wsdl/suname
458: Path p = new Path(path);
459: p.removeLastSegments(3);
460: String serviceAssemblyName = p.lastSegment();
461: String tnsUrl = "http://cbesb.bostechcorp.com/"
462: + serviceAssemblyName + "/" + suName + "";
463: definition.addNamespace("", tnsUrl);
464: definition.setTargetNamespace(tnsUrl);
465: definition.addNamespace("tns", tnsUrl);
466: }
467:
468: public void writeToFile() throws JDOMException, Exception {
469: if (definition == null)
470: return;
471: WSDLWriter writer = WSDLFactory.newInstance().newWSDLWriter();
472: FileOutputStream outputStream = new FileOutputStream(
473: getWsdlFileName());
474: writer.writeWSDL(definition, outputStream);
475: outputStream.close();
476:
477: HTTPPublicWsdlUpdater.updateAllAdressTags(getWsdlFileName());
478: }
479:
480: /**
481: * @return the definition
482: */
483: public Definition getDefinition() {
484: return definition;
485: }
486:
487: // public static void main(String[] args) throws WSDLException, IOException {
488: // try {
489: // SchematicWSDLmodel model1;
490: // CustomWSDLReader modelReader = new CustomWSDLReader(
491: // "c:\\asd\\src\\wsdl\\server1\\.customWsdl");
492: // model1 = modelReader.readModel();
493: // WSDLFileGenerator fg = new WSDLFileGenerator(
494: // "c:\\asd\\src\\wsdl\\server1\\", "server1", new URL(
495: // "https://localhost/stf"), model1);
496: // fg.buildDefinition();
497: // fg.writeToFile();
498: // } catch (XmlException e) {
499: // e.printStackTrace();
500: // } catch (IOException e) {
501: // e.printStackTrace();
502: // } catch (WSDLException e) {
503: // e.printStackTrace();
504: // } catch (Exception e) {
505: // e.printStackTrace();
506: // }
507: // }
508:
509: }
|