View Javadoc

1   package org.restafarian.core.security;
2   
3   import javax.servlet.ServletContext;
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpServletResponse;
6   
7   import org.apache.commons.configuration.Configuration;
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  import org.restafarian.core.beans.Person;
11  
12  /***
13   * <p>This interface specifies the required methods for an authenticated
14   * user manager.</p>
15   */
16  public class DefaultUserManager implements UserManager {
17  	private Log log = LogFactory.getLog(getClass());
18  
19  	/***
20  	 * <p>Initializes the module using the configuration.</p>
21  	 *
22  	 * @param context the <code>ServletContext</code> object
23  	 * @param config the <code>Configuration</code> object
24  	 */
25  	public void init(ServletContext context, Configuration config) {
26  		log.info("Initializing the DefaultUserManager.");
27  	}
28  
29  	/***
30  	 * <p>Returns the currently authenticated user, or null, if there is
31  	 * no user currently authenticated.</p>
32  	 *
33  	 * @param req the <code>HttpServletRequest</code> object
34  	 * @return the currently authenticated user
35  	 */
36  	public Person getAuthenticatedUser(HttpServletRequest req) {
37  		Person user = null;
38  
39  		if (req.getRemoteUser() != null) {
40  			user = new Person();
41  			user.setId(req.getRemoteUser());
42  			user.setUri(getServerRoot(req) + "/id/user/" + req.getRemoteUser());
43  			user.setName(req.getRemoteUser());
44  		}
45  
46  		return user;
47  	}
48  
49  	/***
50  	 * <p>Sets the currently authenticated user.</p>
51  	 *
52  	 * @param req the <code>HttpServletRequest</code> object
53  	 * @param res the <code>HttpServletResponse</code> object
54  	 * @param user the currently authenticated user
55  	 */
56  	public void setAuthenticatedUser(HttpServletRequest req, HttpServletResponse res, Person user) {
57  		// default implementation does nothing
58  	}
59  
60  	/***
61  	 * <p>Computes the root URL of this server.</p>
62  	 *
63  	 * @param req the <code>HttpServletRequest</code> object
64  	 * @return the root URL of this server
65  	 */
66  	private String getServerRoot(HttpServletRequest req) {
67  		String[] parts = req.getRequestURL().toString().split("/");
68  		return parts[0] + "//" + parts[2];
69  	}
70  }