Search in sources :

Example 1 with Session

use of org.apache.wicket.Session in project wicket by apache.

the class WicketTesterTest method sessionBinding.

/**
 * Test for WICKET-3123
 */
@Test
public void sessionBinding() {
    Session session = tester.getSession();
    assertTrue(session.isTemporary());
    session.bind();
    assertFalse(session.isTemporary());
}
Also used : Session(org.apache.wicket.Session) Test(org.junit.Test)

Example 2 with Session

use of org.apache.wicket.Session in project wicket by apache.

the class AbstractRequestLogger method requestTime.

@Override
public void requestTime(long timeTaken) {
    RequestData requestdata = RequestCycle.get().getMetaData(REQUEST_DATA);
    if (requestdata != null) {
        if (activeRequests.get() > 0) {
            requestdata.setActiveRequest(activeRequests.decrementAndGet());
        }
        Session session = Session.exists() ? Session.get() : null;
        String sessionId = session != null ? session.getId() : "N/A";
        requestdata.setSessionId(sessionId);
        Object sessionInfo = getSessionInfo(session);
        requestdata.setSessionInfo(sessionInfo);
        long sizeInBytes = -1;
        if (Application.exists() && Application.get().getRequestLoggerSettings().getRecordSessionSize()) {
            try {
                sizeInBytes = session != null ? session.getSizeInBytes() : -1;
            } catch (Exception e) {
                // log the error and let the request logging continue (this is what happens in
                // the detach phase of the request cycle anyway. This provides better
                // diagnostics).
                LOG.error("Exception while determining the size of the session in the request logger: " + e.getMessage(), e);
            }
        }
        requestdata.setSessionSize(sizeInBytes);
        requestdata.setTimeTaken(timeTaken);
        addRequest(requestdata);
        SessionData sessiondata;
        if (sessionId != null) {
            sessiondata = liveSessions.get(sessionId);
            if (sessiondata == null) {
                // if the session has been destroyed during the request by
                // Session#invalidateNow, retrieve the old session data from the RequestCycle.
                sessiondata = RequestCycle.get().getMetaData(SESSION_DATA);
            }
            if (sessiondata == null) {
                // passivated session or logger only started after it.
                sessionCreated(sessionId);
                sessiondata = liveSessions.get(sessionId);
            }
            if (sessiondata != null) {
                sessiondata.setSessionInfo(sessionInfo);
                sessiondata.setSessionSize(sizeInBytes);
                sessiondata.addTimeTaken(timeTaken);
                RequestCycle.get().setMetaData(SESSION_DATA, sessiondata);
            }
        }
    }
}
Also used : Session(org.apache.wicket.Session)

Example 3 with Session

use of org.apache.wicket.Session in project wicket by apache.

the class DateValidator method decorate.

@Override
protected IValidationError decorate(IValidationError error, IValidatable<Date> validatable) {
    error = super.decorate(error, validatable);
    if (error instanceof ValidationError) {
        ValidationError ve = (ValidationError) error;
        ve.setVariable("inputdate", validatable.getValue());
        // format variables if format has been specified
        if (format != null) {
            Locale locale;
            if (Session.exists()) {
                Session session = Session.get();
                locale = session.getLocale();
                if (locale == null) {
                    locale = Locale.getDefault(Locale.Category.FORMAT);
                }
            } else {
                locale = Locale.getDefault(Locale.Category.FORMAT);
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
            if (getMinimum() != null) {
                ve.setVariable("minimum", sdf.format(getMinimum()));
            }
            if (getMaximum() != null) {
                ve.setVariable("maximum", sdf.format(getMaximum()));
            }
            ve.setVariable("inputdate", sdf.format(validatable.getValue()));
        }
    }
    return error;
}
Also used : Locale(java.util.Locale) IValidationError(org.apache.wicket.validation.IValidationError) ValidationError(org.apache.wicket.validation.ValidationError) SimpleDateFormat(java.text.SimpleDateFormat) Session(org.apache.wicket.Session)

Example 4 with Session

use of org.apache.wicket.Session in project wicket by apache.

the class AbstractWebSocketProcessor method broadcastMessage.

/**
 * Exports the Wicket thread locals and broadcasts the received message from the client to all
 * interested components and behaviors in the page with id {@code #pageId}
 * <p>
 *     Note: ConnectedMessage and ClosedMessage messages are notification-only. I.e. whatever the
 *     components/behaviors write in the WebSocketRequestHandler will be ignored because the protocol
 *     doesn't expect response from the user.
 * </p>
 *
 * @param message
 *      the message to broadcast
 */
public final void broadcastMessage(final IWebSocketMessage message) {
    IKey key = getRegistryKey();
    IWebSocketConnection connection = connectionRegistry.getConnection(application, sessionId, key);
    if (connection != null && (connection.isOpen() || message instanceof ClosedMessage)) {
        Application oldApplication = ThreadContext.getApplication();
        Session oldSession = ThreadContext.getSession();
        RequestCycle oldRequestCycle = ThreadContext.getRequestCycle();
        WebResponse webResponse = webSocketSettings.newWebSocketResponse(connection);
        try {
            WebSocketRequestMapper requestMapper = new WebSocketRequestMapper(application.getRootRequestMapper());
            RequestCycle requestCycle = createRequestCycle(requestMapper, webResponse);
            ThreadContext.setRequestCycle(requestCycle);
            ThreadContext.setApplication(application);
            Session session;
            if (oldSession == null || message instanceof IWebSocketPushMessage) {
                ISessionStore sessionStore = application.getSessionStore();
                session = sessionStore.lookup(webRequest);
                ThreadContext.setSession(session);
            } else {
                session = oldSession;
            }
            IPageManager pageManager = session.getPageManager();
            Page page = getPage(pageManager);
            if (page != null) {
                WebSocketRequestHandler requestHandler = webSocketSettings.newWebSocketRequestHandler(page, connection);
                WebSocketPayload payload = createEventPayload(message, requestHandler);
                if (!(message instanceof ConnectedMessage || message instanceof ClosedMessage || message instanceof AbortedMessage)) {
                    requestCycle.scheduleRequestHandlerAfterCurrent(requestHandler);
                }
                IRequestHandler broadcastingHandler = new WebSocketMessageBroadcastHandler(pageId, resourceName, payload);
                requestMapper.setHandler(broadcastingHandler);
                requestCycle.processRequestAndDetach();
            } else {
                LOG.debug("Page with id '{}' has been expired. No message will be broadcast!", pageId);
            }
        } catch (Exception x) {
            LOG.error("An error occurred during processing of a WebSocket message", x);
        } finally {
            try {
                webResponse.close();
            } finally {
                ThreadContext.setApplication(oldApplication);
                ThreadContext.setRequestCycle(oldRequestCycle);
                ThreadContext.setSession(oldSession);
            }
        }
    } else {
        LOG.debug("Either there is no connection({}) or it is closed.", connection);
    }
}
Also used : IPageManager(org.apache.wicket.page.IPageManager) WebResponse(org.apache.wicket.request.http.WebResponse) ISessionStore(org.apache.wicket.session.ISessionStore) IRequestHandler(org.apache.wicket.request.IRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ClosedMessage(org.apache.wicket.protocol.ws.api.message.ClosedMessage) Page(org.apache.wicket.Page) WebPage(org.apache.wicket.markup.html.WebPage) WebSocketPayload(org.apache.wicket.protocol.ws.api.event.WebSocketPayload) IWebSocketPushMessage(org.apache.wicket.protocol.ws.api.message.IWebSocketPushMessage) ConnectedMessage(org.apache.wicket.protocol.ws.api.message.ConnectedMessage) AbortedMessage(org.apache.wicket.protocol.ws.api.message.AbortedMessage) IKey(org.apache.wicket.protocol.ws.api.registry.IKey) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication) HttpSession(javax.servlet.http.HttpSession) Session(org.apache.wicket.Session)

Example 5 with Session

use of org.apache.wicket.Session in project wicket by apache.

the class AbstractInjectorTest method before.

@Before
public void before() {
    app.setServletContext(new MockServletContext(app, null));
    ThreadContext.setApplication(app);
    app.setName(getClass().getName());
    app.initApplication();
    Session session = new WebSession(new MockWebRequest(Url.parse("/")));
    app.getSessionStore().bind(null, session);
    ThreadContext.setSession(session);
    GuiceComponentInjector injector = new GuiceComponentInjector(app, new Module() {

        @Override
        public void configure(final Binder binder) {
            binder.bind(ITestService.class).to(TestService.class);
            binder.bind(ITestService.class).annotatedWith(Red.class).to(TestServiceRed.class);
            binder.bind(ITestService.class).annotatedWith(Blue.class).to(TestServiceBlue.class);
            binder.bind(new TypeLiteral<Map<String, String>>() {
            }).toProvider(new Provider<Map<String, String>>() {

                @Override
                public Map<String, String> get() {
                    Map<String, String> strings = new HashMap<>();
                    strings.put(ITestService.RESULT, ITestService.RESULT);
                    return strings;
                }
            });
            binder.bind(String.class).annotatedWith(Names.named("named1")).toInstance("NAMED_1");
            binder.bind(String.class).annotatedWith(Names.named("named2")).toInstance("NAMED_2");
            binder.bind(String.class).annotatedWith(new Jsr330Named("named1")).toInstance("NAMED_1");
            binder.bind(String.class).annotatedWith(new Jsr330Named("named2")).toInstance("NAMED_2");
            binder.bind(EvilTestService.class).toInstance(new EvilTestService("evil123", 5));
        }
    });
    app.getComponentInstantiationListeners().add(injector);
}
Also used : HashMap(java.util.HashMap) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext) Provider(com.google.inject.Provider) Binder(com.google.inject.Binder) WebSession(org.apache.wicket.protocol.http.WebSession) MockWebRequest(org.apache.wicket.mock.MockWebRequest) Module(com.google.inject.Module) HashMap(java.util.HashMap) Map(java.util.Map) WebSession(org.apache.wicket.protocol.http.WebSession) Session(org.apache.wicket.Session) Before(org.junit.Before)

Aggregations

Session (org.apache.wicket.Session)16 HttpSession (javax.servlet.http.HttpSession)4 Test (org.junit.Test)4 Locale (java.util.Locale)3 WebPage (org.apache.wicket.markup.html.WebPage)3 WebSession (org.apache.wicket.protocol.http.WebSession)3 Page (org.apache.wicket.Page)2 MockApplication (org.apache.wicket.mock.MockApplication)2 Request (org.apache.wicket.request.Request)2 Response (org.apache.wicket.request.Response)2 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)2 Authentication (org.springframework.security.core.Authentication)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 SecurityContextAwareCallable (com.evolveum.midpoint.web.component.SecurityContextAwareCallable)1 CallableResult (com.evolveum.midpoint.web.component.util.CallableResult)1 VisibleEnableBehaviour (com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour)1 CaseWorkItemsPanel (com.evolveum.midpoint.web.page.admin.cases.CaseWorkItemsPanel)1 AsyncDashboardPanel (com.evolveum.midpoint.web.page.admin.home.component.AsyncDashboardPanel)1 AccountCallableResult (com.evolveum.midpoint.web.page.admin.home.dto.AccountCallableResult)1 CasesTablePanel (com.evolveum.midpoint.web.page.admin.server.CasesTablePanel)1