/*
* Created on Jan 29, 2010
*
*/
package com.filter;
import java.io.IOException;
import java.util.TimeZone;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import com.common.Constants;
public class SecurityFilter implements Filter{
private static Logger log = Logger.getLogger(SecurityFilter.class);
public static final String BROWSER_REQUEST_URL = "__BROWSER_REQUEST_URL";
public static final String BROWSER_REQUEST_QUERY = "__BROWSER_REQUEST_QUERY";
public SecurityFilter() {
super();
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
long millis = System.currentTimeMillis();
log.debug("SecurityFilter.doFilter() entry -------------------------------");
log.debug( "ServletResponse runtime class: " + response.getClass().getName());
try {
HttpServletRequest r = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
HttpSession session = r.getSession(false);
if((session == null || session.getAttribute(Constants.CURRENT_USER) == null)&& (!r.getRequestURI().equals(r.getContextPath() + "/")
&& !r.getRequestURI().equals(r.getContextPath() + "/index.jsp")
&& !r.getRequestURI().equals(r.getContextPath() + "/login.action")
&& !r.getRequestURI().equals(r.getContextPath() + "/logout.action")
&& !r.getRequestURI().equals(r.getContextPath() + "/selectedDonationSite.action")
&& !r.getRequestURI().equals(r.getContextPath() + "/retiveUserPassword.action")
&& !r.getRequestURI().equals(r.getContextPath() + "/retiveUserPassword.action")
&& !r.getRequestURI().equals(r.getContextPath() + "/feedback.action")
&& !r.getRequestURI().equals(r.getContextPath() + "/userRegistrationAction.action")
)){
res.sendRedirect(r.getContextPath() + "/index.jsp");
}else{
//TimeZone savedZone = TimeZone.getDefault();
//TimeZone tz = TimeZone.getTimeZone("GMT");
//TimeZone.setDefault(tz);
chain.doFilter(request, response);
//TimeZone.setDefault(savedZone);
}
} catch(Exception e){
e.printStackTrace();
}finally {
log.debug("SecurityFilter.doFilter() exit -------------------------------");
}
}
public void destroy() {
}
public void init(FilterConfig fc) throws ServletException {
}
public static String getUrl(ServletRequest r ) {
return (String)r.getAttribute( BROWSER_REQUEST_URL );
}
public static String getQueryString(ServletRequest r ) {
return (String)r.getAttribute( BROWSER_REQUEST_QUERY );
}
}