KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/PollMethod.java,v 1.3 2004/08/02 15:45:48 unico Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/08/02 15:45:48 $
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.io.InputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.apache.commons.httpclient.HttpConnection;
34 import org.apache.commons.httpclient.HttpException;
35 import org.apache.commons.httpclient.HttpState;
36 import org.apache.commons.httpclient.HttpStatus;
37
38 import org.apache.webdav.lib.util.DOMUtils;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41
42
43 /**
44  * Implements the POLL WebDAV method.
45  *
46  * @see <a HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_webdav_poll.asp">Reference</a>
47  */

48 public class PollMethod extends XMLResponseMethodBase
49 {
50    private static final String JavaDoc HEADER_SUBSCRIPTION_ID = "Subscription-Id";
51    private static final String JavaDoc EXCHANGE_NS = "http://schemas.microsoft.com/Exchange/";
52    private List JavaDoc subscriptionIds = new ArrayList JavaDoc();
53    private List JavaDoc subscriptionsWithEvents = new ArrayList JavaDoc();
54    private List JavaDoc subscriptionsWithoutEvents = new ArrayList JavaDoc();
55    
56    
57    public PollMethod() {
58       
59    }
60    
61    public PollMethod(String JavaDoc path) {
62       super(path);
63    }
64    
65    /**
66     * Adds an ID for a subscription that is to be polled. All added subscription
67     * IDs should have the got same Content-Location uri from the SUBSCRIBE method.
68     */

69    public void addSubscriptionId(int id) {
70       checkNotUsed();
71       this.subscriptionIds.add(new Integer JavaDoc(id));
72    }
73    
74    /**
75     * Returns a list of number objects containing the subscription IDs for
76     * subscriptions for which events are reported.
77     * @return Collection of {@link Integer}s
78     */

79    public Collection JavaDoc getSubscriptionsWithEvents() {
80       checkUsed();
81       return this.subscriptionsWithEvents;
82    }
83    /**
84     * Returns a list of number objects containing the subscription IDs for
85     * subscriptions for which <em>NO</em> events are reported.
86     * @return Collection of {@link Integer}s
87     */

88    public Collection JavaDoc getSubscriptionsWithoutEvents() {
89       checkUsed();
90       return this.subscriptionsWithoutEvents;
91    }
92    
93    // --------------------------------------------------- WebdavMethod Methods
94

95    public String JavaDoc getName()
96    {
97       return "POLL";
98    }
99    
100    public void recycle()
101    {
102       super.recycle();
103       this.subscriptionIds.clear();
104    }
105    
106    protected void addRequestHeaders(HttpState state, HttpConnection conn)
107       throws IOException JavaDoc, HttpException
108    {
109       super.addRequestHeaders(state, conn);
110       if (this.subscriptionIds.size() > 0) {
111          StringBuffer JavaDoc b = new StringBuffer JavaDoc();
112          boolean first = true;
113          for (Iterator JavaDoc i = this.subscriptionIds.iterator(); i.hasNext();) {
114             if (first) first = false; else b.append(", ");
115             b.append(i.next());
116          }
117          super.addRequestHeader(HEADER_SUBSCRIPTION_ID, b.toString());
118       }
119    }
120    
121    /**
122     * Adds special checking of header values of the POLL method to
123     * the super class implementation.
124     */

125    public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue)
126    {
127       if (headerName.equalsIgnoreCase(HEADER_SUBSCRIPTION_ID)){
128          StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(headerValue, ", ");
129          try {
130             for(;t.hasMoreTokens();) {
131                addSubscriptionId(Integer.parseInt(t.nextToken()));
132             }
133          } catch (NumberFormatException JavaDoc e) {
134             throw new IllegalArgumentException JavaDoc("Invalid header value '" +
135                   headerValue + "' for header " + headerName + "!");
136          }
137       } else {
138          super.setRequestHeader(headerName, headerValue);
139       }
140    }
141    
142    public void parseResponse(InputStream JavaDoc input, HttpState state,
143          HttpConnection conn) throws IOException JavaDoc, HttpException
144    {
145       int status = getStatusLine().getStatusCode();
146       
147       if (status == HttpStatus.SC_MULTI_STATUS) {
148          parseXMLResponse(input);
149          NodeList JavaDoc list = getResponseDocument().getDocumentElement()
150             .getElementsByTagNameNS("DAV:", "response");
151          
152          for(int i = 0; i < list.getLength(); i++) {
153             Element JavaDoc e = (Element JavaDoc)list.item(i);
154             
155             NodeList JavaDoc s = e.getElementsByTagNameNS("DAV:", "status");
156             if (s.getLength() > 0) {
157                Element JavaDoc response = ((Element JavaDoc)(s.item(0)).getParentNode());
158                String JavaDoc statusText = DOMUtils.getTextValue((Element JavaDoc)s.item(0));
159                if (statusText.indexOf(" 200 ") != -1) {
160                   // polled subscriptions for which *AN* event is fired
161
NodeList JavaDoc p = response.getElementsByTagNameNS(EXCHANGE_NS, "subscriptionID");
162                   if (p.getLength()>0) {
163                      NodeList JavaDoc li = ((Element JavaDoc)p.item(0)).getElementsByTagName("li");
164                      for(int l = 0; l < li.getLength();++l) {
165                         String JavaDoc id = DOMUtils.getTextValue(li.item(i));
166                         this.subscriptionsWithEvents.add(Integer.getInteger(id));
167                      }
168                   }
169                } else
170                if (statusText.indexOf(" 204 ") != -1) {
171                   // polled subscriptions for which *NO* event is fired
172
NodeList JavaDoc p = response.getElementsByTagNameNS(EXCHANGE_NS, "subscriptionID");
173                   if (p.getLength()>0) {
174                      NodeList JavaDoc li = ((Element JavaDoc)p.item(0)).getElementsByTagName("li");
175                      for(int l = 0; l < li.getLength();++l) {
176                         String JavaDoc id = DOMUtils.getTextValue(li.item(i));
177                         this.subscriptionsWithoutEvents.add(Integer.getInteger(id));
178                      }
179                   }
180                }
181             }
182          }
183       }
184    }
185 }
186
Popular Tags