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;
018    
019    import java.util.List;
020    
021    import javax.jws.WebMethod;
022    import javax.jws.WebParam;
023    import javax.jws.WebResult;
024    import javax.jws.WebService;
025    import javax.xml.ws.wsaddressing.W3CEndpointReference;
026    
027    import org.oasis_open.docs.wsn.b_2.InvalidTopicExpressionFaultType;
028    import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
029    import org.oasis_open.docs.wsn.b_2.TopicExpressionType;
030    import org.oasis_open.docs.wsn.br_2.DestroyRegistration;
031    import org.oasis_open.docs.wsn.br_2.DestroyRegistrationResponse;
032    import org.oasis_open.docs.wsn.br_2.PublisherRegistrationFailedFaultType;
033    import org.oasis_open.docs.wsn.br_2.RegisterPublisher;
034    import org.oasis_open.docs.wsn.br_2.ResourceNotDestroyedFaultType;
035    import org.oasis_open.docs.wsn.brw_2.PublisherRegistrationFailedFault;
036    import org.oasis_open.docs.wsn.brw_2.PublisherRegistrationManager;
037    import org.oasis_open.docs.wsn.brw_2.PublisherRegistrationRejectedFault;
038    import org.oasis_open.docs.wsn.brw_2.ResourceNotDestroyedFault;
039    import org.oasis_open.docs.wsn.bw_2.InvalidTopicExpressionFault;
040    import org.oasis_open.docs.wsn.bw_2.TopicNotSupportedFault;
041    import org.oasis_open.docs.wsrf.rw_2.ResourceUnknownFault;
042    
043    @WebService(endpointInterface = "org.oasis_open.docs.wsn.brw_2.PublisherRegistrationManager")
044    public abstract class AbstractPublisher extends AbstractEndpoint implements PublisherRegistrationManager {
045    
046        protected W3CEndpointReference publisherReference;
047    
048        protected boolean demand;
049    
050        protected List<TopicExpressionType> topic;
051    
052        public AbstractPublisher(String name) {
053            super(name);
054        }
055    
056        /**
057         * 
058         * @param destroyRequest
059         * @return returns org.oasis_open.docs.wsn.br_1.DestroyResponse
060         * @throws ResourceNotDestroyedFault
061         * @throws ResourceUnknownFault
062         */
063        @WebMethod(operationName = "DestroyRegistration")
064        @WebResult(name = "DestroyRegistrationResponse", 
065                   targetNamespace = "http://docs.oasis-open.org/wsn/br-2", 
066                   partName = "DestroyRegistrationResponse")
067        public DestroyRegistrationResponse destroyRegistration(
068                @WebParam(name = "DestroyRegistration", 
069                          targetNamespace = "http://docs.oasis-open.org/wsn/br-2", 
070                          partName = "DestroyRegistrationRequest")
071                DestroyRegistration destroyRegistrationRequest) throws ResourceNotDestroyedFault, ResourceUnknownFault {
072    
073            destroy();
074            return new DestroyRegistrationResponse();
075        }
076    
077        public abstract void notify(NotificationMessageHolderType messageHolder);
078    
079        protected void destroy() throws ResourceNotDestroyedFault {
080            try {
081                unregister();
082            } catch (EndpointRegistrationException e) {
083                ResourceNotDestroyedFaultType fault = new ResourceNotDestroyedFaultType();
084                throw new ResourceNotDestroyedFault("Error unregistering endpoint", fault, e);
085            }
086        }
087    
088        protected String createAddress() {
089            return "http://servicemix.org/wsnotification/Publisher/" + getName();
090        }
091    
092        public void create(RegisterPublisher registerPublisherRequest) throws InvalidTopicExpressionFault,
093                PublisherRegistrationFailedFault, PublisherRegistrationRejectedFault, ResourceUnknownFault,
094                TopicNotSupportedFault {
095            validatePublisher(registerPublisherRequest);
096            start();
097        }
098    
099        protected void validatePublisher(RegisterPublisher registerPublisherRequest) throws InvalidTopicExpressionFault,
100                PublisherRegistrationFailedFault, PublisherRegistrationRejectedFault, ResourceUnknownFault,
101                TopicNotSupportedFault {
102            // Check consumer reference
103            publisherReference = registerPublisherRequest.getPublisherReference();
104            // Check topic
105            topic = registerPublisherRequest.getTopic();
106            // Check demand based
107            demand = registerPublisherRequest.isDemand() != null ? registerPublisherRequest.isDemand().booleanValue()
108                    : false;
109            // Check all parameters
110            if (publisherReference == null) {
111                PublisherRegistrationFailedFaultType fault = new PublisherRegistrationFailedFaultType();
112                throw new PublisherRegistrationFailedFault("Invalid PublisherReference: null", fault);
113            }
114            if (demand && (topic == null || topic.size() == 0)) {
115                InvalidTopicExpressionFaultType fault = new InvalidTopicExpressionFaultType();
116                throw new InvalidTopicExpressionFault(
117                        "Must specify at least one topic for demand-based publishing", fault);
118            }
119        }
120    
121        protected abstract void start() throws PublisherRegistrationFailedFault;
122    }