package org.blaazinsoftware.centaur.web.user.filter;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import javax.servlet.*;
import java.io.IOException;

/**
 * Sets the Current User object as well as Login and Logout URLs to the Request
 */
public class CentaurUserFilter implements Filter {

    public static final String LOGIN_URL_PARAMETER_NAME = "loginURL";
    public static final String LOGOUT_URL_PARAMETER_NAME = "logoutURL";
    public static final String CURRENT_USER_PARAMETER_NAME = "currentUser";

    private String postLoginURL = null;
    private String postLogoutURL = null;

    private FilterConfig filterConfig = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        UserService userService = UserServiceFactory.getUserService();
        User currentUser = userService.getCurrentUser();

        request.setAttribute(LOGIN_URL_PARAMETER_NAME, userService.createLoginURL(createPostLoginURL()));
        request.setAttribute(LOGOUT_URL_PARAMETER_NAME, userService.createLogoutURL(createPostLogoutURL()));
        request.setAttribute(CURRENT_USER_PARAMETER_NAME, currentUser);

        chain.doFilter(request, response);
    }

    public String createPostLoginURL() {
        if (postLoginURL == null) {
            postLoginURL = (filterConfig.getInitParameter("postLoginURL"));
        }

        return postLoginURL;
    }

    public String createPostLogoutURL() {
        if (postLogoutURL == null) {
            postLogoutURL = (filterConfig.getInitParameter("postLogoutURL"));
        }

        return postLogoutURL;
    }

    @Override
    public void destroy() {
        // Nothing needed here
    }
}

@@ -225,16 +225,62 @@
 vlet.*;%0A
+import javax.servlet.http.HttpServletRequest;%0A
 import j
@@ -784,24 +784,70 @@
 ig = null;%0A%0A
+    private boolean returnToLastUrl = false;%0A%0A
     @Overrid
@@ -1344,24 +1344,51 @@
 ostLoginURL(
+(HttpServletRequest)request
 )));%0A       
@@ -1645,24 +1645,50 @@
 ostLoginURL(
+HttpServletRequest request
 ) %7B%0A        
@@ -1804,38 +1804,585 @@
    %7D
-%0A%0A        return postLoginURL;
+ else if (%22~%22.equals(postLoginURL) %7C%7C returnToLastUrl) %7B%0A            returnToLastUrl = true;%0A            // Return to this URL%0A%0A            postLoginURL = getFullURL(request);%0A        %7D%0A%0A        return postLoginURL;%0A    %7D%0A%0A    protected String getFullURL(HttpServletRequest request) %7B%0A        StringBuffer requestURL = request.getRequestURL();%0A        String queryString = request.getQueryString();%0A%0A        if (queryString == null) %7B%0A            return requestURL.toString();%0A        %7D else %7B%0A            return requestURL.append('?').append(queryString).toString();%0A        %7D
 %0A   

Allow for the User to be redirected to the current page after login.