Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package fr.openwide.core.basicapp.web.application;
import org.apache.wicket.Application;
import org.apache.wicket.Session;
import org.apache.wicket.core.request.handler.IPageRequestHandler;
import org.apache.wicket.page.PageAccessSynchronizer;
import org.apache.wicket.request.Request;
import org.apache.wicket.request.cycle.PageRequestHandlerTracker;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.settings.IRequestCycleSettings;
import org.apache.wicket.util.IProvider;
import org.apache.wicket.util.LazyInitializer;
import org.apache.wicket.util.time.Duration;
import fr.openwide.core.basicapp.core.business.user.model.User;
import fr.openwide.core.wicket.more.AbstractCoreSession;
import fr.openwide.core.wicket.request.component.ILongRunningPage;
import fr.openwide.core.wicket.settings.def.AdaptativeTimeoutRequestCycleSettings;
public class BasicApplicationSession extends AbstractCoreSession<User> {
private static final long serialVersionUID = 1870827020904365541L;
public BasicApplicationSession(Request request) {
super(request);
}
public static BasicApplicationSession get() {
return (BasicApplicationSession) Session.get();
}
@Override
protected IProvider<PageAccessSynchronizer> newPageAccessSynchronizerProvider() {
return new AdaptativeTimeoutPageAccessSynchronizerProvider();
}
private static final class AdaptativeTimeoutPageAccessSynchronizerProvider extends LazyInitializer<PageAccessSynchronizer> {
private static final long serialVersionUID = 1L;
@Override
protected PageAccessSynchronizer createInstance() {
Duration defaultTimeout = Duration.minutes(1);
Duration longRunningPageTimeout = Duration.minutes(10);
if (Application.exists()) {
IRequestCycleSettings requestCycleSettings = Application.get().getRequestCycleSettings();
defaultTimeout = requestCycleSettings.getTimeout();
if (requestCycleSettings instanceof AdaptativeTimeoutRequestCycleSettings) {
longRunningPageTimeout = ((AdaptativeTimeoutRequestCycleSettings) requestCycleSettings).getLongRunningPageTimeout();
}
}
return new AdaptativeTimeoutPageAccessSynchronizer(defaultTimeout, longRunningPageTimeout);
}
}
private static final class AdaptativeTimeoutPageAccessSynchronizer extends PageAccessSynchronizer {
private static final long serialVersionUID = -2540076668796432679L;
private Duration longRunningPageTimeout = Duration.minutes(10);
public AdaptativeTimeoutPageAccessSynchronizer(Duration defaultTimeout, Duration longRunningPageTimeout) {
super(defaultTimeout);
this.longRunningPageTimeout = longRunningPageTimeout;
}
@Override
public Duration getTimeout() {
RequestCycle cycle = RequestCycle.get();
if (cycle != null) {
IPageRequestHandler handler = PageRequestHandlerTracker.getFirstHandler(cycle);
if (handler != null && handler.getPageClass() != null
&& (ILongRunningPage.class.isAssignableFrom(handler.getPageClass()))) {
return longRunningPageTimeout;
}
}
return super.getTimeout();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment