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);
}
}
Aggregations