Search in sources :

Example 31 with ApplicationException

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

the class Collection method setPath.

/**
 * set the value path
 *
 * @param path value to set
 * @throws PageException
 */
public void setPath(String strPath) throws PageException {
    if (strPath == null)
        return;
    this.path = ResourceUtil.toResourceNotExisting(pageContext, strPath.trim());
    // this.path=new File(path.toLowerCase().trim());
    pageContext.getConfig().getSecurityManager().checkFileLocation(this.path);
    if (!this.path.exists()) {
        Resource parent = this.path.getParentResource();
        if (parent != null && parent.exists())
            this.path.mkdirs();
        else {
            throw new ApplicationException("attribute path of the tag collection must be a existing directory");
        }
    } else if (!this.path.isDirectory())
        throw new ApplicationException("attribute path of the tag collection must be a existing directory");
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) Resource(lucee.commons.io.res.Resource)

Example 32 with ApplicationException

use of lucee.runtime.exp.ApplicationException 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)

Example 33 with ApplicationException

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

the class Directory method actionRename.

/**
 * rename a directory to a new Name
 * @throws PageException
 */
public static void actionRename(PageContext pc, Resource directory, String strNewdirectory, String serverPassword, boolean createPath, Object acl, String storage) throws PageException {
    // check directory
    SecurityManager securityManager = pc.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pc.getConfig(), directory, serverPassword);
    if (!directory.exists())
        throw new ApplicationException("the directory [" + directory.toString() + "] doesn't exist");
    if (!directory.isDirectory())
        throw new ApplicationException("the file [" + directory.toString() + "] exists, but it isn't a directory");
    if (!directory.canRead())
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    if (strNewdirectory == null)
        throw new ApplicationException("the attribute [newDirectory] is not defined");
    // real to source
    Resource newdirectory = toDestination(pc, strNewdirectory, directory);
    securityManager.checkFileLocation(pc.getConfig(), newdirectory, serverPassword);
    if (newdirectory.exists())
        throw new ApplicationException("new directory [" + newdirectory.toString() + "] already exists");
    if (createPath) {
        newdirectory.getParentResource().mkdirs();
    }
    try {
        directory.moveTo(newdirectory);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    }
    // set S3 stuff
    setS3Attrs(pc, directory, acl, storage);
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) SecurityManager(lucee.runtime.security.SecurityManager) Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource)

Example 34 with ApplicationException

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

the class Directory method actionList.

/**
 * list all files and directories inside a directory
 * @throws PageException
 */
public static Object actionList(PageContext pageContext, Resource directory, String serverPassword, int type, ResourceFilter filter, int listInfo, boolean recurse, String sort) throws PageException {
    // check directory
    SecurityManager securityManager = pageContext.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pageContext.getConfig(), directory, serverPassword);
    if (type != TYPE_ALL) {
        ResourceFilter typeFilter = (type == TYPE_DIR) ? DIRECTORY_FILTER : FILE_FILTER;
        if (filter == null)
            filter = typeFilter;
        else
            filter = new AndResourceFilter(new ResourceFilter[] { typeFilter, filter });
    }
    // create query Object
    String[] names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory" };
    String[] types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR" };
    boolean hasMeta = directory instanceof ResourceMetaData;
    if (hasMeta) {
        names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory", "meta" };
        types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR", "OBJECT" };
    }
    Array array = null;
    Query query = null;
    Object rtn;
    if (listInfo == LIST_INFO_QUERY_ALL || listInfo == LIST_INFO_QUERY_NAME) {
        boolean listOnlyNames = listInfo == LIST_INFO_QUERY_NAME;
        rtn = query = new QueryImpl(listOnlyNames ? new String[] { "name" } : names, listOnlyNames ? new String[] { "VARCHAR" } : types, 0, "query");
    } else
        rtn = array = new ArrayImpl();
    if (!directory.exists()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    }
    if (!directory.isDirectory()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    }
    if (!directory.isReadable()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    }
    long startNS = System.nanoTime();
    try {
        // Query All
        if (listInfo == LIST_INFO_QUERY_ALL)
            _fillQueryAll(query, directory, filter, 0, hasMeta, recurse);
        else // Query Name
        if (listInfo == LIST_INFO_QUERY_NAME) {
            if (recurse || type != TYPE_ALL)
                _fillQueryNamesRec("", query, directory, filter, 0, recurse);
            else
                _fillQueryNames(query, directory, filter, 0);
        } else // Array Name/Path
        if (listInfo == LIST_INFO_ARRAY_NAME || listInfo == LIST_INFO_ARRAY_PATH) {
            boolean onlyName = listInfo == LIST_INFO_ARRAY_NAME;
            if (// QueryNamesRec("",query, directory, filter, 0,recurse);
            !onlyName || recurse || type != TYPE_ALL)
                // QueryNamesRec("",query, directory, filter, 0,recurse);
                _fillArrayPathOrName(array, directory, filter, 0, recurse, onlyName);
            else
                _fillArrayName(array, directory, filter, 0);
        }
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    // sort
    if (sort != null && query != null) {
        String[] arr = sort.toLowerCase().split(",");
        for (int i = arr.length - 1; i >= 0; i--) {
            try {
                String[] col = arr[i].trim().split("\\s+");
                if (col.length == 1)
                    query.sort(col[0].trim());
                else if (col.length == 2) {
                    String order = col[1].toLowerCase().trim();
                    if (order.equals("asc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_ASC);
                    else if (order.equals("desc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_DESC);
                    else
                        throw new ApplicationException("invalid order type [" + col[1] + "]");
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
            }
        }
    }
    if (query != null)
        query.setExecutionTime(System.nanoTime() - startNS);
    return rtn;
}
Also used : SecurityManager(lucee.runtime.security.SecurityManager) Query(lucee.runtime.type.Query) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) ArrayImpl(lucee.runtime.type.ArrayImpl) FileResource(lucee.commons.io.res.type.file.FileResource) IOException(java.io.IOException) Array(lucee.runtime.type.Array) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) QueryImpl(lucee.runtime.type.QueryImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ResourceMetaData(lucee.commons.io.res.ResourceMetaData)

Example 35 with ApplicationException

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

the class Directory method actionCopy.

public static void actionCopy(PageContext pc, Resource directory, String strDestination, String serverPassword, boolean createPath, Object acl, String storage, final ResourceFilter filter, boolean recurse, int nameconflict) throws PageException {
    // check directory
    SecurityManager securityManager = pc.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pc.getConfig(), directory, serverPassword);
    if (!directory.exists())
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    if (!directory.isDirectory())
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    if (!directory.canRead())
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    if (StringUtil.isEmpty(strDestination))
        throw new ApplicationException("attribute destination is not defined");
    // real to source
    Resource newdirectory = toDestination(pc, strDestination, directory);
    if (nameconflict == NAMECONFLICT_ERROR && newdirectory.exists())
        throw new ApplicationException("new directory [" + newdirectory.toString() + "] already exist");
    securityManager.checkFileLocation(pc.getConfig(), newdirectory, serverPassword);
    try {
        boolean clearEmpty = false;
        // has already a filter
        ResourceFilter f = null;
        if (filter != null) {
            if (!recurse) {
                f = new AndResourceFilter(new ResourceFilter[] { filter, new NotResourceFilter(DirectoryResourceFilter.FILTER) });
            } else {
                clearEmpty = true;
                f = new OrResourceFilter(new ResourceFilter[] { filter, DirectoryResourceFilter.FILTER });
            }
        } else {
            if (!recurse)
                f = new NotResourceFilter(DirectoryResourceFilter.FILTER);
        }
        if (!createPath) {
            Resource p = newdirectory.getParentResource();
            if (p != null && !p.exists())
                throw new ApplicationException("parent directory for [" + newdirectory + "] doesn't exist");
        }
        ResourceUtil.copyRecursive(directory, newdirectory, f);
        if (clearEmpty)
            ResourceUtil.removeEmptyFolders(newdirectory, f == null ? null : new NotResourceFilter(filter));
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw new ApplicationException(t.getMessage());
    }
    // set S3 stuff
    setS3Attrs(pc, directory, acl, storage);
}
Also used : AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) ApplicationException(lucee.runtime.exp.ApplicationException) SecurityManager(lucee.runtime.security.SecurityManager) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter)

Aggregations

ApplicationException (lucee.runtime.exp.ApplicationException)173 IOException (java.io.IOException)41 Resource (lucee.commons.io.res.Resource)36 PageException (lucee.runtime.exp.PageException)30 Struct (lucee.runtime.type.Struct)25 SecurityException (lucee.runtime.exp.SecurityException)17 BundleException (org.osgi.framework.BundleException)16 StructImpl (lucee.runtime.type.StructImpl)15 MalformedURLException (java.net.MalformedURLException)13 Element (org.w3c.dom.Element)13 Array (lucee.runtime.type.Array)12 Key (lucee.runtime.type.Collection.Key)12 Iterator (java.util.Iterator)11 InputStream (java.io.InputStream)10 Query (lucee.runtime.type.Query)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ExpressionException (lucee.runtime.exp.ExpressionException)9 Entry (java.util.Map.Entry)8 PageContextImpl (lucee.runtime.PageContextImpl)8 ClassDefinition (lucee.runtime.db.ClassDefinition)8