KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/UnsubscribeMethod.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 package org.apache.webdav.lib.methods;
24
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import org.apache.commons.httpclient.HttpConnection;
32 import org.apache.commons.httpclient.HttpException;
33 import org.apache.commons.httpclient.HttpState;
34
35
36 /**
37  * Implements the UNSUBSCRIBE method.
38  *
39  * @see <a HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_unsubscribe.asp">Reference</a>
40  */

41 public class UnsubscribeMethod extends XMLResponseMethodBase
42 {
43    private static final String JavaDoc HEADER_SUBSCRIPTION_ID = "Subscription-Id";
44    
45    private List JavaDoc subscriptionIds = new ArrayList JavaDoc();
46    
47    public UnsubscribeMethod() {
48    }
49    
50    public UnsubscribeMethod(String JavaDoc path) {
51       super(path);
52    }
53    
54    /**
55     * Adds an ID for a subscription that is to be withdrawn.
56     */

57    public void addSubscriptionId(int id) {
58       this.subscriptionIds.add(new Integer JavaDoc(id));
59    }
60
61    // --------------------------------------------------- WebdavMethod Methods
62

63    public String JavaDoc getName()
64    {
65       return "UNSUBSCRIBE";
66    }
67    
68    public void recycle()
69    {
70       super.recycle();
71       this.subscriptionIds.clear();
72    }
73    
74    protected void addRequestHeaders(HttpState state, HttpConnection conn)
75       throws IOException JavaDoc, HttpException
76    {
77       super.addRequestHeaders(state, conn);
78       if (this.subscriptionIds.size() > 0) {
79          StringBuffer JavaDoc b = new StringBuffer JavaDoc();
80          boolean first = true;
81          for (Iterator JavaDoc i = this.subscriptionIds.iterator(); i.hasNext();) {
82             if (first) first = false; else b.append(", ");
83             b.append(i.next());
84          }
85          super.addRequestHeader(HEADER_SUBSCRIPTION_ID, b.toString());
86       }
87    }
88
89    /**
90     * Adds special checking of header values of the UNSUBSCRIBE method to
91     * the super class implementation.
92     */

93    public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue)
94    {
95       if (headerName.equalsIgnoreCase(HEADER_SUBSCRIPTION_ID)){
96          StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(headerValue, ", ");
97          try {
98             for(;t.hasMoreTokens();) {
99                addSubscriptionId(Integer.parseInt(t.nextToken()));
100             }
101          } catch (NumberFormatException JavaDoc e) {
102             throw new IllegalArgumentException JavaDoc("Invalid header value '" +
103                   headerValue + "' for header " + headerName + "!");
104          }
105       } else {
106          super.setRequestHeader(headerName, headerValue);
107       }
108    }
109 }
110
Popular Tags