use of org.apache.wicket.request.http.WebResponse in project wicket by apache.
the class WebPageRenderer method renderPage.
/**
* Renders page to a {@link BufferedWebResponse}. All URLs in page will be rendered relative to
* <code>targetUrl</code>
*
* @param targetUrl
* @param requestCycle
* @return BufferedWebResponse containing page body
*/
protected BufferedWebResponse renderPage(Url targetUrl, RequestCycle requestCycle) {
// get the page before checking for a scheduled request handler because
// the page may call setResponsePage in its constructor
IRequestablePage requestablePage = getPage();
IRequestHandler scheduled = requestCycle.getRequestHandlerScheduledAfterCurrent();
if (scheduled != null) {
// no need to render
return null;
}
// keep the original response
final WebResponse originalResponse = (WebResponse) requestCycle.getResponse();
// buffered web response for page
BufferedWebResponse response = new BufferedWebResponse(originalResponse);
// keep the original base URL
Url originalBaseUrl = requestCycle.getUrlRenderer().setBaseUrl(targetUrl);
try {
requestCycle.setResponse(response);
requestablePage.renderPage();
if (requestCycle.getRequestHandlerScheduledAfterCurrent() != null) {
// This is a special case.
// During page render another request handler got scheduled and will want to
// overwrite the response, so we need to let it.
// Just preserve the meta data headers. Clear the initial actions because they are
// already copied into the new response's actions
originalResponse.reset();
response.writeMetaData(originalResponse);
return null;
} else {
return response;
}
} finally {
// restore original response and base URL
requestCycle.setResponse(originalResponse);
requestCycle.getUrlRenderer().setBaseUrl(originalBaseUrl);
}
}
use of org.apache.wicket.request.http.WebResponse in project wicket by apache.
the class WebPageRenderer method respond.
/*
* TODO: simplify the code below. See WICKET-3347
*/
@Override
public void respond(RequestCycle requestCycle) {
Url currentUrl = requestCycle.getUrlRenderer().getBaseUrl();
Url targetUrl = requestCycle.mapUrlFor(getRenderPageRequestHandler());
if (shouldRenderPageAndWriteResponse(requestCycle, currentUrl, targetUrl)) {
BufferedWebResponse response = renderPage(currentUrl, requestCycle);
if (response != null) {
response.writeTo((WebResponse) requestCycle.getResponse());
}
} else if (shouldRedirectToTargetUrl(requestCycle, currentUrl, targetUrl)) {
redirectTo(targetUrl, requestCycle);
// note: if we had session here we would render the page to buffer and then
// redirect to URL generated *after* page has been rendered (the statelessness
// may change during render). this would save one redirect because now we have
// to render to URL generated *before* page is rendered, render the page, get
// URL after render and if the URL is different (meaning page is not stateless),
// save the buffer and redirect again (which is pretty much what the next step
// does)
} else {
if (isRedirectToBuffer() == false && logger.isDebugEnabled()) {
String details = String.format("redirect strategy: '%s', isAjax: '%s', redirect policy: '%s', " + "current url: '%s', target url: '%s', is new: '%s', is stateless: '%s', is temporary: '%s'", Application.get().getRequestCycleSettings().getRenderStrategy(), isAjax(requestCycle), getRedirectPolicy(), currentUrl, targetUrl, isNewPageInstance(), isPageStateless(), isSessionTemporary());
logger.debug("Falling back to Redirect_To_Buffer render strategy because none of the conditions " + "matched. Details: " + details);
}
// force creation of possible stateful page to get the final target url
getPage();
Url beforeRenderUrl = requestCycle.mapUrlFor(getRenderPageRequestHandler());
// redirect to buffer
BufferedWebResponse response = renderPage(beforeRenderUrl, requestCycle);
if (response == null) {
return;
}
// the url might have changed after page has been rendered (e.g. the
// stateless flag might have changed because stateful components
// were added)
final Url afterRenderUrl = requestCycle.mapUrlFor(getRenderPageRequestHandler());
if (beforeRenderUrl.getSegments().equals(afterRenderUrl.getSegments()) == false) {
// the amount of segments is different - generated relative URLs
// will not work, we need to rerender the page. This can happen
// with IRequestHandlers that produce different URLs with
// different amount of segments for stateless and stateful pages
response = renderPage(afterRenderUrl, requestCycle);
}
if (currentUrl.equals(afterRenderUrl)) {
// no need to redirect when both urls are exactly the same
response.writeTo((WebResponse) requestCycle.getResponse());
} else // if page is still stateless after render
if (isPageStateless() && !enableRedirectForStatelessPage()) {
// we don't want the redirect to happen for stateless page
// example:
// when a normal mounted stateful page is hit at /mount/point
// wicket renders the page to buffer and redirects to /mount/point?12
// but for stateless page the redirect is not necessary
// also for request listeners on stateful page we want to redirect
// after the listener is invoked, but on stateless page the user
// must ask for redirect explicitly
response.writeTo((WebResponse) requestCycle.getResponse());
} else {
storeBufferedResponse(afterRenderUrl, response);
redirectTo(afterRenderUrl, requestCycle);
}
}
}
use of org.apache.wicket.request.http.WebResponse in project wicket by apache.
the class CookieUtils method getWebResponse.
/**
* Convenience method to get the http response.
*
* @return WebResponse related to the RequestCycle
*/
private WebResponse getWebResponse() {
RequestCycle cycle = RequestCycle.get();
Response response = cycle.getResponse();
if (!(response instanceof WebResponse)) {
response = cycle.getOriginalResponse();
}
return (WebResponse) response;
}
use of org.apache.wicket.request.http.WebResponse 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.http.WebResponse 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