Search in sources :

Example 1 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project the-app by devops-dojo.

the class AbstractPlainTextPage method newStringResponse.

private WebResponse newStringResponse() {
    WebResponse response = (WebResponse) getResponse();
    response.setContentType("text/plain");
    response.write(responseText());
    response.flush();
    response.setStatus(HttpStatus.OK.value());
    return response;
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse)

Example 2 with WebResponse

use of org.apache.wicket.request.http.WebResponse in project the-app by devops-dojo.

the class DirectBuyRequestCycleListener method redirectTo.

private void redirectTo(RequestCycle cycle, Url urlWithoutDirectBuy) {
    Url requestUrl = cycle.getRequest().getUrl();
    if (!requestUrl.equals(urlWithoutDirectBuy)) {
        WebResponse response = (WebResponse) cycle.getResponse();
        response.reset();
        response.sendRedirect(urlWithoutDirectBuy.toString(Url.StringMode.FULL));
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Url(org.apache.wicket.request.Url)

Example 3 with WebResponse

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

the class BufferedWebResponseTest method testBufferedResponsePostponeWriteResponseAction.

/**
 * Asserting that set header actions are invoked before write in response actions.
 *
 * WICKET-3618
 */
@Test
public void testBufferedResponsePostponeWriteResponseAction() {
    final ArrayList<TestAction> actionsSequence = new ArrayList<TestAction>();
    WebResponse originalResponse = new MockWebResponse() {

        @Override
        public void setContentLength(long length) {
            actionsSequence.add(TestAction.SET_CONTENT_LENGTH);
        }

        @Override
        public void write(CharSequence sequence) {
            actionsSequence.add(TestAction.WRITE_RESPONSE);
        }

        /**
         * WICKET-5863
         */
        @Override
        public void disableCaching() {
            actionsSequence.add(TestAction.DISABLE_CACHING);
        }
    };
    BufferedWebResponse response = new BufferedWebResponse(originalResponse);
    response.setText("some text");
    response.setContentLength(9);
    response.disableCaching();
    response.writeTo(originalResponse);
    assertEquals(0, actionsSequence.indexOf(TestAction.SET_CONTENT_LENGTH));
    assertEquals(1, actionsSequence.indexOf(TestAction.DISABLE_CACHING));
    assertEquals(2, actionsSequence.indexOf(TestAction.WRITE_RESPONSE));
}
Also used : MockWebResponse(org.apache.wicket.mock.MockWebResponse) MockWebResponse(org.apache.wicket.mock.MockWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with WebResponse

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

the class WebApplication method createWebResponse.

/**
 * Pre- and post- configures the {@link WebResponse} returned from
 * {@link #newWebResponse(WebRequest, HttpServletResponse)}
 *
 * @param webRequest
 *            the {@link WebRequest} that will handle the current HTTP Servlet request
 * @param httpServletResponse
 *            the current HTTP Servlet response
 * @return the configured WebResponse object
 */
WebResponse createWebResponse(final WebRequest webRequest, final HttpServletResponse httpServletResponse) {
    WebResponse webResponse = newWebResponse(webRequest, httpServletResponse);
    boolean shouldBufferResponse = getRequestCycleSettings().getBufferResponse();
    return shouldBufferResponse ? new HeaderBufferingWebResponse(webResponse) : webResponse;
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) ServletWebResponse(org.apache.wicket.protocol.http.servlet.ServletWebResponse)

Example 5 with WebResponse

use of org.apache.wicket.request.http.WebResponse 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)

Aggregations

WebResponse (org.apache.wicket.request.http.WebResponse)34 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)9 Test (org.junit.Test)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 Response (org.apache.wicket.request.Response)5 WebRequest (org.apache.wicket.request.http.WebRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)4 IRequestHandler (org.apache.wicket.request.IRequestHandler)3 Url (org.apache.wicket.request.Url)3 IOException (java.io.IOException)2 Application (org.apache.wicket.Application)2 Page (org.apache.wicket.Page)2 Attributes (org.apache.wicket.request.resource.IResource.Attributes)2 JSONObject (com.github.openjson.JSONObject)1 Member (com.hazelcast.core.Member)1 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)1 MembershipEvent (com.hazelcast.core.MembershipEvent)1 MembershipListener (com.hazelcast.core.MembershipListener)1