KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > http > handler > IPAccessHandler


1 // ========================================================================
2
// Author : Gosta Jonasson
3
// Copyright (c) 2002 Gösta Jonasson gosta@kth.se. All rights reserved.
4
// Permission to use, copy, modify and distribute this software
5
// for non-commercial or commercial purposes and without fee is
6
// hereby granted provided that this copyright notice appears in
7
// all copies.
8
// ========================================================================
9

10 package org.mortbay.http.handler;
11
12 import java.io.IOException JavaDoc;
13 import java.util.Hashtable JavaDoc;
14
15 import org.mortbay.http.HttpException;
16 import org.mortbay.http.HttpRequest;
17 import org.mortbay.http.HttpResponse;
18
19 /**
20  * Handler to authenticate access from certain IP-addresses. <br>
21  * <br>
22  * A server configuration-XML-file can look something like this: <br>
23  * &lt;Configure class="org.mortbay.jetty.Server"&gt; <br>
24  * ... <br>
25  * &lt;Call name="addContext"&gt; <br>
26  * ... <br>
27  * &lt;Call name="addHandler"&gt; <br>
28  * &lt;Arg&gt; <br>
29  * &lt;New class="IPAccessHandler"&gt; <br>
30  * &lt;Set name="Standard"&gt;deny&lt;/Set&gt; <br>
31  * &lt;Set name="AllowIP"&gt;192.168.0.103&lt;/Set&gt; <br>
32  * &lt;Set name="AllowIP"&gt;192.168.0.100&lt;/Set&gt; <br>
33  * &lt;/New&gt; <br>
34  * &lt;/Arg&gt; <br>
35  * &lt;/Call&gt; <br>
36  * ... <br>
37  * <br>
38  * This would deny access for everyone except the requests from the IPs 192.168.0.100 and
39  * 192.168.0.103.
40  *
41  * @version v0.1 2002/03/20
42  * @author Gösta Jonasson <a HREF="mailto:gosta@kth.se">gosta@kth.se </a>
43  */

44 public class IPAccessHandler extends AbstractHttpHandler
45 {
46
47     /** The standard way to deal with not configured IPs (true=allowed) */
48     boolean standard = false;
49
50     /** Hashtable where the configured IPs are kept */
51     Hashtable JavaDoc ips;
52
53     /**
54      * Constructor for the class
55      */

56     public IPAccessHandler()
57     {
58         super();
59         ips = new Hashtable JavaDoc();
60     }
61
62     /**
63      * Checks if the given ipstring (x.x.x.x) is authorized or not
64      *
65      * @param ipstring The ip-address as a String
66      * @return True if the IP is allowed access, otherwise false.
67      */

68     public boolean checkIP(String JavaDoc ipstring)
69     {
70         Boolean JavaDoc ipconstrain = (Boolean JavaDoc) ips.get(ipstring);
71         if (ipconstrain != null)
72         {
73             return ipconstrain.booleanValue();
74         }
75         else
76         {
77             return standard;
78         }
79     }
80
81     /**
82      * Handles the incoming request
83      *
84      * @param pathInContext
85      * @param pathParams
86      * @param request The incoming HTTP-request
87      * @param response The outgoing HTTP-response
88      */

89     public void handle(String JavaDoc pathInContext, String JavaDoc pathParams, HttpRequest request,
90             HttpResponse response) throws HttpException, IOException JavaDoc
91     {
92
93         // exempt error pages
94
// TODO This probably should be more general?
95
if (request.getAttribute("javax.servlet.error.status_code") != null) return;
96
97         try
98         {
99
100             String JavaDoc ip = request.getRemoteAddr();
101             boolean authorized = checkIP(ip);
102
103             if (!authorized)
104             {
105                 // The IP is NOT allowed
106
response.sendError(HttpResponse.__403_Forbidden);
107                 request.setHandled(true);
108                 return;
109             }
110             else
111             {
112                 // The IP is allowed
113
return;
114             }
115         }
116         catch (Exception JavaDoc ex)
117         {
118             System.out.println(ex);
119             response.sendError(HttpResponse.__500_Internal_Server_Error);
120             request.setHandled(true);
121         }
122     }
123
124     /**
125      * Allow the given ip-address access
126      *
127      * @param ipstring The ip-address as a String on the format "x.x.x.x"
128      */

129     public void setAllowIP(String JavaDoc ipstring)
130     {
131         ips.put(ipstring, Boolean.TRUE);
132     }
133
134     /**
135      * Deny the given ip-address access
136      *
137      * @param ipstring The ip-address as a String on the format "x.x.x.x"
138      */

139     public void setDenyIP(String JavaDoc ipstring)
140     {
141         ips.put(ipstring, Boolean.FALSE);
142     }
143
144     /**
145      * Set the standard action beeing taken when not registred IPs wants access
146      *
147      * @param s The standard-string (either 'allow' or 'deny')
148      */

149     public void setStandard(String JavaDoc s)
150     {
151         s = s.toLowerCase();
152         if (s.indexOf("allow") > -1)
153         {
154             standard = true;
155         }
156         else
157         {
158             standard = false;
159         }
160     }
161
162     /**
163      * Main method for testing & debugging.
164      *
165      */

166     private static void main(String JavaDoc[] args)
167     {
168         IPAccessHandler ipah = new IPAccessHandler();
169         ipah.setStandard("deny");
170         ipah.setAllowIP("217.215.71.167");
171         ipah.setDenyIP("217.215.71.149");
172         System.out.println(ipah.checkIP("217.215.71.245") + " = false");
173         System.out.println(ipah.checkIP("217.215.71.167") + " = true");
174         System.out.println(ipah.checkIP("217.215.71.149") + " = false");
175         System.out.println(ipah.checkIP("0.0.0.0") + " = false");
176
177         IPAccessHandler ipah2 = new IPAccessHandler();
178         ipah2.setStandard("allow");
179         ipah2.setAllowIP("217.215.71.167");
180         ipah2.setDenyIP("217.215.71.149");
181         System.out.println(ipah2.checkIP("217.215.71.245") + " = true");
182         System.out.println(ipah2.checkIP("217.215.71.167") + " = true");
183         System.out.println(ipah2.checkIP("217.215.71.149") + " = false");
184         System.out.println(ipah2.checkIP("0.0.0.0") + " = true");
185     }
186 }
Popular Tags