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);
}
}
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);
}
}
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;
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class RhinoEngine method execFunc.
private static Object execFunc(String funcName, Object[] args, ScriptableObject thiz, ScriptableObject scope, Context cx) throws ScriptException {
Object object = scope.get(funcName, scope);
if (!(object instanceof Function)) {
String msg = "Function cannot be found with the name '" + funcName + "', but a " + object.toString();
log.error(msg);
throw new ScriptException(msg);
}
try {
return ((Function) object).call(cx, scope, thiz, args);
} catch (Exception e) {
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class FileHostObject method loadMimeMap.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
private static FileTypeMap loadMimeMap() throws ScriptException {
String configDirPath = CarbonUtils.getEtcCarbonConfigDirPath();
File configFile = new File(configDirPath, RESOURCE_MEDIA_TYPE_MAPPINGS_FILE);
if (!configFile.exists()) {
String msg = "Resource media type definitions file (mime.types) file does " + "not exist in the path " + configDirPath;
log.error(msg);
throw new ScriptException(msg);
}
final Map<String, String> mimeMappings = new HashMap<String, String>();
final String mappings;
try {
mappings = FileUtils.readFileToString(configFile, "UTF-8");
} catch (IOException e) {
String msg = "Error opening resource media type definitions file " + "(mime.types) : " + e.getMessage();
throw new ScriptException(msg, e);
}
String[] lines = mappings.split("[\\r\\n]+");
for (String line : lines) {
if (!line.startsWith("#")) {
String[] parts = line.split("\\s+");
for (int i = 1; i < parts.length; i++) {
mimeMappings.put(parts[i], parts[0]);
}
}
}
return new FileTypeMap() {
@Override
public String getContentType(File file) {
return getContentType(file.getName());
}
@Override
public String getContentType(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > 0) {
String mimeType = mimeMappings.get(fileName.substring(i + 1));
if (mimeType != null) {
return mimeType;
}
}
return "application/octet-stream";
}
};
}
Aggregations