diff servlet/src/digilib/auth/AuthOpsImpl.java @ 531:9cedd170b581 digilibPDF

* PDF generation works now even with subdirectories * genericsification and clean up
author robcast
date Fri, 05 Feb 2010 20:58:38 +0100
parents 0ff3ede32060
children e758a49258e8
line wrap: on
line diff
--- a/servlet/src/digilib/auth/AuthOpsImpl.java	Thu Jan 14 14:30:30 2010 +0100
+++ b/servlet/src/digilib/auth/AuthOpsImpl.java	Fri Feb 05 20:58:38 2010 +0100
@@ -20,58 +20,112 @@
 
 package digilib.auth;
 
+import java.util.List;
+
 import javax.servlet.http.HttpServletRequest;
-import java.util.*;
+
+import org.apache.log4j.Logger;
 
-import digilib.*;
+import digilib.servlet.DigilibRequest;
 
+/** Basic implementation of AuthOps interface.
+ *
+ * Provides basic implementations. Only rolesForPath needs to be implemented
+ * by specific implementations.
+ */
 public abstract class AuthOpsImpl implements AuthOps {
 
-  protected Utils util;
-
+	/** general logger for this class */
+	protected Logger logger = Logger.getLogger(this.getClass());
+	
+  /** Default constructor. */  
   public AuthOpsImpl() {
-    util = new Utils();
-    try {
-      init();
-    } catch (AuthOpException e) {
-    }
-  }
-
-  public AuthOpsImpl(Utils u) {
-    util = u;
     try {
       init();
     } catch (AuthOpException e) {
     }
   }
 
+
+  /** Test if the request is allowed to access filepath.
+   * @param filepath filepath to be acessed.
+   * @param request Request with user information.
+   * @throws AuthOpException Exception thrown on error.
+   * @return true if the request is allowed.
+   */
   public boolean isAuthRequired(String filepath, HttpServletRequest request) throws AuthOpException {
     // check permissions
-    List rolesRequired = rolesForPath(filepath, request);
+    List<String> rolesRequired = rolesForPath(filepath, request);
     return (rolesRequired != null);
   }
 
+  /**
+   * @see digilib.auth.AuthOps#isAuthRequired(digilib.servlet.DigilibRequest)
+   */
+  public boolean isAuthRequired(DigilibRequest request)
+	  throws AuthOpException {
+		// check permissions
+		List<String> rolesRequired = rolesForPath(request);
+		return (rolesRequired != null);
+  }
+
+  /** Return authorization roles needed for request.
+   *
+   * Returns a list of authorization roles that would be allowed to access the
+   * specified path. The location information of the request is considered also.
+   * @param filepath filepath to be accessed.
+   * @param request ServletRequest with address information.
+   * @throws AuthOpException Exception thrown on error.
+   * @return List of Strings with role names.
+   */
   public boolean isAuthorized(String filepath, HttpServletRequest request) throws AuthOpException {
-    List rolesAllowed = rolesForPath(filepath, request);
+    List<String> rolesAllowed = rolesForPath(filepath, request);
     return isRoleAuthorized(rolesAllowed, request);
   }
 
-  public boolean isRoleAuthorized(List roles, HttpServletRequest request) {
-    ListIterator r = roles.listIterator();
-    String s = "";
-    while (r.hasNext()) {
-      s = (String)r.next();
-      util.dprintln(5, "Testing role: "+s);
+  /**
+   * @see digilib.auth.AuthOps#isAuthorized(digilib.servlet.DigilibRequest)
+   */
+  public boolean isAuthorized(DigilibRequest request)
+	  throws AuthOpException {
+		List<String> rolesAllowed = rolesForPath(request);
+		return isRoleAuthorized(rolesAllowed, request);
+  }
+
+  /** Test request authorization against a list of roles.
+   * @param roles List of Strings with role names.
+   * @param request ServletRequest with address information.
+   * @return true if the user information in the request authorizes one of the roles.
+   */
+  public boolean isRoleAuthorized(List<String> roles, HttpServletRequest request) {
+    for (String s: roles) {
+      logger.debug("Testing role: "+s);
       if (request.isUserInRole(s)) {
-        util.dprintln(5, "Role Authorized");
+      	logger.debug("Role Authorized");
         return true;
       }
     }
     return false;
   }
 
+  /**
+   * @see digilib.auth.AuthOps#isRoleAuthorized(java.util.List, digilib.servlet.DigilibRequest)
+   */
+  public boolean isRoleAuthorized(List<String> roles, DigilibRequest request) {
+	for (String s: roles) {
+	  logger.debug("Testing role: "+s);
+	  if (((HttpServletRequest)request.getServletRequest()).isUserInRole(s)) {
+	  	logger.debug("Role Authorized");
+		return true;
+	  }
+	}
+	return false;
+  }
+
   public abstract void init() throws AuthOpException;
 
-  public abstract List rolesForPath(String filepath, HttpServletRequest request) throws AuthOpException;
+  public abstract List<String> rolesForPath(String filepath, HttpServletRequest request) throws AuthOpException;
+
+  public abstract List<String> rolesForPath(DigilibRequest request) throws AuthOpException;
 
 }