/*
 * Copyright 2005 The JA-SIG Collaborative. All rights reserved. See license
 * distributed with this file and available online at
 * http://www.uportal.org/license.html
 */
package org.jasig.cas.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.cas.CentralAuthenticationService;
import org.jasig.cas.web.support.ViewNames;
import org.jasig.cas.web.support.WebConstants;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;

/**
 * Controller to delete ticket granting ticket cookie in order to log out of
 * single sign on. This controller implements the idea of the ESUP Portail's
 * Logout patch to allow for redirecting to a url on logout. It also exposes a
 * log out link to the view via the WebConstants.LOGOUT constant.
 * 
 * @author Scott Battaglia
 * @version $Revision$ $Date$
 * @since 3.0
 */
public final class LogoutController extends AbstractController implements
    InitializingBean {

    /** The log instance. */
    private final Log log = LogFactory.getLog(getClass());

    /** The CORE to which we delegate for all CAS functionality. */
    private CentralAuthenticationService centralAuthenticationService;

    /**
     * Boolean to determine if we will redirect to any url provided in the
     * service request parameter.
     */
    private boolean followServiceRedirects = false;

    public void afterPropertiesSet() throws Exception {
        Assert.notNull(this.centralAuthenticationService,
            "centralAuthenticationService must be set on "
                + this.getClass().getName());
    }

    protected ModelAndView handleRequestInternal(
        final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {
        Cookie cookie = WebUtils.getCookie(request, WebConstants.COOKIE_TGC_ID);
        String service = request.getParameter(WebConstants.SERVICE);

        if (cookie != null) {
            this.centralAuthenticationService
                .destroyTicketGrantingTicket(cookie.getValue());
            destroyTicketGrantingTicketCookie(request, response);
        }

        if (this.followServiceRedirects && service != null) {
            return new ModelAndView(new RedirectView(service));
        }

        return new ModelAndView(ViewNames.CONST_LOGOUT, WebConstants.LOGOUT,
            request.getParameter(WebConstants.LOGOUT));
    }

    /**
     * Method to destroy the cookie for the TicketGrantingTicket.
     * 
     * @param request The HttpServletRequest
     * @param response The HttpServletResponse
     */
    private void destroyTicketGrantingTicketCookie(
        final HttpServletRequest request, final HttpServletResponse response) {
        log.debug("Destroying TicketGrantingTicket cookie.");
        Cookie cookie = new Cookie(WebConstants.COOKIE_TGC_ID, "");
        cookie.setMaxAge(0);
        cookie.setPath(request.getContextPath());
        cookie.setSecure(true);
        response.addCookie(cookie);
    }

    /**
     * @param centralAuthenticationService The centralAuthenticationService to
     * set.
     */
    public void setCentralAuthenticationService(
        final CentralAuthenticationService centralAuthenticationService) {
        this.centralAuthenticationService = centralAuthenticationService;
    }

    public void setFollowServiceRedirects(final boolean followServiceRedirects) {
        this.followServiceRedirects = followServiceRedirects;
    }
}

@@ -2148,33 +2148,36 @@
 %0A        Cookie 
-c
+tgcC
 ookie = WebUtils
@@ -2307,17 +2307,20 @@
     if (
-c
+tgcC
 ookie !=
@@ -2419,17 +2419,20 @@
 gTicket(
-c
+tgcC
 ookie.ge
@@ -2504,24 +2504,77 @@
  response);%0A
+            destroyPrivacyCookie(request, response);%0A
         %7D%0A%0A 
@@ -3221,24 +3221,24 @@
  cookie.%22);%0A
-
         Cook
@@ -3437,32 +3437,595 @@
 e(cookie);%0A    %7D
+%0A    %0A    /**%0A     * Method to destroy the privacy (warn) cookie.%0A     * %0A     * @param request The HttpServletRequest%0A     * @param response The HttpServletResponse%0A     */%0A    private void destroyPrivacyCookie(%0A        final HttpServletRequest request, final HttpServletResponse response) %7B%0A        log.debug(%22Destroying privacy cookie.%22);%0A        Cookie cookie = new Cookie(WebConstants.COOKIE_PRIVACY, %22%22);%0A        cookie.setMaxAge(0);%0A        cookie.setPath(request.getContextPath());%0A        cookie.setSecure(true);%0A        response.addCookie(cookie);%0A    %7D
 %0A%0A    /**%0A     *

Delete the privacy cookie upon logout (CAS-198)