use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.
the class DefaultUnauthorizedResourceRequestListener method onUnauthorizedRequest.
@Override
public void onUnauthorizedRequest(IResource resource, PageParameters parameters) {
RequestCycle cycle = RequestCycle.get();
if (cycle != null) {
IRequestHandler handler = new ErrorCodeRequestHandler(HttpServletResponse.SC_FORBIDDEN, createErrorMessage(resource, parameters));
cycle.replaceAllRequestHandlers(handler);
}
}
use of org.apache.wicket.request.cycle.RequestCycle 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);
}
}
use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.
the class AbstractWebSocketProcessor method createRequestCycle.
private RequestCycle createRequestCycle(WebSocketRequestMapper requestMapper, WebResponse webResponse) {
RequestCycleContext context = new RequestCycleContext(webRequest, webResponse, requestMapper, application.getExceptionMapperProvider().get());
RequestCycle requestCycle = application.getRequestCycleProvider().apply(context);
requestCycle.getListeners().add(application.getRequestCycleListeners());
requestCycle.getListeners().add(new IRequestCycleListener() {
@Override
public void onDetach(final RequestCycle requestCycle) {
if (Session.exists()) {
Session.get().getPageManager().commitRequest();
}
}
});
requestCycle.getUrlRenderer().setBaseUrl(baseUrl);
return requestCycle;
}
use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.
the class RequestSettingRequestHandler method respond.
@Override
public void respond(IRequestCycle requestCycle) {
RequestCycle cycle = (RequestCycle) requestCycle;
Request originalRequest = cycle.getRequest();
try {
cycle.setRequest(request);
delegate.respond(requestCycle);
} finally {
cycle.setRequest(originalRequest);
}
}
use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.
the class AjaxRequestHandler method respond.
/**
* @see org.apache.wicket.core.request.handler.IPageRequestHandler#respond(org.apache.wicket.request.IRequestCycle)
*/
@Override
public final void respond(final IRequestCycle requestCycle) {
final RequestCycle rc = (RequestCycle) requestCycle;
final WebResponse response = (WebResponse) requestCycle.getResponse();
if (shouldRedirectToPage(requestCycle)) {
// the page itself has been added to the request target, we simply issue a redirect
// back to the page
IRequestHandler handler = new RenderPageRequestHandler(new PageProvider(page));
final String url = rc.urlFor(handler).toString();
response.sendRedirect(url);
return;
}
respondersFrozen = true;
for (ITargetRespondListener listener : respondListeners) {
listener.onTargetRespond(this);
}
final Application app = page.getApplication();
page.send(app, Broadcast.BREADTH, this);
// Determine encoding
final String encoding = app.getRequestCycleSettings().getResponseRequestEncoding();
// Set content type based on markup type for page
update.setContentType(response, encoding);
// Make sure it is not cached by a client
response.disableCaching();
final StringResponse bodyResponse = new StringResponse();
update.writeTo(bodyResponse, encoding);
CharSequence filteredResponse = invokeResponseFilters(bodyResponse);
response.write(filteredResponse);
}
Aggregations