Search in sources :

Example 1 with CannedResponseRequestHandler

use of com.unboundid.ldap.listener.CannedResponseRequestHandler in project ldapsdk by pingidentity.

the class TestLDAPSDKPerformance method doToolProcessing.

/**
 * Performs the core set of processing for this tool.
 *
 * @return  A result code that indicates whether the processing completed
 *          successfully.
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Create the socket factory to use for accepting connections.  If the
    // --useSSL argument was provided, then create a temporary keystore and
    // generate a certificate in it.
    final ServerSocketFactory serverSocketFactory;
    if (useSSLArg.isPresent()) {
        try {
            final File keyStoreFile = File.createTempFile("test-ldap-sdk-performance-keystore-", ".jks");
            keyStoreFile.deleteOnExit();
            keyStoreFile.delete();
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            final ResultCode manageCertificatesResultCode = ManageCertificates.main(null, out, out, "generate-self-signed-certificate", "--keystore", keyStoreFile.getAbsolutePath(), "--keystore-password", keyStoreFile.getAbsolutePath(), "--keystore-type", CryptoHelper.KEY_STORE_TYPE_JKS, "--alias", "server-cert", "--subject-dn", "CN=Test LDAP SDK Performance");
            if (manageCertificatesResultCode != ResultCode.SUCCESS) {
                final String message = "ERROR:  Unable to use the " + "manage-certificates tool to generate a self-signed server " + "certificate to use for SSL communication.";
                completionMessage.compareAndSet(null, message);
                wrapErr(0, WRAP_COLUMN, message);
                err();
                wrapErr(0, WRAP_COLUMN, "The manage-certificates output was:");
                err();
                err(StaticUtils.toUTF8String(out.toByteArray()));
                return manageCertificatesResultCode;
            }
            final SSLUtil sslUtil = new SSLUtil(new KeyStoreKeyManager(keyStoreFile, keyStoreFile.getAbsolutePath().toCharArray(), CryptoHelper.KEY_STORE_TYPE_JKS, "server-cert"), new TrustAllTrustManager());
            serverSocketFactory = sslUtil.createSSLServerSocketFactory();
        } catch (final Exception e) {
            Debug.debugException(e);
            final String message = "ERROR:  Unable to initialize support for SSL " + "communication:  " + StaticUtils.getExceptionMessage(e);
            completionMessage.compareAndSet(null, message);
            wrapErr(0, WRAP_COLUMN, message);
            return ResultCode.LOCAL_ERROR;
        }
    } else {
        serverSocketFactory = ServerSocketFactory.getDefault();
    }
    // Create the search result entries to return in response to each search.
    final int numEntries = entriesPerSearchArg.getValue();
    final List<Entry> entries = new ArrayList<>(numEntries);
    for (int i = 1; i <= numEntries; i++) {
        entries.add(new Entry("uid=user." + i + ",ou=People,dc=example,dc=com", new Attribute("objectClass", "top", "person", "organizationalPerson", "inetOrgPerson"), new Attribute("uid", "user." + i), new Attribute("givenName", "User"), new Attribute("sn", String.valueOf(i)), new Attribute("cn", "User " + i), new Attribute("mail", "user." + i + "@example.com"), new Attribute("userPassword", "password")));
    }
    // Create a canned response request handler to use to return the responses.
    final CannedResponseRequestHandler cannedResponseRequestHandler = new CannedResponseRequestHandler(ResultCode.valueOf(resultCodeArg.getValue()), // Matched DN
    null, diagnosticMessageArg.getValue(), // Referral URLs
    Collections.<String>emptyList(), entries, Collections.<SearchResultReference>emptyList());
    // Create the LDAP listener to handle the requests.
    final LDAPListenerConfig listenerConfig = new LDAPListenerConfig(0, cannedResponseRequestHandler);
    listenerConfig.setServerSocketFactory(serverSocketFactory);
    final LDAPListener ldapListener = new LDAPListener(listenerConfig);
    try {
        ldapListener.startListening();
    } catch (final Exception e) {
        Debug.debugException(e);
        final String message = "ERROR:  Unable to start listening for client " + "connections:  " + StaticUtils.getExceptionMessage(e);
        completionMessage.compareAndSet(null, message);
        wrapErr(0, WRAP_COLUMN, message);
        return ResultCode.LOCAL_ERROR;
    }
    try {
        final int listenPort = ldapListener.getListenPort();
        final String toolName = StaticUtils.toLowerCase(toolArg.getValue());
        switch(toolName) {
            case TOOL_NAME_SEARCHRATE:
                return invokeSearchRate(listenPort);
            case TOOL_NAME_MODRATE:
                return invokeModRate(listenPort);
            case TOOL_NAME_AUTHRATE:
                return invokeAuthRate(listenPort);
            case TOOL_NAME_SEARCH_AND_MOD_RATE:
                return invokeSearchAndModRate(listenPort);
            default:
                // This should never happen.
                final String message = "ERROR:  Unrecognized tool name:  " + toolName;
                completionMessage.compareAndSet(null, message);
                wrapErr(0, WRAP_COLUMN, message);
                return ResultCode.PARAM_ERROR;
        }
    } finally {
        ldapListener.shutDown(true);
    }
}
Also used : KeyStoreKeyManager(com.unboundid.util.ssl.KeyStoreKeyManager) Attribute(com.unboundid.ldap.sdk.Attribute) ServerSocketFactory(javax.net.ServerSocketFactory) CannedResponseRequestHandler(com.unboundid.ldap.listener.CannedResponseRequestHandler) LDAPListener(com.unboundid.ldap.listener.LDAPListener) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrustAllTrustManager(com.unboundid.util.ssl.TrustAllTrustManager) ArgumentException(com.unboundid.util.args.ArgumentException) SSLUtil(com.unboundid.util.ssl.SSLUtil) Entry(com.unboundid.ldap.sdk.Entry) LDAPListenerConfig(com.unboundid.ldap.listener.LDAPListenerConfig) File(java.io.File) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Aggregations

CannedResponseRequestHandler (com.unboundid.ldap.listener.CannedResponseRequestHandler)1 LDAPListener (com.unboundid.ldap.listener.LDAPListener)1 LDAPListenerConfig (com.unboundid.ldap.listener.LDAPListenerConfig)1 Attribute (com.unboundid.ldap.sdk.Attribute)1 Entry (com.unboundid.ldap.sdk.Entry)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 NotNull (com.unboundid.util.NotNull)1 ArgumentException (com.unboundid.util.args.ArgumentException)1 KeyStoreKeyManager (com.unboundid.util.ssl.KeyStoreKeyManager)1 SSLUtil (com.unboundid.util.ssl.SSLUtil)1 TrustAllTrustManager (com.unboundid.util.ssl.TrustAllTrustManager)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 ServerSocketFactory (javax.net.ServerSocketFactory)1