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.wsn.client;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.xml.bind.JAXBContext;
022    import javax.xml.bind.JAXBException;
023    import javax.xml.namespace.QName;
024    import javax.xml.parsers.ParserConfigurationException;
025    import javax.xml.transform.Source;
026    import javax.xml.transform.dom.DOMResult;
027    import javax.xml.ws.wsaddressing.W3CEndpointReference;
028    
029    import org.w3c.dom.Element;
030    import org.w3c.dom.NodeList;
031    
032    import org.apache.servicemix.client.DefaultServiceMixClient;
033    import org.apache.servicemix.client.ServiceMixClient;
034    import org.apache.servicemix.client.ServiceMixClientFacade;
035    import org.apache.servicemix.jbi.container.JBIContainer;
036    import org.apache.servicemix.jbi.jaxp.SourceTransformer;
037    import org.apache.servicemix.jbi.jaxp.StringSource;
038    import org.apache.servicemix.jbi.resolver.EndpointResolver;
039    import org.apache.servicemix.jbi.resolver.ServiceAndEndpointNameResolver;
040    import org.apache.servicemix.jbi.resolver.URIResolver;
041    import org.apache.servicemix.jbi.util.DOMUtil;
042    import org.oasis_open.docs.wsn.b_2.Subscribe;
043    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
044    
045    public abstract class AbstractWSAClient {
046    
047        private W3CEndpointReference endpoint;
048    
049        private EndpointResolver resolver;
050    
051        private ServiceMixClient client;
052    
053        public AbstractWSAClient() {
054        }
055    
056        public AbstractWSAClient(W3CEndpointReference endpoint, ServiceMixClient client) {
057            this.endpoint = endpoint;
058            this.resolver = resolveWSA(endpoint);
059            this.client = client;
060        }
061    
062        public static W3CEndpointReference createWSA(String address) {
063            Source src = new StringSource("<EndpointReference xmlns='http://www.w3.org/2005/08/addressing'><Address>"
064                                            + address + "</Address></EndpointReference>");
065            return new W3CEndpointReference(src);
066        }
067    
068        public static String getWSAAddress(W3CEndpointReference ref) {
069            try {
070                Element element = new SourceTransformer().createDocument().createElement("elem");
071                ref.writeTo(new DOMResult(element));
072                NodeList nl = element.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
073                if (nl != null && nl.getLength() > 0) {
074                    Element e = (Element) nl.item(0);
075                    return DOMUtil.getElementText(e).trim();
076                }
077            } catch (ParserConfigurationException e) {
078                // Ignore
079            }
080            return null;
081        }
082    
083        public static ServiceMixClient createJaxbClient(JBIContainer container) throws JBIException, JAXBException {
084            DefaultServiceMixClient client = new DefaultServiceMixClient(container);
085            client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
086            return client;
087        }
088    
089        public static ServiceMixClient createJaxbClient(ComponentContext context) throws JAXBException {
090            ServiceMixClientFacade client = new ServiceMixClientFacade(context);
091            client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
092            return client;
093        }
094    
095        public static EndpointResolver resolveWSA(W3CEndpointReference ref) {
096            String[] parts = URIResolver.split3(getWSAAddress(ref));
097            return new ServiceAndEndpointNameResolver(new QName(parts[0], parts[1]), parts[2]);
098        }
099    
100        public W3CEndpointReference getEndpoint() {
101            return endpoint;
102        }
103    
104        public void setEndpoint(W3CEndpointReference endpoint) {
105            this.endpoint = endpoint;
106        }
107    
108        public EndpointResolver getResolver() {
109            return resolver;
110        }
111    
112        public void setResolver(EndpointResolver resolver) {
113            this.resolver = resolver;
114        }
115    
116        public ServiceMixClient getClient() {
117            return client;
118        }
119    
120        public void setClient(ServiceMixClient client) {
121            this.client = client;
122        }
123    
124        protected Object request(Object request) throws JBIException {
125            return client.request(resolver, null, null, request);
126        }
127    
128        protected void send(Object request) throws JBIException {
129            client.sendSync(resolver, null, null, request);
130        }
131    
132    }