Search in sources :

Example 1 with LDAPListenerRequestHandler

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

the class LDAPDebugger method doToolProcessing.

/**
 * Performs the actual processing for this tool.  In this case, it gets a
 * connection to the directory server and uses it to perform the requested
 * search.
 *
 * @return  The result code for the processing that was performed.
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Create the proxy request handler that will be used to forward requests to
    // a remote directory.
    final ProxyRequestHandler proxyHandler;
    try {
        proxyHandler = new ProxyRequestHandler(createServerSet());
    } catch (final LDAPException le) {
        err("Unable to prepare to connect to the target server:  ", le.getMessage());
        return le.getResultCode();
    }
    // Create the log handler to use for the output.
    final Handler logHandler;
    if (outputFile.isPresent()) {
        try {
            logHandler = new FileHandler(outputFile.getValue().getAbsolutePath());
        } catch (final IOException ioe) {
            err("Unable to open the output file for writing:  ", StaticUtils.getExceptionMessage(ioe));
            return ResultCode.LOCAL_ERROR;
        }
    } else {
        logHandler = new ConsoleHandler();
    }
    StaticUtils.setLogHandlerLevel(logHandler, Level.INFO);
    logHandler.setFormatter(new MinimalLogFormatter(MinimalLogFormatter.DEFAULT_TIMESTAMP_FORMAT, false, false, true));
    // Create the debugger request handler that will be used to write the
    // debug output.
    LDAPListenerRequestHandler requestHandler = new LDAPDebuggerRequestHandler(logHandler, proxyHandler);
    // handler to accomplish that.
    if (codeLogFile.isPresent()) {
        try {
            requestHandler = new ToCodeRequestHandler(codeLogFile.getValue(), true, requestHandler);
        } catch (final Exception e) {
            err("Unable to open code log file '", codeLogFile.getValue().getAbsolutePath(), "' for writing:  ", StaticUtils.getExceptionMessage(e));
            return ResultCode.LOCAL_ERROR;
        }
    }
    // Create and start the LDAP listener.
    final LDAPListenerConfig config = new LDAPListenerConfig(listenPort.getValue(), requestHandler);
    if (listenAddress.isPresent()) {
        try {
            config.setListenAddress(LDAPConnectionOptions.DEFAULT_NAME_RESOLVER.getByName(listenAddress.getValue()));
        } catch (final Exception e) {
            err("Unable to resolve '", listenAddress.getValue(), "' as a valid address:  ", StaticUtils.getExceptionMessage(e));
            return ResultCode.PARAM_ERROR;
        }
    }
    if (listenUsingSSL.isPresent()) {
        try {
            final SSLUtil sslUtil;
            if (generateSelfSignedCertificate.isPresent()) {
                final ObjectPair<File, char[]> keyStoreInfo = SelfSignedCertificateGenerator.generateTemporarySelfSignedCertificate(getToolName(), CryptoHelper.KEY_STORE_TYPE_JKS);
                sslUtil = new SSLUtil(new KeyStoreKeyManager(keyStoreInfo.getFirst(), keyStoreInfo.getSecond(), CryptoHelper.KEY_STORE_TYPE_JKS, null, true), new TrustAllTrustManager(false));
            } else {
                sslUtil = createSSLUtil(true);
            }
            config.setServerSocketFactory(sslUtil.createSSLServerSocketFactory());
        } catch (final Exception e) {
            err("Unable to create a server socket factory to accept SSL-based " + "client connections:  ", StaticUtils.getExceptionMessage(e));
            return ResultCode.LOCAL_ERROR;
        }
    }
    listener = new LDAPListener(config);
    try {
        listener.startListening();
    } catch (final Exception e) {
        err("Unable to start listening for client connections:  ", StaticUtils.getExceptionMessage(e));
        return ResultCode.LOCAL_ERROR;
    }
    // Display a message with information about the port on which it is
    // listening for connections.
    int port = listener.getListenPort();
    while (port <= 0) {
        try {
            Thread.sleep(1L);
        } catch (final Exception e) {
            Debug.debugException(e);
            if (e instanceof InterruptedException) {
                Thread.currentThread().interrupt();
            }
        }
        port = listener.getListenPort();
    }
    if (listenUsingSSL.isPresent()) {
        out("Listening for SSL-based LDAP client connections on port ", port);
    } else {
        out("Listening for LDAP client connections on port ", port);
    }
    // Note that at this point, the listener will continue running in a
    // separate thread, so we can return from this thread without exiting the
    // program.  However, we'll want to register a shutdown hook so that we can
    // close the logger.
    shutdownListener = new LDAPDebuggerShutdownListener(listener, logHandler);
    Runtime.getRuntime().addShutdownHook(shutdownListener);
    return ResultCode.SUCCESS;
}
Also used : KeyStoreKeyManager(com.unboundid.util.ssl.KeyStoreKeyManager) MinimalLogFormatter(com.unboundid.util.MinimalLogFormatter) LDAPListenerRequestHandler(com.unboundid.ldap.listener.LDAPListenerRequestHandler) LDAPListener(com.unboundid.ldap.listener.LDAPListener) ProxyRequestHandler(com.unboundid.ldap.listener.ProxyRequestHandler) LDAPDebuggerRequestHandler(com.unboundid.ldap.listener.LDAPDebuggerRequestHandler) ToCodeRequestHandler(com.unboundid.ldap.listener.ToCodeRequestHandler) ToCodeRequestHandler(com.unboundid.ldap.listener.ToCodeRequestHandler) FileHandler(java.util.logging.FileHandler) LDAPListenerRequestHandler(com.unboundid.ldap.listener.LDAPListenerRequestHandler) ProxyRequestHandler(com.unboundid.ldap.listener.ProxyRequestHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) LDAPDebuggerRequestHandler(com.unboundid.ldap.listener.LDAPDebuggerRequestHandler) IOException(java.io.IOException) TrustAllTrustManager(com.unboundid.util.ssl.TrustAllTrustManager) ConsoleHandler(java.util.logging.ConsoleHandler) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler) SSLUtil(com.unboundid.util.ssl.SSLUtil) LDAPListenerConfig(com.unboundid.ldap.listener.LDAPListenerConfig) LDAPException(com.unboundid.ldap.sdk.LDAPException) File(java.io.File) NotNull(com.unboundid.util.NotNull)

Aggregations

LDAPDebuggerRequestHandler (com.unboundid.ldap.listener.LDAPDebuggerRequestHandler)1 LDAPListener (com.unboundid.ldap.listener.LDAPListener)1 LDAPListenerConfig (com.unboundid.ldap.listener.LDAPListenerConfig)1 LDAPListenerRequestHandler (com.unboundid.ldap.listener.LDAPListenerRequestHandler)1 ProxyRequestHandler (com.unboundid.ldap.listener.ProxyRequestHandler)1 ToCodeRequestHandler (com.unboundid.ldap.listener.ToCodeRequestHandler)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 MinimalLogFormatter (com.unboundid.util.MinimalLogFormatter)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 File (java.io.File)1 IOException (java.io.IOException)1 ConsoleHandler (java.util.logging.ConsoleHandler)1 FileHandler (java.util.logging.FileHandler)1 Handler (java.util.logging.Handler)1