Search in sources :

Example 96 with ApplicationException

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

the class Execute method _execute.

private void _execute() throws Exception {
    Object monitor = new SerializableObject();
    String command = "";
    if (name == null) {
        if (StringUtil.isEmpty(body)) {
            required("execute", "name", name);
            required("execute", "arguments", arguments);
        } else
            command = body;
    } else {
        if (arguments == null)
            command = name;
        else
            command = name + arguments;
    }
    _Execute execute = new _Execute(pageContext, monitor, command, outputfile, variable, errorFile, errorVariable);
    // if(timeout<=0)execute._run();
    // else {
    execute.start();
    if (timeout > 0) {
        try {
            synchronized (monitor) {
                monitor.wait(timeout);
            }
        } finally {
            execute.abort(terminateOnTimeout);
        }
        if (execute.hasException()) {
            throw execute.getException();
        }
        if (!execute.hasFinished())
            throw new ApplicationException("timeout [" + (timeout) + " ms] expired while executing [" + command + "]");
    // }
    }
}
Also used : SerializableObject(lucee.commons.lang.SerializableObject) ApplicationException(lucee.runtime.exp.ApplicationException) SerializableObject(lucee.commons.lang.SerializableObject)

Example 97 with ApplicationException

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

the class Feed method doActionRead.

private void doActionRead() throws IOException, SAXException, PageException {
    required("Feed", "read", "source", source);
    if (outputFile != null && outputFile.exists() && !overwrite)
        throw new ApplicationException("outputFile file [" + outputFile + "] already exists");
    String charset = null;
    // xmlVar
    if (outputFile != null) {
        IOUtil.copy(source, outputFile);
    }
    // outputFile
    String strFeed = null;
    if (!StringUtil.isEmpty(xmlVar)) {
        strFeed = IOUtil.toString(outputFile != null ? outputFile : source, charset);
        pageContext.setVariable(xmlVar, strFeed);
    }
    // Input Source
    InputSource is = null;
    Reader r = null;
    if (strFeed != null)
        is = new InputSource(new StringReader(strFeed));
    else if (outputFile != null)
        is = new InputSource(r = IOUtil.getReader(outputFile, charset));
    else
        is = new InputSource(r = IOUtil.getReader(source, charset));
    is.setSystemId(source.getPath());
    try {
        FeedHandler feed = new FeedHandler(source);
        Struct data = feed.getData();
        // properties
        if (properties != null) {
            String strProp = Caster.toString(properties, null);
            if (strProp == null)
                throw new ApplicationException("attribute [properties] should be of type string");
            pageContext.setVariable(strProp, FeedProperties.toProperties(data));
        }
        // query or enclosure
        lucee.runtime.type.Query qry = null;
        if (query != null || enclosureDir != null) {
            qry = FeedQuery.toQuery(data, feed.hasDC());
        }
        // query
        if (query != null) {
            String strQuery = Caster.toString(query, null);
            if (strQuery == null)
                throw new ApplicationException("attribute [query] should be of type string");
            pageContext.setVariable(strQuery, qry);
        }
        if (enclosureDir != null) {
            int rows = qry.getRowCount();
            String strUrl = null;
            Resource src, dest;
            for (int row = 1; row <= rows; row++) {
                strUrl = Caster.toString(qry.getAt(FeedQuery.LINKHREF, row, null), null);
                if (!StringUtil.isEmpty(strUrl)) {
                    src = ResourceUtil.toResourceNotExisting(pageContext, strUrl);
                    dest = enclosureDir.getRealResource(src.getName());
                    if (!ignoreEnclosureError && !overwriteEnclosure && dest.exists())
                        throw new ApplicationException("enclosure file [" + dest + "] already exists");
                    try {
                        IOUtil.copy(src, dest);
                    } catch (IOException ioe) {
                        if (!ignoreEnclosureError)
                            throw ioe;
                    }
                }
            }
        }
        // name
        if (name != null) {
            String strName = Caster.toString(name, null);
            if (strName == null)
                throw new ApplicationException("attribute [name] should be of type string");
            pageContext.setVariable(strName, data);
        }
    } finally {
        IOUtil.closeEL(r);
    }
}
Also used : InputSource(org.xml.sax.InputSource) HTTPResource(lucee.commons.io.res.type.http.HTTPResource) Resource(lucee.commons.io.res.Resource) Reader(java.io.Reader) StringReader(java.io.StringReader) GetHttpTimeString(lucee.runtime.functions.dateTime.GetHttpTimeString) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct) ApplicationException(lucee.runtime.exp.ApplicationException) FeedHandler(lucee.runtime.text.feed.FeedHandler) StringReader(java.io.StringReader) Query(lucee.runtime.type.Query)

Example 98 with ApplicationException

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

the class FileTag method actionMove.

/**
 * move source file to destination path or file
 * @throws PageException
 */
public static void actionMove(PageContext pageContext, lucee.runtime.security.SecurityManager securityManager, Resource source, String strDestination, int nameconflict, String serverPassword, Object acl, int mode, String attributes) throws PageException {
    if (nameconflict == NAMECONFLICT_UNDEFINED)
        nameconflict = NAMECONFLICT_OVERWRITE;
    if (source == null)
        throw new ApplicationException("attribute source is not defined for tag file");
    if (StringUtil.isEmpty(strDestination))
        throw new ApplicationException("attribute destination is not defined for tag file");
    Resource destination = toDestination(pageContext, strDestination, source);
    securityManager.checkFileLocation(pageContext.getConfig(), source, serverPassword);
    securityManager.checkFileLocation(pageContext.getConfig(), destination, serverPassword);
    if (source.equals(destination))
        return;
    // source
    if (!source.exists())
        throw new ApplicationException("source file [" + source.toString() + "] doesn't exist");
    else if (!source.isFile())
        throw new ApplicationException("source file [" + source.toString() + "] is not a file");
    else if (!source.isReadable() || !source.isWriteable())
        throw new ApplicationException("no access to source file [" + source.toString() + "]");
    // destination
    if (destination.isDirectory())
        destination = destination.getRealResource(source.getName());
    if (destination.exists()) {
        // SKIP
        if (nameconflict == NAMECONFLICT_SKIP)
            return;
        else // OVERWRITE
        if (nameconflict == NAMECONFLICT_OVERWRITE)
            destination.delete();
        else // MAKEUNIQUE
        if (nameconflict == NAMECONFLICT_MAKEUNIQUE)
            destination = makeUnique(destination);
        else
            // ERROR
            throw new ApplicationException("destiniation file [" + destination.toString() + "] already exist");
    }
    setACL(pageContext, destination, acl);
    try {
        source.moveTo(destination);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw new ApplicationException(t.getMessage());
    }
    setMode(destination, mode);
    setAttributes(destination, attributes);
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) Resource(lucee.commons.io.res.Resource)

Example 99 with ApplicationException

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

the class FileTag method actionRead.

/**
 * read source file
 * @throws PageException
 */
private void actionRead(boolean isBinary) throws PageException {
    if (variable == null)
        throw new ApplicationException("attribute variable is not defined for tag file");
    // check if we can use cache
    if (StringUtil.isEmpty(cachedWithin)) {
        Object tmp = ((PageContextImpl) pageContext).getCachedWithin(ConfigWeb.CACHEDWITHIN_FILE);
        if (tmp != null)
            setCachedwithin(tmp);
    }
    String cacheId = createCacheId(isBinary);
    CacheHandler cacheHandler = null;
    if (cachedWithin != null) {
        cacheHandler = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_FILE, null).getInstanceMatchingObject(cachedWithin, null);
        if (cacheHandler instanceof CacheHandlerPro) {
            CacheItem cacheItem = ((CacheHandlerPro) cacheHandler).get(pageContext, cacheId, cachedWithin);
            if (cacheItem instanceof FileCacheItem) {
                pageContext.setVariable(variable, ((FileCacheItem) cacheItem).getData());
                return;
            }
        } else if (cacheHandler != null) {
            // TODO this else block can be removed when all cache handlers implement CacheHandlerPro
            CacheItem cacheItem = cacheHandler.get(pageContext, cacheId);
            if (cacheItem instanceof FileCacheItem) {
                pageContext.setVariable(variable, ((FileCacheItem) cacheItem).getData());
                return;
            }
        }
    }
    // cache not found, process and cache result if needed
    checkFile(pageContext, securityManager, file, serverPassword, false, false, true, false);
    try {
        long start = System.nanoTime();
        Object data = isBinary ? IOUtil.toBytes(file) : IOUtil.toString(file, CharsetUtil.toCharset(charset));
        pageContext.setVariable(variable, data);
        if (cacheHandler != null)
            cacheHandler.set(pageContext, cacheId, cachedWithin, FileCacheItem.getInstance(file.getAbsolutePath(), data, System.nanoTime() - start));
    } catch (IOException e) {
        throw new ApplicationException("can't read file [" + file.toString() + "]", e.getMessage());
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) CacheHandlerPro(lucee.runtime.cache.tag.CacheHandlerPro) FileCacheItem(lucee.runtime.cache.tag.file.FileCacheItem) PageContextImpl(lucee.runtime.PageContextImpl) FileCacheItem(lucee.runtime.cache.tag.file.FileCacheItem) CacheItem(lucee.runtime.cache.tag.CacheItem) IOException(java.io.IOException) CacheHandler(lucee.runtime.cache.tag.CacheHandler)

Example 100 with ApplicationException

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

the class FileTag method actionAppend.

/**
 * append data to source file
 * @throws PageException
 */
private void actionAppend() throws PageException {
    if (output == null)
        throw new ApplicationException("attribute output is not defined for tag file");
    checkFile(pageContext, securityManager, file, serverPassword, createPath, true, false, true);
    setACL(pageContext, file, acl);
    try {
        if (!file.exists())
            file.createNewFile();
        String content = Caster.toString(output);
        if (fixnewline)
            content = doFixNewLine(content);
        if (addnewline)
            content += SystemUtil.getOSSpecificLineSeparator();
        IOUtil.write(file, content, CharsetUtil.toCharset(charset), true);
    } catch (UnsupportedEncodingException e) {
        throw new ApplicationException("Unsupported Charset Definition [" + charset + "]", e.getMessage());
    } catch (IOException e) {
        throw new ApplicationException("can't write file", e.getMessage());
    }
    setMode(file, mode);
    setAttributes(file, attributes);
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

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