Search in sources :

Example 21 with Page

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

the class PartialPageUpdate method detach.

/**
 * Detaches the page if at least one of its components was updated.
 *
 * @param requestCycle
 *      the current request cycle
 */
public void detach(IRequestCycle requestCycle) {
    Iterator<Component> iterator = markupIdToComponent.values().iterator();
    while (iterator.hasNext()) {
        final Component component = iterator.next();
        final Page parentPage = component.findParent(Page.class);
        if (parentPage != null) {
            parentPage.detach();
            break;
        }
    }
}
Also used : Page(org.apache.wicket.Page) Component(org.apache.wicket.Component)

Example 22 with Page

use of org.apache.wicket.Page 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 23 with Page

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

the class ListenerRequestHandler method internalInvoke.

private void internalInvoke(final IRequestCycle requestCycle, RedirectPolicy policy, boolean ajax, final Component component, final Object target) {
    // save a reference to the page because the component can be removed
    // during the invocation of the listener and thus lose its parent
    Page page = component.getPage();
    // initialization is required for stateless pages
    if (!page.isInitialized()) {
        page.internalInitialize();
    }
    IRequestListener requestListener = (IRequestListener) target;
    if (requestListener.rendersPage() && !ajax) {
        // schedule page render after current request handler is done. this can be
        // overridden during invocation of listener
        // method (i.e. by calling RequestCycle#setResponsePage)
        requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(new PageProvider(page), policy));
    }
    requestListener.onRequest();
}
Also used : IRequestListener(org.apache.wicket.IRequestListener) Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage)

Example 24 with Page

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

the class ListenerRequestHandler method respond.

@Override
public void respond(final IRequestCycle requestCycle) {
    final IRequestablePage page = getPage();
    final boolean freshPage = pageComponentProvider.doesProvideNewPage();
    final boolean isAjax = ((WebRequest) requestCycle.getRequest()).isAjax();
    IRequestableComponent component;
    try {
        component = getComponent();
    } catch (ComponentNotFoundException e) {
        // either the page is stateless and the component we are looking for is not added in the
        // constructor
        // or the page is stateful+stale and a new instances was created by pageprovider
        // we denote this by setting component to null
        component = null;
    }
    if ((component == null && !freshPage) || (component != null && component.getPage() != page)) {
        throw new ComponentNotFoundException("Component '" + getComponentPath() + "' has been removed from page.");
    }
    if (page instanceof Page) {
        // initialize the page to be able to check whether it is stateless
        ((Page) page).internalInitialize();
    }
    RedirectPolicy policy = page.isPageStateless() ? RedirectPolicy.NEVER_REDIRECT : RedirectPolicy.AUTO_REDIRECT;
    boolean blockIfExpired = component != null && !component.canCallListenerAfterExpiry();
    boolean lateComponent = component == null && freshPage;
    if ((pageComponentProvider.wasExpired() && blockIfExpired) || lateComponent) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("An IRequestListener was called but its page/component({}) couldn't be resolved. " + "Scheduling re-create of the page and ignoring the listener interface...", getComponentPath());
        }
        if (isAjax) {
            policy = RedirectPolicy.ALWAYS_REDIRECT;
        }
        requestCycle.scheduleRequestHandlerAfterCurrent(new RenderPageRequestHandler(new PageProvider(page), policy));
        return;
    }
    invokeListener(requestCycle, policy, isAjax);
}
Also used : RedirectPolicy(org.apache.wicket.core.request.handler.RenderPageRequestHandler.RedirectPolicy) IRequestableComponent(org.apache.wicket.request.component.IRequestableComponent) WebRequest(org.apache.wicket.request.http.WebRequest) IRequestablePage(org.apache.wicket.request.component.IRequestablePage) Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage)

Example 25 with Page

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

the class PageAndComponentProvider method getComponent.

@Override
public IRequestableComponent getComponent() {
    if (component == null) {
        IRequestablePage page = getPageInstance();
        component = page != null ? page.get(componentPath) : null;
        if (component == null) {
            // make sure this page instance was just created so the page can be stateless
            if (page.isPageStateless()) {
                Page p = (Page) page;
                p.internalInitialize();
                // preparation of feedbacks is delayed into the render phase
                try (FeedbackDelay delay = new FeedbackDelay(p.getRequestCycle())) {
                    p.beforeRender();
                    p.markRendering(false);
                // note: no invocation of delay.onBeforeRender()
                }
                component = page.get(componentPath);
            }
        }
    }
    if (component == null) {
        throw new ComponentNotFoundException("Could not find component '" + componentPath + "' on page '" + getPageClass());
    }
    return component;
}
Also used : IRequestablePage(org.apache.wicket.request.component.IRequestablePage) FeedbackDelay(org.apache.wicket.feedback.FeedbackDelay) Page(org.apache.wicket.Page) IRequestablePage(org.apache.wicket.request.component.IRequestablePage)

Aggregations

Page (org.apache.wicket.Page)94 Test (org.junit.Test)50 WebPage (org.apache.wicket.markup.html.WebPage)22 Component (org.apache.wicket.Component)11 AjaxLink (org.apache.wicket.ajax.markup.html.AjaxLink)7 IRequestablePage (org.apache.wicket.request.component.IRequestablePage)7 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)6 IRequestHandler (org.apache.wicket.request.IRequestHandler)6 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)6 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)6 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)5 PageProvider (org.apache.wicket.core.request.handler.PageProvider)5 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)5 DummyPage (org.apache.wicket.resource.DummyPage)5 ArrayList (java.util.ArrayList)4 AbstractAjaxBehavior (org.apache.wicket.behavior.AbstractAjaxBehavior)4 AccessDeniedPage (org.apache.wicket.markup.html.pages.AccessDeniedPage)4 Url (org.apache.wicket.request.Url)4 MockInnerClassPage (org.apache.wicket.util.tester.MockPageParameterPage.MockInnerClassPage)4 SuccessPage (org.apache.wicket.util.tester.apps_1.SuccessPage)4