001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.soap.wsdl;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.wsdl.BindingFault;
026    import javax.wsdl.BindingOperation;
027    import javax.wsdl.Message;
028    import javax.wsdl.Part;
029    import javax.wsdl.WSDLException;
030    import javax.wsdl.extensions.ElementExtensible;
031    import javax.wsdl.extensions.soap.SOAPBody;
032    import javax.wsdl.extensions.soap.SOAPHeader;
033    import javax.wsdl.extensions.soap.SOAPHeaderFault;
034    import javax.wsdl.extensions.soap12.SOAP12Body;
035    import javax.wsdl.extensions.soap12.SOAP12Header;
036    import javax.wsdl.extensions.soap12.SOAP12HeaderFault;
037    import javax.wsdl.factory.WSDLFactory;
038    import javax.wsdl.xml.WSDLReader;
039    
040    import org.apache.servicemix.soap.api.Fault;
041    
042    import com.ibm.wsdl.Constants;
043    
044    public class WSDLUtils {
045    
046        public static final String WSDL1_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
047        public static final String WSDL2_NAMESPACE = "http://www.w3.org/2006/01/wsdl";
048        
049        public static final String WSDL1_STYLE_RPC = "rpc";
050        public static final String WSDL1_STYLE_DOCUMENT = "document";
051        public static final String WSDL1_USE_LITERAL = "literal";
052        
053        private static WSDLFactory wsdl11Factory;
054        
055        public static WSDLReader createWSDL11Reader() {
056            WSDLReader reader = getWSDL11Factory().newWSDLReader();
057            reader.setFeature(Constants.FEATURE_VERBOSE, false);
058            return reader;
059        }
060        
061        public static WSDLFactory getWSDL11Factory() {
062            if (wsdl11Factory == null) {
063                try {
064                    wsdl11Factory = WSDLFactory.newInstance();
065                } catch (WSDLException e) {
066                    throw new Fault(e);
067                }
068            }
069            return wsdl11Factory;
070        }
071        
072        @SuppressWarnings("unchecked")
073        public static List<String> getParts(SOAPBody body) {
074            return (List<String>) body.getParts();
075        }
076        
077        @SuppressWarnings("unchecked")
078        public static List<String> getParts(SOAP12Body body) {
079            return (List<String>) body.getParts();
080        }
081        
082        @SuppressWarnings("unchecked")
083        public static Map<String, Part> getParts(Message msg) {
084            return (Map<String, Part>) msg.getParts();
085        }
086        
087        @SuppressWarnings("unchecked")
088        public static Collection<BindingFault> getBindingFaults(BindingOperation bop) {
089            return (Collection<BindingFault>) bop.getBindingFaults().values();
090        }
091        
092        @SuppressWarnings("unchecked")
093        public static List<SOAPHeaderFault> getSOAPHeaderFaults(SOAPHeader sh) {
094            return (List<SOAPHeaderFault>) sh.getSOAPHeaderFaults();
095        }
096        
097        @SuppressWarnings("unchecked")
098        public static List<SOAP12HeaderFault> getSOAPHeaderFaults(SOAP12Header sh) {
099            return (List<SOAP12HeaderFault>) sh.getSOAP12HeaderFaults();
100        }
101        
102        public static List<ElementExtensible> getElements(BindingOperation bop) {
103            List<ElementExtensible> l = new ArrayList<ElementExtensible>();
104            l.add(bop);
105            l.add(bop.getBindingInput());
106            l.add(bop.getBindingOutput());
107            l.addAll(getBindingFaults(bop));
108            return l;
109        }
110        
111        public static <T> T getExtension(ElementExtensible element, Class<T> format) {
112            if (element != null) {
113                for (Iterator it = element.getExtensibilityElements().iterator(); it.hasNext();) {
114                    Object ex = it.next();
115                    if (format.isInstance(ex)) {
116                        return format.cast(ex);
117                    }
118                }
119            }
120            return null;
121        }
122        
123        public static <T> List<T> getExtensions(ElementExtensible element, Class<T> format) {
124            List<T> l = new ArrayList<T>();
125            if (element != null) {
126                for (Iterator it = element.getExtensibilityElements().iterator(); it.hasNext();) {
127                    Object ex = it.next();
128                    if (format.isInstance(ex)) {
129                        l.add(format.cast(ex));
130                    }
131                }
132            }
133            return l;
134        }
135        
136        public static <T> List<T> getExtensions(List<ElementExtensible> elements, Class<T> format) {
137            List<T> l = new ArrayList<T>();
138            for (ElementExtensible e : elements) {
139                l.addAll(getExtensions(e, format));
140            }
141            return l;
142        }
143        
144    }