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.util;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Proxy;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import javax.wsdl.Binding;
028: import javax.wsdl.BindingFault;
029: import javax.wsdl.BindingInput;
030: import javax.wsdl.BindingOperation;
031: import javax.wsdl.BindingOutput;
032: import javax.wsdl.Definition;
033: import javax.wsdl.Port;
034: import javax.wsdl.WSDLException;
035: import javax.wsdl.extensions.ExtensibilityElement;
036: import javax.wsdl.extensions.ExtensionRegistry;
037: import javax.wsdl.extensions.soap.SOAPAddress;
038: import javax.wsdl.extensions.soap.SOAPBinding;
039: import javax.wsdl.extensions.soap.SOAPBody;
040: import javax.wsdl.extensions.soap.SOAPFault;
041: import javax.wsdl.extensions.soap.SOAPHeader;
042: import javax.wsdl.extensions.soap.SOAPOperation;
043: import javax.wsdl.extensions.soap12.SOAP12Address;
044: import javax.wsdl.extensions.soap12.SOAP12Binding;
045: import javax.wsdl.extensions.soap12.SOAP12Body;
046: import javax.wsdl.extensions.soap12.SOAP12Fault;
047: import javax.wsdl.extensions.soap12.SOAP12Header;
048: import javax.wsdl.extensions.soap12.SOAP12Operation;
049: import javax.xml.namespace.QName;
050:
051: import org.apache.cxf.common.util.StringUtils;
052: import org.apache.cxf.tools.common.ExtensionInvocationHandler;
053: import org.apache.cxf.tools.common.extensions.soap.SoapAddress;
054: import org.apache.cxf.tools.common.extensions.soap.SoapBinding;
055: import org.apache.cxf.tools.common.extensions.soap.SoapBody;
056: import org.apache.cxf.tools.common.extensions.soap.SoapFault;
057: import org.apache.cxf.tools.common.extensions.soap.SoapHeader;
058: import org.apache.cxf.tools.common.extensions.soap.SoapOperation;
059: import org.apache.cxf.wsdl.WSDLConstants;
060:
061: public final class SOAPBindingUtil {
062: private static Map<String, String> bindingMap = new HashMap<String, String>();
063:
064: static {
065: bindingMap.put("RPC", "SOAPBinding.Style.RPC");
066: bindingMap.put("DOCUMENT", "SOAPBinding.Style.DOCUMENT");
067: bindingMap.put("LITERAL", "SOAPBinding.Use.LITERAL");
068: bindingMap.put("ENCODED", "SOAPBinding.Use.ENCODED");
069: bindingMap.put("BARE", "SOAPBinding.ParameterStyle.BARE");
070: bindingMap.put("WRAPPED", "SOAPBinding.ParameterStyle.WRAPPED");
071: }
072:
073: private SOAPBindingUtil() {
074: }
075:
076: public static String getBindingAnnotation(String key) {
077: return bindingMap.get(key.toUpperCase());
078: }
079:
080: public static <T> T getProxy(Class<T> cls, Object obj) {
081: InvocationHandler ih = new ExtensionInvocationHandler(obj);
082: Object proxy = Proxy.newProxyInstance(cls.getClassLoader(),
083: new Class[] { cls }, ih);
084: return cls.cast(proxy);
085: }
086:
087: public static boolean isSOAPBinding(Binding binding) {
088: Iterator ite = binding.getExtensibilityElements().iterator();
089: while (ite.hasNext()) {
090: Object obj = ite.next();
091: if (isSOAPBinding(obj)) {
092: return true;
093: }
094: }
095: return false;
096: }
097:
098: public static String getBindingStyle(Binding binding) {
099: Iterator ite = binding.getExtensibilityElements().iterator();
100: while (ite.hasNext()) {
101: Object obj = ite.next();
102: if (isSOAPBinding(obj)) {
103: return getSoapBinding(obj).getStyle();
104: }
105: }
106: return "";
107: }
108:
109: public static SoapOperation getSoapOperation(
110: List<ExtensibilityElement> exts) {
111: if (exts != null) {
112: for (ExtensibilityElement ext : exts) {
113: if (isSOAPOperation(ext)) {
114: return getSoapOperation(ext);
115: }
116: }
117: }
118: return null;
119: }
120:
121: public static SoapOperation getSoapOperation(Object obj) {
122: if (isSOAPOperation(obj)) {
123: return getProxy(SoapOperation.class, obj);
124: }
125: return null;
126: }
127:
128: public static String getSOAPOperationStyle(BindingOperation bop) {
129: String style = "";
130: if (bop != null) {
131: Iterator ite = bop.getExtensibilityElements().iterator();
132: while (ite.hasNext()) {
133: Object obj = ite.next();
134: if (isSOAPOperation(obj)) {
135: style = getSoapOperation(obj).getStyle();
136: break;
137: }
138: }
139: }
140: return style;
141: }
142:
143: public static SoapBody getBindingInputSOAPBody(BindingOperation bop) {
144: BindingInput bindingInput = bop.getBindingInput();
145: if (bindingInput != null) {
146: Iterator ite = bindingInput.getExtensibilityElements()
147: .iterator();
148: while (ite.hasNext()) {
149: Object obj = ite.next();
150: if (isSOAPBody(obj)) {
151: return getSoapBody(obj);
152: }
153: }
154: }
155:
156: return null;
157: }
158:
159: public static SoapBody getBindingOutputSOAPBody(BindingOperation bop) {
160: BindingOutput bindingOutput = bop.getBindingOutput();
161: if (bindingOutput != null) {
162: Iterator ite = bindingOutput.getExtensibilityElements()
163: .iterator();
164: while (ite.hasNext()) {
165: Object obj = ite.next();
166: if (isSOAPBody(obj)) {
167: return getSoapBody(obj);
168: }
169: }
170: }
171:
172: return null;
173: }
174:
175: public static SoapBody getSoapBody(List<ExtensibilityElement> exts) {
176: for (ExtensibilityElement ext : exts) {
177: if (isSOAPBody(ext)) {
178: return getSoapBody(ext);
179: }
180: }
181: return null;
182: }
183:
184: public static SoapBody getSoapBody(Object obj) {
185: if (isSOAPBody(obj)) {
186: return getProxy(SoapBody.class, obj);
187: }
188: return null;
189: }
190:
191: public static boolean isSOAPBody(Object obj) {
192: return obj instanceof SOAPBody || obj instanceof SOAP12Body;
193: }
194:
195: public static boolean isSOAPHeader(Object obj) {
196: return obj instanceof SOAPHeader || obj instanceof SOAP12Header;
197: }
198:
199: public static List<SoapHeader> getSoapHeaders(
200: List<ExtensibilityElement> exts) {
201: List<SoapHeader> headers = new ArrayList<SoapHeader>();
202: for (ExtensibilityElement ext : exts) {
203: if (isSOAPHeader(ext)) {
204: headers.add(getSoapHeader(ext));
205: }
206: }
207: return headers;
208: }
209:
210: public static SoapHeader getSoapHeader(Object obj) {
211: if (isSOAPHeader(obj)) {
212: return getProxy(SoapHeader.class, obj);
213: }
214: return null;
215: }
216:
217: public static SoapAddress getSoapAddress(Object obj) {
218: if (isSOAPAddress(obj)) {
219: return getProxy(SoapAddress.class, obj);
220: }
221: return null;
222: }
223:
224: public static boolean isSOAPAddress(Object obj) {
225: return obj instanceof SOAPAddress
226: || obj instanceof SOAP12Address;
227: }
228:
229: public static SoapHeader getBindingInputSOAPHeader(
230: BindingOperation bop) {
231: BindingInput bindingInput = bop.getBindingInput();
232: if (bindingInput != null) {
233: Iterator ite = bindingInput.getExtensibilityElements()
234: .iterator();
235: while (ite.hasNext()) {
236: Object obj = ite.next();
237: if (isSOAPHeader(obj)) {
238: return getProxy(SoapHeader.class, obj);
239: }
240: }
241: }
242:
243: return null;
244: }
245:
246: public static SoapHeader getBindingOutputSOAPHeader(
247: BindingOperation bop) {
248: BindingOutput bindingOutput = bop.getBindingOutput();
249: if (bindingOutput != null) {
250: Iterator ite = bindingOutput.getExtensibilityElements()
251: .iterator();
252: while (ite.hasNext()) {
253: Object obj = ite.next();
254: if (isSOAPHeader(obj)) {
255: return getProxy(SoapHeader.class, obj);
256: }
257: }
258: }
259:
260: return null;
261: }
262:
263: public static SoapBinding getSoapBinding(
264: List<ExtensibilityElement> exts) {
265: for (ExtensibilityElement ext : exts) {
266: if (isSOAPBinding(ext)) {
267: return getSoapBinding(ext);
268: }
269: }
270: return null;
271: }
272:
273: public static SoapBinding getSoapBinding(Object obj) {
274: if (isSOAPBinding(obj)) {
275: return getProxy(SoapBinding.class, obj);
276: }
277: return null;
278: }
279:
280: public static boolean isSOAPBinding(Object obj) {
281: return obj instanceof SOAPBinding
282: || obj instanceof SOAP12Binding;
283: }
284:
285: @SuppressWarnings("unchecked")
286: public static List<SoapFault> getBindingOperationSoapFaults(
287: BindingOperation bop) {
288: List<SoapFault> faults = new ArrayList<SoapFault>();
289: Map bindingFaults = bop.getBindingFaults();
290: for (Object obj : bindingFaults.values()) {
291: if (!(obj instanceof BindingFault)) {
292: continue;
293: }
294: BindingFault faultElement = (BindingFault) obj;
295: Iterator ite = faultElement.getExtensibilityElements()
296: .iterator();
297: while (ite.hasNext()) {
298: SoapFault fault = getSoapFault(ite.next());
299: if (fault != null) {
300: faults.add(fault);
301: }
302: }
303: }
304: return faults;
305: }
306:
307: public static SoapFault getSoapFault(Object obj) {
308: if (isSOAPFault(obj)) {
309: return getProxy(SoapFault.class, obj);
310: }
311: return null;
312: }
313:
314: public static boolean isMixedStyle(Binding binding) {
315: Iterator ite = binding.getExtensibilityElements().iterator();
316: String bindingStyle = "";
317: String previousOpStyle = "";
318: String style = "";
319: while (ite.hasNext()) {
320: Object obj = ite.next();
321: if (isSOAPBinding(obj)) {
322: SoapBinding soapBinding = getSoapBinding(obj);
323: bindingStyle = soapBinding.getStyle();
324: if (bindingStyle == null) {
325: bindingStyle = "";
326: }
327: }
328: }
329: Iterator ite2 = binding.getBindingOperations().iterator();
330: while (ite2.hasNext()) {
331: BindingOperation bop = (BindingOperation) ite2.next();
332: Iterator ite3 = bop.getExtensibilityElements().iterator();
333: while (ite3.hasNext()) {
334: Object obj = ite3.next();
335:
336: if (isSOAPOperation(obj)) {
337: SoapOperation soapOperation = getSoapOperation(obj);
338: style = soapOperation.getStyle();
339: if (style == null) {
340: style = "";
341: }
342:
343: if ("".equals(bindingStyle)
344: && "".equals(previousOpStyle)
345: || "".equals(bindingStyle)
346: && previousOpStyle.equalsIgnoreCase(style)) {
347: previousOpStyle = style;
348:
349: } else if (!"".equals(bindingStyle)
350: && "".equals(previousOpStyle)
351: && bindingStyle.equalsIgnoreCase(style)
352: || bindingStyle
353: .equalsIgnoreCase(previousOpStyle)
354: && bindingStyle.equalsIgnoreCase(style)) {
355: previousOpStyle = style;
356: } else if (!"".equals(bindingStyle)
357: && "".equals(style)
358: && "".equals(previousOpStyle)) {
359: continue;
360: } else {
361: return true;
362: }
363:
364: }
365:
366: }
367: }
368:
369: return false;
370:
371: }
372:
373: public static String getCanonicalBindingStyle(Binding binding) {
374: String bindingStyle = getBindingStyle(binding);
375: if (!StringUtils.isEmpty(bindingStyle)) {
376: return bindingStyle;
377: }
378: for (Iterator ite2 = binding.getBindingOperations().iterator(); ite2
379: .hasNext();) {
380: BindingOperation bindingOp = (BindingOperation) ite2.next();
381: String bopStyle = getSOAPOperationStyle(bindingOp);
382: if (!"".equals(bopStyle)) {
383: return bopStyle;
384: }
385: }
386: return "";
387:
388: }
389:
390: public static boolean isSOAPOperation(Object obj) {
391: return obj instanceof SOAPOperation
392: || obj instanceof SOAP12Operation;
393: }
394:
395: public static boolean isSOAPFault(Object obj) {
396: return obj instanceof SOAPFault || obj instanceof SOAP12Fault;
397: }
398:
399: public static SoapAddress createSoapAddress(
400: ExtensionRegistry extReg, boolean isSOAP12)
401: throws WSDLException {
402: ExtensibilityElement extElement = null;
403: if (isSOAP12) {
404: extElement = (SOAP12Address) extReg
405: .createExtension(Port.class,
406: WSDLConstants.NS_SOAP12_BINDING_ADDRESS);
407: } else {
408: extElement = (SOAPAddress) extReg.createExtension(
409: Port.class, WSDLConstants.NS_SOAP_BINDING_ADDRESS);
410: }
411: return getSoapAddress(extElement);
412: }
413:
414: public static SoapBody createSoapBody(ExtensionRegistry extReg,
415: Class clz, boolean isSOAP12) throws WSDLException {
416: ExtensibilityElement extElement = null;
417: if (isSOAP12) {
418: extElement = (SOAP12Body) extReg.createExtension(clz,
419: new QName(WSDLConstants.SOAP12_NAMESPACE, "body"));
420: } else {
421: extElement = (SOAPBody) extReg.createExtension(clz,
422: new QName(WSDLConstants.SOAP11_NAMESPACE, "body"));
423: }
424: return getSoapBody(extElement);
425: }
426:
427: public static SoapBinding createSoapBinding(
428: ExtensionRegistry extReg, boolean isSOAP12)
429: throws WSDLException {
430: ExtensibilityElement extElement = null;
431: if (isSOAP12) {
432: extElement = (SOAP12Binding) extReg.createExtension(
433: Binding.class, new QName(
434: WSDLConstants.SOAP12_NAMESPACE, "binding"));
435: ((SOAP12Binding) extElement)
436: .setTransportURI(WSDLConstants.SOAP12_HTTP_TRANSPORT);
437: } else {
438: extElement = (SOAPBinding) extReg.createExtension(
439: Binding.class, new QName(
440: WSDLConstants.SOAP11_NAMESPACE, "binding"));
441: ((SOAPBinding) extElement)
442: .setTransportURI(WSDLConstants.NS_SOAP11_HTTP_BINDING);
443: }
444: return getSoapBinding(extElement);
445: }
446:
447: public static SoapOperation createSoapOperation(
448: ExtensionRegistry extReg, boolean isSOAP12)
449: throws WSDLException {
450: ExtensibilityElement extElement = null;
451: if (isSOAP12) {
452: extElement = (SOAP12Operation) extReg.createExtension(
453: BindingOperation.class,
454: new QName(WSDLConstants.SOAP12_NAMESPACE,
455: "operation"));
456: } else {
457: extElement = (SOAPOperation) extReg.createExtension(
458: BindingOperation.class,
459: new QName(WSDLConstants.SOAP11_NAMESPACE,
460: "operation"));
461: }
462: return getSoapOperation(extElement);
463: }
464:
465: public static SoapFault createSoapFault(ExtensionRegistry extReg,
466: boolean isSOAP12) throws WSDLException {
467: ExtensibilityElement extElement = null;
468: if (isSOAP12) {
469: extElement = (SOAP12Fault) extReg.createExtension(
470: BindingFault.class, new QName(
471: WSDLConstants.SOAP12_NAMESPACE, "fault"));
472: } else {
473: extElement = (SOAPFault) extReg.createExtension(
474: BindingFault.class, new QName(
475: WSDLConstants.SOAP11_NAMESPACE, "fault"));
476: }
477: return getSoapFault(extElement);
478: }
479:
480: public static SoapHeader createSoapHeader(ExtensionRegistry extReg,
481: Class clz, boolean isSOAP12) throws WSDLException {
482: ExtensibilityElement extElement = null;
483: if (isSOAP12) {
484: extElement = (SOAP12Header) extReg
485: .createExtension(clz, new QName(
486: WSDLConstants.SOAP12_NAMESPACE, "header"));
487: } else {
488: extElement = (SOAPHeader) extReg
489: .createExtension(clz, new QName(
490: WSDLConstants.SOAP11_NAMESPACE, "header"));
491: }
492: return getSoapHeader(extElement);
493: }
494:
495: public static void addSOAPNamespace(Definition definition,
496: boolean isSOAP12) {
497: Map namespaces = definition.getNamespaces();
498: if (isSOAP12
499: && !namespaces.values().contains(
500: WSDLConstants.SOAP12_NAMESPACE)) {
501: definition.addNamespace("soap12",
502: WSDLConstants.SOAP12_NAMESPACE);
503: } else if (!namespaces.values().contains(
504: WSDLConstants.SOAP11_NAMESPACE)) {
505: definition.addNamespace("soap",
506: WSDLConstants.SOAP11_NAMESPACE);
507: }
508: }
509:
510: public static javax.jws.soap.SOAPBinding.Style getSoapStyle(
511: String soapStyle) {
512: if ("".equals(soapStyle)) {
513: return null;
514: } else if ("RPC".equalsIgnoreCase(soapStyle)) {
515: return javax.jws.soap.SOAPBinding.Style.RPC;
516: } else {
517: return javax.jws.soap.SOAPBinding.Style.DOCUMENT;
518: }
519: }
520:
521: public static javax.jws.soap.SOAPBinding.Use getSoapUse(
522: String soapUse) {
523: if ("".equals(soapUse)) {
524: return null;
525: } else if ("ENCODED".equalsIgnoreCase(soapUse)) {
526: return javax.jws.soap.SOAPBinding.Use.ENCODED;
527: } else {
528: return javax.jws.soap.SOAPBinding.Use.LITERAL;
529: }
530: }
531:
532: }
|