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.math.BigInteger;
020    import java.util.List;
021    
022    import javax.jws.Oneway;
023    import javax.jws.WebMethod;
024    import javax.jws.WebParam;
025    import javax.jws.WebResult;
026    import javax.jws.WebService;
027    
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    import org.oasis_open.docs.wsn.b_2.CreatePullPoint;
031    import org.oasis_open.docs.wsn.b_2.DestroyPullPoint;
032    import org.oasis_open.docs.wsn.b_2.DestroyPullPointResponse;
033    import org.oasis_open.docs.wsn.b_2.GetMessages;
034    import org.oasis_open.docs.wsn.b_2.GetMessagesResponse;
035    import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
036    import org.oasis_open.docs.wsn.b_2.Notify;
037    import org.oasis_open.docs.wsn.b_2.UnableToDestroyPullPointFaultType;
038    import org.oasis_open.docs.wsn.bw_2.NotificationConsumer;
039    import org.oasis_open.docs.wsn.bw_2.PullPoint;
040    import org.oasis_open.docs.wsn.bw_2.UnableToCreatePullPointFault;
041    import org.oasis_open.docs.wsn.bw_2.UnableToDestroyPullPointFault;
042    import org.oasis_open.docs.wsn.bw_2.UnableToGetMessagesFault;
043    import org.oasis_open.docs.wsrf.rw_2.ResourceUnknownFault;
044    
045    @WebService(endpointInterface = "org.oasis_open.docs.wsn.bw_2.PullPoint")
046    public abstract class AbstractPullPoint extends AbstractEndpoint implements PullPoint, NotificationConsumer {
047    
048        private static Log log = LogFactory.getLog(AbstractPullPoint.class);
049    
050        protected AbstractCreatePullPoint createPullPoint;
051    
052        public AbstractPullPoint(String name) {
053            super(name);
054        }
055    
056        /**
057         * 
058         * @param notify
059         */
060        @WebMethod(operationName = "Notify")
061        @Oneway
062        public void notify(
063                @WebParam(name = "Notify", 
064                          targetNamespace = "http://docs.oasis-open.org/wsn/b-1", 
065                          partName = "Notify")
066                Notify notify) {
067    
068            log.debug("Notify");
069            for (NotificationMessageHolderType messageHolder : notify.getNotificationMessage()) {
070                store(messageHolder);
071            }
072        }
073    
074        /**
075         * 
076         * @param getMessagesRequest
077         * @return returns org.oasis_open.docs.wsn.b_1.GetMessagesResponse
078         * @throws ResourceUnknownFault
079         */
080        @WebMethod(operationName = "GetMessages")
081        @WebResult(name = "GetMessagesResponse", 
082                   targetNamespace = "http://docs.oasis-open.org/wsn/b-1", 
083                   partName = "GetMessagesResponse")
084        public GetMessagesResponse getMessages(
085                @WebParam(name = "GetMessages", 
086                          targetNamespace = "http://docs.oasis-open.org/wsn/b-1", 
087                          partName = "GetMessagesRequest")
088                GetMessages getMessagesRequest) throws ResourceUnknownFault, UnableToGetMessagesFault {
089    
090            log.debug("GetMessages");
091            BigInteger max = getMessagesRequest.getMaximumNumber();
092            List<NotificationMessageHolderType> messages = getMessages(max != null ? max.intValue() : 0);
093            GetMessagesResponse response = new GetMessagesResponse();
094            response.getNotificationMessage().addAll(messages);
095            return response;
096        }
097    
098        /**
099         * 
100         * @param destroyRequest
101         * @return returns org.oasis_open.docs.wsn.b_1.DestroyResponse
102         * @throws UnableToDestroyPullPoint
103         */
104        @WebMethod(operationName = "DestroyPullPoint")
105        @WebResult(name = "DestroyPullPointResponse", 
106                   targetNamespace = "http://docs.oasis-open.org/wsn/b-2", 
107                   partName = "DestroyPullPointResponse")
108        public DestroyPullPointResponse destroyPullPoint(
109                @WebParam(name = "DestroyPullPoint", 
110                          targetNamespace = "http://docs.oasis-open.org/wsn/b-2", 
111                          partName = "DestroyPullPointRequest")
112                DestroyPullPoint destroyPullPointRequest) throws ResourceUnknownFault, UnableToDestroyPullPointFault {
113    
114            log.debug("Destroy");
115            createPullPoint.destroyPullPoint(getAddress());
116            return new DestroyPullPointResponse();
117        }
118    
119        public void create(CreatePullPoint createPullPointRequest) throws UnableToCreatePullPointFault {
120        }
121    
122        protected abstract void store(NotificationMessageHolderType messageHolder);
123    
124        protected abstract List<NotificationMessageHolderType> getMessages(int max) throws ResourceUnknownFault,
125                UnableToGetMessagesFault;
126    
127        protected void destroy() throws UnableToDestroyPullPointFault {
128            try {
129                unregister();
130            } catch (EndpointRegistrationException e) {
131                UnableToDestroyPullPointFaultType fault = new UnableToDestroyPullPointFaultType();
132                throw new UnableToDestroyPullPointFault("Error unregistering endpoint", fault, e);
133            }
134        }
135    
136        protected String createAddress() {
137            return "http://servicemix.org/wsnotification/PullPoint/" + getName();
138        }
139    
140        public AbstractCreatePullPoint getCreatePullPoint() {
141            return createPullPoint;
142        }
143    
144        public void setCreatePullPoint(AbstractCreatePullPoint createPullPoint) {
145            this.createPullPoint = createPullPoint;
146        }
147    }