use of com.orientechnologies.orient.core.command.script.OCommandScriptException in project orientdb by orientechnologies.
the class OServerCommandAbstractLogic method execute.
@Override
public boolean execute(final OHttpRequest iRequest, final OHttpResponse iResponse) throws Exception {
final String[] parts = init(iRequest, iResponse);
ODatabaseDocument db = null;
try {
db = getProfiledDatabaseInstance(iRequest);
final OFunction f = db.getMetadata().getFunctionLibrary().getFunction(parts[2]);
if (f == null)
throw new IllegalArgumentException("Function '" + parts[2] + "' is not configured");
if (iRequest.httpMethod.equalsIgnoreCase("GET") && !f.isIdempotent()) {
iResponse.send(OHttpUtils.STATUS_BADREQ_CODE, OHttpUtils.STATUS_BADREQ_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, "GET method is not allowed to execute function '" + parts[2] + "' because has been declared as non idempotent. Use POST instead.", null);
return false;
}
Object[] args = new String[parts.length - 3];
for (int i = 3; i < parts.length; ++i) args[i - 3] = parts[i];
// BIND CONTEXT VARIABLES
final OBasicCommandContext context = new OBasicCommandContext();
context.setVariable("session", OHttpSessionManager.getInstance().getSession(iRequest.sessionId));
context.setVariable("request", new OHttpRequestWrapper(iRequest, (String[]) args));
context.setVariable("response", new OHttpResponseWrapper(iResponse));
final Object functionResult;
if (args.length == 0 && iRequest.content != null && !iRequest.content.isEmpty()) {
// PARSE PARAMETERS FROM CONTENT PAYLOAD
try {
final ODocument params = new ODocument().fromJSON(iRequest.content);
functionResult = f.executeInContext(context, params.toMap());
} catch (Exception e) {
throw OException.wrapException(new OCommandScriptException("Error on parsing parameters from request body"), e);
}
} else
functionResult = f.executeInContext(context, args);
handleResult(iRequest, iResponse, functionResult);
} catch (OCommandScriptException e) {
// EXCEPTION
final StringBuilder msg = new StringBuilder(256);
for (Exception currentException = e; currentException != null; currentException = (Exception) currentException.getCause()) {
if (msg.length() > 0)
msg.append("\n");
msg.append(currentException.getMessage());
}
if (isJsonResponse(iResponse)) {
sendJsonError(iResponse, OHttpUtils.STATUS_BADREQ_CODE, OHttpUtils.STATUS_BADREQ_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, msg.toString(), null);
} else {
iResponse.send(OHttpUtils.STATUS_BADREQ_CODE, OHttpUtils.STATUS_BADREQ_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, msg.toString(), null);
}
} finally {
if (db != null)
db.close();
}
return false;
}
use of com.orientechnologies.orient.core.command.script.OCommandScriptException in project orientdb by orientechnologies.
the class OClassTrigger method executeFunction.
private RESULT executeFunction(final ODocument iDocument, final OFunction func) {
if (func == null)
return RESULT.RECORD_NOT_CHANGED;
final OScriptManager scriptManager = Orient.instance().getScriptManager();
final OPartitionedObjectPool.PoolEntry<ScriptEngine> entry = scriptManager.acquireDatabaseEngine(database.getName(), func.getLanguage());
final ScriptEngine scriptEngine = entry.object;
try {
final Bindings binding = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
scriptManager.bind(binding, (ODatabaseDocumentTx) database, null, null);
binding.put("doc", iDocument);
String result = null;
try {
if (func.getLanguage() == null)
throw new OConfigurationException("Database function '" + func.getName() + "' has no language");
final String funcStr = scriptManager.getFunctionDefinition(func);
if (funcStr != null) {
try {
scriptEngine.eval(funcStr);
} catch (ScriptException e) {
scriptManager.throwErrorMessage(e, funcStr);
}
}
if (scriptEngine instanceof Invocable) {
final Invocable invocableEngine = (Invocable) scriptEngine;
Object[] EMPTY = OCommonConst.EMPTY_OBJECT_ARRAY;
result = (String) invocableEngine.invokeFunction(func.getName(), EMPTY);
}
} catch (ScriptException e) {
throw OException.wrapException(new OCommandScriptException("Error on execution of the script", func.getName(), e.getColumnNumber()), e);
} catch (NoSuchMethodException e) {
throw OException.wrapException(new OCommandScriptException("Error on execution of the script", func.getName(), 0), e);
} catch (OCommandScriptException e) {
// PASS THROUGH
throw e;
} finally {
scriptManager.unbind(binding, null, null);
}
if (result == null) {
return RESULT.RECORD_NOT_CHANGED;
}
return RESULT.valueOf(result);
} finally {
scriptManager.releaseDatabaseEngine(func.getLanguage(), database.getName(), entry);
}
}
Aggregations