KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.util.Vector JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.io.StringWriter JavaDoc;
20 import java.io.StringReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.StreamTokenizer JavaDoc;
23
24 /** A Precondition represents some condition or collection of conditions
25  * representing states of a resource. If the state of the resource does not
26  * match any of the specified states in any of the conditions, then the
27  * method must fail. Conditions in a Precondition are OR'd together while
28  * ConditionFactors in a ConditionTerm are AND'ed.
29  * @author Jim Amsden <jamsden@us.ibm.com>
30  * @see com.ibm.webdav.Precondition
31  * @see com.ibm.webdav.ConditionTerm
32  * @see com.ibm.webdav.ConditionFactor
33  * @see com.ibm.webdav.EntityTag
34  * @see com.ibm.webdav.StateToken
35  */

36 public class Precondition
37 {
38    private Vector JavaDoc conditions = new Vector JavaDoc();
39    /** Construct an empty Precondition. The client must add Conditions.
40     */

41    public Precondition()
42    {
43    }
44    /** Construct a Precondition by parsing the given If header as defined by
45     * section 8.4 in the WebDAV spec.
46     * @param ifHeader the contents of a WebDAV If header
47     */

48    public Precondition(String JavaDoc ifHeader) throws WebDAVException
49    {
50       StreamTokenizer JavaDoc tokenizer = new StreamTokenizer JavaDoc(new StringReader JavaDoc(ifHeader));
51       // URI characters
52
tokenizer.wordChars('!', '/');
53       tokenizer.wordChars(':', '@');
54           tokenizer.wordChars('[', '_');
55       tokenizer.ordinaryChar('(');
56       tokenizer.ordinaryChar(')');
57       tokenizer.ordinaryChar('<');
58       tokenizer.ordinaryChar('>');
59       tokenizer.ordinaryChar('[');
60       tokenizer.ordinaryChar(']');
61
62       tokenizer.quoteChar('"');
63
64       int token = 0;
65       try {
66          token = tokenizer.nextToken();
67          switch (token) {
68          case '<': {
69             while (token == '<') {
70                addCondition(Condition.create(tokenizer));
71                token = tokenizer.ttype;
72             }
73             break;
74          }
75          case '(': {
76             while (token == '(') {
77                addCondition(Condition.create(tokenizer));
78                token = tokenizer.ttype;
79             }
80             break;
81          }
82          }
83          if (token != StreamTokenizer.TT_EOF) {
84             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: "+(char)token+" expected: EOF");
85          }
86          if (!getConditions().hasMoreElements()) {
87             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Syntax error in If header: list is empty: "+ifHeader);
88          }
89       } catch (IOException JavaDoc exc) {
90       }
91    }
92 /** Add a Condition to this Precondition. Conditions are OR'd together to
93  * check for a matching resource.
94  * @param condition the Condition to add
95  * @exception com.ibm.webdav.WebDAVException thrown if the precondition already contains this condition
96 */

97 public void addCondition(Condition condition) throws WebDAVException {
98     // a Resource URI can only be specified once in a Precondition
99
Enumeration JavaDoc conditions = getConditions();
100     if (condition.getResourceURI() != null) {
101         while (conditions.hasMoreElements()) {
102             Condition existingCondition = (Condition) conditions.nextElement();
103             if (existingCondition.getResourceURI() != null && existingCondition.getResourceURI().equals(condition.getResourceURI())) {
104                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, condition.getResourceURI() + " cannot be specified more than once in an If header");
105             }
106         }
107     }
108     this.conditions.addElement(condition);
109 }
110 /** Add a condition created from the given URI and state token. This is a
111  * convenience method used primarily to create preconditions for lock tokens
112  * that must be provided in the resource context for methods that update
113  * the resource.
114  *
115  * @param resourceURI the URI of the resource the state token applies to. Null
116  * implicitly specifies the resource processing the request
117  * @param stateToken the state token to match
118  */

119 public void addStateTokenCondition(String JavaDoc resourceURI, String JavaDoc stateToken) throws WebDAVException {
120     Condition condition = new Condition(resourceURI);
121     ConditionTerm term = new ConditionTerm();
122     term.addConditionFactor(new StateToken(stateToken));
123     condition.addConditionTerm(term);
124     addCondition(condition);
125 }
126 /** Construct a Precondition by parsing the given If header as defined by
127  * section 9.4 in the WebDAV spec.
128  * @param ifHeader the contents of a WebDAV If header
129  * @return the parser If header
130  * @exception com.ibm.webdav.WebDAVException thrown if there is a syntax error in the If header
131  */

132 public static Precondition create(String JavaDoc ifHeader) throws WebDAVException {
133     return new Precondition(ifHeader);
134 }
135 /** Get the Conditions contained in this Precondition. At least one must match
136  * in order for a valid match to occur.
137  * @return an Enumeration of Conditions
138  */

139 public Enumeration JavaDoc getConditions() {
140     return conditions.elements();
141 }
142 /** See if this Precondition contains a matching Condition.
143  * @param condition the condition to match
144  * @return true if this precondition contains atleast one condition matching the given condition
145  */

146 public boolean matches(Condition condition) {
147     boolean match = false;
148     Enumeration JavaDoc conditions = getConditions();
149     while (!match && conditions.hasMoreElements()) {
150         Condition existingCondition = (Condition) conditions.nextElement();
151         match = existingCondition.matches(condition);
152     }
153     return match;
154 }
155 /** Return a String representation of this Precondition as defined by section 9.4
156  * of the WebDAV Spec. The string is the value of an If header.
157  * @return a string representation of this precondition
158  */

159 public String JavaDoc toString() {
160     StringWriter JavaDoc os = new StringWriter JavaDoc();
161     Enumeration JavaDoc conditions = getConditions();
162     while (conditions.hasMoreElements()) {
163         Condition condition = (Condition) conditions.nextElement();
164         os.write(condition.toString());
165         if (conditions.hasMoreElements()) {
166             os.write(' ');
167         }
168     }
169     try {
170         os.close();
171     } catch (Exception JavaDoc exc) {
172     }
173     return os.toString();
174 }
175 }
176
Popular Tags