Example usage for org.apache.wicket Application fetchCreateAndSetSession

List of usage examples for org.apache.wicket Application fetchCreateAndSetSession

Introduction

In this page you can find the example usage for org.apache.wicket Application fetchCreateAndSetSession.

Prototype

public Session fetchCreateAndSetSession(final RequestCycle requestCycle) 

Source Link

Usage

From source file:com.vanillasource.jaywire.wicket.WicketModule.java

License:Open Source License

private void registerInfrastructure(Application application) {
    application.getRequestCycleListeners().add(new AbstractRequestCycleListener() {
        @Override//from  w ww . j a  v  a 2 s. c om
        public void onBeginRequest(RequestCycle requestCycle) {
            if (requestCycle.getRequest() != null && requestCycle.getRequest() instanceof ServletWebRequest) {
                ServletRequest request = ((ServletWebRequest) requestCycle.getRequest()).getContainerRequest();
                getServletRequestScope().setStorage(request);
                // Force creation of session
                if (request instanceof HttpServletRequest) {
                    application.getSessionStore().getSessionId(requestCycle.getRequest(), true);
                    Session session = application.fetchCreateAndSetSession(requestCycle);
                    if (session == null) {
                        throw new WicketRuntimeException(
                                "Could not create session, which is necessary for JayWire session scope.");
                    }
                    getHttpSessionScope().setStorage(((HttpServletRequest) request).getSession());
                }
            }
        }

        @Override
        public void onEndRequest(RequestCycle requestCycle) {
            getHttpSessionScope().clearStorage();
            getServletRequestScope().clearStorage();
        }
    });
    application.getApplicationListeners().add(new IApplicationListener() {
        @Override
        public void onAfterInitialized(Application application) {
        }

        @Override
        public void onBeforeDestroyed(Application application) {
            try {
                close();
            } catch (Exception e) {
                throw new WicketRuntimeException("Could not close JayWire module", e);
            }
        }
    });
}