001: // RuleParserException.java
002: // $Id: Rule.java,v 1.8 2000/08/16 21:38:05 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.protocol.http.proxy;
007:
008: import java.net.MalformedURLException;
009: import java.net.URL;
010:
011: import java.io.DataOutputStream;
012: import java.io.IOException;
013:
014: import org.w3c.www.protocol.http.Reply;
015: import org.w3c.www.protocol.http.Request;
016:
017: import org.w3c.www.http.HTTP;
018: import org.w3c.www.http.HttpCredential;
019: import org.w3c.www.http.HttpEntityMessage;
020: import org.w3c.www.http.HttpFactory;
021: import org.w3c.www.http.HttpMessage;
022: import org.w3c.www.http.HttpRequestMessage;
023:
024: import org.w3c.tools.codec.Base64Encoder;
025:
026: import org.w3c.tools.sorter.Comparable;
027:
028: /**
029: * The ForbidRule implements the <code>forbid</code> directive.
030: * Forbid prevents all accesses, by the client (be it a proxy or hotjava)
031: * to a given set of hosts.
032: */
033:
034: class ForbidRule extends Rule {
035:
036: /**
037: * Forbid access to the given request.
038: * @param request The request to apply the rule too.
039: */
040:
041: public Reply apply(Request request) {
042: Reply reply = request.makeReply(HTTP.OK);
043: reply.setContent("<h1>Access forbidden</h1>" + "<p>Access to "
044: + request.getURL()
045: + " is forbidden by proxy dispatcher rules.");
046: reply.setContentType(org.w3c.www.mime.MimeType.TEXT_HTML);
047: return reply;
048: }
049:
050: /**
051: * Initialize a forbid rule.
052: * @param tokens The token array.
053: * @param offset Offset within above array, of tokens to initialize
054: * from.
055: * @param length Total number of tokens in above array.
056: * @exception RuleParserExctpion If the rule couldn't be initialized
057: * from given tokens.
058: */
059:
060: public void initialize(String tokens[], int offset, int length)
061: throws RuleParserException {
062: super .initialize(tokens, offset, length);
063: }
064:
065: public ForbidRule() {
066: name = "forbid";
067: }
068:
069: }
070:
071: class ProxyRule extends Rule {
072: URL proxy = null;
073:
074: /**
075: * Convert a proxy rule to a String.
076: * @return A String instance.
077: */
078:
079: public String toString() {
080: return host + " " + name + " " + proxy;
081: }
082:
083: /**
084: * Set the appropriate proxy for the given requested URL.
085: * @param request The request to apply the rule to.
086: * @return Always <strong>null</strong>, will only hack the given
087: * request
088: * if needed.
089: */
090:
091: public Reply apply(Request request) {
092: if (proxy != null)
093: request.setProxy(proxy);
094: return null;
095: }
096:
097: /**
098: * Initialize that proxy rule.
099: * @param tokens The token array.
100: * @param offset Offset within above array, of tokens to initialize
101: * from.
102: * @param length Total number of tokens in above array.
103: * @exception RuleParserExctpion If the rule couldn't be initialized
104: * from given tokens.
105: */
106:
107: public void initialize(String tokens[], int offset, int length)
108: throws RuleParserException {
109: // We have to get the proxy here
110: if (offset + 1 != length)
111: throw new RuleParserException("No target proxy.");
112: try {
113: args = tokens[offset];
114: proxy = new URL(args);
115: } catch (MalformedURLException ex) {
116: throw new RuleParserException("Invalid target proxy \""
117: + tokens[offset] + "\".");
118: }
119: host = tokens[0];
120:
121: }
122:
123: public ProxyRule() {
124: name = "proxy";
125: }
126:
127: }
128:
129: class RedirectRule extends Rule {
130: URL redirect = null;
131:
132: /**
133: * Convert a redirect rule to a String.
134: * @return A String instance.
135: */
136:
137: public String toString() {
138: return host + " " + name + " " + redirect;
139: }
140:
141: /**
142: * Set the appropriate redirect URL for the given requested URL.
143: * @param request The request to apply the rule to.
144: * @return Always <strong>null</strong>, will only hack the given
145: * request
146: * if needed.
147: */
148:
149: public Reply apply(Request request) {
150: if (redirect != null)
151: request.setURL(redirect);
152: return null;
153: }
154:
155: /**
156: * Initialize that redirect rule.
157: * @param tokens The token array.
158: * @param offset Offset within above array, of tokens to initialize
159: * from.
160: * @param length Total number of tokens in above array.
161: * @exception RuleParserExctpion If the rule couldn't be initialized
162: * from given tokens.
163: */
164:
165: public void initialize(String tokens[], int offset, int length)
166: throws RuleParserException {
167: // We have to get the redirect URL here
168: if (offset + 1 != length)
169: throw new RuleParserException("No target redirect URL.");
170: try {
171: args = tokens[offset];
172: redirect = new URL(args);
173: } catch (MalformedURLException ex) {
174: throw new RuleParserException(
175: "Invalid target redirect URL \"" + tokens[offset]
176: + "\".");
177: }
178: host = tokens[0];
179: }
180:
181: public RedirectRule() {
182: name = "redirect";
183: }
184:
185: }
186:
187: /**
188: * The DirectRule implements the <code>DIRECT</code> directive.
189: * Applying that rule is basically a <em>noop</em>.
190: */
191:
192: class DirectRule extends Rule {
193:
194: public DirectRule() {
195: name = "direct";
196: }
197:
198: }
199:
200: /**
201: * The authorization rule adds Basic credentials to all requests.
202: */
203:
204: class AuthorizationRule extends Rule {
205: /**
206: * The credentials to add to the request.
207: */
208: HttpCredential credential = null;
209:
210: String user = null;
211: String password = null;
212:
213: public String toString() {
214: return host + " " + name + " " + user + " " + password;
215: }
216:
217: /**
218: * Appky this rule to the given request.
219: * @param request The request to apply the rule to.
220: * @return Always <strong>null</strong>.
221: */
222:
223: public Reply apply(Request request) {
224: if (!request.hasHeader(HttpRequestMessage.H_AUTHORIZATION))
225: request.setHeaderValue(HttpRequestMessage.H_AUTHORIZATION,
226: credential);
227: return null;
228: }
229:
230: /**
231: * Initialize this Authorization rule.
232: * @param tokens The token array.
233: * @param offset Offset within above array, of tokens to initialize
234: * from.
235: * @param length Total number of tokens in above array.
236: * @exception RuleParserExctpion If the rule couldn't be initialized
237: * from given tokens.
238: */
239:
240: public void initialize(String tokens[], int offset, int length)
241: throws RuleParserException {
242: if (offset + 2 != length)
243: throw new RuleParserException(
244: "Invalid authorization rule: "
245: + " should be authorization "
246: + " <user> <password>.");
247: credential = HttpFactory.makeCredential("Basic");
248: user = tokens[offset];
249: password = tokens[offset + 1];
250: args = user + " " + password;
251: Base64Encoder base64 = new Base64Encoder(user + ":" + password);
252: credential.setAuthParameter("cookie", base64.processString());
253: host = tokens[0];
254: }
255:
256: public AuthorizationRule() {
257: name = "authorization";
258: }
259:
260: }
261:
262: class ProxyAuthRule extends Rule {
263: URL proxy = null;
264: /**
265: * The credentials to add to the request.
266: */
267: HttpCredential credential = null;
268:
269: String user = null;
270: String password = null;
271:
272: /**
273: * Convert a proxy rule to a String.
274: * @return A String instance.
275: */
276:
277: public String toString() {
278: return host + " " + name + " " + user + " " + password + " "
279: + proxy;
280: }
281:
282: /**
283: * Set the appropriate proxy for the given requested URL.
284: * @param request The request to apply the rule to.
285: * @return Always <strong>null</strong>, will only hack the given
286: * request
287: * if needed.
288: */
289:
290: public Reply apply(Request request) {
291: if (proxy != null)
292: request.setProxy(proxy);
293: if (!request
294: .hasHeader(HttpRequestMessage.H_PROXY_AUTHORIZATION))
295: request.setProxyAuthorization(credential);
296: return null;
297: }
298:
299: /**
300: * Initialize that proxy rule.
301: * @param tokens The token array.
302: * @param offset Offset within above array, of tokens to initialize
303: * from.
304: * @param length Total number of tokens in above array.
305: * @exception RuleParserExctpion If the rule couldn't be initialized
306: * from given tokens.
307: */
308:
309: public void initialize(String tokens[], int offset, int length)
310: throws RuleParserException {
311: // We have to get the proxy here
312: if (offset + 3 != length)
313: throw new RuleParserException("Invalid proxyauth rule: "
314: + " should be authorization "
315: + " <user> <password> <proxy>.");
316: try {
317: user = tokens[offset];
318: password = tokens[offset + 1];
319: credential = HttpFactory.makeCredential("Basic");
320: Base64Encoder base64 = new Base64Encoder(user + ":"
321: + password);
322: credential.setAuthParameter("cookie", base64
323: .processString());
324: proxy = new URL(tokens[offset + 2]);
325: args = user + " " + password + " " + proxy;
326: } catch (MalformedURLException ex) {
327: throw new RuleParserException("Invalid target proxy \""
328: + tokens[offset] + "\".");
329: }
330: host = tokens[0];
331: }
332:
333: public ProxyAuthRule() {
334: name = "proxyauth";
335: }
336:
337: }
338:
339: public class Rule implements Comparable {
340:
341: protected static String names[] = { "direct", "forbid", "proxy",
342: "redirect", "authorization", "proxyauth" };
343:
344: String host = null;
345: String args = null;
346: String name = null;
347:
348: public String toString() {
349: return host + " " + name;
350: }
351:
352: public String getStringValue() {
353: return toString();
354: }
355:
356: public boolean greaterThan(Comparable comp) {
357: return (getStringValue().compareTo(comp.getStringValue()) > 0);
358: }
359:
360: public void writeRule(DataOutputStream out) throws IOException {
361: out.writeBytes(toString() + "\n");
362: }
363:
364: public String getHost() {
365: return host;
366: }
367:
368: public String getRuleName() {
369: return name;
370: }
371:
372: public String getRuleArgs() {
373: return args;
374: }
375:
376: /**
377: * Initialize the rule with given set of tokens.
378: * @param tokens The token array.
379: * @param offset Offset within above array, of tokens to initialize from.
380: * @param length Total number of tokens in above array.
381: * @exception RuleParserException If the rule couldn't be initialized
382: * from given tokens.
383: */
384:
385: protected void initialize(String tokens[], int offset, int length)
386: throws RuleParserException {
387: if (offset != length)
388: throw new RuleParserException("Unexpected token: "
389: + tokens[offset]);
390: host = tokens[0];
391: }
392:
393: /**
394: * Create a rule, given an array of String.
395: * @param tokens Parsed tokens, as a String array.
396: * @param offset Offset of the rule tokens within above array.
397: * @param length Total number of available tokens.
398: * @exception RuleParserException If no rule could be created out of given
399: * tokens.
400: */
401:
402: public static Rule createRule(String tokens[], int offset,
403: int length) throws RuleParserException {
404: Rule rule = null;
405: // Make sure there is something to build:
406: if ((tokens == null) || (length - offset == 0))
407: return null;
408: // Check the rule name:
409: String name = tokens[offset];
410: if (name.equalsIgnoreCase("direct")) {
411: rule = new DirectRule();
412: } else if (name.equalsIgnoreCase("proxy")) {
413: rule = new ProxyRule();
414: } else if (name.equalsIgnoreCase("forbid")) {
415: rule = new ForbidRule();
416: } else if (name.equalsIgnoreCase("redirect")) {
417: rule = new RedirectRule();
418: } else if (name.equalsIgnoreCase("authorization")) {
419: rule = new AuthorizationRule();
420: } else if (name.equalsIgnoreCase("proxyauth")) {
421: rule = new ProxyAuthRule();
422: } else {
423: throw new RuleParserException("Unknown rule name \"" + name
424: + "\"");
425: }
426: rule.initialize(tokens, offset + 1, length);
427: return rule;
428: }
429:
430: public static String[] getRulesName() {
431: return names;
432: }
433:
434: /**
435: * Apply given rule to the given request.
436: * @param request The request to apply the rule to.
437: */
438:
439: public Reply apply(Request request) {
440: return null;
441: }
442:
443: /**
444: * Empty constructor for dynamic instantiation.
445: */
446:
447: public Rule() {
448: }
449:
450: }
|