KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > Authenticator


1 /*
2  * Copyright 2005 David M Johnson (For RSS and Atom In Action)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.roller.webservices.adminapi;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import org.apache.roller.RollerException;
20 import org.apache.roller.model.Roller;
21 import org.apache.roller.model.RollerFactory;
22 import org.apache.roller.pojos.UserData;
23
24 /**
25  * TODO
26  *
27  * @author jtb
28  */

29 abstract class Authenticator {
30     private HttpServletRequest JavaDoc request;
31     private Roller roller;
32     private String JavaDoc userName;
33     
34     /** Creates a new instance of HttpBasicAuthenticator */
35     public Authenticator(HttpServletRequest JavaDoc req) {
36         setRequest(req);
37         setRoller(RollerFactory.getRoller());
38     }
39     
40     public abstract void authenticate() throws HandlerException;
41     
42     /**
43      * This method should be called by extensions of this class within their
44      * implementation of authenticate().
45      */

46     protected void verifyUser() throws HandlerException {
47         try {
48             UserData user = getRoller().getUserManager().getUserByUserName(getUserName());
49             if (user != null && user.hasRole("admin") && user.getEnabled().booleanValue()) {
50                 // success! no exception
51
} else {
52                 throw new UnauthorizedException("ERROR: User must have the admin role to use the AAPP endpoint: " + getUserName());
53             }
54         } catch (RollerException re) {
55             throw new InternalException("ERROR: Could not verify user: " + getUserName(), re);
56         }
57     }
58     
59     public HttpServletRequest JavaDoc getRequest() {
60         return request;
61     }
62     
63     protected void setRequest(HttpServletRequest JavaDoc request) {
64         this.request = request;
65     }
66     
67     public String JavaDoc getUserName() {
68         return userName;
69     }
70     
71     protected void setUserName(String JavaDoc userId) {
72         this.userName = userId;
73     }
74     
75     protected Roller getRoller() {
76         return roller;
77     }
78     
79     protected void setRoller(Roller roller) {
80         this.roller = roller;
81     }
82 }
83
Popular Tags