/* Copyright 2006-2015 SpringSource.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package grails.plugin.springsecurity.web.authentication;

import grails.plugin.springsecurity.SpringSecurityUtils;

import java.io.IOException;

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

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.RequestCache;

/**
 * @author <a href='mailto:burt@burtbeckwith.com'>Burt Beckwith</a>
 */
public class AjaxAwareAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

	protected String ajaxSuccessUrl;
	protected RequestCache requestCache;

	@Override
	public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response,
			final Authentication authentication) throws ServletException, IOException {
		try {
            if (SpringSecurityUtils.isAjax(request)) {
                this.clearAuthenticationAttributes(request);
                this.logger.debug("Redirecting to Ajax Success Url: " + ajaxSuccessUrl);
                this.getRedirectStrategy().sendRedirect(request, response, ajaxSuccessUrl);
            } else {
                super.onAuthenticationSuccess(request, response, authentication);
            }
		}
		finally {
			// always remove the saved request
			requestCache.removeRequest(request, response);
		}
	}

	/**
	 * Dependency injection for the Ajax success url, e.g. '/login/ajaxSuccess'.
	 * @param url the url
	 */
	public void setAjaxSuccessUrl(final String url) {
		ajaxSuccessUrl = url;
	}

	@Override
	public void setRequestCache(RequestCache cache) {
		super.setRequestCache(cache);
		requestCache = cache;
	}
}

@@ -1560,28 +1560,19 @@
 %09%09try %7B%0A
-            
+%09%09%09
 if (Spri
@@ -1606,32 +1606,23 @@
 est)) %7B%0A
-         
+%09%09%09
     
-   
 this.cle
@@ -1658,28 +1658,19 @@
 quest);%0A
-            
+%09%09%09
     this
@@ -1738,28 +1738,19 @@
 ssUrl);%0A
-            
+%09%09%09
     this
@@ -1825,41 +1825,23 @@
 l);%0A
-            %7D else %7B%0A            
+%09%09%09%7D else %7B%0A%09%09%09
     
@@ -1910,20 +1910,11 @@
 n);%0A
-            
+%09%09%09
 %7D%0A%09%09

Fix spacing