001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * 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, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.servicemix.common;
019    
020    import org.apache.servicemix.jbi.resolver.URIResolver;
021    import org.apache.servicemix.jbi.util.DOMUtil;
022    import org.w3c.dom.DocumentFragment;
023    import org.w3c.dom.Element;
024    import org.w3c.dom.Node;
025    import org.w3c.dom.NodeList;
026    
027    import javax.jbi.servicedesc.ServiceEndpoint;
028    import javax.xml.namespace.QName;
029    
030    /**
031     * A default implementation of a resolved endpoint
032     */
033    public class ResolvedEndpoint implements ServiceEndpoint {
034    
035        private DocumentFragment reference;
036        private String endpointName;
037        private QName serviceName;
038        private QName[] interfaces = null;
039    
040        public ResolvedEndpoint(String uri, QName serviceName) {
041            this(URIResolver.createWSAEPR(uri), uri, serviceName);
042        }
043    
044        public ResolvedEndpoint(DocumentFragment reference, String epName, QName serviceName) {
045            this.reference = reference;
046            this.endpointName = epName;
047            this.serviceName = serviceName;
048        }
049    
050    
051        public ResolvedEndpoint(DocumentFragment reference, String epName, QName serviceName, QName[] interfaces) {
052            this.reference = reference;
053            this.endpointName = epName;
054            this.serviceName = serviceName;
055            this.interfaces = interfaces;
056        }
057    
058        public DocumentFragment getAsReference(QName operationName) {
059            return reference;
060        }
061    
062        public String getEndpointName() {
063            return endpointName;
064        }
065    
066        public QName getServiceName() {
067            return serviceName;
068        }
069    
070        public QName[] getInterfaces() {
071            return interfaces;
072        }
073    
074        public static ServiceEndpoint resolveEndpoint(DocumentFragment epr, QName elementName, QName serviceName, String uriPrefix) {
075            if (epr.getChildNodes().getLength() == 1) {
076                Node child = epr.getFirstChild();
077                if (child instanceof Element) {
078                    Element elem = (Element) child;
079                    String nsUri = elem.getNamespaceURI();
080                    String name = elem.getLocalName();
081                    // Check simple endpoints
082                    if (elementName.getNamespaceURI().equals(nsUri) && elementName.getLocalPart().equals(name)) {
083                        return new ResolvedEndpoint(epr, DOMUtil.getElementText(elem), serviceName);
084                        // Check WSA endpoints
085                    }
086                    else {
087                        NodeList nl = elem.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
088                        if (nl.getLength() == 1) {
089                            Element address = (Element) nl.item(0);
090                            String uri = DOMUtil.getElementText(address);
091                            if (uri != null && uriPrefix != null) {
092                                uri = uri.trim();
093                                if (uri.startsWith(uriPrefix)) {
094                                    return new ResolvedEndpoint(epr, uri, serviceName);
095                                }
096                            }
097                        }
098                    }
099                }
100            }
101            return null;
102        }
103    
104    }