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    
025    import org.apache.servicemix.client.DefaultServiceMixClient;
026    import org.apache.servicemix.client.ServiceMixClient;
027    import org.apache.servicemix.client.ServiceMixClientFacade;
028    import org.apache.servicemix.jbi.container.JBIContainer;
029    import org.apache.servicemix.jbi.resolver.EndpointResolver;
030    import org.apache.servicemix.jbi.resolver.ServiceAndEndpointNameResolver;
031    import org.apache.servicemix.jbi.resolver.URIResolver;
032    import org.oasis_open.docs.wsn.b_2.Subscribe;
033    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
034    import org.w3._2005._08.addressing.AttributedURIType;
035    import org.w3._2005._08.addressing.EndpointReferenceType;
036    
037    public abstract class AbstractWSAClient {
038    
039        private EndpointReferenceType endpoint;
040    
041        private EndpointResolver resolver;
042    
043        private ServiceMixClient client;
044    
045        public AbstractWSAClient() {
046        }
047    
048        public AbstractWSAClient(EndpointReferenceType endpoint, ServiceMixClient client) {
049            this.endpoint = endpoint;
050            this.resolver = resolveWSA(endpoint);
051            this.client = client;
052        }
053    
054        public static EndpointReferenceType createWSA(String address) {
055            EndpointReferenceType epr = new EndpointReferenceType();
056            AttributedURIType attUri = new AttributedURIType();
057            attUri.setValue(address);
058            epr.setAddress(attUri);
059            return epr;
060        }
061    
062        public static ServiceMixClient createJaxbClient(JBIContainer container) throws JBIException, JAXBException {
063            DefaultServiceMixClient client = new DefaultServiceMixClient(container);
064            client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
065            return client;
066        }
067    
068        public static ServiceMixClient createJaxbClient(ComponentContext context) throws JAXBException {
069            ServiceMixClientFacade client = new ServiceMixClientFacade(context);
070            client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class)));
071            return client;
072        }
073    
074        public static EndpointResolver resolveWSA(EndpointReferenceType ref) {
075            String[] parts = URIResolver.split3(ref.getAddress().getValue());
076            return new ServiceAndEndpointNameResolver(new QName(parts[0], parts[1]), parts[2]);
077        }
078    
079        public EndpointReferenceType getEndpoint() {
080            return endpoint;
081        }
082    
083        public void setEndpoint(EndpointReferenceType endpoint) {
084            this.endpoint = endpoint;
085        }
086    
087        public EndpointResolver getResolver() {
088            return resolver;
089        }
090    
091        public void setResolver(EndpointResolver resolver) {
092            this.resolver = resolver;
093        }
094    
095        public ServiceMixClient getClient() {
096            return client;
097        }
098    
099        public void setClient(ServiceMixClient client) {
100            this.client = client;
101        }
102    
103        protected Object request(Object request) throws JBIException {
104            return client.request(resolver, null, null, request);
105        }
106    
107        protected void send(Object request) throws JBIException {
108            client.sendSync(resolver, null, null, request);
109        }
110    
111    }