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.jbi;
018    
019    import javax.jbi.JBIException;
020    import javax.jbi.component.ComponentContext;
021    import javax.jbi.servicedesc.ServiceEndpoint;
022    import javax.xml.namespace.QName;
023    
024    import org.apache.servicemix.wsn.ComponentContextAware;
025    import org.apache.servicemix.wsn.client.AbstractWSAClient;
026    import org.apache.servicemix.wsn.client.NotificationBroker;
027    import org.apache.servicemix.wsn.client.Subscription;
028    import org.apache.servicemix.wsn.jms.JmsPublisher;
029    import org.oasis_open.docs.wsn.br_2.PublisherRegistrationFailedFaultType;
030    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
031    import org.oasis_open.docs.wsn.brw_2.PublisherRegistrationFailedFault;
032    import org.oasis_open.docs.wsn.brw_2.PublisherRegistrationRejectedFault;
033    import org.oasis_open.docs.wsn.bw_2.InvalidTopicExpressionFault;
034    import org.oasis_open.docs.wsn.bw_2.TopicNotSupportedFault;
035    import org.oasis_open.docs.wsrf.rw_2.ResourceUnknownFault;
036    
037    public class JbiPublisher extends JmsPublisher implements ComponentContextAware {
038    
039        private ComponentContext context;
040    
041        private ServiceEndpoint endpoint;
042    
043        private String notificationBrokerAddress;
044    
045        public JbiPublisher(String name) {
046            super(name);
047        }
048    
049        public String getNotificationBrokerAddress() {
050            return notificationBrokerAddress;
051        }
052    
053        public void setNotificationBrokerAddress(String notificationBrokerAddress) {
054            this.notificationBrokerAddress = notificationBrokerAddress;
055        }
056    
057        @Override
058        protected Object startSubscription() {
059            Subscription subscription = null;
060            try {
061                NotificationBroker broker = new NotificationBroker(getContext(), publisherReference);
062                subscription = broker.subscribe(AbstractWSAClient.createWSA(notificationBrokerAddress), "noTopic", null);
063            } catch (JBIException e) {
064                // TODO Auto-generated catch block
065                e.printStackTrace();
066            }
067            return subscription;
068        }
069    
070        @Override
071        protected void destroySubscription(Object subscription) {
072            try {
073                ((Subscription) subscription).unsubscribe();
074            } catch (JBIException e) {
075                // TODO Auto-generated catch block
076                e.printStackTrace();
077            }
078        }
079    
080        @Override
081        protected void validatePublisher(RegisterPublisher registerPublisherRequest) throws InvalidTopicExpressionFault,
082                PublisherRegistrationFailedFault, PublisherRegistrationRejectedFault, ResourceUnknownFault,
083                TopicNotSupportedFault {
084            super.validatePublisher(registerPublisherRequest);
085            String[] parts = split(AbstractWSAClient.getWSAAddress(publisherReference));
086            endpoint = getContext().getEndpoint(new QName(parts[0], parts[1]), parts[2]);
087            if (endpoint == null) {
088                PublisherRegistrationFailedFaultType fault = new PublisherRegistrationFailedFaultType();
089                throw new PublisherRegistrationFailedFault("Unable to resolve consumer reference endpoint", fault);
090            }
091        }
092    
093        protected String[] split(String uri) {
094            char sep;
095            if (uri.indexOf('/') > 0) {
096                sep = '/';
097            } else {
098                sep = ':';
099            }
100            int idx1 = uri.lastIndexOf(sep);
101            int idx2 = uri.lastIndexOf(sep, idx1 - 1);
102            String epName = uri.substring(idx1 + 1);
103            String svcName = uri.substring(idx2 + 1, idx1);
104            String nsUri = uri.substring(0, idx2);
105            return new String[] {nsUri, svcName, epName };
106        }
107    
108        public ComponentContext getContext() {
109            return context;
110        }
111    
112        public void setContext(ComponentContext context) {
113            this.context = context;
114        }
115    }