001: package com.ibm.webdav;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: */
015: import java.io.StringWriter;
016: import java.io.IOException;
017: import java.io.StreamTokenizer;
018:
019: /** An EntityTag is a ConditionFactor describing some state of a resource represented
020: * as an opaque string. See section 3.11 of the HTTP/1.1 spec.
021: * @author Jim Amsden <jamsden@us.ibm.com>
022: * @see com.ibm.webdav.Precondition
023: * @see com.ibm.webdav.ConditionFactor
024: * @see com.ibm.webdav.ConditionTerm
025: * @see com.ibm.webdav.EntityTag
026: * @see com.ibm.webdav.StateToken
027: */
028: public class EntityTag extends ConditionFactor {
029:
030: private static int bcnt = 0;
031: private static String basetime = Long
032: .toHexString(new java.util.Date().getTime());
033:
034: private String eTag = null; // represents some state of a resource expressed as a ETag
035: private boolean weak = false;
036:
037: /** Construct a EntityTag. Should never be called.
038: */
039: private EntityTag() {
040: }
041:
042: /** Construct a EntityTag with the given opaque string tag.
043: * @param tag the opaque string defining the entity tag
044: */
045: public EntityTag(String tag) {
046: this .eTag = tag;
047: }
048:
049: /** Create an EntityTag by parsing the given If header as defined by
050: * section 3.11 of the HTTP/1.1 spec.
051: *
052: * @param tokenizer a StreamTokenizer on the contents of a WebDAV If header
053: * @return the parsed ConditionFactor (EntityTag)
054: * @exception com.ibm.webdav.WebDAVException thrown if there is a syntax error in the If header
055: */
056: public static ConditionFactor create(StreamTokenizer tokenizer)
057: throws WebDAVException {
058: EntityTag entityTag = new EntityTag();
059: try {
060: int token = tokenizer.ttype;
061: if (token == '[') {
062: token = tokenizer.nextToken();
063: } else {
064: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
065: "Error parsing If header: saw: " + (char) token
066: + " expected: [");
067: }
068: if (token == '"') {
069: entityTag.setETag(tokenizer.sval);
070: token = tokenizer.nextToken();
071: } else {
072: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
073: "Error parsing If header: saw: " + (char) token
074: + " expected a quoted string");
075: }
076: if (token == ']') {
077: token = tokenizer.nextToken();
078: } else {
079: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
080: "Error parsing If header: saw: " + (char) token
081: + " expected: ]");
082: }
083: } catch (IOException exc) {
084: }
085: return entityTag;
086: }
087:
088: /** Compare with another EntityTag.
089: * @param etag the entity tag to compare
090: * @return true if the tags are equal, false otherwise
091: */
092: public boolean equals(Object etag) {
093: return etag != null && etag instanceof EntityTag
094: && getETag().equals(((EntityTag) etag).getETag());
095: }
096:
097: /** Construct a unique EntityTag. The tag is constructed by concatening the current time with
098: * the current thread's hash code.
099: * @return a unique entity tag that servers may use for any purpose
100: */
101: public static EntityTag generateEntityTag() {
102: String xx = basetime
103: + ":"
104: + Integer
105: .toHexString(Thread.currentThread().hashCode());
106: bcnt++;
107: xx += ":" + bcnt;
108: return new EntityTag(xx);
109: }
110:
111: /** Get the ETag of this EntityTag. The ETag represents some state of the
112: * resource in the containing Condition.
113: * @return the etag
114: */
115: public String getETag() {
116: return eTag;
117: }
118:
119: /** Is this a weak EntityTag?
120: * @return true if this is a weak entity tag
121: */
122: public boolean isWeak() {
123: return weak;
124: }
125:
126: /** Set the ETag of this EntityTag. The ETag represents some state of the
127: * resource in the containing Condition, for example, the lock token.
128: * @value the etag to set
129: */
130: public void setETag(String value) {
131: eTag = value;
132: }
133:
134: /** Set the strength of this EntityTag.
135: * value true indicates this is a weak entity tag
136: */
137: public void setWeak(boolean value) {
138: weak = value;
139: }
140:
141: /** Return a String representation of this EntityTag as defined by the If
142: * header in section 9.4 of the WebDAV spec.
143: * @return a string representation of this entity tag
144: */
145: public String toString() {
146: StringWriter os = new StringWriter();
147: if (not()) {
148: os.write("Not ");
149: }
150: if (isWeak()) {
151: os.write("W/");
152: }
153: os.write("[\"");
154: os.write(getETag());
155: os.write("\"]");
156: try {
157: os.close();
158: } catch (Exception exc) {
159: }
160: return os.toString();
161: }
162: }
|