Search in sources :

Example 1 with HttpHeaderCollection

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

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpHeaderCollection (org.apache.wicket.request.HttpHeaderCollection)1 Response (org.apache.wicket.request.Response)1 WebResponse (org.apache.wicket.request.http.WebResponse)1 Time (org.apache.wicket.util.time.Time)1