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(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 /* Database was not previously opened. Connect to it. */
57 var ret = ldb.connect(params[0]);
60 return session.resources.set(ldb, "ldb:" + params[0], error);
64 error.setError(-1, "ldb.connect failed");
68 jsonrpc.method.connect = _connect;
75 * The resource id of the open database, previously returned by connect()
78 * An object of class JsonRpcError.
82 * Failure: Will only fail with invalid parameters, and throws an error
84 function _close(params, error)
86 if (params.length != 1)
88 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
89 "usage: <resource_id>");
93 ldb = session.resources.get(params[0], error);
94 if (ldb["__type"] == "_JsonRpcError")
99 var ret = ldb.close();
101 /* If close succeeded, release the stored resource */
104 session.resources.release(params[0], error);
109 jsonrpc.method.close = _close;
113 * Begin a transaction
116 * The resource id of the open database, previously returned by connect()
119 * An object of class JsonRpcError.
125 function _transaction_start(params, error)
127 if (params.length != 1)
129 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
130 "usage: <resource_id>");
134 ldb = session.resources.get(params[0], error);
135 if (ldb["__type"] == "_JsonRpcError")
140 return ldb.transaction_start();
142 jsonrpc.method.transaction_start = _transaction_start;
146 * Cancel a transaction
149 * The resource id of the open database, previously returned by connect()
152 * An object of class JsonRpcError.
158 function _transaction_cancel(params, error)
160 if (params.length != 1)
162 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
163 "usage: <resource_id>");
167 ldb = session.resources.get(params[0], error);
168 if (ldb["__type"] == "_JsonRpcError")
173 return ldb.transaction_cancel();
175 jsonrpc.method.transaction_cancel = _transaction_cancel;
179 * Commit a transaction
182 * The resource id of the open database, previously returned by connect()
185 * An object of class JsonRpcError.
191 function _transaction_commit(params, error)
193 if (params.length != 1)
195 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
196 "usage: <resource_id>");
200 ldb = session.resources.get(params[0], error);
201 if (ldb["__type"] == "_JsonRpcError")
206 return ldb.transaction_commit();
208 jsonrpc.method.transaction_commit = _transaction_commit;
212 * Issue a Search request
215 * The resource id of the open database, previously returned by connect()
224 * Scope: "default", "base", "one" or "subtree"
227 * Attributes: an array object
230 * An object of class JsonRpcError.
233 * Success: found object
234 * Failure: `undefined`
237 * If params[4] is missing, assume no attributes
238 * If params[3..4] are missing, also assume "default" scope
239 * If params[2..4] are missing, also assume null base DN
241 function _search(params, error)
243 if (params.length < 2 || params.length > 5)
245 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
247 "<resource_id> <expr> [<baseDN> [<scope> [<attrs>]]]");
251 ldb = session.resources.get(params[0], error);
252 if (ldb["__type"] == "_JsonRpcError")
257 /* Retrieve parameters */
258 var expr = params[1];
259 var baseDN = params[2];
260 var scope = params[3];
261 var attrs = params[4];
263 /* Fill in optional parameters */
264 if (params.length < 3) baseDN = null;
265 if (params.length < 4) scope = "one";
266 if (params.length < 5) attrs = null;
268 /* Determine scope value */
271 scope = ldb.SCOPE_BASE;
273 else if (scope == "one")
275 scope = ldb.SCOPE_ONE;
277 else if (scope == "subtree")
279 scope = ldb.SCOPE_SUBTREE;
281 else if (scope == "default")
283 scope = ldb.SCOPE_DEFAULT;
287 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
288 "invalid scope: " + scope);
292 return ldb.search(expr, baseDN, scope, attrs);
294 jsonrpc.method.search = _search;
298 * Add data to the database
301 * The resource id of the open database, previously returned by connect()
304 * An LDIF string representing the data to be added
307 * An object of class JsonRpcError.
313 function _add(params, error)
315 if (params.length != 2)
317 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
318 "usage: <resource_id> <ldif>");
322 ldb = session.resources.get(params[0], error);
323 if (ldb["__type"] == "_JsonRpcError")
328 return ldb.add(params[1]);
330 jsonrpc.method.add = _add;
334 * Modify data in the database
337 * The resource id of the open database, previously returned by connect()
340 * An LDIF string representing the data to be modified
343 * An object of class JsonRpcError.
349 function _modify(params, error)
351 if (params.length != 2)
353 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
354 "usage: <resource_id> <ldif>");
358 ldb = session.resources.get(params[0], error);
359 if (ldb["__type"] == "_JsonRpcError")
364 return ldb.modify(params[1]);
366 jsonrpc.method.modify = _modify;
370 * Delete data from the database
373 * The resource id of the open database, previously returned by connect()
376 * The DN to be located and deleted
379 * An object of class JsonRpcError.
385 function _del(params, error)
387 if (params.length != 2)
389 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
390 "usage: <resource_id> <dn>");
394 ldb = session.resources.get(params[0], error);
395 if (ldb["__type"] == "_JsonRpcError")
400 return ldb.del(params[1]);
402 jsonrpc.method.del = _del;
406 * Rename data in the database
409 * The resource id of the open database, previously returned by connect()
412 * The DN to be renamed
415 * The new name for the DN being renamed
418 * An object of class JsonRpcError.
424 function _rename(params, error)
426 if (params.length != 3)
428 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
429 "usage: <resource_id> <old_dn> <new_dn>");
433 ldb = session.resources.get(params[0], error);
434 if (ldb["__type"] == "_JsonRpcError")
439 return ldb.rename(params[1], params[2]);
441 jsonrpc.method.rename = _rename;
445 * Base64-encode a string
448 * The resource id of the open database, previously returned by connect()
451 * The string to be base64 encoded
454 * An object of class JsonRpcError.
457 * Success: encoded string
458 * Failure: `undefined`
460 function _base64encode(params, error)
462 if (params.length != 2)
464 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
465 "usage: <resource_id> <string_to_be_encoded>");
469 ldb = session.resources.get(params[0], error);
470 if (ldb["__type"] == "_JsonRpcError")
475 return ldb.base64encode(params[1]);
477 jsonrpc.method.base64encode = _base64encode;
481 * Base64-decode a string
484 * The resource id of the open database, previously returned by connect()
487 * The string to be base64 decoded
490 * An object of class JsonRpcError.
493 * Success: decoded string
494 * Failure: `undefined`
496 function _base64decode(params, error)
498 if (params.length != 2)
500 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
501 "usage: <resource_id> <string_to_be_decoded>");
505 ldb = session.resources.get(params[0], error);
506 if (ldb["__type"] == "_JsonRpcError")
511 return ldb.base64decode(params[1]);
513 jsonrpc.method.base64decode = _base64decode;
520 * The resource id of the open database, previously returned by connect()
523 * The DN to be escaped
526 * An object of class JsonRpcError.
529 * Success: escaped string
532 function _base64decode(params, error)
534 if (params.length != 2)
536 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
537 "usage: <resource_id> <string_to_be_decoded>");
541 ldb = session.resources.get(params[0], error);
542 if (ldb["__type"] == "_JsonRpcError")
547 return ldb.base64decode(params[1]);
549 jsonrpc.method.base64decode = _base64decode;
553 * Retrieve a description of the most recent error
556 * The resource id of the open database, previously returned by connect()
559 * An object of class JsonRpcError.
562 * The most recent error string for the ldb specified by the resource id
564 function _errstring(params, error)
566 if (params.length != 1)
568 error.setError(jsonrpc.Constant.ErrorCode.ParameterMismatch,
569 "usage: <resource_id>");
573 ldb = session.resources.get(params[0], error);
574 if (ldb["__type"] == "_JsonRpcError")
579 return ldb.errstring();
581 jsonrpc.method.errstring = _errstring;