Merge "Web UI Branding support This fix provides an extensible branding support with...
[controller.git] / opendaylight / web / root / src / main / java / org / opendaylight / controller / web / ControllerAuthenticationSuccessHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9
10 package org.opendaylight.controller.web;
11
12 import java.io.IOException;
13 import java.util.Map;
14
15 import javax.servlet.ServletException;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19 import org.springframework.security.core.Authentication;
20 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
21 import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
22 import org.springframework.security.web.savedrequest.RequestCache;
23 import org.springframework.security.web.savedrequest.SavedRequest;
24 import org.springframework.util.StringUtils;
25
26 public class ControllerAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
27     private RequestCache requestCache = new HttpSessionRequestCache();
28
29     @Override
30     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
31             Authentication authentication) throws ServletException, IOException {
32         SavedRequest savedRequest = requestCache.getRequest(request, response);
33
34         if (savedRequest == null) {
35             super.onAuthenticationSuccess(request, response, authentication);
36
37             return;
38         }
39         String targetUrlParameter = getTargetUrlParameter();
40         if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
41             requestCache.removeRequest(request, response);
42             super.onAuthenticationSuccess(request, response, authentication);
43
44             return;
45         }
46
47         clearAuthenticationAttributes(request);
48
49         // Use the DefaultSavedRequest URL
50         
51         String targetUrl = savedRequest.getRedirectUrl();
52         //workaround to avoid being redirected to ajax calls
53         Map<String, String[]> m = savedRequest.getParameterMap();
54         if(m!= null)
55         {
56             String[] value = m.get("x-page-url");
57             if(value != null && value.length > 0)
58                 targetUrl = request.getContextPath() + "#" + value[0];
59         }
60         logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
61         
62         
63         
64         getRedirectStrategy().sendRedirect(request, response, targetUrl);
65     }
66
67     public void setRequestCache(RequestCache requestCache) {
68         this.requestCache = requestCache;
69     }
70 }