| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DefaultUserManager |
|
| 1.25;1.25 |
| 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 | 0 | public class DefaultUserManager implements UserManager { |
| 17 | 0 | 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 | 0 | log.info("Initializing the DefaultUserManager."); |
| 27 | 0 | } |
| 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 | 0 | Person user = null; |
| 38 | ||
| 39 | 0 | if (req.getRemoteUser() != null) { |
| 40 | 0 | user = new Person(); |
| 41 | 0 | user.setId(req.getRemoteUser()); |
| 42 | 0 | user.setUri(getServerRoot(req) + "/id/user/" + req.getRemoteUser()); |
| 43 | 0 | user.setName(req.getRemoteUser()); |
| 44 | } | |
| 45 | ||
| 46 | 0 | 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 | 0 | } |
| 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 | 0 | String[] parts = req.getRequestURL().toString().split("/"); |
| 68 | 0 | return parts[0] + "//" + parts[2]; |
| 69 | } | |
| 70 | } |