1 23 24 package org.apache.slide.webdav.event; 25 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.StringTokenizer ; 29 import java.util.TimerTask ; 30 31 import org.apache.slide.event.ResourceEvent; 32 import org.apache.slide.webdav.method.AbstractWebdavMethod; 33 34 37 public class Subscriber { 38 protected static final String LOG_CHANNEL = Subscriber.class.getName(); 39 final static String UPDATE = "Update"; 40 final static String DELETE = "Delete"; 41 final static String MOVE = "Move"; 42 final static String NEW_MEMBER = "Update/newmember"; 43 final static String NEW_MAIL = "pragma/<http://schemas.microsoft.com/exchange/newmail>"; 44 45 private String callback; 46 private String notificationType, uri; 47 private int depth, notificationDelay, subscriptionLifetime, id; 48 private long subscriptionEnd; 49 private List events = new ArrayList (); 50 private TimerTask lifetime, notify; 51 52 public Subscriber(String uri, String callback, String notificationType, int notificationDelay, int subscriptionLifetime, int depth) { 53 this.callback = callback; 54 this.notificationType = notificationType; 55 this.notificationDelay = notificationDelay; 56 this.subscriptionLifetime = subscriptionLifetime; 57 this.subscriptionEnd = System.currentTimeMillis() + subscriptionLifetime*1000; 58 this.uri = uri; 59 this.depth = depth; 60 } 61 62 public void addEvent(ResourceEvent event) { 63 events.add(event); 64 } 65 66 public List getEvents() { 67 return events; 68 } 69 70 public void clearEvents() { 71 events = new ArrayList (); 72 } 73 74 public TimerTask getLifetime() { 75 return lifetime; 76 } 77 78 public long getSubscriptionEnd() { 79 return subscriptionEnd; 80 } 81 82 public void setLifetime(TimerTask lifetime) { 83 this.lifetime = lifetime; 84 } 85 86 public TimerTask getNotify() { 87 return notify; 88 } 89 90 public void setNotify(TimerTask notify) { 91 this.notify = notify; 92 } 93 94 public int getDepth() { 95 return depth; 96 } 97 98 public String getUri() { 99 return uri; 100 } 101 102 public boolean matches(String type, ResourceEvent event) { 103 if ( type.equalsIgnoreCase(notificationType) || 107 ( type.equalsIgnoreCase(NEW_MEMBER) && notificationType.equalsIgnoreCase(UPDATE) && depth > 0 ) || 108 ( type.equalsIgnoreCase(DELETE) && notificationType.equalsIgnoreCase(UPDATE) && depth > 0 ) ){ 109 String eventUri = event.getUri(); 110 if ( eventUri != null && uri != null ) { 111 if ( depth == 0 && eventUri.equals(uri.toString()) ) return true; 112 if ( depth == AbstractWebdavMethod.INFINITY && eventUri.startsWith(uri.toString()) ) return true; 113 if ( eventUri.startsWith(uri.toString() )) { 114 String subpath = eventUri.substring(uri.toString().length()); 115 StringTokenizer tokenizer = new StringTokenizer (subpath, "/"); 116 if ( tokenizer.countTokens() <= depth ) return true; 117 } 118 } 119 } 120 return false; 121 } 122 123 public boolean hasCallback() { 124 return this.callback != null && this.callback.length() > 0; 125 } 126 127 public String getCallback() { 128 return callback; 129 } 130 131 public String getNotificationType() { 132 return notificationType; 133 } 134 135 public int getNotificationDelay() { 136 return notificationDelay; 137 } 138 139 public void setSubscriptionLifetime(int subscriptionLifetime) { 140 this.subscriptionLifetime = subscriptionLifetime; 141 } 142 143 public int getSubscriptionLifetime() { 144 return subscriptionLifetime; 145 } 146 147 public int getId() { 148 return id; 149 } 150 151 public void setId(int id) { 152 this.id = id; 153 } 154 } | Popular Tags |