package fr.openwide.maven.artifact.notifier.web.application.navigation.page;

import org.springframework.security.web.savedrequest.SavedRequest;

import fr.openwide.core.spring.util.StringUtils;
import fr.openwide.core.wicket.more.AbstractCoreSession;
import fr.openwide.core.wicket.more.markup.html.CoreWebPage;
import fr.openwide.core.wicket.more.request.cycle.RequestCycleUtils;
import fr.openwide.maven.artifact.notifier.web.application.MavenArtifactNotifierSession;

public abstract class AbstractLoginSuccessPage extends CoreWebPage {
	
	private static final long serialVersionUID = -875304387617628398L;
	
	private static final String WICKET_BEHAVIOR_LISTENER_URL_FRAGMENT = "IBehaviorListener";
	
	protected void redirectToSavedPage() {
		AbstractCoreSession<?> session = AbstractCoreSession.get();
		
		String redirectUrl = null;
		if (StringUtils.hasText(session.getRedirectUrl())) {
			redirectUrl = session.getRedirectUrl();
		} else {
			Object savedRequest = RequestCycleUtils.getCurrentContainerRequest().getSession()
					.getAttribute(MavenArtifactNotifierSession.SPRING_SECURITY_SAVED_REQUEST);
			if (savedRequest instanceof SavedRequest) {
				redirectUrl = ((SavedRequest) savedRequest).getRedirectUrl();
			}
		}
		if (isUrlValid(redirectUrl)) {
			redirect(redirectUrl);
		} else {
			redirect(DashboardPage.class);
		}
	}
	
	protected boolean isUrlValid(String url) {
		return StringUtils.hasText(url) && !StringUtils.contains(url, WICKET_BEHAVIOR_LISTENER_URL_FRAGMENT);
	}
}
@@ -1224,16 +1224,162 @@
 );%0A%09%09%09%7D%0A
+%09%09%09RequestCycleUtils.getCurrentContainerRequest().getSession()%0A%09%09%09%09%09.removeAttribute(MavenArtifactNotifierSession.SPRING_SECURITY_SAVED_REQUEST);%0A
 %09%09%7D%0A%09%09if

Remove the SPRING_SECURITY_SAVED_REQUEST attribute from the session after getting it