KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > webconnector > ContextFactory


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.webconnector;
20
21 import org.lucane.webconnector.JspHandler;
22 import org.mortbay.http.Authenticator;
23 import org.mortbay.http.HttpContext;
24 import org.mortbay.http.SecurityConstraint;
25 import org.mortbay.http.UserRealm;
26 import org.mortbay.http.handler.NotFoundHandler;
27 import org.mortbay.http.handler.ResourceHandler;
28 import org.mortbay.http.handler.SecurityHandler;
29
30 public class ContextFactory
31 {
32     private UserRealm realm;
33     private Authenticator authenticator;
34     private SecurityConstraint security;
35     
36     public ContextFactory(UserRealm realm, Authenticator auth, SecurityConstraint security)
37     {
38         this.realm = realm;
39         this.authenticator = auth;
40         this.security = security;
41     }
42     
43     public HttpContext createApplicationContext(WebApp app)
44     {
45         HttpContext context = new HttpContext();
46     
47         context.setAttribute(HttpContext.__ErrorHandler, new ErrorPageHandler());
48         context.setClassLoader(ContextFactory.class.getClassLoader());
49         context.setContextPath("/" + app.getName());
50         context.setResourceBase(app.getDirectory() + WebApp.WEB_DIR);
51         context.setRealm(realm);
52         context.setAuthenticator(authenticator);
53         context.addSecurityConstraint("/", security);
54     
55         context.addHandler(new SecurityHandler());
56         context.addHandler(new JspHandler(app));
57         context.addHandler(new ResourceHandler());
58         context.addHandler(new NotFoundHandler());
59         
60         return context;
61     }
62     
63     public HttpContext createImageContext(WebApp app)
64     {
65         HttpContext context = new HttpContext();
66         
67         context.setAttribute(HttpContext.__ErrorHandler, new ErrorPageHandler());
68         context.setContextPath("/" + app.getName() + "/" + WebApp.IMAGES_DIR);
69         context.setResourceBase(app.getDirectory() + WebApp.IMAGES_DIR);
70         
71         context.addHandler(new ResourceHandler());
72         context.addHandler(new NotFoundHandler());
73             
74         return context;
75     }
76 }
Popular Tags