KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > EntityTag


1 package com.ibm.webdav;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  */

15 import java.io.StringWriter JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.StreamTokenizer JavaDoc;
18
19 /** An EntityTag is a ConditionFactor describing some state of a resource represented
20  * as an opaque string. See section 3.11 of the HTTP/1.1 spec.
21  * @author Jim Amsden <jamsden@us.ibm.com>
22  * @see com.ibm.webdav.Precondition
23  * @see com.ibm.webdav.ConditionFactor
24  * @see com.ibm.webdav.ConditionTerm
25  * @see com.ibm.webdav.EntityTag
26  * @see com.ibm.webdav.StateToken
27  */

28 public class EntityTag extends ConditionFactor {
29
30    private static int bcnt = 0;
31    private static String JavaDoc basetime = Long.toHexString( new java.util.Date JavaDoc().getTime() );
32
33    private String JavaDoc eTag = null; // represents some state of a resource expressed as a ETag
34
private boolean weak = false;
35 /** Construct a EntityTag. Should never be called.
36  */

37 private EntityTag() {
38 }
39 /** Construct a EntityTag with the given opaque string tag.
40  * @param tag the opaque string defining the entity tag
41  */

42 public EntityTag(String JavaDoc tag) {
43     this.eTag = tag;
44 }
45 /** Create an EntityTag by parsing the given If header as defined by
46  * section 3.11 of the HTTP/1.1 spec.
47  *
48  * @param tokenizer a StreamTokenizer on the contents of a WebDAV If header
49  * @return the parsed ConditionFactor (EntityTag)
50  * @exception com.ibm.webdav.WebDAVException thrown if there is a syntax error in the If header
51  */

52 public static ConditionFactor create(StreamTokenizer JavaDoc 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 JavaDoc exc) {
73     }
74     return entityTag;
75 }
76 /** Compare with another EntityTag.
77  * @param etag the entity tag to compare
78  * @return true if the tags are equal, false otherwise
79  */

80 public boolean equals(Object JavaDoc etag) {
81     return etag != null && etag instanceof EntityTag &&
82            getETag().equals(((EntityTag) etag).getETag());
83 }
84 /** Construct a unique EntityTag. The tag is constructed by concatening the current time with
85  * the current thread's hash code.
86  * @return a unique entity tag that servers may use for any purpose
87  */

88 public static EntityTag generateEntityTag() {
89     String JavaDoc xx = basetime + ":" + Integer.toHexString(Thread.currentThread().hashCode());
90     bcnt++;
91     xx += ":" + bcnt;
92     return new EntityTag(xx);
93 }
94 /** Get the ETag of this EntityTag. The ETag represents some state of the
95  * resource in the containing Condition.
96  * @return the etag
97  */

98 public String JavaDoc getETag() {
99     return eTag;
100 }
101 /** Is this a weak EntityTag?
102  * @return true if this is a weak entity tag
103  */

104 public boolean isWeak() {
105     return weak;
106 }
107 /** Set the ETag of this EntityTag. The ETag represents some state of the
108  * resource in the containing Condition, for example, the lock token.
109  * @value the etag to set
110  */

111 public void setETag(String JavaDoc value) {
112     eTag = value;
113 }
114 /** Set the strength of this EntityTag.
115  * value true indicates this is a weak entity tag
116  */

117 public void setWeak(boolean value) {
118     weak = value;
119 }
120 /** Return a String representation of this EntityTag as defined by the If
121  * header in section 9.4 of the WebDAV spec.
122  * @return a string representation of this entity tag
123  */

124 public String JavaDoc toString() {
125     StringWriter JavaDoc os = new StringWriter JavaDoc();
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 JavaDoc exc) {
138     }
139     return os.toString();
140 }
141 }
142
Popular Tags