001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.misc.processor;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import javax.wsdl.Binding;
027: import javax.wsdl.BindingInput;
028: import javax.wsdl.BindingOperation;
029: import javax.wsdl.BindingOutput;
030: import javax.wsdl.Input;
031: import javax.wsdl.Operation;
032: import javax.wsdl.Output;
033: import javax.wsdl.Port;
034: import javax.wsdl.PortType;
035: import javax.wsdl.Service;
036: import javax.wsdl.WSDLException;
037: import javax.wsdl.extensions.ExtensibilityElement;
038: import javax.wsdl.extensions.ExtensionRegistry;
039: import javax.wsdl.xml.WSDLWriter;
040: import javax.xml.namespace.QName;
041:
042: import org.apache.cxf.common.i18n.Message;
043: import org.apache.cxf.tools.common.ToolConstants;
044: import org.apache.cxf.tools.common.ToolException;
045: import org.apache.cxf.tools.misc.processor.address.Address;
046: import org.apache.cxf.tools.misc.processor.address.AddressFactory;
047: import org.apache.cxf.wsdl.WSDLConstants;
048: import org.apache.cxf.wsdl.WSDLExtensibilityPlugin;
049:
050: public class WSDLToXMLProcessor extends AbstractWSDLToProcessor {
051:
052: private static final String NEW_FILE_NAME_MODIFIER = "-xmlbinding";
053:
054: private ExtensionRegistry extReg;
055:
056: private Map services;
057: private Service service;
058: private Map ports;
059: private Port port;
060:
061: private Map portTypes;
062: private PortType portType;
063: private Binding binding;
064:
065: public void process() throws ToolException {
066: init();
067: if (isBindingExisted()) {
068: Message msg = new Message("BINDING_ALREADY_EXIST", LOG);
069: throw new ToolException(msg);
070: }
071: if (!isPortTypeExisted()) {
072: Message msg = new Message("PORTTYPE_NOT_EXIST", LOG);
073: throw new ToolException(msg);
074: }
075: if (isServicePortExisted()) {
076: Message msg = new Message("SERVICE_PORT_EXIST", LOG);
077: throw new ToolException(msg);
078: }
079: extReg = this .wsdlReader.getExtensionRegistry();
080: doAppendBinding();
081: doAppendService();
082: writeToWSDL();
083: }
084:
085: private boolean isServicePortExisted() {
086: return isServiceExisted() && isPortExisted();
087: }
088:
089: private boolean isServiceExisted() {
090: services = wsdlDefinition.getServices();
091: if (services == null) {
092: return false;
093: }
094: Iterator it = services.keySet().iterator();
095: while (it.hasNext()) {
096: QName serviceQName = (QName) it.next();
097: String serviceName = serviceQName.getLocalPart();
098: if (serviceName.equals(env.get(ToolConstants.CFG_SERVICE))) {
099: service = (Service) services.get(serviceQName);
100: break;
101: }
102: }
103: return (service == null) ? false : true;
104: }
105:
106: private boolean isPortExisted() {
107: ports = service.getPorts();
108: if (ports == null) {
109: return false;
110: }
111: Iterator it = ports.keySet().iterator();
112: while (it.hasNext()) {
113: String portName = (String) it.next();
114: if (portName.equals(env.get(ToolConstants.CFG_PORT))) {
115: port = (Port) ports.get(portName);
116: break;
117: }
118: }
119: return (port == null) ? false : true;
120: }
121:
122: private boolean isPortTypeExisted() {
123: portTypes = wsdlDefinition.getPortTypes();
124: if (portTypes == null) {
125: return false;
126: }
127: Iterator it = portTypes.keySet().iterator();
128: while (it.hasNext()) {
129: QName existPortQName = (QName) it.next();
130: String existPortName = existPortQName.getLocalPart();
131: if (existPortName.equals(env
132: .get(ToolConstants.CFG_PORTTYPE))) {
133: portType = (PortType) portTypes.get(existPortQName);
134: break;
135: }
136: }
137: return (portType == null) ? false : true;
138: }
139:
140: private boolean isBindingExisted() {
141: Map bindings = wsdlDefinition.getBindings();
142: if (bindings == null) {
143: return false;
144: }
145: Iterator it = bindings.keySet().iterator();
146: while (it.hasNext()) {
147: QName existBindingQName = (QName) it.next();
148: String existBindingName = existBindingQName.getLocalPart();
149: String bindingName = (String) env
150: .get(ToolConstants.CFG_BINDING);
151: if (bindingName.equals(existBindingName)) {
152: binding = (Binding) bindings.get(existBindingQName);
153: }
154: }
155: return (binding == null) ? false : true;
156: }
157:
158: protected void init() throws ToolException {
159: parseWSDL((String) env.get(ToolConstants.CFG_WSDLURL));
160: if (wsdlDefinition
161: .getNamespace(ToolConstants.XML_FORMAT_PREFIX) == null) {
162: wsdlDefinition.addNamespace(
163: ToolConstants.XML_FORMAT_PREFIX,
164: ToolConstants.NS_XML_FORMAT);
165: }
166: if (wsdlDefinition.getNamespace(ToolConstants.XML_HTTP_PREFIX) == null) {
167: wsdlDefinition.addNamespace(ToolConstants.XML_HTTP_PREFIX,
168: ToolConstants.NS_XML_HTTP);
169: }
170: }
171:
172: private void doAppendBinding() throws ToolException {
173: if (binding == null) {
174: binding = wsdlDefinition.createBinding();
175: binding.setQName(new QName(wsdlDefinition
176: .getTargetNamespace(), (String) env
177: .get(ToolConstants.CFG_BINDING)));
178: binding.setUndefined(false);
179: binding.setPortType(portType);
180: }
181: setXMLBindingExtElement();
182: addBindingOperation();
183: wsdlDefinition.addBinding(binding);
184: }
185:
186: private void setXMLBindingExtElement() throws ToolException {
187: if (extReg == null) {
188: extReg = wsdlFactory.newPopulatedExtensionRegistry();
189: }
190: try {
191: binding.addExtensibilityElement(getWSDLPlugin("xml",
192: Binding.class).createExtension(null));
193: } catch (WSDLException wse) {
194: Message msg = new Message("FAIL_TO_CREATE_XMLBINDING", LOG);
195: throw new ToolException(msg);
196: }
197: }
198:
199: @SuppressWarnings("unchecked")
200: private void addBindingOperation() throws ToolException {
201: List<Operation> ops = portType.getOperations();
202: for (Operation op : ops) {
203: BindingOperation bindingOperation = wsdlDefinition
204: .createBindingOperation();
205: bindingOperation.setName(op.getName());
206: if (op.getInput() != null) {
207: bindingOperation.setBindingInput(getBindingInput(op
208: .getInput(), op.getName()));
209: }
210: if (op.getOutput() != null) {
211: bindingOperation.setBindingOutput(getBindingOutput(op
212: .getOutput(), op.getName()));
213: }
214: if (op.getFaults() != null && op.getFaults().size() > 0) {
215: addXMLFaults(op, bindingOperation);
216: }
217: bindingOperation.setOperation(op);
218: binding.addBindingOperation(bindingOperation);
219: }
220: }
221:
222: private BindingInput getBindingInput(Input input,
223: String operationName) throws ToolException {
224: BindingInput bi = wsdlDefinition.createBindingInput();
225: bi.setName(input.getName());
226: //This ext element in some scenario is optional, but if provided, won't cause error
227: bi.addExtensibilityElement(getXMLBody(BindingInput.class,
228: operationName));
229: return bi;
230: }
231:
232: private BindingOutput getBindingOutput(Output output,
233: String operationName) throws ToolException {
234: BindingOutput bo = wsdlDefinition.createBindingOutput();
235: bo.setName(output.getName());
236: bo.addExtensibilityElement(getXMLBody(BindingOutput.class,
237: operationName));
238: return bo;
239: }
240:
241: private void addXMLFaults(Operation op, BindingOperation bo) {
242: // TODO
243: }
244:
245: private ExtensibilityElement getXMLBody(Class clz,
246: String operationName) throws ToolException {
247: if (extReg == null) {
248: extReg = wsdlFactory.newPopulatedExtensionRegistry();
249: }
250: Map<String, Object> args = new HashMap<String, Object>();
251: args.put(QName.class.getName(), new QName(wsdlDefinition
252: .getTargetNamespace(), operationName));
253: args.put(Class.class.getName(), clz);
254:
255: try {
256: return getWSDLPlugin("xml", clz).createExtension(args);
257: } catch (WSDLException wse) {
258: Message msg = new Message("FAIL_TO_CREATE_XMLBINDING", LOG);
259: throw new ToolException(msg);
260: }
261: }
262:
263: private void doAppendService() throws ToolException {
264: if (service == null) {
265: service = wsdlDefinition.createService();
266: service.setQName(new QName(WSDLConstants.WSDL_PREFIX,
267: (String) env.get(ToolConstants.CFG_SERVICE)));
268: }
269: if (port == null) {
270: port = wsdlDefinition.createPort();
271: port.setName((String) env.get(ToolConstants.CFG_PORT));
272: port.setBinding(binding);
273: }
274: setAddrElement();
275: service.addPort(port);
276: wsdlDefinition.addService(service);
277: }
278:
279: private void setAddrElement() throws ToolException {
280: extReg = this .wsdlReader.getExtensionRegistry();
281: if (extReg == null) {
282: extReg = wsdlFactory.newPopulatedExtensionRegistry();
283: }
284:
285: Address address = AddressFactory.getInstance().getAddresser(
286: "xml");
287:
288: for (String key : address.getNamespaces(env).keySet()) {
289: wsdlDefinition.addNamespace(key, address.getNamespaces(env)
290: .get(key));
291: }
292:
293: WSDLExtensibilityPlugin generator = getWSDLPlugin("xml",
294: Port.class);
295: try {
296: ExtensibilityElement extElement = generator
297: .createExtension(address.buildAddressArguments(env));
298:
299: port.addExtensibilityElement(extElement);
300: } catch (WSDLException wse) {
301: Message msg = new Message("FAIL_TO_CREATE_SOAPADDRESS", LOG);
302: throw new ToolException(msg);
303: }
304: }
305:
306: private void writeToWSDL() throws ToolException {
307: WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
308: Writer outputWriter = getOutputWriter(NEW_FILE_NAME_MODIFIER);
309: try {
310: wsdlWriter.writeWSDL(wsdlDefinition, outputWriter);
311: } catch (WSDLException wse) {
312: Message msg = new Message("FAIL_TO_WRITE_WSDL", LOG);
313: throw new ToolException(msg);
314: }
315: try {
316: outputWriter.close();
317: } catch (IOException ioe) {
318: Message msg = new Message("FAIL_TO_CLOSE_WSDL_FILE", LOG);
319: throw new ToolException(msg);
320: }
321: }
322:
323: }
|