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.jbi.framework.support;
018    
019    import javax.xml.namespace.QName;
020    
021    import org.w3c.dom.Document;
022    
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.apache.servicemix.jbi.framework.Registry;
026    import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
027    import org.apache.woden.WSDLReader;
028    import org.apache.woden.internal.DOMWSDLReader;
029    import org.apache.woden.internal.DOMWSDLSource;
030    import org.apache.woden.types.NCName;
031    import org.apache.woden.wsdl20.Description;
032    import org.apache.woden.wsdl20.Endpoint;
033    import org.apache.woden.wsdl20.Interface;
034    import org.apache.woden.wsdl20.Service;
035    import org.apache.woden.wsdl20.xml.DescriptionElement;
036    
037    /**
038     * Retrieve interface implemented by the given endpoint using the WSDL 2 description.
039     * 
040     * @author gnodet
041     */
042    public class WSDL2Processor implements EndpointProcessor {
043    
044        public static final String WSDL2_NAMESPACE = "http://www.w3.org/2006/01/wsdl";
045    
046        private static final Log LOG = LogFactory.getLog(WSDL2Processor.class);
047        
048        private Registry registry;
049        
050        public void init(Registry reg) {
051            this.registry = reg;
052        }
053    
054        /**
055         * Retrieve interface implemented by the given endpoint using the WSDL 2 description.
056         * 
057         * @param serviceEndpoint the endpoint being checked
058         */
059        public void process(InternalEndpoint serviceEndpoint) {
060            try {
061                Document document = registry.getEndpointDescriptor(serviceEndpoint);
062                if (document == null || document.getDocumentElement() == null) {
063                    if (LOG.isDebugEnabled()) {
064                        LOG.debug("Endpoint " + serviceEndpoint + " has no service description");
065                    }
066                    return;
067                }
068                if (!WSDL2_NAMESPACE.equals(document.getDocumentElement().getNamespaceURI())) {
069                    if (LOG.isDebugEnabled()) {
070                        LOG.debug("Endpoint " + serviceEndpoint + " has a non WSDL2 service description");
071                    }
072                    return;
073                }
074                WSDLReader reader = new DOMWSDLReader();
075                DOMWSDLSource source = (DOMWSDLSource) reader.createWSDLSource();
076                source.setSource(document);
077                DescriptionElement descElement = reader.readWSDL(source);
078                Description desc = descElement.toComponent();
079                // Check if the wsdl is only a port type
080                // In these cases, only the port type is used, as the service name and endpoint name
081                // are provided on the jbi endpoint
082                if (desc.getInterfaces().length == 1 && desc.getServices().length == 0) {
083                    Interface itf = desc.getInterfaces()[0];
084                    QName interfaceName = itf.getName();
085                    if (LOG.isDebugEnabled()) {
086                        LOG.debug("Endpoint " + serviceEndpoint + " implements interface " + interfaceName);
087                    }
088                    serviceEndpoint.addInterface(interfaceName);
089                } else {
090                    Service service = desc.getService(serviceEndpoint.getServiceName());
091                    if (service == null) {
092                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no matching service found in "
093                                        + desc.getServices());
094                        return;
095                    }
096                    Endpoint endpoint = service.getEndpoint(new NCName(serviceEndpoint.getEndpointName()));
097                    if (endpoint == null) {
098                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no matching endpoint found in "
099                                        + service.getEndpoints());
100                        return;
101                    }
102                    if (endpoint.getBinding() == null) {
103                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no binding found");
104                        return;
105                    }
106                    if (endpoint.getBinding().getInterface() == null) {
107                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no port type found");
108                        return;
109                    }
110                    QName interfaceName = endpoint.getBinding().getInterface().getName();
111                    if (LOG.isDebugEnabled()) {
112                        LOG.debug("Endpoint " + serviceEndpoint + " implements interface " + interfaceName);
113                    }
114                    serviceEndpoint.addInterface(interfaceName);
115                }
116            } catch (Exception e) {
117                LOG.warn("Error retrieving interfaces from service description: " + e.getMessage());
118                if (LOG.isDebugEnabled()) {
119                    LOG.debug("Error retrieving interfaces from service description", e);
120                }
121            }
122        }
123    }