/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.jsfunit.framework;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
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.jboss.jsfunit.context.JSFUnitFacesContext;
/**
* <p>
* The JSFUnitFilter is used to set up JSFUnit tests during a client
* invocation of JUnit. It must run before the JSFUnitServletRedirector or
* Cactus ServletTestRunner.
* </p>
* <p>
* For details, see <a href="http://jakarta.apache.org/cactus/">Apache Cactus</a>.
* </p>
* <p>
* Suggested setup in web.xml is to map the filter to the Cactus servlet(s) as
* follows:
* <code><pre>
* <filter>
* <filter-name>JSFUnitFilter</filter-name>
* <filter-class>org.jboss.jsfunit.framework.JSFUnitFilter</filter-class>
* </filter>
*
* <filter-mapping>
* <filter-name>JSFUnitFilter</filter-name>
* <servlet-name>ServletRedirector</servlet-name>
* </filter-mapping>
*
* <filter-mapping>
* <filter-name>JSFUnitFilter</filter-name>
* <servlet-name>ServletTestRunner</servlet-name>
* </filter-mapping>
* </pre></code>
* </p>
* <p>
* If you are using a secure Cacuts redirector, you will also
* need to declare the filter for that servlet:
* <code><pre>
* <filter-mapping>
* <filter-name>JSFUnitFilter</filter-name>
* <servlet-name>ServletRedirectorSecure</servlet-name>
* </filter-mapping>
* </pre></code>
* </p>
*
* @author Stan Silvert
* @since 1.0
*/
public class JSFUnitFilter implements Filter
{
public static final String REDIRECTOR_REQUEST_PARAMS_KEY = JSFUnitFilter.class.getName() + ".sessionkey";
private ServletContext servletContext;
private void putWarURLinApplication(HttpServletRequest request)
{
if (servletContext.getAttribute(WebConversationFactory.WAR_URL) != null) return;
servletContext.setAttribute(WebConversationFactory.WAR_URL,
WebConversationFactory.makeWARURL(request));
}
public void doFilter(ServletRequest req,
ServletResponse res,
FilterChain filterChain) throws IOException, ServletException
{
JSFUnitFacesContext.cleanUpOldFacesContext();
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
putWarURLinApplication(request);
storeRequestParamsInSession(request);
try
{
WebConversationFactory.setThreadLocals(request);
System.setProperty("cactus.contextURL", WebConversationFactory.getWARURL());
if (isSnoopRequest(request))
{
snoop(request, response);
return;
}
filterChain.doFilter(req, res);
}
finally
{
HttpSession session = ((HttpServletRequest)req).getSession(false);
if (session != null)
{
session.removeAttribute(JSFUnitFacesContext.SESSION_KEY);
session.removeAttribute(WebClientSpec.SESSION_KEY);
}
WebConversationFactory.removeThreadLocals();
}
}
private void storeRequestParamsInSession(HttpServletRequest request)
{
HttpSession session = request.getSession();
session.setAttribute(REDIRECTOR_REQUEST_PARAMS_KEY, request.getParameterMap());
}
private boolean isSnoopRequest(HttpServletRequest request)
{
return request.getParameter("jsfunit.snoop") != null;
}
private void snoop(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
PrintWriter writer = response.getWriter();
writer.println("<p>HttpSession Values</p>");
HttpSession session = request.getSession();
for (Enumeration attribs = session.getAttributeNames(); attribs.hasMoreElements();)
{
String attribute = (String)attribs.nextElement();
writer.print(attribute + " = " + session.getAttribute(attribute).toString());
writer.println("<br/>");
}
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.servletContext = filterConfig.getServletContext();
}
public void destroy()
{
this.servletContext = null;
}
}