

package com.discursive.cas.extend.client.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

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

/**
 * <p>Wraps the <code>HttpServletRequest</code> object, replacing
 * <code>getRemoteUser()</code> with a version that returns the current
 * CAS logged-in user.</p>
 *
 * @author Drew Mazurek
 */
public class CASFilterRequestWrapper extends HttpServletRequestWrapper {

	private static Log log = LogFactory.getLog(CASFilterRequestWrapper.class);

    public CASFilterRequestWrapper(HttpServletRequest request) {
			super(request);
    	if (log.isTraceEnabled()){
    		log.trace("wrapping an HttpServletRequest in a CASFilterRequestWrapper.");
    	}
    }

    /**
     * <p>Returns the currently logged in CAS user.</p>
     * <p>Specifically, this returns the value of the session attribute,
     * <code>CASFilter.CAS_FILTER_USER</code>.</p>
     */
    public String getRemoteUser() {
    	String user = (String)getSession().getAttribute(CASFilter.CAS_FILTER_USER);
    	if (log.isTraceEnabled()){
    		log.trace("getRemoteUser() returning [" + user + "]");
    	}
    	return user;
    }
}
