KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > method > SubscribeMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/method/SubscribeMethod.java,v 1.11 2004/08/05 14:43:29 dflorey Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/05 14:43:29 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.method;
25
26 import org.apache.slide.common.NamespaceAccessToken;
27 import org.apache.slide.event.EventDispatcher;
28 import org.apache.slide.webdav.WebdavException;
29 import org.apache.slide.webdav.WebdavServletConfig;
30 import org.apache.slide.webdav.event.NotificationTrigger;
31 import org.apache.slide.webdav.event.Subscriber;
32 import org.apache.slide.webdav.event.WebdavEvent;
33 import org.apache.slide.webdav.util.NotificationConstants;
34 import org.apache.slide.webdav.util.WebdavStatus;
35
36
37 /**
38  * Subscribe Method.
39  *
40  */

41 public class SubscribeMethod extends AbstractWebdavMethod implements NotificationConstants {
42     protected static final String JavaDoc LOG_CHANNEL = SubscribeMethod.class.getName();
43
44     private final static int DEFAULT_SUBSCRIPTION_LIFETIME = 3600;
45
46     /**
47      * Constructor.
48      *
49      * @param token the token for accessing the namespace
50      * @param config configuration of the WebDAV servlet
51      */

52     public SubscribeMethod(NamespaceAccessToken token,
53                          WebdavServletConfig config) {
54         super(token, config);
55     }
56     
57     protected void parseRequest() throws WebdavException {
58     }
59     
60     protected void executeRequest() throws WebdavException {
61         try {
62             if ( WebdavEvent.SUBSCRIBE.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(WebdavEvent.SUBSCRIBE, new WebdavEvent(this));
63             String JavaDoc contentType = requestHeaders.getContentType();
64             String JavaDoc callback = requestHeaders.getCallback();
65             String JavaDoc notificationType = requestHeaders.getNotificationType();
66             int notificationDelay = requestHeaders.getNotificationDelay(0);
67             int subscriptionLifetime = requestHeaders.getSubscriptionLifetime(DEFAULT_SUBSCRIPTION_LIFETIME);
68             int depth = requestHeaders.getDepth(INFINITY);
69             int []subscriptionIDs = requestHeaders.getSubscriptionId();
70             
71             if ( subscriptionIDs.length > 0 ) {
72                 // renew subscribers
73
for ( int i = 0; i < subscriptionIDs.length; i++ ) {
74                     Subscriber subscriber = NotificationTrigger.getInstance().getSubscriber(subscriptionIDs[i]);
75                     if ( subscriber != null ) {
76                         NotificationTrigger.getInstance().refreshSubscriber(subscriber, true);
77                     }
78                 }
79             } else {
80                 if (notificationType == null) {
81                    sendError(WebdavStatus.SC_BAD_REQUEST, "Notification-Type header missing.");
82                    return;
83                 } else {
84                    // FIXME check for valid notification types
85
}
86                 Subscriber subscriber = new Subscriber(requestUri, callback, notificationType, notificationDelay,
87                       subscriptionLifetime, depth);
88                 int subscriptionID = NotificationTrigger.getInstance().addSubscriber(subscriber);
89                 resp.setHeader(H_CALL_BACK, callback);
90                 resp.setHeader(H_NOTIFICATION_TYPE, notificationType);
91                 resp.setHeader(H_SUBSCRIPTION_LIFETIME, String.valueOf(subscriptionLifetime));
92                 resp.setHeader(H_SUBSCRIPTION_ID, String.valueOf(subscriptionID));
93                 resp.setHeader(H_CONTENT_LOCATION,
94                       getSlideContextPath() + requestUri);
95                 resp.setStatus(WebdavStatus.SC_OK);
96             }
97         } catch (Exception JavaDoc e) {
98             int statusCode = getErrorCode( (Exception JavaDoc)e );
99             sendError( statusCode, e );
100             throw new WebdavException( statusCode );
101         }
102     }
103 }
Popular Tags