KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
16 import java.util.*;
17
18 /** A Condition represents some state configuration of a particular resource that must be
19  * satisfied in order for the associated request to be valid. At least one of
20  * the ConditionTerms in a Condition must match with states of the resource, i.e.,
21  * they are OR'd together. Conditions are contained in a Precondition which is used in a
22  * WebDAV If header.
23  * @author Jim Amsden <jamsden@us.ibm.com>
24  * @see com.ibm.webdav.Precondition
25  * @see com.ibm.webdav.ConditionFactor
26  * @see com.ibm.webdav.ConditionTerm
27  * @see com.ibm.webdav.EntityTag
28  * @see com.ibm.webdav.StateToken
29  */

30 public class Condition {
31
32    private String JavaDoc uri = null;
33    private Vector conditionTerms = new Vector();
34 /** Construct a Condition on the default resource.
35  */

36 public Condition() {
37 }
38 /** Construct a Condition with the given URI.
39  * @param uri the URI of the resource associated with this condition
40  */

41 public Condition(String JavaDoc uri) {
42     this.uri = uri;
43 }
44 /** Add a ConditionTerm to a Condition.
45  * @param term the term to add
46  */

47 public void addConditionTerm(ConditionTerm term) throws WebDAVException {
48     conditionTerms.addElement(term);
49 }
50 /** Does this Condition contain the given ConditionTerm?
51  * @param term the term to check for
52  * @return true if the condition contains the given term, false otherwise
53  */

54 public boolean contains(ConditionTerm term) {
55     // iterate through the factors looking for a match
56
boolean match = false;
57     Enumeration terms = getConditionTerms();
58     while (!match && terms.hasMoreElements()) {
59         ConditionTerm t = (ConditionTerm) terms.nextElement();
60         match = term.matches(t);
61     }
62     return match;
63 }
64 /** Create a Condition by parsing the given If header as defined by
65  * section 9.4 in the WebDAV spec.
66  *
67  * @param tokenizer a StreamTokenizer on the contents of a WebDAV If header
68  * @return the parsed condition
69  */

70 public static Condition create(StreamTokenizer tokenizer) throws WebDAVException {
71     Condition condition = new Condition();
72     try {
73         int token = tokenizer.ttype;
74         if (token == '<') {
75             token = tokenizer.nextToken();
76             if (token == StreamTokenizer.TT_WORD) {
77                 condition.setResourceURI(tokenizer.sval);
78             } else {
79                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: missing resource URI");
80             }
81             token = tokenizer.nextToken();
82             if (token == '>') {
83                 token = tokenizer.nextToken();
84             } else {
85                 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: >");
86             }
87         }
88         if (token == '(') {
89             while (token == '(') {
90                 condition.addConditionTerm(ConditionTerm.create(tokenizer));
91                 token = tokenizer.ttype;
92             }
93         } else {
94             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: ( or <");
95         }
96     } catch (IOException exc) {
97     }
98     return condition;
99 }
100 /** Create a Condition by parsing the given If header as defined by
101  * section 9.4 in the WebDAV spec.
102  *
103  * @param ifHeader the contents of a WebDAV If header
104  * @return the parsed condition
105  * @exception com.ibm.webdav.WebDAVException thrown if there is a syntax error in the header
106  */

107 public static Condition create(String JavaDoc ifHeader) throws WebDAVException {
108     StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(ifHeader));
109     // URI characters
110
tokenizer.wordChars('!', '/');
111     tokenizer.wordChars(':', '@');
112     tokenizer.ordinaryChar('(');
113     tokenizer.ordinaryChar(')');
114     tokenizer.ordinaryChar('<');
115     tokenizer.ordinaryChar('>');
116     tokenizer.ordinaryChar('[');
117     tokenizer.ordinaryChar(']');
118     tokenizer.quoteChar('"');
119     Condition condition = null;
120     try {
121         int token = tokenizer.nextToken();
122         condition = Condition.create(tokenizer);
123         token = tokenizer.ttype;
124         if (token != StreamTokenizer.TT_EOF) {
125             throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Error parsing If header: saw: " + (char) token + " expected: EOF");
126         }
127     } catch (IOException exc) {
128     }
129     return condition;
130 }
131 /** Get all the ConditionTerms for this Condition. At least one of the ConditionTerms in
132  * a Condition must match with states of the resource, i.e., they are OR'd
133  * together. Conditions are contained in a Precondition which is used in a
134  * WebDAV If header.
135  */

136 public Enumeration getConditionTerms() {
137     return conditionTerms.elements();
138 }
139 /** Get the URI of the associated Resource. The condition must match on this
140  * resource. This is useful for Preconditions that span multiple resources.
141  * @return the resource URI whose state is described by this Condition, may be null
142  * indicating the condition applies to the resource receiving the request
143  */

144 public String JavaDoc getResourceURI() {
145     return uri;
146 }
147 /** See if this Condition matches the given Condition. This is an
148  * OR operation.
149  * @param condition the condition to match against
150  * @return true if the conditions match, false otherwise.
151  */

152 public boolean matches(Condition condition) {
153     // check the Resource if one was given
154
boolean match = true;
155     if (uri != null) {
156         try {
157             /* Don't match on the protocol, host, and port, they don't really matter.
158              * If we're here on this server, then the client must have provided
159              * a protocol, host, and port that gets to a resource this server manages.
160              * So we shouldn't interpret these URIs as URLs, but rather as opaque
161              * strings that identify the resource on this server
162              */

163             /*
164             URL url1 = new URL(uri);
165             URL url2 = new URL(condition.getResourceURI());
166             match = match && url1.getProtocol().equals(url2.getProtocol());
167             match = match && url1.getHost().equals(url2.getHost());
168             int port1 = url1.getPort();
169             if (port1 == -1) { // use the default port
170                 port1 = 80;
171             }
172             int port2 = url2.getPort();
173             if (port2 == -1) {
174                 port2 = 80;
175             }
176             match = match && (port1 == port2);
177             match = match && url1.getFile().equals(url2.getFile());
178             */

179             match = uri.equals(condition.getResourceURI());
180         } catch (Exception JavaDoc exc) {
181             match = false;
182         }
183     }
184     if (!match) {
185         return false;
186     }
187     // is each term in the condition in the given condition
188
match = false;
189     Enumeration terms = getConditionTerms();
190     while (!match && terms.hasMoreElements()) {
191         ConditionTerm term = (ConditionTerm) terms.nextElement();
192         match = condition.contains(term);
193     }
194     return match;
195 }
196 /** Set the URI of the associated Resource. The condition must match on this
197  * resource. This is useful for Preconditions that span multiple resources.
198  * @param value the resource URI whose state is described by this Condition.
199  * value can be null if the condition applies to the resource executing
200  * the method.
201  */

202 public void setResourceURI(String JavaDoc value) {
203     uri = value;
204 }
205 /** Return a String representation of this Condition as defined by section 9.4
206  * of the WebDAV Spec.
207  * @return a String representation of this condition
208  */

209 public String JavaDoc toString() {
210     StringWriter os = new StringWriter();
211     if (getResourceURI() != null) {
212         os.write('<');
213         os.write(getResourceURI());
214         os.write("> ");
215     }
216     Enumeration terms = getConditionTerms();
217     while (terms.hasMoreElements()) {
218         ConditionTerm term = (ConditionTerm) terms.nextElement();
219         os.write(term.toString());
220         if (terms.hasMoreElements()) {
221             os.write(' ');
222         }
223     }
224     try {
225         os.close();
226     } catch (Exception JavaDoc exc) {
227     }
228     return os.toString();
229 }
230 }
231
Popular Tags