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.component;
018    
019    import javax.jbi.management.DeploymentException;
020    import javax.jbi.messaging.MessageExchange;
021    import javax.xml.namespace.QName;
022    
023    import org.oasis_open.docs.wsn.b_2.Subscribe;
024    import org.oasis_open.docs.wsn.b_2.SubscribeResponse;
025    import org.apache.servicemix.id.IdGenerator;
026    import org.apache.servicemix.wsn.client.AbstractWSAClient;
027    
028    public class WSNSubscriptionEndpoint extends WSNDeployableEndpoint {
029    
030        private Subscribe request;
031    
032        private SubscribeResponse response;
033    
034        public WSNSubscriptionEndpoint(Subscribe request) throws DeploymentException {
035            this(request, null, null);
036        }
037    
038        public WSNSubscriptionEndpoint(Subscribe request, QName service, String endpoint) {
039            this.request = request;
040            if (service != null) {
041                this.service = service;
042            } else {
043                this.service = new QName("http://servicemix.org/wsnotification", "Subscription");
044            }
045            if (endpoint != null) {
046                this.endpoint = endpoint;
047            } else {
048                this.endpoint = new IdGenerator().generateSanitizedId();
049            }
050        }
051    
052        @Override
053        public MessageExchange.Role getRole() {
054            return MessageExchange.Role.CONSUMER;
055        }
056    
057        @Override
058        public void activate() throws Exception {
059            response = getNotificationBroker().handleSubscribe(request, this);
060        }
061    
062        @Override
063        public void deactivate() throws Exception {
064            getNotificationBroker().unsubscribe(AbstractWSAClient.getWSAAddress(response.getSubscriptionReference()));
065        }
066    
067    }