1 23 24 package org.apache.webdav.lib.methods; 25 26 import java.io.IOException ; 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 38 public class SubscribeMethod extends XMLResponseMethodBase 39 implements DepthSupport 40 { 41 42 private static final String HEADER_SUBSCRIPTION_ID = "Subscription-Id"; 43 private static final String HEADER_SUBSCRIPTION_LIFETIME = "Subscription-Lifetime"; 44 private static final String HEADER_NOTIFICATION_TYPE = "Notification-Type"; 45 private static final String HEADER_NOTIFICATION_DELAY = "Notification-Delay"; 46 private static final String HEADER_DEPTH = "Depth"; 47 private static final String HEADER_CALL_BACK = "Call-Back"; 48 private static final String HEADER_CONTENT_LOCATION = "Content-Location"; 49 50 public static final String TYPE_UPDATE = "update"; 51 public static final String TYPE_UPDATE_NEW_MEMBER = "update/newmember"; 52 public static final String TYPE_DELETE = "delete"; 53 public static final String TYPE_MOVE = "move"; 54 55 private String callback = null; 56 private String 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 responsedContentLocation = null; 65 66 public SubscribeMethod() { 67 68 } 69 70 public SubscribeMethod(String path) { 71 super(path); 72 } 73 74 public String getCallback() 75 { 76 return callback; 77 } 78 81 public void setCallback(String callback) 82 { 83 if (callback != null && callback.length() > 0) { 84 this.callback = callback; 85 } 86 } 87 public String getNotificationType() 88 { 89 return notificationType; 90 } 91 99 public void setNotificationType(String notificationType) 100 { 101 this.notificationType = notificationType; 102 } 103 public long getSubsciptionLifetime() 104 { 105 return subsciptionLifetime; 106 } 107 110 public void setSubsciptionLifetime(long subsciptionLifetime) 111 { 112 this.subsciptionLifetime = subsciptionLifetime; 113 } 114 public long getSubscriptionId() 115 { 116 return subscriptionId; 117 } 118 122 public void setSubscriptionId(int subscriptionId) 123 { 124 this.subscriptionId = subscriptionId; 125 } 126 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 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 ( 152 "Depth must be 0, 1 or "+DEPTH_INFINITY+"."); 153 } 154 } 155 156 160 public int getResponsedSubscriptionId() { 161 checkUsed(); 162 return this.responsedSubscriptionId; 163 } 164 168 public long getResponsedSubscriptionLifetime() { 169 checkUsed(); 170 return this.responsedSubscriptionLifetime; 171 } 172 177 public String getResponsedContentLocation() { 178 checkUsed(); 179 return this.responsedContentLocation; 180 } 181 183 public String 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 , 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 234 public void setRequestHeader(String headerName, String 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 e) { 257 throw new IllegalArgumentException ("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 |