use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class CommandLineManager method include.
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
public static void include(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "include";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(CommonManager.HOST_OBJECT_NAME, functionName, argsCount, false);
}
if (!(args[0] instanceof String)) {
HostObjectUtil.invalidArgsError(CommonManager.HOST_OBJECT_NAME, functionName, "1", "string", args[0], false);
}
JaggeryContext jaggeryContext = CommonManager.getJaggeryContext();
Stack<String> includesCallstack = CommonManager.getCallstack(jaggeryContext);
Map<String, Boolean> includedScripts = CommonManager.getIncludes(jaggeryContext);
String parent = includesCallstack.lastElement();
String fileURL = (String) args[0];
if (CommonManager.isHTTP(fileURL) || CommonManager.isHTTP(parent)) {
CommonManager.include(cx, thisObj, args, funObj);
return;
}
ScriptableObject scope = jaggeryContext.getScope();
RhinoEngine engine = jaggeryContext.getEngine();
if (fileURL.startsWith("/")) {
fileURL = includesCallstack.firstElement() + fileURL;
} else {
fileURL = FilenameUtils.getFullPath(parent) + fileURL;
}
fileURL = FilenameUtils.normalize(fileURL);
if (includesCallstack.search(fileURL) != -1) {
return;
}
ScriptReader source = null;
try {
source = new ScriptReader(new FileInputStream(fileURL));
includedScripts.put(fileURL, true);
includesCallstack.push(fileURL);
engine.exec(source, scope, null);
includesCallstack.pop();
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class ResponseHostObject method jsFunction_sendError.
public static void jsFunction_sendError(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "sendError";
int argsCount = args.length;
if (argsCount > 2 || argsCount < 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof Integer)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "integer", args[0], false);
}
ResponseHostObject rho = (ResponseHostObject) thisObj;
if (argsCount == 2) {
if (!(args[1] instanceof String)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "2", "string", args[1], false);
}
try {
rho.response.sendError((Integer) args[0], (String) args[1]);
} catch (IOException e) {
String msg = "Error sending error. Status : " + args[0] + ", Message : " + args[1];
log.warn(msg, e);
throw new ScriptException(msg, e);
}
} else {
try {
rho.response.sendError((Integer) args[0]);
} catch (IOException e) {
String msg = "Error sending error. Status : " + args[0];
log.warn(msg, e);
throw new ScriptException(msg, e);
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class WebSocketHostObject method jsFunction_send.
public static void jsFunction_send(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "send";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof String) && !(args[1] instanceof StreamHostObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "string | Stream", args[0], false);
}
WebSocketHostObject who = (WebSocketHostObject) thisObj;
if (args[0] instanceof String) {
try {
who.inbound.getWsOutbound().writeTextMessage(CharBuffer.wrap((String) args[0]));
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
} else {
StreamHostObject sho = (StreamHostObject) args[0];
InputStream is = sho.getStream();
try {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
who.inbound.getWsOutbound().writeBinaryMessage(ByteBuffer.wrap(buffer, 0, length));
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class XMLHttpRequestHostObject method executeRequest.
private static void executeRequest(Context cx, XMLHttpRequestHostObject xhr) throws ScriptException {
try {
xhr.httpClient.executeMethod(xhr.method);
xhr.statusLine = xhr.method.getStatusLine();
xhr.responseHeaders = xhr.method.getResponseHeaders();
updateReadyState(cx, xhr, HEADERS_RECEIVED);
byte[] response = xhr.method.getResponseBody();
if (response != null) {
if (response.length > 0) {
xhr.responseText = new String(response);
}
}
Header contentType = xhr.method.getResponseHeader("Content-Type");
if (contentType != null) {
xhr.responseType = contentType.getValue();
}
updateReadyState(cx, xhr, DONE);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
} finally {
xhr.method.releaseConnection();
}
}
use of org.jaggeryjs.scriptengine.exceptions.ScriptException in project jaggery by wso2.
the class XMLHttpRequestHostObject method send.
private void send(Context cx, Object obj) throws ScriptException {
final HttpMethodBase method;
if ("GET".equalsIgnoreCase(methodName)) {
method = new GetMethod(this.url);
} else if ("HEAD".equalsIgnoreCase(methodName)) {
method = new HeadMethod(this.url);
} else if ("POST".equalsIgnoreCase(methodName)) {
PostMethod post = new PostMethod(this.url);
if (obj instanceof FormDataHostObject) {
FormDataHostObject fd = ((FormDataHostObject) obj);
List<Part> parts = new ArrayList<Part>();
for (Map.Entry<String, String> entry : fd) {
parts.add(new StringPart(entry.getKey(), entry.getValue()));
}
post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
} else {
String content = getRequestContent(obj);
if (content != null) {
post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
}
}
method = post;
} else if ("PUT".equalsIgnoreCase(methodName)) {
PutMethod put = new PutMethod(this.url);
String content = getRequestContent(obj);
if (content != null) {
put.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
}
method = put;
} else if ("DELETE".equalsIgnoreCase(methodName)) {
method = new DeleteMethod(this.url);
} else if ("TRACE".equalsIgnoreCase(methodName)) {
method = new TraceMethod(this.url);
} else if ("OPTIONS".equalsIgnoreCase(methodName)) {
method = new OptionsMethod(this.url);
} else {
throw new ScriptException("Unknown HTTP method : " + methodName);
}
for (Header header : requestHeaders) {
method.addRequestHeader(header);
}
if (username != null) {
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}
this.method = method;
final XMLHttpRequestHostObject xhr = this;
if (async) {
updateReadyState(cx, xhr, LOADING);
final ContextFactory factory = cx.getFactory();
final ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(new Callable() {
public Object call() throws Exception {
Context ctx = RhinoEngine.enterContext(factory);
try {
executeRequest(ctx, xhr);
} catch (ScriptException e) {
log.error(e.getMessage(), e);
} finally {
es.shutdown();
RhinoEngine.exitContext();
}
return null;
}
});
} else {
executeRequest(cx, xhr);
}
}
Aggregations