Search in sources :

Example 36 with ApplicationException

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

the class Directory method actionCreate.

/**
 * create a directory
 * @throws PageException
 */
public static void actionCreate(PageContext pc, Resource directory, String serverPassword, boolean createPath, int mode, Object acl, String storage, int nameConflict) throws PageException {
    SecurityManager securityManager = pc.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pc.getConfig(), directory, serverPassword);
    if (directory.exists()) {
        if (directory.isDirectory()) {
            if (nameConflict == NAMECONFLICT_SKIP)
                return;
            throw new ApplicationException("directory [" + directory.toString() + "] already exist");
        } else if (directory.isFile())
            throw new ApplicationException("can't create directory [" + directory.toString() + "], it exist a file with same name");
    }
    // if(!directory.mkdirs())	throw new ApplicationException("can't create directory ["+directory.toString()+"]");
    try {
        directory.createDirectory(createPath);
    } catch (IOException ioe) {
        throw Caster.toPageException(ioe);
    }
    // set S3 stuff
    setS3Attrs(pc, directory, acl, storage);
    // Set Mode
    if (mode != -1) {
        try {
            directory.setMode(mode);
        // FileUtil.setMode(directory,mode);
        } catch (IOException e) {
            throw Caster.toPageException(e);
        }
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) SecurityManager(lucee.runtime.security.SecurityManager) IOException(java.io.IOException)

Example 37 with ApplicationException

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

the class Feed method doActionCreate.

private void doActionCreate() throws PageException {
    // name
    Query qry;
    Struct props;
    boolean splitString = true;
    if (name != null) {
        Struct data;
        if (name instanceof String) {
            data = Caster.toStruct(pageContext.getVariable(Caster.toString(name)));
        } else
            data = Caster.toStruct(name, false);
        qry = FeedQuery.toQuery(data, false);
        props = FeedProperties.toProperties(data);
        splitString = false;
    } else if (query != null && properties != null) {
        qry = FeedQuery.toQuery(Caster.toQuery(query));
        props = FeedProperties.toProperties(Caster.toStruct(properties, false));
    } else {
        throw new ApplicationException("missing attribute [name] or attributes [query] and [properties]");
    }
    StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    if (type == TYPE_AUTO) {
        String version = Caster.toString(props.get("version", "rss"), "rss");
        type = StringUtil.startsWithIgnoreCase(version, "rss") ? TYPE_RSS : TYPE_ATOM;
    }
    if (type == TYPE_RSS) {
        createRSS(xml, qry, props, splitString);
    } else {
        createAtom(xml, qry, props, splitString);
    }
    // variable
    if (!StringUtil.isEmpty(xmlVar)) {
        pageContext.setVariable(xmlVar, xml);
    }
    // file
    if (outputFile != null) {
        if (outputFile.exists() && !overwrite)
            throw new ApplicationException("destiniation file [" + outputFile + "] already exist");
        if (StringUtil.isEmpty(charset))
            charset = ((PageContextImpl) pageContext).getResourceCharset().name();
        try {
            IOUtil.write(outputFile, xml.toString(), charset, false);
        } catch (IOException e) {
            throw Caster.toPageException(e);
        }
    }
/*
<cffeed
    action = "create"
    name = "#structure#"
        One or both of the following:
    outputFile = "path"
    xmlVar = "variable name"
    optional 
    overwrite = "no|yes">
 
    <cffeed
    action = "create"
    properties = "#metadata structure#"
    query = "#items/entries query name#"
        One or both of the following:
    outputFile = "path"
    xmlVar = "variable name"
    optional
    columnMap = "mapping structure"
    overwrite = "no|yes"> 
		 */
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) FeedQuery(lucee.runtime.text.feed.FeedQuery) Query(lucee.runtime.type.Query) GetHttpTimeString(lucee.runtime.functions.dateTime.GetHttpTimeString) IOException(java.io.IOException) Struct(lucee.runtime.type.Struct)

Example 38 with ApplicationException

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

the class FileTag method actionDelete.

/**
 * copy source file to destination file or path
 * @throws PageException
 */
private void actionDelete() throws PageException {
    checkFile(pageContext, securityManager, file, serverPassword, false, false, false, false);
    setACL(pageContext, file, acl);
    try {
        if (!file.delete())
            throw new ApplicationException("can't delete file [" + file + "]");
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw new ApplicationException(t.getMessage());
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException)

Example 39 with ApplicationException

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

the class FileTag method actionWrite.

/**
 * write to the source file
 * @throws PageException
 */
private void actionWrite() 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 (output instanceof InputStream) {
            IOUtil.copy((InputStream) output, file, false);
        } else if (Decision.isCastableToBinary(output, false)) {
            IOUtil.copy(new ByteArrayInputStream(Caster.toBinary(output)), file, true);
        } else {
            String content = Caster.toString(output);
            if (fixnewline)
                content = doFixNewLine(content);
            if (addnewline)
                content += SystemUtil.getOSSpecificLineSeparator();
            IOUtil.write(file, content, CharsetUtil.toCharset(charset), false);
        }
    } catch (UnsupportedEncodingException e) {
        throw new ApplicationException("Unsupported Charset Definition [" + charset + "]", e.getMessage());
    } catch (IOException e) {
        throw new ApplicationException("can't write file " + file.getAbsolutePath(), e.getMessage());
    }
    setMode(file, mode);
    setAttributes(file, attributes);
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 40 with ApplicationException

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

the class FileTag method _actionUpload.

private static Struct _actionUpload(PageContext pageContext, lucee.runtime.security.SecurityManager securityManager, FormItem formItem, String strDestination, int nameconflict, String accept, boolean strict, int mode, String attributes, Object acl, String serverPassword) throws PageException {
    if (nameconflict == NAMECONFLICT_UNDEFINED)
        nameconflict = NAMECONFLICT_ERROR;
    boolean fileWasRenamed = false;
    boolean fileWasAppended = false;
    boolean fileExisted = false;
    boolean fileWasOverwritten = false;
    String contentType = ResourceUtil.getMimeType(formItem.getResource(), formItem.getContentType());
    // set cffile struct
    Struct cffile = new StructImpl();
    long length = formItem.getResource().length();
    cffile.set("timecreated", new DateTimeImpl(pageContext.getConfig()));
    cffile.set("timelastmodified", new DateTimeImpl(pageContext.getConfig()));
    cffile.set("datelastaccessed", new DateImpl(pageContext));
    cffile.set("oldfilesize", Long.valueOf(length));
    cffile.set("filesize", Long.valueOf(length));
    cffile.set("contenttype", ListFirst.call(pageContext, contentType, "/"));
    cffile.set("contentsubtype", ListLast.call(pageContext, contentType, "/"));
    // client file
    String strClientFile = formItem.getName();
    while (strClientFile.indexOf('\\') != -1) strClientFile = strClientFile.replace('\\', '/');
    Resource clientFile = pageContext.getConfig().getResource(strClientFile);
    String clientFileName = clientFile.getName();
    // check file type
    checkContentType(contentType, accept, getFileExtension(clientFile), strict);
    cffile.set("clientdirectory", getParent(clientFile));
    cffile.set("clientfile", clientFile.getName());
    cffile.set("clientfileext", getFileExtension(clientFile));
    cffile.set("clientfilename", getFileName(clientFile));
    // check destination
    if (StringUtil.isEmpty(strDestination))
        throw new ApplicationException("attribute destination is not defined in tag file");
    Resource destination = toDestination(pageContext, strDestination, null);
    securityManager.checkFileLocation(pageContext.getConfig(), destination, serverPassword);
    if (destination.isDirectory())
        destination = destination.getRealResource(clientFileName);
    else if (!destination.exists() && (strDestination.endsWith("/") || strDestination.endsWith("\\")))
        destination = destination.getRealResource(clientFileName);
    else if (!clientFileName.equalsIgnoreCase(destination.getName())) {
        if (ResourceUtil.getExtension(destination, null) == null)
            destination = destination.getRealResource(clientFileName);
        else
            fileWasRenamed = true;
    }
    // check parent destination -> directory of the desinatrion
    Resource parentDestination = destination.getParentResource();
    if (!parentDestination.exists()) {
        Resource pp = parentDestination.getParentResource();
        if (pp == null || !pp.exists())
            throw new ApplicationException("attribute destination has an invalid value [" + destination + "], directory [" + parentDestination + "] doesn't exist");
        try {
            parentDestination.createDirectory(true);
        } catch (IOException e) {
            throw Caster.toPageException(e);
        }
    } else if (!parentDestination.canWrite())
        throw new ApplicationException("can't write to destination directory [" + parentDestination + "], no access to write");
    // set server variables
    cffile.set("serverdirectory", getParent(destination));
    cffile.set("serverfile", destination.getName());
    cffile.set("serverfileext", getFileExtension(destination));
    cffile.set("serverfilename", getFileName(destination));
    cffile.set("attemptedserverfile", destination.getName());
    // check nameconflict
    if (destination.exists()) {
        fileExisted = true;
        if (nameconflict == NAMECONFLICT_ERROR) {
            throw new ApplicationException("destination file [" + destination + "] already exist");
        } else if (nameconflict == NAMECONFLICT_SKIP) {
            cffile.set("fileexisted", Caster.toBoolean(fileExisted));
            cffile.set("filewasappended", Boolean.FALSE);
            cffile.set("filewasoverwritten", Boolean.FALSE);
            cffile.set("filewasrenamed", Boolean.FALSE);
            cffile.set("filewassaved", Boolean.FALSE);
            return cffile;
        } else if (nameconflict == NAMECONFLICT_MAKEUNIQUE) {
            destination = makeUnique(destination);
            fileWasRenamed = true;
            // if(fileWasRenamed) {
            cffile.set("serverdirectory", getParent(destination));
            cffile.set("serverfile", destination.getName());
            cffile.set("serverfileext", getFileExtension(destination));
            cffile.set("serverfilename", getFileName(destination));
            cffile.set("attemptedserverfile", destination.getName());
        // }
        } else if (nameconflict == NAMECONFLICT_OVERWRITE) {
            // fileWasAppended=true;
            fileWasOverwritten = true;
            if (!destination.delete())
                if (// hier hatte ich concurrent problem das damit ausgeraeumt ist
                destination.exists())
                    throw new ApplicationException("can't delete destination file [" + destination + "]");
        }
    // for "overwrite" no action is neded
    }
    setACL(pageContext, destination, acl);
    try {
        destination.createNewFile();
        IOUtil.copy(formItem.getResource(), destination);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    }
    // Set cffile/file struct
    cffile.set("fileexisted", Caster.toBoolean(fileExisted));
    cffile.set("filewasappended", Caster.toBoolean(fileWasAppended));
    cffile.set("filewasoverwritten", Caster.toBoolean(fileWasOverwritten));
    cffile.set("filewasrenamed", Caster.toBoolean(fileWasRenamed));
    cffile.set("filewassaved", Boolean.TRUE);
    setMode(destination, mode);
    setAttributes(destination, attributes);
    return cffile;
}
Also used : StructImpl(lucee.runtime.type.StructImpl) ApplicationException(lucee.runtime.exp.ApplicationException) Resource(lucee.commons.io.res.Resource) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) IOException(java.io.IOException) DateImpl(lucee.runtime.type.dt.DateImpl) Struct(lucee.runtime.type.Struct)

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