4 * (C) 2006 by Derrell Lipman
8 * LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
12 * JSON-RPC mappings to the ldb ejs functions
15 /* We'll be saving resources in the session */
16 jsonrpc_include("resources.esp");
20 * Connect to a database
26 * Option (e.g. "modules:modlist")
29 * An object of class JsonRpcError.
32 * Success: The resource id to be used for future access to the database
33 * Failure: error event
36 * Credentials or session_info may be set up first.
38 function _connect(params, error)
40 if (params.length < 1)
42 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
43 "usage: <db_name> [<option> ...]");
47 /* First, see if this database was already opened */
48 var resourceId = session.resources.find("ldb:" + params[0], error);
49 if (resourceId != undefined)
51 /* It was. Give 'em the resource id */
55 /* Ensure there are no slashes in the database name */
56 var components = split('/', params[0]);
57 if (components.length > 1)
59 error.setError(1, "Invalid database name (contains '/')");
63 /* Get access to loadparm functions */
64 var lp = loadparm_init();
66 /* Determine the private directory */
67 var private_dir = lp.get("private dir");
69 /* Database was not previously opened. Connect to it. */
71 var ret = ldb.connect(private_dir + "/" + params[0]);
74 return session.resources.set(ldb, "ldb:" + params[0], error);
78 error.setError(-1, "ldb.connect failed");
82 jsonrpc.method.connect = _connect;
89 * The resource id of the open database, previously returned by connect()
92 * An object of class JsonRpcError.
96 * Failure: Will only fail with invalid parameters, and throws an error
98 function _close(params, error)
100 if (params.length != 1)
102 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
103 "usage: <resource_id>");
107 ldb = session.resources.get(params[0], error);
108 if (ldb["__type"] == "_JsonRpcError")
113 var ret = ldb.close();
115 /* If close succeeded, release the stored resource */
118 session.resources.release(params[0], error);
123 jsonrpc.method.close = _close;
127 * Begin a transaction
130 * The resource id of the open database, previously returned by connect()
133 * An object of class JsonRpcError.
139 function _transaction_start(params, error)
141 if (params.length != 1)
143 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
144 "usage: <resource_id>");
148 ldb = session.resources.get(params[0], error);
149 if (ldb["__type"] == "_JsonRpcError")
154 return ldb.transaction_start();
156 jsonrpc.method.transaction_start = _transaction_start;
160 * Cancel a transaction
163 * The resource id of the open database, previously returned by connect()
166 * An object of class JsonRpcError.
172 function _transaction_cancel(params, error)
174 if (params.length != 1)
176 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
177 "usage: <resource_id>");
181 ldb = session.resources.get(params[0], error);
182 if (ldb["__type"] == "_JsonRpcError")
187 return ldb.transaction_cancel();
189 jsonrpc.method.transaction_cancel = _transaction_cancel;
193 * Commit a transaction
196 * The resource id of the open database, previously returned by connect()
199 * An object of class JsonRpcError.
205 function _transaction_commit(params, error)
207 if (params.length != 1)
209 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
210 "usage: <resource_id>");
214 ldb = session.resources.get(params[0], error);
215 if (ldb["__type"] == "_JsonRpcError")
220 return ldb.transaction_commit();
222 jsonrpc.method.transaction_commit = _transaction_commit;
226 * Issue a Search request
229 * The resource id of the open database, previously returned by connect()
238 * Scope: "default", "base", "one" or "subtree"
241 * Attributes: an array object
244 * An object of class JsonRpcError.
247 * Success: found object
248 * Failure: `undefined`
251 * If params[4] is missing, assume no attributes
252 * If params[3..4] are missing, also assume "default" scope
253 * If params[2..4] are missing, also assume null base DN
255 function _search(params, error)
257 if (params.length < 2 || params.length > 5)
259 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
261 "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
265 ldb = session.resources.get(params[0], error);
266 if (ldb["__type"] == "_JsonRpcError")
271 /* Retrieve parameters */
272 var expr = params[1];
273 var baseDN = params[2];
274 var scope = params[3];
275 var attrs = params[4];
277 /* Fill in optional parameters */
278 if (params.length < 3) baseDN = null;
279 if (params.length < 4) scope = "one";
280 if (params.length < 5) attrs = null;
282 /* Determine scope value */
285 scope = ldb.SCOPE_BASE;
287 else if (scope == "one")
289 scope = ldb.SCOPE_ONE;
291 else if (scope == "subtree")
293 scope = ldb.SCOPE_SUBTREE;
295 else if (scope == "default")
297 scope = ldb.SCOPE_DEFAULT;
301 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
302 "invalid scope: " + scope);
306 return ldb.search(expr, baseDN, scope, attrs);
308 jsonrpc.method.search = _search;
312 * Add data to the database
315 * The resource id of the open database, previously returned by connect()
318 * An LDIF string representing the data to be added
321 * An object of class JsonRpcError.
327 function _add(params, error)
329 if (params.length != 2)
331 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
332 "usage: <resource_id> <ldif>");
336 ldb = session.resources.get(params[0], error);
337 if (ldb["__type"] == "_JsonRpcError")
342 return ldb.add(params[1]);
344 jsonrpc.method.add = _add;
348 * Modify data in the database
351 * The resource id of the open database, previously returned by connect()
354 * An LDIF string representing the data to be modified
357 * An object of class JsonRpcError.
363 function _modify(params, error)
365 if (params.length != 2)
367 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
368 "usage: <resource_id> <ldif>");
372 ldb = session.resources.get(params[0], error);
373 if (ldb["__type"] == "_JsonRpcError")
378 return ldb.modify(params[1]);
380 jsonrpc.method.modify = _modify;
384 * Delete data from the database
387 * The resource id of the open database, previously returned by connect()
390 * The DN to be located and deleted
393 * An object of class JsonRpcError.
399 function _del(params, error)
401 if (params.length != 2)
403 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
404 "usage: <resource_id> <dn>");
408 ldb = session.resources.get(params[0], error);
409 if (ldb["__type"] == "_JsonRpcError")
414 return ldb.del(params[1]);
416 jsonrpc.method.del = _del;
420 * Rename data in the database
423 * The resource id of the open database, previously returned by connect()
426 * The DN to be renamed
429 * The new name for the DN being renamed
432 * An object of class JsonRpcError.
438 function _rename(params, error)
440 if (params.length != 3)
442 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
443 "usage: <resource_id> <old_dn> <new_dn>");
447 ldb = session.resources.get(params[0], error);
448 if (ldb["__type"] == "_JsonRpcError")
453 return ldb.rename(params[1], params[2]);
455 jsonrpc.method.rename = _rename;
459 * Base64-encode a string
462 * The resource id of the open database, previously returned by connect()
465 * The string to be base64 encoded
468 * An object of class JsonRpcError.
471 * Success: encoded string
472 * Failure: `undefined`
474 function _base64encode(params, error)
476 if (params.length != 2)
478 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
479 "usage: <resource_id> <string_to_be_encoded>");
483 ldb = session.resources.get(params[0], error);
484 if (ldb["__type"] == "_JsonRpcError")
489 return ldb.base64encode(params[1]);
491 jsonrpc.method.base64encode = _base64encode;
495 * Base64-decode a string
498 * The resource id of the open database, previously returned by connect()
501 * The string to be base64 decoded
504 * An object of class JsonRpcError.
507 * Success: decoded string
508 * Failure: `undefined`
510 function _base64decode(params, error)
512 if (params.length != 2)
514 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
515 "usage: <resource_id> <string_to_be_decoded>");
519 ldb = session.resources.get(params[0], error);
520 if (ldb["__type"] == "_JsonRpcError")
525 return ldb.base64decode(params[1]);
527 jsonrpc.method.base64decode = _base64decode;
534 * The resource id of the open database, previously returned by connect()
537 * The DN to be escaped
540 * An object of class JsonRpcError.
543 * Success: escaped string
546 function _base64decode(params, error)
548 if (params.length != 2)
550 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
551 "usage: <resource_id> <string_to_be_decoded>");
555 ldb = session.resources.get(params[0], error);
556 if (ldb["__type"] == "_JsonRpcError")
561 return ldb.base64decode(params[1]);
563 jsonrpc.method.base64decode = _base64decode;
567 * Retrieve a description of the most recent error
570 * The resource id of the open database, previously returned by connect()
573 * An object of class JsonRpcError.
576 * The most recent error string for the ldb specified by the resource id
578 function _errstring(params, error)
580 if (params.length != 1)
582 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
583 "usage: <resource_id>");
587 ldb = session.resources.get(params[0], error);
588 if (ldb["__type"] == "_JsonRpcError")
593 return ldb.errstring();
595 jsonrpc.method.errstring = _errstring;