1 package com.ibm.webdav; 2 3 15 import java.io.StringWriter ; 16 import java.io.IOException ; 17 import java.io.StreamTokenizer ; 18 19 28 public class EntityTag extends ConditionFactor { 29 30 private static int bcnt = 0; 31 private static String basetime = Long.toHexString( new java.util.Date ().getTime() ); 32 33 private String eTag = null; private boolean weak = false; 35 37 private EntityTag() { 38 } 39 42 public EntityTag(String tag) { 43 this.eTag = tag; 44 } 45 52 public static ConditionFactor create(StreamTokenizer tokenizer) throws WebDAVException { 53 EntityTag entityTag = new EntityTag(); 54 try { 55 int token = tokenizer.ttype; 56 if (token == '[') { 57 token = tokenizer.nextToken(); 58 } else { 59 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: ["); 60 } 61 if (token == '"') { 62 entityTag.setETag(tokenizer.sval); 63 token = tokenizer.nextToken(); 64 } else { 65 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected a quoted string"); 66 } 67 if (token == ']') { 68 token = tokenizer.nextToken(); 69 } else { 70 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: ]"); 71 } 72 } catch (IOException exc) { 73 } 74 return entityTag; 75 } 76 80 public boolean equals(Object etag) { 81 return etag != null && etag instanceof EntityTag && 82 getETag().equals(((EntityTag) etag).getETag()); 83 } 84 88 public static EntityTag generateEntityTag() { 89 String xx = basetime + ":" + Integer.toHexString(Thread.currentThread().hashCode()); 90 bcnt++; 91 xx += ":" + bcnt; 92 return new EntityTag(xx); 93 } 94 98 public String getETag() { 99 return eTag; 100 } 101 104 public boolean isWeak() { 105 return weak; 106 } 107 111 public void setETag(String value) { 112 eTag = value; 113 } 114 117 public void setWeak(boolean value) { 118 weak = value; 119 } 120 124 public String toString() { 125 StringWriter os = new StringWriter (); 126 if (not()) { 127 os.write("Not "); 128 } 129 if (isWeak()) { 130 os.write("W/"); 131 } 132 os.write("[\""); 133 os.write(getETag()); 134 os.write("\"]"); 135 try { 136 os.close(); 137 } catch (Exception exc) { 138 } 139 return os.toString(); 140 } 141 } 142 | Popular Tags |