Search in sources :

Example 11 with XWikiResponse

use of com.xpn.xwiki.web.XWikiResponse 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

XWikiResponse (com.xpn.xwiki.web.XWikiResponse)11 OutputStream (java.io.OutputStream)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 URL (java.net.URL)4 File (java.io.File)3 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)2 Test (org.junit.Test)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)1 XWikiURLFactory (com.xpn.xwiki.web.XWikiURLFactory)1 CachePolicy (com.xpn.xwiki.web.sx.SxSource.CachePolicy)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SchedulerException (org.quartz.SchedulerException)1