Search in sources :

Example 26 with WebResponse

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

the class AbstractResource method configureCache.

/**
 * Configure the web response header for client cache control.
 *
 * @param data
 *            resource data
 * @param attributes
 *            request attributes
 */
protected void configureCache(final ResourceResponse data, final Attributes attributes) {
    Response response = attributes.getResponse();
    if (response instanceof WebResponse) {
        Duration duration = data.getCacheDuration();
        WebResponse webResponse = (WebResponse) response;
        if (duration.compareTo(Duration.NONE) > 0) {
            webResponse.enableCaching(duration, data.getCacheScope());
        } else {
            webResponse.disableCaching();
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebResponse(org.apache.wicket.request.http.WebResponse) Duration(org.apache.wicket.util.time.Duration)

Example 27 with WebResponse

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

the class AbstractResource method setResponseHeaders.

/**
 * Sets the response header of resource response to the response received from the attributes
 *
 * @param resourceResponse
 *            the resource response to get the header fields from
 * @param attributes
 *            the attributes to get the response from to which the header information are going
 *            to be applied
 */
protected void setResponseHeaders(final ResourceResponse resourceResponse, final Attributes attributes) {
    Response response = attributes.getResponse();
    if (response instanceof WebResponse) {
        WebResponse webResponse = (WebResponse) response;
        // 1. Last Modified
        Time lastModified = resourceResponse.getLastModified();
        if (lastModified != null) {
            webResponse.setLastModifiedTime(lastModified);
        }
        // 2. Caching
        configureCache(resourceResponse, attributes);
        if (resourceResponse.getErrorCode() != null) {
            webResponse.sendError(resourceResponse.getErrorCode(), resourceResponse.getErrorMessage());
            return;
        }
        if (resourceResponse.getStatusCode() != null) {
            webResponse.setStatus(resourceResponse.getStatusCode());
        }
        if (!resourceResponse.dataNeedsToBeWritten(attributes)) {
            webResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
            return;
        }
        // 3. Content Disposition
        String fileName = resourceResponse.getFileName();
        ContentDisposition disposition = resourceResponse.getContentDisposition();
        if (ContentDisposition.ATTACHMENT == disposition) {
            webResponse.setAttachmentHeader(fileName);
        } else if (ContentDisposition.INLINE == disposition) {
            webResponse.setInlineHeader(fileName);
        }
        // 4. Mime Type (+ encoding)
        String mimeType = resourceResponse.getContentType();
        if (mimeType != null) {
            final String encoding = resourceResponse.getTextEncoding();
            if (encoding == null) {
                webResponse.setContentType(mimeType);
            } else {
                webResponse.setContentType(mimeType + "; charset=" + encoding);
            }
        }
        // 5. Accept Range
        ContentRangeType acceptRange = resourceResponse.getAcceptRange();
        if (acceptRange != null) {
            webResponse.setAcceptRange(acceptRange.getTypeName());
        }
        long contentLength = resourceResponse.getContentLength();
        boolean contentRangeApplied = false;
        // 6. Content Range
        // for more information take a look here:
        // http://stackoverflow.com/questions/8293687/sample-http-range-request-session
        // if the content range header has been set directly
        // to the resource response use it otherwise calculate it
        String contentRange = resourceResponse.getContentRange();
        if (contentRange != null) {
            webResponse.setContentRange(contentRange);
        } else {
            // moment
            if (contentLength != -1 && ContentRangeType.BYTES.equals(acceptRange)) {
                contentRangeApplied = setResponseContentRangeHeaderFields(webResponse, attributes, contentLength);
            }
        }
        // 7. Content Length
        if (contentLength != -1 && !contentRangeApplied) {
            webResponse.setContentLength(contentLength);
        }
        // add custom headers and values
        final HttpHeaderCollection headers = resourceResponse.getHeaders();
        for (String name : headers.getHeaderNames()) {
            checkHeaderAccess(name);
            for (String value : headers.getHeaderValues(name)) {
                webResponse.addHeader(name, value);
            }
        }
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) WebResponse(org.apache.wicket.request.http.WebResponse) Time(org.apache.wicket.util.time.Time) HttpHeaderCollection(org.apache.wicket.request.HttpHeaderCollection)

Example 28 with WebResponse

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

the class WebPageRenderer method redirectTo.

/**
 * @param url
 * @param requestCycle
 */
protected void redirectTo(Url url, RequestCycle requestCycle) {
    bindSessionIfNeeded();
    WebResponse response = (WebResponse) requestCycle.getResponse();
    String relativeUrl = requestCycle.getUrlRenderer().renderUrl(url);
    response.sendRedirect(relativeUrl);
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) WebResponse(org.apache.wicket.request.http.WebResponse)

Example 29 with WebResponse

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

the class DefaultExceptionMapperTest method shouldDisableCaching.

/**
 * <a href="https://issues.apache.org/jira/browse/WICKET-4659">WICKET-4659</a>
 */
@Test
public void shouldDisableCaching() {
    WebResponse response = mock(WebResponse.class);
    tester.getRequestCycle().setResponse(response);
    new DefaultExceptionMapper().map(mock(Exception.class));
    verify(response).disableCaching();
    tester.destroy();
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Test(org.junit.Test)

Example 30 with WebResponse

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

the class CookiePage method onConfigure.

@Override
protected void onConfigure() {
    super.onConfigure();
    Cookie cookie = ((WebRequest) getRequest()).getCookie(cookieName);
    Assert.assertEquals(cookieValue, cookie.getValue());
    WebResponse response = (WebResponse) getResponse();
    response.addCookie(cookie);
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(org.apache.wicket.request.http.WebResponse) WebRequest(org.apache.wicket.request.http.WebRequest)

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