01: /*
02: * RestrictClientHandler.java
03: *
04: * Brazil project web application Framework,
05: * export version: 1.1
06: * Copyright (c) 2000 Sun Microsystems, Inc.
07: *
08: * Sun Public License Notice
09: *
10: * The contents of this file are subject to the Sun Public License Version
11: * 1.0 (the "License"). You may not use this file except in compliance with
12: * the License. A copy of the License is included as the file "license.terms",
13: * and also available at http://www.sun.com/
14: *
15: * The Original Code is from:
16: * Brazil project web application Framework release 1.1.
17: * The Initial Developer of the Original Code is: suhler.
18: * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
19: * All Rights Reserved.
20: *
21: * Contributor(s): suhler.
22: *
23: * Version: 1.1
24: * Created by suhler on 00/12/21
25: * Last modified by suhler on 00/12/21 11:33:58
26: */
27:
28: package sunlabs.brazil.handler;
29:
30: import java.io.IOException;
31: import sunlabs.brazil.server.Handler;
32: import sunlabs.brazil.server.Request;
33: import sunlabs.brazil.server.Server;
34: import sunlabs.brazil.util.regexp.Regexp;
35:
36: /**
37: * Simple access control hander based on source ip addresses.
38: * Compare the ip address of the client with a regular expression.
39: * Only allow access to the specified url prefix if there is a match.
40: * <p>
41: * Properties:
42: * <dl class=props>
43: * <dt>prefix <dd>The URL prefix that triggers this handler
44: * <dt>match <dd>The regular expression that matches the
45: * ip addresses of clients (in xxx.xxx.xxx.xxx format)
46: * that are permitted to access url's starting with
47: * <code>prefix</code>.
48: * </dl>
49: *
50: * @author Stephen Uhler
51: * @version 1.1, 00/12/21
52: */
53:
54: public class RestrictClientHandler implements Handler {
55: String propsPrefix; // our name in the properties file
56: String urlPrefix; // our own prefix
57: Regexp re; // url to match ip addresses
58:
59: public boolean init(Server server, String prefix) {
60: propsPrefix = prefix;
61: urlPrefix = server.props.getProperty(propsPrefix + "prefix",
62: "/");
63: try {
64: re = new Regexp(server.props.getProperty(propsPrefix
65: + "match"));
66: } catch (Exception e) {
67: server
68: .log(Server.LOG_WARNING, prefix,
69: "Invalid or missing regular expression for \"match\"");
70: return false;
71: }
72: return true;
73: }
74:
75: public boolean respond(Request request) throws IOException {
76: if (!request.url.startsWith(urlPrefix)) {
77: return false;
78: }
79: String ip = ""
80: + request.getSocket().getInetAddress().getHostAddress();
81: if (re.match(ip) != null) {
82: request.log(Server.LOG_WARNING, propsPrefix, "Allowing: "
83: + ip);
84: return false;
85: } else {
86: request.sendError(403, ip + " is not authorized to obtain "
87: + urlPrefix);
88: return true;
89: }
90: }
91: }
|