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 + "]");
// }
}
}
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);
}
}
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);
}
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());
}
}
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);
}
Aggregations