Commits

Vineet Reynolds committed 3852233

Added the ResourceFilter to protect "static" Facelet resources used by the application, which should not be exposed to end-users.
Also documented the filters in use by the application.

Comments (0)

Files changed (4)

galleria-jsf/src/main/java/info/galleria/filters/AuthenticationFilter.java

 package info.galleria.filters;
 
 import java.io.IOException;
+import java.security.Principal;
 
 import javax.servlet.*;
 import javax.servlet.http.*;
 
 /**
- * Servlet Filter implementation class AuthenticationFilter
+ * This filter is used to protect the private section of the application from
+ * unauthenticated users.
+ * 
+ * @author Vineet Reynolds
+ * 
  */
 public class AuthenticationFilter implements Filter
 {
 	}
 
 	/**
+	 * Verifies if the request is associated with a {@link Principal}. If no
+	 * {@link Principal} is found, then the user is redirected to the Login page
+	 * of the application. Otherwise, the request is chained to next filter, or
+	 * the web-resource.
+	 * 
 	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 	 */
 	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,

galleria-jsf/src/main/java/info/galleria/filters/ResourceFilter.java

+package info.galleria.filters;
+
+import java.io.IOException;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * This filter is meant to protect facelet resources that are never meant to be
+ * accessed directly by end-users. Facelet resources include stylesheets,
+ * templates and other files that may be present in the document root, and
+ * browseable by end-users.
+ * 
+ * @author Vineet Reynolds
+ * 
+ */
+public class ResourceFilter implements Filter
+{
+
+	private String contextPath;
+
+	/**
+	 * Default constructor.
+	 */
+	public ResourceFilter()
+	{
+	}
+
+	/**
+	 * @see Filter#destroy()
+	 */
+	public void destroy()
+	{
+	}
+
+	/**
+	 * Redirects all requests arriving at the URLs protected by the user, to the
+	 * login page.
+	 * 
+	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
+	 */
+	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
+			ServletException
+	{
+		HttpServletResponse httpResponse = (HttpServletResponse) response;
+		httpResponse.sendRedirect(contextPath + "/Login.xhtml");
+		return;
+	}
+
+	/**
+	 * @see Filter#init(FilterConfig)
+	 */
+	public void init(FilterConfig fConfig) throws ServletException
+	{
+		contextPath = fConfig.getServletContext().getContextPath();
+	}
+
+}

galleria-jsf/src/main/java/info/galleria/filters/UserRedirectFilter.java

 package info.galleria.filters;
 
 import java.io.IOException;
+import java.security.Principal;
 
 import javax.servlet.*;
 import javax.servlet.http.*;
 
 /**
- * Servlet Filter implementation class UserRedirectFilter
+ * This filter is used to redirect authenticated users to the landing page for
+ * authenticated users. This will ensure that users will not be confused when
+ * accessing the Login page amongst others, when already logged in.
+ * 
+ * @author Vineet Reynolds
+ * 
  */
 public class UserRedirectFilter implements Filter
 {
 	}
 
 	/**
+	 * Redirects the user to the landing page of the application for
+	 * authenticated users, if the request is associated with a
+	 * {@link Principal}.
+	 * 
+	 * If no {@link Principal} is found, then the request is chained to the next
+	 * filter or the web-resource.
+	 * 
 	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 	 */
 	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,

galleria-jsf/src/main/webapp/WEB-INF/web.xml

 		<filter-class>info.galleria.filters.AuthenticationFilter</filter-class>
 	</filter>
 	<filter>
+		<filter-name>Facelet Resource Filter</filter-name>
+		<filter-class>info.galleria.filters.ResourceFilter</filter-class>
+	</filter>
+	<filter>
 		<filter-name>PrimeFaces FileUpload Filter</filter-name>
 		<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
 		<init-param>
 		<url-pattern>/private/*</url-pattern>
 	</filter-mapping>
 	<filter-mapping>
-		<filter-name>Galleria Authentication Filter</filter-name>
+		<filter-name>Facelet Resource Filter</filter-name>
 		<url-pattern>/templates/*</url-pattern>
-		<dispatcher>REQUEST</dispatcher>
 	</filter-mapping>
 	<filter-mapping>
-		<filter-name>Galleria Authentication Filter</filter-name>
+		<filter-name>Facelet Resource Filter</filter-name>
 		<url-pattern>/resources/*</url-pattern>
-		<dispatcher>REQUEST</dispatcher>
 	</filter-mapping>
 	<filter-mapping>
 		<filter-name>PrimeFaces FileUpload Filter</filter-name>