use of com.unboundid.ldap.listener.LDAPDebuggerRequestHandler 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;
}
Aggregations