1 23 package org.apache.webdav.lib.methods; 24 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 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 ; 40 import org.w3c.dom.NodeList ; 41 42 43 48 public class PollMethod extends XMLResponseMethodBase 49 { 50 private static final String HEADER_SUBSCRIPTION_ID = "Subscription-Id"; 51 private static final String EXCHANGE_NS = "http://schemas.microsoft.com/Exchange/"; 52 private List subscriptionIds = new ArrayList (); 53 private List subscriptionsWithEvents = new ArrayList (); 54 private List subscriptionsWithoutEvents = new ArrayList (); 55 56 57 public PollMethod() { 58 59 } 60 61 public PollMethod(String path) { 62 super(path); 63 } 64 65 69 public void addSubscriptionId(int id) { 70 checkNotUsed(); 71 this.subscriptionIds.add(new Integer (id)); 72 } 73 74 79 public Collection getSubscriptionsWithEvents() { 80 checkUsed(); 81 return this.subscriptionsWithEvents; 82 } 83 88 public Collection getSubscriptionsWithoutEvents() { 89 checkUsed(); 90 return this.subscriptionsWithoutEvents; 91 } 92 93 95 public String 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 , HttpException 108 { 109 super.addRequestHeaders(state, conn); 110 if (this.subscriptionIds.size() > 0) { 111 StringBuffer b = new StringBuffer (); 112 boolean first = true; 113 for (Iterator 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 125 public void setRequestHeader(String headerName, String headerValue) 126 { 127 if (headerName.equalsIgnoreCase(HEADER_SUBSCRIPTION_ID)){ 128 StringTokenizer t = new StringTokenizer (headerValue, ", "); 129 try { 130 for(;t.hasMoreTokens();) { 131 addSubscriptionId(Integer.parseInt(t.nextToken())); 132 } 133 } catch (NumberFormatException e) { 134 throw new IllegalArgumentException ("Invalid header value '" + 135 headerValue + "' for header " + headerName + "!"); 136 } 137 } else { 138 super.setRequestHeader(headerName, headerValue); 139 } 140 } 141 142 public void parseResponse(InputStream input, HttpState state, 143 HttpConnection conn) throws IOException , HttpException 144 { 145 int status = getStatusLine().getStatusCode(); 146 147 if (status == HttpStatus.SC_MULTI_STATUS) { 148 parseXMLResponse(input); 149 NodeList list = getResponseDocument().getDocumentElement() 150 .getElementsByTagNameNS("DAV:", "response"); 151 152 for(int i = 0; i < list.getLength(); i++) { 153 Element e = (Element )list.item(i); 154 155 NodeList s = e.getElementsByTagNameNS("DAV:", "status"); 156 if (s.getLength() > 0) { 157 Element response = ((Element )(s.item(0)).getParentNode()); 158 String statusText = DOMUtils.getTextValue((Element )s.item(0)); 159 if (statusText.indexOf(" 200 ") != -1) { 160 NodeList p = response.getElementsByTagNameNS(EXCHANGE_NS, "subscriptionID"); 162 if (p.getLength()>0) { 163 NodeList li = ((Element )p.item(0)).getElementsByTagName("li"); 164 for(int l = 0; l < li.getLength();++l) { 165 String id = DOMUtils.getTextValue(li.item(i)); 166 this.subscriptionsWithEvents.add(Integer.getInteger(id)); 167 } 168 } 169 } else 170 if (statusText.indexOf(" 204 ") != -1) { 171 NodeList p = response.getElementsByTagNameNS(EXCHANGE_NS, "subscriptionID"); 173 if (p.getLength()>0) { 174 NodeList li = ((Element )p.item(0)).getElementsByTagName("li"); 175 for(int l = 0; l < li.getLength();++l) { 176 String id = DOMUtils.getTextValue(li.item(i)); 177 this.subscriptionsWithoutEvents.add(Integer.getInteger(id)); 178 } 179 } 180 } 181 } 182 } 183 } 184 } 185 } 186 | Popular Tags |