Search in sources :

Example 16 with ScriptException

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

the class RequestHostObject method parseMultipart.

private static void parseMultipart(RequestHostObject rho) throws ScriptException {
    if (rho.files != null) {
        return;
    }
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = null;
    try {
        items = upload.parseRequest(rho.request);
    } catch (FileUploadException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
    // Process the uploaded items
    String name;
    rho.files = rho.context.newObject(rho);
    for (Object obj : items) {
        FileItem item = (FileItem) obj;
        name = item.getFieldName();
        if (item.isFormField()) {
            ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
            if (x == null) {
                ArrayList<FileItem> array = new ArrayList<FileItem>(1);
                array.add(item);
                rho.parameterMap.put(name, array);
            } else {
                x.add(item);
            }
        } else {
            rho.files.put(item.getFieldName(), rho.files, rho.context.newObject(rho, "File", new Object[] { item }));
        }
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ScriptableObject(org.mozilla.javascript.ScriptableObject) LogHostObject(org.jaggeryjs.hostobjects.log.LogHostObject) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 17 with ScriptException

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

the class RequestHostObject method jsFunction_getStream.

public static Object jsFunction_getStream(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getStream";
    int argsCount = args.length;
    if (argsCount != 0) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    RequestHostObject rho = (RequestHostObject) thisObj;
    try {
        return cx.newObject(thisObj, "Stream", new Object[] { rho.request.getInputStream() });
    } catch (IOException e) {
        String msg = "Error occurred while reading Servlet InputStream";
        log.warn(msg, e);
        throw new ScriptException(msg, e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) IOException(java.io.IOException)

Example 18 with ScriptException

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

the class RequestHostObject method jsFunction_getParameter.

public static Object jsFunction_getParameter(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getParameter";
    FileItem item;
    int argsCount = args.length;
    if (argsCount != 1 && argsCount != 2) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string", args[0], false);
    }
    if (argsCount == 2 && !(args[1] instanceof String)) {
        HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "string", args[1], false);
    }
    String parameter = (String) args[0];
    RequestHostObject rho = (RequestHostObject) thisObj;
    if (!rho.isMultipart) {
        return getParameter(parameter, rho.request, rho);
    }
    parseMultipart(rho);
    if (rho.parameterMap.get(parameter) != null) {
        item = rho.parameterMap.get(parameter).get(0);
    } else {
        return null;
    }
    if (argsCount == 1) {
        return item.getString();
    }
    try {
        return item.getString((String) args[1]);
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
        throw new ScriptException(e);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 19 with ScriptException

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

the class RequestHostObject method jsFunction_getContent.

public static Object jsFunction_getContent(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
    String functionName = "getContent";
    int argsCount = args.length;
    if (argsCount != 0) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
    }
    RequestHostObject rho = (RequestHostObject) thisObj;
    try {
        if (rho.content != null) {
            return rho.content;
        }
        String data = HostObjectUtil.streamToString(rho.request.getInputStream());
        String contentType = rho.request.getContentType();
        if (contentType != null) {
            contentType = contentType.trim().toLowerCase();
            if (contentType.startsWith("application/json") || contentType.startsWith("application/json/badgerfish")) {
                rho.content = (data != null && !"".equals(data)) ? HostObjectUtil.parseJSON(cx, thisObj, data) : null;
            } else {
                rho.content = data;
            }
        } else {
            rho.content = data;
        }
        return rho.content;
    } catch (IOException e) {
        String msg = "Error occurred while reading Servlet InputStream";
        log.warn(msg, e);
        throw new ScriptException(msg, e);
    }
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) IOException(java.io.IOException)

Example 20 with ScriptException

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

the class WebAppFileManager method getFile.

@SuppressFBWarnings({ "PATH_TRAVERSAL_IN", "PATH_TRAVERSAL_IN" })
@Override
public File getFile(String path) throws ScriptException {
    if (path.startsWith(FILE_PATH)) {
        return new JavaScriptFileManagerImpl().getFile(path);
    }
    String oldPath = path;
    path = FilenameUtils.normalizeNoEndSeparator(path);
    if (path == null) {
        String msg = "Invalid file path : " + oldPath;
        log.error(msg);
        throw new ScriptException(msg);
    }
    File file = new File(context.getRealPath("/"), path);
    if (file.isDirectory()) {
        String msg = "File hostobject doesn't handle directories. Specified path contains a directory : " + path;
        log.error(msg);
        throw new ScriptException(msg);
    }
    return file;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) JavaScriptFileManagerImpl(org.jaggeryjs.hostobjects.file.JavaScriptFileManagerImpl) JavaScriptFile(org.jaggeryjs.hostobjects.file.JavaScriptFile) File(java.io.File) 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