KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > SubscribeMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/SubscribeMethod.java,v 1.3 2004/07/28 09:30:37 ib Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/28 09:30:37 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 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.webdav.lib.methods;
25
26 import java.io.IOException JavaDoc;
27
28 import org.apache.commons.httpclient.Header;
29 import org.apache.commons.httpclient.HttpConnection;
30 import org.apache.commons.httpclient.HttpException;
31 import org.apache.commons.httpclient.HttpState;
32
33 /**
34  * Implements the SUBSCRIBE method.
35  *
36  * @see <a HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_subscribe.asp">Reference</a>
37  */

38 public class SubscribeMethod extends XMLResponseMethodBase
39    implements DepthSupport
40 {
41
42    private static final String JavaDoc HEADER_SUBSCRIPTION_ID = "Subscription-Id";
43    private static final String JavaDoc HEADER_SUBSCRIPTION_LIFETIME = "Subscription-Lifetime";
44    private static final String JavaDoc HEADER_NOTIFICATION_TYPE = "Notification-Type";
45    private static final String JavaDoc HEADER_NOTIFICATION_DELAY = "Notification-Delay";
46    private static final String JavaDoc HEADER_DEPTH = "Depth";
47    private static final String JavaDoc HEADER_CALL_BACK = "Call-Back";
48    private static final String JavaDoc HEADER_CONTENT_LOCATION = "Content-Location";
49    
50    public static final String JavaDoc TYPE_UPDATE = "update";
51    public static final String JavaDoc TYPE_UPDATE_NEW_MEMBER = "update/newmember";
52    public static final String JavaDoc TYPE_DELETE = "delete";
53    public static final String JavaDoc TYPE_MOVE = "move";
54    
55    private String JavaDoc callback = null;
56    private String JavaDoc notificationType = null;
57    private int depth = -1;
58    private long subsciptionLifetime = -1;
59    private int subscriptionId = -1;
60    private long notificationDelay = -1;
61    
62    private long responsedSubscriptionLifetime = -1;
63    private int responsedSubscriptionId = -1;
64    private String JavaDoc responsedContentLocation = null;
65    
66    public SubscribeMethod() {
67       
68    }
69    
70    public SubscribeMethod(String JavaDoc path) {
71       super(path);
72    }
73
74    public String JavaDoc getCallback()
75    {
76       return callback;
77    }
78    /**
79     * Sets the URI that's to be notified if the subscribed event does occur.
80     */

81    public void setCallback(String JavaDoc callback)
82    {
83       if (callback != null && callback.length() > 0) {
84          this.callback = callback;
85       }
86    }
87    public String JavaDoc getNotificationType()
88    {
89       return notificationType;
90    }
91    /**
92     * Sets the notification type, i.e. determines the events that are
93     * subscribed.
94     * @see #TYPE_DELETE
95     * @see #TYPE_MOVE
96     * @see #TYPE_UPDATE
97     * @see #TYPE_UPDATE_NEW_MEMBER
98     */

99    public void setNotificationType(String JavaDoc notificationType)
100    {
101       this.notificationType = notificationType;
102    }
103    public long getSubsciptionLifetime()
104    {
105       return subsciptionLifetime;
106    }
107    /**
108     * Sets the duration of the subscription in seconds.
109     */

110    public void setSubsciptionLifetime(long subsciptionLifetime)
111    {
112       this.subsciptionLifetime = subsciptionLifetime;
113    }
114    public long getSubscriptionId()
115    {
116       return subscriptionId;
117    }
118    /**
119     * Sets the ID of a subscription to be refreshed.
120     * @param subscriptionId
121     */

122    public void setSubscriptionId(int subscriptionId)
123    {
124       this.subscriptionId = subscriptionId;
125    }
126    /**
127     * Sets the notification delay in seconds.
128     */

129    public void setNotificationDelay(long delay) {
130       this.notificationDelay = delay;
131    }
132    public long getNotificationDelay() {
133       return this.notificationDelay;
134    }
135    public int getDepth()
136    {
137       return this.depth;
138    }
139    /**
140     * Sets the depth.
141     */

142    public void setDepth(int depth)
143    {
144       switch(depth) {
145          case DEPTH_0:
146          case DEPTH_1:
147          case DEPTH_INFINITY:
148             this.depth = depth;
149             break;
150          default:
151             throw new IllegalArgumentException JavaDoc(
152                   "Depth must be 0, 1 or "+DEPTH_INFINITY+".");
153       }
154    }
155    
156    /**
157     * Returns the subscription ID responsed from the server.
158     * @return -1 if no subscription id was in the response
159     */

160    public int getResponsedSubscriptionId() {
161       checkUsed();
162       return this.responsedSubscriptionId;
163    }
164    /**
165     * Returns the subscription lifetime responsed from the server.
166     * @return -1 if no subscription lifetime was given in the response
167     */

168    public long getResponsedSubscriptionLifetime() {
169       checkUsed();
170       return this.responsedSubscriptionLifetime;
171    }
172    /**
173     * Returns the value of the content-location header of the response.
174     * This shall be used to the request uri for a POLL method querying this
175     * subscription.
176     */

177    public String JavaDoc getResponsedContentLocation() {
178       checkUsed();
179       return this.responsedContentLocation;
180    }
181    // --------------------------------------------------- WebdavMethod Methods
182

183    public String JavaDoc getName()
184    {
185       return "SUBSCRIBE";
186    }
187    
188    public void recycle()
189    {
190       super.recycle();
191       this.callback = null;
192       this.depth = -1;
193       this.notificationDelay = -1;
194       this.notificationType = null;
195       this.responsedSubscriptionId = -1;
196       this.responsedSubscriptionLifetime = -1;
197       this.subsciptionLifetime = -1;
198       this.subscriptionId = -1;
199    }
200    protected void addRequestHeaders(HttpState state, HttpConnection conn)
201          throws IOException JavaDoc, HttpException
202    {
203       super.addRequestHeaders(state, conn);
204       
205       if (this.callback != null) {
206          super.setRequestHeader(HEADER_CALL_BACK, this.callback);
207       }
208       if (this.depth > -1) {
209          super.setRequestHeader(HEADER_DEPTH,
210                this.depth == DEPTH_INFINITY ? "infinity"
211                      : String.valueOf(this.depth));
212       }
213       if (this.notificationType != null) {
214          super.setRequestHeader(HEADER_NOTIFICATION_TYPE, this.notificationType);
215       }
216       if (this.subsciptionLifetime > 0) {
217          super.setRequestHeader(HEADER_SUBSCRIPTION_LIFETIME,
218                Long.toString(this.subsciptionLifetime));
219       }
220       if (this.subscriptionId > 0) {
221          super.setRequestHeader(HEADER_SUBSCRIPTION_ID, Long.toString(
222                this.subscriptionId));
223       }
224       if (this.notificationDelay > 0) {
225          super.setRequestHeader(HEADER_NOTIFICATION_DELAY, Long.toString(
226                this.notificationDelay));
227       }
228    }
229    
230    /**
231     * Adds special checking of header values of the SUBSCRIBE method to
232     * the super class implementation.
233     */

234    public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue)
235    {
236        try {
237          if (headerName.equalsIgnoreCase(HEADER_DEPTH)){
238             if ("infinity".equalsIgnoreCase(headerValue)) {
239                setDepth(DEPTH_INFINITY);
240             } else {
241                setDepth(Integer.parseInt(headerValue));
242             }
243           }
244           else if(headerName.equals(HEADER_SUBSCRIPTION_ID)) {
245              setSubscriptionId(Integer.parseInt(headerValue));
246           }
247           else if(headerName.equals(HEADER_SUBSCRIPTION_LIFETIME)) {
248              setSubscriptionId(Integer.parseInt(headerValue));
249           }
250           else if(headerName.equals(HEADER_NOTIFICATION_DELAY)) {
251              setNotificationDelay(Long.parseLong(headerValue));
252           }
253           else {
254              super.setRequestHeader(headerName, headerValue);
255           }
256       } catch (NumberFormatException JavaDoc e) {
257          throw new IllegalArgumentException JavaDoc("Invalid header value '" +
258                headerValue + "' for header " + headerName + "!");
259       }
260    }
261    
262    protected void processResponseHeaders(HttpState state, HttpConnection conn)
263    {
264       super.processResponseHeaders(state, conn);
265       
266       Header header;
267       
268       header = getResponseHeader(HEADER_SUBSCRIPTION_ID);
269       if (header != null) {
270          this.responsedSubscriptionId = Integer.parseInt(header.getValue());
271       }
272       
273       header = getResponseHeader(HEADER_SUBSCRIPTION_LIFETIME);
274       if (header != null) {
275          this.responsedSubscriptionLifetime = Long.parseLong(header.getValue());
276       }
277       
278       header = getResponseHeader(HEADER_CONTENT_LOCATION);
279       if (header != null) {
280          this.responsedContentLocation = header.getValue();
281       }
282    }
283 }
284
Popular Tags