Search in sources :

Example 1 with PostContentAbort

use of lucee.runtime.exp.PostContentAbort in project Lucee by lucee.

the class Range method _doStartTag.

private int _doStartTag() throws PageException {
    // check the file before doing anyrhing else
    Resource file = null;
    if (content == null && !StringUtil.isEmpty(strFile))
        file = ResourceUtil.toResourceExisting(pageContext, strFile);
    // get response object
    HttpServletResponse rsp = pageContext.getHttpServletResponse();
    // check committed
    if (rsp.isCommitted())
        throw new ApplicationException("content is already flushed", "you can't rewrite head of response after part of the page is flushed");
    // set type
    if (!StringUtil.isEmpty(type, true)) {
        type = type.trim();
        ReqRspUtil.setContentType(rsp, type);
        // TODO more dynamic implementation, configuration in admin?
        if (!HTTPUtil.isTextMimeType(type)) {
            ((PageContextImpl) pageContext).getRootOut().setAllowCompression(false);
        }
    }
    Range[] ranges = getRanges();
    boolean hasRanges = ranges != null && ranges.length > 0;
    if (_range == RANGE_YES || hasRanges) {
        rsp.setHeader("Accept-Ranges", "bytes");
    } else if (_range == RANGE_NO) {
        rsp.setHeader("Accept-Ranges", "none");
        hasRanges = false;
    }
    // set content
    if (this.content != null || file != null) {
        pageContext.clear();
        InputStream is = null;
        OutputStream os = null;
        long totalLength, contentLength;
        try {
            os = getOutputStream();
            if (content != null) {
                // ReqRspUtil.setContentLength(rsp,content.length);
                contentLength = content.length;
                totalLength = content.length;
                is = new BufferedInputStream(new ByteArrayInputStream(content));
            } else {
                // ReqRspUtil.setContentLength(rsp,file.length());
                pageContext.getConfig().getSecurityManager().checkFileLocation(file);
                contentLength = totalLength = file.length();
                is = IOUtil.toBufferedInputStream(file.getInputStream());
            }
            // write
            if (!hasRanges)
                IOUtil.copy(is, os, false, false);
            else {
                contentLength = 0;
                long off, len, to;
                for (int i = 0; i < ranges.length; i++) {
                    off = ranges[i].from;
                    if (ranges[i].to == -1) {
                        len = -1;
                        to = totalLength - 1;
                    } else {
                        to = ranges[i].to;
                        if (to >= totalLength)
                            to = totalLength - 1;
                        len = to - ranges[i].from + 1;
                    }
                    rsp.addHeader("Content-Range", "bytes " + off + "-" + to + "/" + Caster.toString(totalLength));
                    rsp.setStatus(206);
                    // print.e("Content-Range: bytes "+off+"-"+to+"/"+Caster.toString(totalLength));
                    contentLength += to - off + 1L;
                    // ReqRspUtil.setContentLength(rsp,len);
                    IOUtil.copy(is, os, off, len);
                }
            }
            if (!(os instanceof GZIPOutputStream))
                ReqRspUtil.setContentLength(rsp, contentLength);
        } catch (IOException ioe) {
        } finally {
            IOUtil.flushEL(os);
            IOUtil.closeEL(is, os);
            if (deletefile && file != null)
                ResourceUtil.removeEL(file, true);
            ((PageContextImpl) pageContext).getRootOut().setClosed(true);
        }
        throw new PostContentAbort();
    } else // clear current content
    if (reset)
        pageContext.clear();
    // EVAL_PAGE;
    return EVAL_BODY_INCLUDE;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Resource(lucee.commons.io.res.Resource) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ApplicationException(lucee.runtime.exp.ApplicationException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PostContentAbort(lucee.runtime.exp.PostContentAbort)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Resource (lucee.commons.io.res.Resource)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 PostContentAbort (lucee.runtime.exp.PostContentAbort)1