/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vedit.beans.LoginStatusBean;

public class VitroHttpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected static DateFormat publicDateFormat = new SimpleDateFormat(
			"M/dd/yyyy");

	private static final Log log = LogFactory.getLog(VitroHttpServlet.class
			.getName());

	public final static String XHTML_MIMETYPE = "application/xhtml+xml";
	public final static String HTML_MIMETYPE = "text/html";

	public final static String RDFXML_MIMETYPE = "application/rdf+xml";
	public final static String N3_MIMETYPE = "text/n3"; // unofficial and
														// unregistered
	public final static String TTL_MIMETYPE = "text/turtle"; // unofficial and
																// unregistered

	/**
	 * Setup the auth flag, portal flag and portal bean objects. Put them in the
	 * request attributes.
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		setup(request);
	}

	protected final void setup(HttpServletRequest request) {

		// check to see if VitroRequestPrep filter was run
		if (request.getAttribute("appBean") == null
				|| request.getAttribute("webappDaoFactory") == null) {
			log.warn("request scope was not prepared by VitroRequestPrep");
		}
	}

	/**
	 * doPost does the same thing as the doGet method
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// ----------------------------------------------------------------------
	// static utility methods for all Vitro servlets
	// ----------------------------------------------------------------------

	/**
	 * If not logged in, send them to the login page.
	 */
	public static boolean checkLoginStatus(HttpServletRequest request,
			HttpServletResponse response) {
		if (LoginStatusBean.getBean(request).isLoggedIn()) {
			return true;
		} else {
			try {
				redirectToLoginPage(request, response);
			} catch (IOException ioe) {
				log.error("checkLoginStatus() could not redirect to login page");
			}
			return false;
		}
	}

	/**
	 * If not logged in at the minimum level or higher, send them to the login
	 * page.
	 */
	public static boolean checkLoginStatus(HttpServletRequest request,
			HttpServletResponse response, int minimumLevel) {
		if (LoginStatusBean.getBean(request).isLoggedInAtLeast(minimumLevel)) {
			return true;
		} else {
			try {
				redirectToLoginPage(request, response);
			} catch (IOException ioe) {
				log.error("checkLoginStatus() could not redirect to login page");
			}
			return false;
		}
	}

	/**
	 * Not adequately logged in. Send them to the login page, and then back to
	 * the page that invoked this.
	 */
	public static void redirectToLoginPage(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String postLoginRequest;

		String queryString = request.getQueryString();
		if ((queryString == null) || queryString.isEmpty()) {
			postLoginRequest = request.getRequestURI();
		} else {
			postLoginRequest = request.getRequestURI() + "?" + queryString;
		}

		request.getSession().setAttribute("postLoginRequest", postLoginRequest);
		String loginPage = request.getContextPath() + Controllers.LOGIN;
		response.sendRedirect(loginPage);
	}

}

@@ -228,46 +228,227 @@
 at;%0A
-%0Aimport javax.servlet.ServletException
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Enumeration;%0Aimport java.util.List;%0A%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.ServletRequest;%0Aimport javax.servlet.ServletResponse
 ;%0Aim
@@ -4013,10 +4013,916 @@
 e);%0A%09%7D%0A%0A
+%09/** Don't dump the contents of these headers, even if log.trace is enabled. */%0A%09private static final List%3CString%3E BORING_HEADERS = new ArrayList%3CString%3E(%0A%09%09%09Arrays.asList(new String%5B%5D %7B %22host%22, %22user-agent%22, %22accept%22,%0A%09%09%09%09%09%22accept-language%22, %22accept-encoding%22, %22accept-charset%22,%0A%09%09%09%09%09%22keep-alive%22, %22connection%22 %7D));%0A%09%0A%09@SuppressWarnings(%22unchecked%22)%0A%09@Override%0A%09public void service(ServletRequest req, ServletResponse resp)%0A%09%09%09throws ServletException, IOException %7B%0A%09%09if (log.isTraceEnabled()) %7B%0A%09%09%09HttpServletRequest request = (HttpServletRequest) req;%0A%09%09%09Enumeration%3CString%3E names = request.getHeaderNames();%0A%09%09%09log.trace(%22----------------------request:%22 + request.getRequestURL());%0A%09%09%09while (names.hasMoreElements()) %7B%0A%09%09%09%09String name = names.nextElement();%0A%09%09%09%09if (!BORING_HEADERS.contains(name)) %7B%0A%09%09%09%09%09log.trace(name + %22=%22 + request.getHeader(name));%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09super.service(req, resp);%0A%09%7D%0A%0A
 %7D%0A

Add debugging statements to show request URL and request headers.