package edu.wustl.query.util.filter;

import java.io.IOException;

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 edu.wustl.common.beans.SessionDataBean;
import edu.wustl.common.util.logger.Logger;
import edu.wustl.query.util.global.Utility;

/**
 * Request filter added to check SessionDataBean since Query actions were accessible without loggin in to the system
 * @author vijay_pande
 *
 */
public class QueryRequestFilter implements Filter
{

	private static org.apache.log4j.Logger logger = Logger.getLogger(QueryRequestFilter.class);
	
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException
	{

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		
		
		SessionDataBean sessionDataBean = Utility.getSessionData(request);
		String url = request.getRequestURL().toString().substring(request.getContextPath().length());
		if(sessionDataBean == null && !isValidURL(url))
		{
			logger.error("Redirecting to Login Page!");
			response.sendRedirect("Logout.do");
			return;
		}
		chain.doFilter(req, res);
	}

	/**
	 * Method to check validity of URL
	 * @param url
	 * @param sessionDataBean
	 * @return
	 */
	private boolean isValidURL(String url)
	{
		boolean isValidURL = false;
		String[] array = new String[]{"Logout.do", "RedirectHome.do","Login.do"};
		for(String string : array)
		{
			if(url.indexOf(string)>-1)
			{
				isValidURL = true;
				break;
			}
		}
		return isValidURL;
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig config) throws ServletException
	{

		//add code to initialize resource   
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy()
	{
		//add code to release any resource   
	}
}

@@ -1308,16 +1308,84 @@
 uest);%0D%0A
+%09%09//Trim initial context path to get URL starting from Action name%0D%0A
 %09%09String
@@ -1415,32 +1415,24 @@
 stUR
-L().toString().substring
+I().replaceFirst
 (req
@@ -1456,17 +1456,15 @@
 th()
-.length()
++%22/%22,%22%22
 );%0D%0A
@@ -1499,19 +1499,21 @@
  && 
-!
 is
-ValidURL
+QueryAction
 (url
@@ -1786,24 +1786,27 @@
 olean is
-ValidURL
+QueryAction
 (String 
@@ -1821,252 +1821,52 @@
 %0D%0A%09%09
-boolean isValidURL = false;%0D%0A%09%09String%5B%5D array = new String%5B%5D%7B%22Logout.do%22, %22RedirectHome.do%22,%22Login.do%22%7D;%0D%0A%09%09for(String string : array)%0D%0A%09%09%7B%0D%0A%09%09%09if(url.indexOf(string)%3E-1)%0D%0A%09%09%09%7B%0D%0A%09%09%09%09isValidURL = true;%0D%0A%09%09%09%09break;%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09return isValidURL
+return StrutsConfigReader.isQueryAction(url)
 ;%0D%0A%09

change for StrutsConfigReader class