Search in sources :

Example 46 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class WebAppFile method open.

@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
@Override
public void open(String mode) throws ScriptException {
    if ("r".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "r");
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
    } else if ("r+".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.seek(0);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
        writable = true;
    } else if ("w".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.setLength(0);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        writable = true;
    } else if ("w+".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.setLength(0);
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
        writable = true;
    } else if ("a".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.seek(file.length());
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        writable = true;
    } else if ("a+".equals(mode)) {
        try {
            file = new RandomAccessFile(realPath, "rw");
            file.seek(file.length());
        } catch (FileNotFoundException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new ScriptException(e);
        }
        readable = true;
        writable = true;
    } else {
        String msg = "Invalid or unsupported file mode, path : " + realPath + ", mode : " + mode;
        log.error(msg);
        throw new ScriptException(msg);
    }
    opened = true;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 47 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class WebAppFile method close.

@Override
public void close() throws ScriptException {
    if (!opened) {
        return;
    }
    try {
        file.close();
        opened = false;
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 48 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class WebAppFile method getFilePath.

private String getFilePath(String fileURL) throws ScriptException {
    JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
    Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
    ServletContext context = (ServletContext) jaggeryContext.getProperty(Constants.SERVLET_CONTEXT);
    String parent = includesCallstack.lastElement();
    try {
        String[] keys = WebAppManager.getKeys(context.getContextPath(), parent, fileURL);
        fileURL = "/".equals(keys[1]) ? keys[2] : keys[1] + keys[2];
    } catch (NullPointerException ne) {
        throw new ScriptException("Invalid file path : " + fileURL, ne);
    }
    return fileURL;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) ServletContext(javax.servlet.ServletContext) JaggeryContext(org.jaggeryjs.scriptengine.engine.JaggeryContext)

Example 49 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class HostObjectUtil method readerToString.

public static String readerToString(Reader reader) throws ScriptException {
    StringBuilder sb = new StringBuilder();
    try {
        int data = reader.read();
        while (data != -1) {
            sb.append((char) data);
            data = reader.read();
        }
        return sb.toString();
    } catch (IOException e) {
        String msg = "Error while reading the content from the Reader";
        log.error(msg, e);
        throw new ScriptException(msg, e);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            log.warn(e);
        }
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 50 with ScriptException

use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.

the class JaggerySecurityDomain method getCodeSource.

@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public CodeSource getCodeSource() throws ScriptException {
    if (codeSource != null) {
        return codeSource;
    }
    URL url = null;
    try {
        String contextPath = servletContext.getRealPath("/");
        if (contextPath == null) {
            url = servletContext.getResource(scriptPath);
        } else {
            if (!contextPath.endsWith(File.separator)) {
                contextPath += File.separator;
            }
            url = new File(contextPath + scriptPath).getCanonicalFile().toURI().toURL();
        }
        codeSource = new CodeSource(url, (Certificate[]) null);
        return codeSource;
    } catch (IOException e) {
        throw new ScriptException(e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) IOException(java.io.IOException) CodeSource(java.security.CodeSource) File(java.io.File) URL(java.net.URL) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)83 IOException (java.io.IOException)15 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)13 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 RegistryException (org.wso2.carbon.registry.api.RegistryException)11 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)9 URL (java.net.URL)8 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)8 MalformedURLException (java.net.MalformedURLException)7 ServletContext (javax.servlet.ServletContext)6 ScriptCachingContext (org.jaggeryjs.scriptengine.cache.ScriptCachingContext)6 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 Context (org.mozilla.javascript.Context)5 File (java.io.File)4 StringReader (java.io.StringReader)4 Callable (java.util.concurrent.Callable)4 ExecutorService (java.util.concurrent.ExecutorService)4 FileItem (org.apache.commons.fileupload.FileItem)4 FileHostObject (org.jaggeryjs.hostobjects.file.FileHostObject)4 StreamHostObject (org.jaggeryjs.hostobjects.stream.StreamHostObject)4