Sun Java System Directory Server Enterprise Edition 6.3 Developer's Guide

Computing an Attribute Value

When Directory Server receives a request for the computed attribute value, in this case currentTime, the server calls the functions that are registered to compute attributes. Directory Server provides your function the context, attribute type, and entry with which to compute the attribute.

Your function must therefore check the attribute type to see whether your function should handle the computation. If your function does not handle the attribute type in question, return -1. If your function can handle the attribute, your function must determine the value or values of the attribute. Your function must then add the computed content to the result that the server returns. The following code excerpt shows the compute_attrs() function generating a current time string for the attribute value.

#include "slapi-plugin.h"

/* Compute an attribute called currentTime                        */
static int
compute_attrs(
    computed_attr_context  * context,
    char                   * type,
    Slapi_Entry            * entry,
    slapi_compute_output_t   outfn
)
{
    /* If the attribute type is not recognized return -1.         */
    int rc = -1;

    if (slapi_attr_type_cmp("currentTime", type, 2) == 0) {
        Slapi_Attr  * time_attr;
        time_t        now;
        char          current_time[30];
        Slapi_Value * time_value;
        int           rc = 0;

        /* Construct the computed attribute.                      */
        time_attr = slapi_attr_new();
        slapi_attr_init(time_attr, type);
        now = time(NULL);
        ctime_r(&now;, current_time, 30);
        current_time[(int)strlen(current_time) - 1] = '\0';
        time_value = slapi_value_new_string(current_time);
        slapi_attr_add_value(time_attr, time_value);
        slapi_value_free(&time_value;);

        /* Add the attribute to the result returned.              */
        rc = (*outfn)(context, time_attr, entry);
        attr_done(time_attr);
        return rc;

    /* Handle other computed attributes...
    } else if (slapi_attr_type_cmp("myCompAttr", type, 2) == 0) {
    */

    }
    return rc;
}

Notice the lines of code that add the attribute to the result that is returned. The output function in this case is of type slapi_compute_output_t.

Notice also, when constructing the computed attribute value, that the compute_attrs() function removes the newline character from the string in the ctime_r() function. This removal prevents the attribute value from appearing base64-encoded when the value is displayed by the ldapsearch command.