Search in sources :

Example 1 with ServletResponse

use of org.xwiki.container.servlet.ServletResponse in project xwiki-platform by xwiki.

the class BatikSVGRasterizer method rasterizeToResponse.

@Override
public void rasterizeToResponse(String content, int width, int height) throws IOException {
    if (!(this.container.getResponse() instanceof ServletResponse)) {
        return;
    }
    HttpServletResponse response = ((ServletResponse) this.container.getResponse()).getHttpServletResponse();
    File result = rasterizeToTemporaryFile(content, width, height);
    if (result == null) {
        return;
    }
    response.setContentLength((int) result.length());
    response.setContentType("image/png");
    OutputStream os = response.getOutputStream();
    FileUtils.copyFile(result, os);
    os.flush();
}
Also used : ServletResponse(org.xwiki.container.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) File(java.io.File)

Example 2 with ServletResponse

use of org.xwiki.container.servlet.ServletResponse in project xwiki-platform by xwiki.

the class DefaultXWikiContextInitializer method initialize.

@Override
public XWikiContext initialize(ExecutionContext econtext) throws XWikiException {
    Request request = this.container.getRequest();
    XWikiContext xcontext;
    if (!(request instanceof ServletRequest)) {
        if (this.fallbackOnStub) {
            xcontext = this.contextProvider.createStubContext();
        } else {
            throw new XWikiException(XWikiException.MODULE_XWIKI_USER, XWikiException.ERROR_XWIKI_USER_INIT, "Unsupported request type [" + request.getClass() + "]");
        }
    } else {
        try {
            HttpServletRequest httpServletRequest = ((ServletRequest) request).getHttpServletRequest();
            HttpServletResponse httpServletReponse = ((ServletResponse) this.container.getResponse()).getHttpServletResponse();
            xcontext = initializeXWikiContext(httpServletRequest, httpServletReponse);
            // Put the XWikiContext in the ExecutionContext in case the authenticator needs it
            if (econtext != null) {
                xcontext.declareInExecutionContext(econtext);
            }
            if (this.authenticate) {
                authenticate(xcontext);
            }
        } catch (XWikiException e) {
            if (this.fallbackOnStub) {
                xcontext = this.contextProvider.createStubContext();
            } else {
                throw new XWikiException(XWikiException.MODULE_XWIKI_USER, XWikiException.ERROR_XWIKI_USER_INIT, "Failed to initialize XWikiContext", e);
            }
        }
    }
    // Put the XWikiContext in the ExecutionContext
    if (econtext != null) {
        xcontext.declareInExecutionContext(econtext);
    }
    return xcontext;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) ServletResponse(org.xwiki.container.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) Request(org.xwiki.container.Request) XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.xwiki.container.servlet.ServletRequest) XWikiContext(com.xpn.xwiki.XWikiContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with ServletResponse

use of org.xwiki.container.servlet.ServletResponse in project xwiki-platform by xwiki.

the class AbstractServletResourceReferenceHandler method setResponseHeaders.

/**
 * Sets the response headers needed to cache the resource permanently, if the resource can be cached.
 *
 * @param response the response
 * @param resourceReference the resource that is being served
 */
private void setResponseHeaders(Response response, R resourceReference) {
    // Cache the resource if possible.
    if (response instanceof ServletResponse && isResourceCacheable(resourceReference)) {
        HttpServletResponse httpResponse = ((ServletResponse) response).getHttpServletResponse();
        httpResponse.setHeader(HttpHeaders.CACHE_CONTROL, "public");
        httpResponse.setDateHeader(HttpHeaders.EXPIRES, new Date().getTime() + CACHE_DURATION);
        // Even if the resource is cached permanently, most browsers are still sending a request if the user reloads
        // the page using F5. We send back the "Last-Modified" header in the response so that the browser will send
        // us an "If-Modified-Since" request for any subsequent call for this static resource. When this happens we
        // return a 304 to tell the browser to use its cached version.
        httpResponse.setDateHeader(HttpHeaders.LAST_MODIFIED, new Date().getTime());
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(org.xwiki.container.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Date(java.util.Date)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ServletResponse (org.xwiki.container.servlet.ServletResponse)3 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Request (org.xwiki.container.Request)1 ServletRequest (org.xwiki.container.servlet.ServletRequest)1