KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > external > AbstractRequestHandler


1 package freecs.external;
2
3 import java.net.InetAddress JavaDoc;
4 import java.nio.channels.SelectionKey JavaDoc;
5 import java.nio.channels.SocketChannel JavaDoc;
6
7 import freecs.Server;
8 import freecs.content.ContentContainer;
9 import freecs.interfaces.IRequest;
10 import freecs.interfaces.IResponseHeaders;
11 import freecs.util.HttpAuth;
12
13 public abstract class AbstractRequestHandler implements IRequestHandler {
14
15     private String JavaDoc handlerName;
16     
17     /**
18      * construct a new handler mounted to a given name
19      */

20     public AbstractRequestHandler(String JavaDoc handlerName) {
21         this.handlerName = handlerName;
22     }
23     
24     public void checkAccessIp(IRequest req, ContentContainer c) throws AccessForbiddenException {
25         SelectionKey JavaDoc key = req.getKey ();
26         InetAddress JavaDoc ia = null;
27         try {
28             SocketChannel JavaDoc sc = (SocketChannel JavaDoc) key.channel ();
29             ia = sc.socket ().getInetAddress ();
30         } catch (Exception JavaDoc e) {
31             Server.debug (this, "" + ia.toString (), e, Server.MSG_STATE, Server.LVL_MAJOR);
32             throw new AccessForbiddenException(true);
33         }
34         if (!Server.srv.isAdminHost(ia)) {
35             Server.log (this, "access to " + getHandlerName() + " denied for " + ia.toString (), Server.MSG_STATE, Server.LVL_MAJOR);
36             throw new AccessForbiddenException(true);
37         }
38     }
39
40     public void checkAccessAuth(IRequest req, ContentContainer c) throws AccessForbiddenException {
41         if (Server.srv.ADMIN_HTTP_USERNAME == null || Server.srv.ADMIN_HTTP_PASSWORD == null) {
42             Server.log (this, "authentication not properly configured in server config! access denied.", Server.MSG_CONFIG, Server.LVL_MAJOR);
43             // hide page if auth is not configured ok!
44
throw new AccessForbiddenException(true);
45         }
46         HttpAuth auth = HttpAuth.parse(req.getProperty("authorization"));
47         if (auth == null || auth.username == null || auth.password == null ||
48                 !auth.username.equals(Server.srv.ADMIN_HTTP_USERNAME) ||
49                 !auth.password.equals(Server.srv.ADMIN_HTTP_PASSWORD)) {
50             c.setResCode(IResponseHeaders.AUTHENTICATE_CODE);
51             c.wrap("Access denied.");
52             throw new AccessForbiddenException(false);
53         }
54     }
55     
56     /**
57      * returns the string which this handler is mounted to
58      */

59     public String JavaDoc getHandlerName() {
60         return handlerName;
61     }
62     
63 }
64
Popular Tags