Search in sources :

Example 1 with StoredProcedureRequestImpl

use of org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequestImpl in project directory-ldap-api by apache.

the class StandaloneLdapCodecServiceTest method testLoadingExtendedOperation.

/**
 * Test an extended operation.
 */
@Test
public void testLoadingExtendedOperation() throws Exception {
    LdapApiService codec = LdapApiServiceFactory.getSingleton();
    StoredProcedureRequest req = new StoredProcedureRequestImpl();
    req.setLanguage("Java");
    req.setProcedure(Strings.getBytesUtf8("bogusProc"));
    assertNotNull(req);
    assertNotNull(codec);
    StoredProcedureRequest decorator = (StoredProcedureRequest) codec.decorate(req);
    assertNotNull(decorator);
}
Also used : StandaloneLdapApiService(org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService) StoredProcedureRequest(org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequest) StoredProcedureRequestImpl(org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequestImpl) Test(org.junit.Test)

Example 2 with StoredProcedureRequestImpl

use of org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequestImpl in project directory-ldap-api by apache.

the class JavaStoredProcUtils method callStoredProcedure.

/**
 * Invoke a Stored Procedure
 *
 * @param ctx The execution context
 * @param procedureName The procedure to execute
 * @param arguments The procedure's arguments
 * @return The execution resut
 * @throws NamingException If we have had an error whil executing the stored procedure
 */
public static Object callStoredProcedure(LdapContext ctx, String procedureName, Object[] arguments) throws NamingException {
    String language = "Java";
    Object responseObject;
    try {
        /**
         * Create a new stored procedure execution request.
         */
        StoredProcedureRequestImpl req = new StoredProcedureRequestImpl(0, procedureName, language);
        /**
         * For each argument UTF-8-encode the type name
         * and Java-serialize the value
         * and add them to the request as a parameter object.
         */
        for (int i = 0; i < arguments.length; i++) {
            byte[] type;
            byte[] value;
            type = arguments[i].getClass().getName().getBytes("UTF-8");
            value = SerializationUtils.serialize((Serializable) arguments[i]);
            req.addParameter(type, value);
        }
        /**
         * Call the stored procedure via the extended operation
         * and get back its return value.
         */
        ExtendedRequest jndiReq = LdapApiServiceFactory.getSingleton().toJndi(req);
        ExtendedResponse resp = ctx.extendedOperation(jndiReq);
        /**
         * Restore a Java object from the return value.
         */
        byte[] responseStream = resp.getEncodedValue();
        responseObject = SerializationUtils.deserialize(responseStream);
    } catch (Exception e) {
        NamingException ne = new NamingException();
        ne.setRootCause(e);
        throw ne;
    }
    return responseObject;
}
Also used : Serializable(java.io.Serializable) ExtendedResponse(javax.naming.ldap.ExtendedResponse) ExtendedRequest(javax.naming.ldap.ExtendedRequest) NamingException(javax.naming.NamingException) StoredProcedureRequestImpl(org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequestImpl) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) NamingException(javax.naming.NamingException)

Aggregations

StoredProcedureRequestImpl (org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequestImpl)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 URISyntaxException (java.net.URISyntaxException)1 NamingException (javax.naming.NamingException)1 ExtendedRequest (javax.naming.ldap.ExtendedRequest)1 ExtendedResponse (javax.naming.ldap.ExtendedResponse)1 StandaloneLdapApiService (org.apache.directory.api.ldap.codec.standalone.StandaloneLdapApiService)1 StoredProcedureRequest (org.apache.directory.api.ldap.extras.extended.storedProcedure.StoredProcedureRequest)1 Test (org.junit.Test)1