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
36 * Credentials or session_info may be set up first.
38 function _connect(params, error)
40 if (params.length < 1)
42 error.setError(JsonRpcError_ParameterMismatch,
43 "usage: <db_name> [<option> ...]");
48 var ret = ldb.connect(params[0]);
51 return session.resources.set(ldb,
52 session.resources.Type.ldb,
57 error.setError(-1, "ldb.connect failed");
61 jsonrpc.method.connect = _connect;
68 * The resource id of the open database, previously returned by connect()
71 * An object of class JsonRpcError.
75 * Failure: Will only fail with invalid parameters, and throws an error
77 function _close(params, error)
79 if (params.length != 1)
81 error.setError(JsonRpcError_ParameterMismatch,
82 "usage: <resource_id>");
86 ldb = session.resources.get(params[0],
87 session.resources.Type.ldb,
89 if (ldb["__type"] == "_JsonRpcError")
94 var ret = ldb.close();
96 /* If close succeeded, release the stored resource */
99 session.resources.release(params[0], error);
104 jsonrpc.method.close = _close;
108 * Begin a transaction
111 * The resource id of the open database, previously returned by connect()
114 * An object of class JsonRpcError.
120 function _transaction_start(params, error)
122 if (params.length != 1)
124 error.setError(JsonRpcError_ParameterMismatch,
125 "usage: <resource_id>");
129 ldb = session.resources.get(params[0],
130 session.resources.Type.ldb,
132 if (ldb["__type"] == "_JsonRpcError")
137 return ldb.transaction_start();
139 jsonrpc.method.transaction_start = _transaction_start;
143 * Cancel a transaction
146 * The resource id of the open database, previously returned by connect()
149 * An object of class JsonRpcError.
155 function _transaction_cancel(params, error)
157 if (params.length != 1)
159 error.setError(JsonRpcError_ParameterMismatch,
160 "usage: <resource_id>");
164 ldb = session.resources.get(params[0],
165 session.resources.Type.ldb,
167 if (ldb["__type"] == "_JsonRpcError")
172 return ldb.transaction_cancel();
174 jsonrpc.method.transaction_cancel = _transaction_cancel;
178 * Commit a transaction
181 * The resource id of the open database, previously returned by connect()
184 * An object of class JsonRpcError.
190 function _transaction_commit(params, error)
192 if (params.length != 1)
194 error.setError(JsonRpcError_ParameterMismatch,
195 "usage: <resource_id>");
199 ldb = session.resources.get(params[0],
200 session.resources.Type.ldb,
202 if (ldb["__type"] == "_JsonRpcError")
207 return ldb.transaction_commit();
209 jsonrpc.method.transaction_commit = _transaction_commit;
213 * Issue a Search request
216 * The resource id of the open database, previously returned by connect()
225 * Scope: "default", "base", "one" or "subtree"
228 * Attributes: an array object
231 * An object of class JsonRpcError.
234 * Success: found object
235 * Failure: `undefined`
238 * If params[4] is missing, assume no attributes
239 * If params[3..4] are missing, also assume "default" scope
240 * If params[2..4] are missing, also assume null base DN
242 function _search(params, error)
244 if (params.length < 2 || params.length > 5)
246 error.setError(JsonRpcError_ParameterMismatch,
248 "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
252 ldb = session.resources.get(params[0],
253 session.resources.Type.ldb,
255 if (ldb["__type"] == "_JsonRpcError")
260 /* Retrieve parameters */
261 var expr = params[1];
262 var baseDN = params[2];
263 var scope = params[3];
264 var attrs = params[4];
266 /* Fill in optional parameters */
267 if (params.length < 3) baseDN = null;
268 if (params.length < 4) scope = "one";
269 if (params.length < 5) attrs = null;
271 /* Determine scope value */
274 scope = ldb.SCOPE_BASE;
276 else if (scope == "one")
278 scope = ldb.SCOPE_ONE;
280 else if (scope == "subtree")
282 scope = ldb.SCOPE_SUBTREE;
284 else if (scope == "default")
286 scope = ldb.SCOPE_DEFAULT;
290 error.setError(JsonRpcError_ParameterMismatch,
295 return ldb.transaction_search(expr, baseDN, scope, attrs);
297 jsonrpc.method.search = _search;
301 * Add data to the database
304 * The resource id of the open database, previously returned by connect()
307 * An LDIF string representing the data to be added
310 * An object of class JsonRpcError.
316 function _add(params, error)
318 if (params.length != 2)
320 error.setError(JsonRpcError_ParameterMismatch,
321 "usage: <resource_id> <ldif>");
325 ldb = session.resources.get(params[0],
326 session.resources.Type.ldb,
328 if (ldb["__type"] == "_JsonRpcError")
333 return ldb.add(params[1]);
335 jsonrpc.method.add = _add;
339 * Modify data in the database
342 * The resource id of the open database, previously returned by connect()
345 * An LDIF string representing the data to be modified
348 * An object of class JsonRpcError.
354 function _modify(params, error)
356 if (params.length != 2)
358 error.setError(JsonRpcError_ParameterMismatch,
359 "usage: <resource_id> <ldif>");
363 ldb = session.resources.get(params[0],
364 session.resources.Type.ldb,
366 if (ldb["__type"] == "_JsonRpcError")
371 return ldb.modify(params[1]);
373 jsonrpc.method.modify = _modify;
377 * Delete data from the database
380 * The resource id of the open database, previously returned by connect()
383 * The DN to be located and deleted
386 * An object of class JsonRpcError.
392 function _del(params, error)
394 if (params.length != 2)
396 error.setError(JsonRpcError_ParameterMismatch,
397 "usage: <resource_id> <dn>");
401 ldb = session.resources.get(params[0],
402 session.resources.Type.ldb,
404 if (ldb["__type"] == "_JsonRpcError")
409 return ldb.del(params[1]);
411 jsonrpc.method.del = _del;
415 * Rename data in the database
418 * The resource id of the open database, previously returned by connect()
421 * The DN to be renamed
424 * The new name for the DN being renamed
427 * An object of class JsonRpcError.
433 function _rename(params, error)
435 if (params.length != 3)
437 error.setError(JsonRpcError_ParameterMismatch,
438 "usage: <resource_id> <old_dn> <new_dn>");
442 ldb = session.resources.get(params[0],
443 session.resources.Type.ldb,
445 if (ldb["__type"] == "_JsonRpcError")
450 return ldb.rename(params[1], params[2]);
452 jsonrpc.method.rename = _rename;
456 * Base64-encode a string
459 * The resource id of the open database, previously returned by connect()
462 * The string to be base64 encoded
465 * An object of class JsonRpcError.
468 * Success: encoded string
469 * Failure: `undefined`
471 function _base64encode(params, error)
473 if (params.length != 2)
475 error.setError(JsonRpcError_ParameterMismatch,
476 "usage: <resource_id> <string_to_be_encoded>");
480 ldb = session.resources.get(params[0],
481 session.resources.Type.ldb,
483 if (ldb["__type"] == "_JsonRpcError")
488 return ldb.base64encode(params[1]);
490 jsonrpc.method.base64encode = _base64encode;
494 * Base64-decode a string
497 * The resource id of the open database, previously returned by connect()
500 * The string to be base64 decoded
503 * An object of class JsonRpcError.
506 * Success: decoded string
507 * Failure: `undefined`
509 function _base64decode(params, error)
511 if (params.length != 2)
513 error.setError(JsonRpcError_ParameterMismatch,
514 "usage: <resource_id> <string_to_be_decoded>");
518 ldb = session.resources.get(params[0],
519 session.resources.Type.ldb,
521 if (ldb["__type"] == "_JsonRpcError")
526 return ldb.base64decode(params[1]);
528 jsonrpc.method.base64decode = _base64decode;
535 * The resource id of the open database, previously returned by connect()
538 * The DN to be escaped
541 * An object of class JsonRpcError.
544 * Success: escaped string
547 function _base64decode(params, error)
549 if (params.length != 2)
551 error.setError(JsonRpcError_ParameterMismatch,
552 "usage: <resource_id> <string_to_be_decoded>");
556 ldb = session.resources.get(params[0],
557 session.resources.Type.ldb,
559 if (ldb["__type"] == "_JsonRpcError")
564 return ldb.base64decode(params[1]);
566 jsonrpc.method.base64decode = _base64decode;
570 * Retrieve a description of the most recent error
573 * The resource id of the open database, previously returned by connect()
576 * An object of class JsonRpcError.
579 * The most recent error string for the ldb specified by the resource id
581 function _errstring(params, error)
583 if (params.length != 1)
585 error.setError(JsonRpcError_ParameterMismatch,
586 "usage: <resource_id>");
590 ldb = session.resources.get(params[0],
591 session.resources.Type.ldb,
593 if (ldb["__type"] == "_JsonRpcError")
598 return ldb.errstring();
600 jsonrpc.method.errstring = _errstring;