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();
}
}
}
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);
}
}
}
}
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);
}
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();
}
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);
}
Aggregations