Search in sources :

Example 11 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class WebSession method getClientInfo.

/**
 * {@inheritDoc}
 *
 * <p>
 * To gather the client information this implementation redirects temporarily to a special page
 * ({@link BrowserInfoPage}).
 * <p>
 * Note: Do <strong>not</strong> call this method from your custom {@link Session} constructor
 * because the temporary page needs a constructed {@link Session} to be able to work.
 * <p>
 * If you need to set a default client info property then better use
 * {@link #setClientInfo(org.apache.wicket.core.request.ClientInfo)} directly.
 */
@Override
public WebClientInfo getClientInfo() {
    if (clientInfo == null) {
        RequestCycle requestCycle = RequestCycle.get();
        clientInfo = new WebClientInfo(requestCycle);
        if (getApplication().getRequestCycleSettings().getGatherExtendedBrowserInfo()) {
            WebPage browserInfoPage = newBrowserInfoPage();
            throw new RestartResponseAtInterceptPageException(browserInfoPage);
        }
    }
    return (WebClientInfo) clientInfo;
}
Also used : WebClientInfo(org.apache.wicket.protocol.http.request.WebClientInfo) WebPage(org.apache.wicket.markup.html.WebPage) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) RestartResponseAtInterceptPageException(org.apache.wicket.RestartResponseAtInterceptPageException)

Example 12 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class WicketFilter method processRequest.

/**
 * This is Wicket's main method to execute a request
 *
 * @param request
 * @param response
 * @param chain
 * @return false, if the request could not be processed
 * @throws IOException
 * @throws ServletException
 */
boolean processRequest(ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final ThreadContext previousThreadContext = ThreadContext.detach();
    // Assume we are able to handle the request
    boolean res = true;
    final ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
    final ClassLoader newClassLoader = getClassLoader();
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    try {
        if (previousClassLoader != newClassLoader) {
            Thread.currentThread().setContextClassLoader(newClassLoader);
        }
        // Make sure getFilterPath() gets called before checkIfRedirectRequired()
        String filterPath = getFilterPath(httpServletRequest);
        if (filterPath == null) {
            throw new IllegalStateException("filter path was not configured");
        }
        if (shouldIgnorePath(httpServletRequest)) {
            log.debug("Ignoring request {}", httpServletRequest.getRequestURL());
            if (chain != null) {
                // invoke next filter from within Wicket context
                chain.doFilter(request, response);
            }
            return false;
        }
        if ("OPTIONS".equalsIgnoreCase(httpServletRequest.getMethod())) {
            // handle the OPTIONS request outside of normal request processing.
            // wicket pages normally only support GET and POST methods, but resources and
            // special pages acting like REST clients can also support other methods, so
            // we include them all.
            httpServletResponse.setStatus(HttpServletResponse.SC_OK);
            httpServletResponse.setHeader("Allow", "GET,POST,OPTIONS,PUT,HEAD,PATCH,DELETE,TRACE");
            httpServletResponse.setHeader("Content-Length", "0");
            return true;
        }
        String redirectURL = checkIfRedirectRequired(httpServletRequest);
        if (redirectURL == null) {
            // No redirect; process the request
            ThreadContext.setApplication(application);
            WebRequest webRequest = application.createWebRequest(httpServletRequest, filterPath);
            WebResponse webResponse = application.createWebResponse(webRequest, httpServletResponse);
            RequestCycle requestCycle = application.createRequestCycle(webRequest, webResponse);
            res = processRequestCycle(requestCycle, webResponse, httpServletRequest, httpServletResponse, chain);
        } else {
            if (Strings.isEmpty(httpServletRequest.getQueryString()) == false) {
                redirectURL += "?" + httpServletRequest.getQueryString();
            }
            try {
                // send redirect - this will discard POST parameters if the request is POST
                // - still better than getting an error because of lacking trailing slash
                httpServletResponse.sendRedirect(httpServletResponse.encodeRedirectURL(redirectURL));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    } finally {
        ThreadContext.restore(previousThreadContext);
        if (newClassLoader != previousClassLoader) {
            Thread.currentThread().setContextClassLoader(previousClassLoader);
        }
        if (response.isCommitted() && httpServletRequest.isAsyncStarted() == false) {
            response.flushBuffer();
        }
    }
    return res;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) WebResponse(org.apache.wicket.request.http.WebResponse) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) WebRequest(org.apache.wicket.request.http.WebRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ThreadContext(org.apache.wicket.ThreadContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 13 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class AbstractRequestLogger method sessionDestroyed.

@Override
public void sessionDestroyed(String sessionId) {
    RequestCycle requestCycle = RequestCycle.get();
    SessionData sessionData = liveSessions.remove(sessionId);
    if (requestCycle != null)
        requestCycle.setMetaData(SESSION_DATA, sessionData);
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 14 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class PartialPageUpdate method writeHeaderContribution.

/**
 * @param response
 *      the response to write to
 * @param component
 *      to component which will contribute to the header
 */
protected void writeHeaderContribution(final Response response, final Component component) {
    headerRendering = true;
    // create the htmlheadercontainer if needed
    if (header == null) {
        header = new PartialHtmlHeaderContainer(this);
        page.addOrReplace(header);
    }
    RequestCycle requestCycle = component.getRequestCycle();
    // save old response, set new
    Response oldResponse = requestCycle.setResponse(headerBuffer);
    try {
        headerBuffer.reset();
        IHeaderRenderStrategy strategy = AbstractHeaderRenderStrategy.get();
        strategy.renderHeader(header, null, component);
    } finally {
        // revert to old response
        requestCycle.setResponse(oldResponse);
    }
    writeHeaderContribution(response);
    headerRendering = false;
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) IHeaderRenderStrategy(org.apache.wicket.markup.renderStrategy.IHeaderRenderStrategy)

Example 15 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle 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;
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebResponse(org.apache.wicket.request.http.WebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Aggregations

RequestCycle (org.apache.wicket.request.cycle.RequestCycle)69 WebResponse (org.apache.wicket.request.http.WebResponse)14 IRequestHandler (org.apache.wicket.request.IRequestHandler)9 Url (org.apache.wicket.request.Url)9 Response (org.apache.wicket.request.Response)8 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)7 Page (org.apache.wicket.Page)6 Request (org.apache.wicket.request.Request)6 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)5 Test (org.junit.Test)5 Application (org.apache.wicket.Application)4 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)4 WebClientInfo (org.apache.wicket.protocol.http.request.WebClientInfo)4 WebRequest (org.apache.wicket.request.http.WebRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 WebApplication (org.apache.wicket.protocol.http.WebApplication)3 UrlRenderer (org.apache.wicket.request.UrlRenderer)3