Search in sources :

Example 1 with CachePolicy

use of com.xpn.xwiki.web.sx.SxSource.CachePolicy in project xwiki-platform by xwiki.

the class AbstractSxAction method renderExtension.

/**
 * This method must be called by render(XWikiContext). Render is in charge of creating the proper source and
 * extension type, and pass it as an argument to this method which will forge the proper response using those.
 *
 * @param sxSource the source of the extension.
 * @param sxType the type of extension
 * @param context the XWiki context when rendering the skin extension.
 * @throws XWikiException when an error occurs when building the response.
 */
public void renderExtension(SxSource sxSource, Extension sxType, XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    String extensionContent = sxSource.getContent();
    response.setContentType(sxType.getContentType());
    if (sxSource.getLastModifiedDate() > 0) {
        response.setDateHeader(LAST_MODIFIED_HEADER, sxSource.getLastModifiedDate());
    }
    CachePolicy cachePolicy = sxSource.getCachePolicy();
    if (cachePolicy != CachePolicy.FORBID) {
        response.setHeader(CACHE_CONTROL_HEADER, "public");
    }
    if (cachePolicy == CachePolicy.LONG) {
        // Cache for one month (30 days)
        response.setDateHeader(CACHE_EXPIRES_HEADER, (new Date()).getTime() + LONG_CACHE_DURATION);
    } else if (cachePolicy == CachePolicy.SHORT) {
        // Cache for one day
        response.setDateHeader(CACHE_EXPIRES_HEADER, (new Date()).getTime() + SHORT_CACHE_DURATION);
    } else if (cachePolicy == CachePolicy.FORBID) {
        response.setHeader(CACHE_CONTROL_HEADER, "no-cache, no-store, must-revalidate");
    }
    if (BooleanUtils.toBoolean(StringUtils.defaultIfEmpty(request.get(COMPRESS_SCRIPT_REQUEST_PARAMETER), "true"))) {
        extensionContent = sxType.getCompressor().compress(extensionContent);
    }
    try {
        response.setContentLength(extensionContent.getBytes(RESPONSE_CHARACTER_SET).length);
        response.getOutputStream().write(extensionContent.getBytes(RESPONSE_CHARACTER_SET));
    } catch (IOException ex) {
        getLogger().warn("Failed to send SX content: [{}]", ex.getMessage());
    }
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) CachePolicy(com.xpn.xwiki.web.sx.SxSource.CachePolicy) XWikiResponse(com.xpn.xwiki.web.XWikiResponse) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 XWikiResponse (com.xpn.xwiki.web.XWikiResponse)1 CachePolicy (com.xpn.xwiki.web.sx.SxSource.CachePolicy)1 IOException (java.io.IOException)1 Date (java.util.Date)1