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.w3c.dom.DocumentFragment;
021    import org.w3c.dom.Element;
022    import org.w3c.dom.Node;
023    import org.w3c.dom.NodeList;
024    
025    import javax.jbi.servicedesc.ServiceEndpoint;
026    import javax.xml.namespace.QName;
027    
028    import org.apache.servicemix.common.util.URIResolver;
029    import org.apache.servicemix.common.util.DOMUtil;
030    
031    /**
032     * A default implementation of a resolved endpoint
033     */
034    public class ResolvedEndpoint implements ServiceEndpoint {
035    
036        private DocumentFragment reference;
037        private String endpointName;
038        private QName serviceName;
039        private QName[] interfaces = null;
040    
041        public ResolvedEndpoint(String uri, QName serviceName) {
042            this(URIResolver.createWSAEPR(uri), uri, serviceName);
043        }
044    
045        public ResolvedEndpoint(DocumentFragment reference, String epName, QName serviceName) {
046            this.reference = reference;
047            this.endpointName = epName;
048            this.serviceName = serviceName;
049        }
050    
051    
052        public ResolvedEndpoint(DocumentFragment reference, String epName, QName serviceName, QName[] interfaces) {
053            this.reference = reference;
054            this.endpointName = epName;
055            this.serviceName = serviceName;
056            this.interfaces = interfaces;
057        }
058    
059        public DocumentFragment getAsReference(QName operationName) {
060            return reference;
061        }
062    
063        public String getEndpointName() {
064            return endpointName;
065        }
066    
067        public QName getServiceName() {
068            return serviceName;
069        }
070    
071        public QName[] getInterfaces() {
072            return interfaces;
073        }
074    
075        public static ServiceEndpoint resolveEndpoint(DocumentFragment epr, QName elementName, QName serviceName, String uriPrefix) {
076            if (epr.getChildNodes().getLength() == 1) {
077                Node child = epr.getFirstChild();
078                if (child instanceof Element) {
079                    Element elem = (Element) child;
080                    String nsUri = elem.getNamespaceURI();
081                    String name = elem.getLocalName();
082                    // Check simple endpoints
083                    if (elementName.getNamespaceURI().equals(nsUri) && elementName.getLocalPart().equals(name)) {
084                        return new ResolvedEndpoint(epr, DOMUtil.getElementText(elem), serviceName);
085                        // Check WSA endpoints
086                    }
087                    else {
088                        NodeList nl = elem.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
089                        if (nl.getLength() == 1) {
090                            Element address = (Element) nl.item(0);
091                            String uri = DOMUtil.getElementText(address);
092                            if (uri != null && uriPrefix != null) {
093                                uri = uri.trim();
094                                if (uri.startsWith(uriPrefix)) {
095                                    return new ResolvedEndpoint(epr, uri, serviceName);
096                                }
097                            }
098                        }
099                    }
100                }
101            }
102            return null;
103        }
104    
105    }