public void redirectToRequestedPage() { | public void redirectToRequestedPage() { | ||||
try { | try { | ||||
Optional<String> redirectTo = oAuthRedirection.getAndDelete(request, response); | Optional<String> redirectTo = oAuthRedirection.getAndDelete(request, response); | ||||
getResponse().sendRedirect(server.getContextPath() + redirectTo.orElse("/")); | |||||
getResponse().sendRedirect(redirectTo.orElse(server.getContextPath() + "/")); | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
throw new IllegalStateException("Fail to redirect to home", e); | throw new IllegalStateException("Fail to redirect to home", e); | ||||
} | } |
public class OAuth2Redirection { | public class OAuth2Redirection { | ||||
private static final String REDIRECT_TO_COOKIE = "REDIRECT_TO"; | private static final String REDIRECT_TO_COOKIE = "REDIRECT_TO"; | ||||
/** | |||||
* The HTTP parameter that contains the path where the user should be redirect to. | |||||
* Please note that the web context is included. | |||||
*/ | |||||
private static final String RETURN_TO_PARAMETER = "return_to"; | private static final String RETURN_TO_PARAMETER = "return_to"; | ||||
public void create(HttpServletRequest request, HttpServletResponse response) { | public void create(HttpServletRequest request, HttpServletResponse response) { |
} | } | ||||
@Test | @Test | ||||
public void redirect_to_requested_page_context() throws Exception { | |||||
when(oAuthRedirection.getAndDelete(request, response)).thenReturn(Optional.of("/settings")); | |||||
when(server.getContextPath()).thenReturn("/sonarqube"); | |||||
public void redirect_to_requested_page_doesnt_need_context() throws Exception { | |||||
when(oAuthRedirection.getAndDelete(request, response)).thenReturn(Optional.of("/sonarqube/settings")); | |||||
when(server.getContextPath()).thenReturn("/other"); | |||||
OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); | OAuth2IdentityProvider.CallbackContext callback = newCallbackContext(); | ||||
callback.redirectToRequestedPage(); | callback.redirectToRequestedPage(); |