Search in sources :

Example 1 with WebRequest

use of org.apache.wicket.request.http.WebRequest in project wicket by apache.

the class AbstractResource method setRequestMetaData.

/**
 * Reads the plain request header information and applies enriched information as meta data to
 * the current request. Those information are available for the whole request cycle.
 *
 * @param attributes
 *            the attributes to get the plain request header information
 */
protected void setRequestMetaData(Attributes attributes) {
    Request request = attributes.getRequest();
    if (request instanceof WebRequest) {
        WebRequest webRequest = (WebRequest) request;
        setRequestRangeMetaData(webRequest);
    }
}
Also used : WebRequest(org.apache.wicket.request.http.WebRequest) WebRequest(org.apache.wicket.request.http.WebRequest) Request(org.apache.wicket.request.Request)

Example 2 with WebRequest

use of org.apache.wicket.request.http.WebRequest 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 3 with WebRequest

use of org.apache.wicket.request.http.WebRequest in project wicket by apache.

the class WebPageRenderer method isAjax.

protected boolean isAjax(RequestCycle requestCycle) {
    boolean isAjax = false;
    Request request = requestCycle.getRequest();
    if (request instanceof WebRequest) {
        WebRequest webRequest = (WebRequest) request;
        isAjax = webRequest.isAjax();
    }
    return isAjax;
}
Also used : WebRequest(org.apache.wicket.request.http.WebRequest) WebRequest(org.apache.wicket.request.http.WebRequest) Request(org.apache.wicket.request.Request)

Example 4 with WebRequest

use of org.apache.wicket.request.http.WebRequest 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 5 with WebRequest

use of org.apache.wicket.request.http.WebRequest in project wicket by apache.

the class AjaxRequestHandler method getLastFocusedElementId.

/**
 * @return the markup id of the focused element in the browser
 */
@Override
public String getLastFocusedElementId() {
    WebRequest request = (WebRequest) page.getRequest();
    String id = request.getHeader("Wicket-FocusedElementId");
    return Strings.isEmpty(id) ? null : id;
}
Also used : WebRequest(org.apache.wicket.request.http.WebRequest)

Aggregations

WebRequest (org.apache.wicket.request.http.WebRequest)15 WebResponse (org.apache.wicket.request.http.WebResponse)5 Request (org.apache.wicket.request.Request)4 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)3 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)3 IOException (java.io.IOException)2 Cookie (javax.servlet.http.Cookie)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 Attributes (org.apache.wicket.request.resource.IResource.Attributes)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Page (org.apache.wicket.Page)1 ThreadContext (org.apache.wicket.ThreadContext)1