use of com.unboundid.ldap.protocol.ExtendedRequestProtocolOp in project ldapsdk by pingidentity.
the class InMemoryDirectoryServer method processExtendedOperation.
/**
* Processes the provided extended request. Note that because some types of
* extended operations return unusual result codes under "normal" conditions,
* the server may not always throw an exception for a failed extended
* operation like it does for other types of operations. It will throw an
* exception under conditions where there appears to be a problem with the
* connection or the server to which the connection is established, but there
* may be many circumstances in which an extended operation is not processed
* correctly but this method does not throw an exception. In the event that
* no exception is thrown, it is the responsibility of the caller to interpret
* the result to determine whether the operation was processed as expected.
* <BR><BR>
* This method may be used regardless of whether the server is listening for
* client connections, and regardless of whether extended operations are
* allowed in the server.
*
* @param extendedRequest The extended request to be processed. It must not
* be {@code null}.
*
* @return The extended result object that provides information about the
* result of the request processing. It may or may not indicate that
* the operation was successful.
*
* @throws LDAPException If a problem occurs while sending the request or
* reading the response.
*/
@NotNull()
public ExtendedResult processExtendedOperation(@NotNull final ExtendedRequest extendedRequest) throws LDAPException {
Validator.ensureNotNull(extendedRequest);
final ArrayList<Control> requestControlList = new ArrayList<>(extendedRequest.getControlList());
requestControlList.add(new Control(InMemoryRequestHandler.OID_INTERNAL_OPERATION_REQUEST_CONTROL, false));
final LDAPMessage responseMessage = inMemoryHandler.processExtendedRequest(1, new ExtendedRequestProtocolOp(extendedRequest.getOID(), extendedRequest.getValue()), requestControlList);
final ExtendedResponseProtocolOp extendedResponse = responseMessage.getExtendedResponseProtocolOp();
final ResultCode rc = ResultCode.valueOf(extendedResponse.getResultCode());
final String[] referralURLs;
final List<String> referralURLList = extendedResponse.getReferralURLs();
if ((referralURLList == null) || referralURLList.isEmpty()) {
referralURLs = StaticUtils.NO_STRINGS;
} else {
referralURLs = new String[referralURLList.size()];
referralURLList.toArray(referralURLs);
}
final Control[] responseControls;
final List<Control> controlList = responseMessage.getControls();
if ((controlList == null) || controlList.isEmpty()) {
responseControls = StaticUtils.NO_CONTROLS;
} else {
responseControls = new Control[controlList.size()];
controlList.toArray(responseControls);
}
final ExtendedResult extendedResult = new ExtendedResult(responseMessage.getMessageID(), rc, extendedResponse.getDiagnosticMessage(), extendedResponse.getMatchedDN(), referralURLs, extendedResponse.getResponseOID(), extendedResponse.getResponseValue(), responseControls);
if ((extendedResult.getOID() == null) && (extendedResult.getValue() == null)) {
switch(rc.intValue()) {
case ResultCode.OPERATIONS_ERROR_INT_VALUE:
case ResultCode.PROTOCOL_ERROR_INT_VALUE:
case ResultCode.BUSY_INT_VALUE:
case ResultCode.UNAVAILABLE_INT_VALUE:
case ResultCode.OTHER_INT_VALUE:
case ResultCode.SERVER_DOWN_INT_VALUE:
case ResultCode.LOCAL_ERROR_INT_VALUE:
case ResultCode.ENCODING_ERROR_INT_VALUE:
case ResultCode.DECODING_ERROR_INT_VALUE:
case ResultCode.TIMEOUT_INT_VALUE:
case ResultCode.NO_MEMORY_INT_VALUE:
case ResultCode.CONNECT_ERROR_INT_VALUE:
throw new LDAPException(extendedResult);
}
}
return extendedResult;
}
use of com.unboundid.ldap.protocol.ExtendedRequestProtocolOp in project ldapsdk by pingidentity.
the class InMemoryOperationInterceptorRequestHandler method processExtendedRequest.
/**
* {@inheritDoc}
*/
@Override()
@NotNull()
public LDAPMessage processExtendedRequest(final int messageID, @NotNull final ExtendedRequestProtocolOp request, @NotNull final List<Control> controls) {
final InterceptedExtendedOperation op = new InterceptedExtendedOperation(connection, messageID, request, toArray(controls));
activeOperations.put(messageID, op);
try {
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processExtendedRequest(op);
} catch (final LDAPException le) {
Debug.debugException(le);
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(le.toLDAPResult()), le.getResponseControls());
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_REQUEST_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null, null));
}
}
final LDAPMessage resultMessage = wrappedHandler.processExtendedRequest(messageID, new ExtendedRequestProtocolOp(op.getRequest()), op.getRequest().getControlList());
op.setResult(resultMessage.getExtendedResponseProtocolOp().toExtendedResult(toArray(resultMessage.getControls())));
for (final InMemoryOperationInterceptor i : interceptors) {
try {
i.processExtendedResult(op);
} catch (final Exception e) {
Debug.debugException(e);
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(ResultCode.OTHER_INT_VALUE, null, ERR_DS_INTERCEPTOR_RESULT_ERROR.get(String.valueOf(op), i.getClass().getName(), StaticUtils.getExceptionMessage(e)), null, null, null));
}
}
return new LDAPMessage(messageID, new ExtendedResponseProtocolOp(op.getResult()), op.getResult().getResponseControls());
} finally {
activeOperations.remove(messageID);
}
}
use of com.unboundid.ldap.protocol.ExtendedRequestProtocolOp in project ldapsdk by pingidentity.
the class InterceptedExtendedOperationTestCase method testBasics.
/**
* Provides basic test coverage for an intercepted extended operation.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testBasics() throws Exception {
// Create an intercepted extended operation. We'll use a null connection,
// which shouldn't happen naturally but will be sufficient for this test.
final ExtendedRequestProtocolOp requestOp = new ExtendedRequestProtocolOp(new ExtendedRequest("1.2.3.4"));
final InterceptedExtendedOperation o = new InterceptedExtendedOperation(null, 1, requestOp);
assertNotNull(o.toString());
// Test methods for a generic intercepted operation.
assertNull(o.getClientConnection());
assertEquals(o.getConnectionID(), -1L);
assertNull(o.getConnectedAddress());
assertEquals(o.getConnectedPort(), -1);
assertEquals(o.getMessageID(), 1);
assertNull(o.getProperty("propX"));
o.setProperty("propX", "valX");
assertNotNull(o.getProperty("propX"));
assertEquals(o.getProperty("propX"), "valX");
assertNotNull(o.toString());
o.setProperty("propX", null);
assertNull(o.getProperty("propX"));
// Test methods specific to an intercepted extended operation.
assertNotNull(o.getRequest());
assertEquals(o.getRequest().getOID(), "1.2.3.4");
assertNotNull(o.toString());
final ExtendedRequest r = new ExtendedRequest("1.2.3.5");
o.setRequest(r);
assertNotNull(o.getRequest());
assertEquals(o.getRequest().getOID(), "1.2.3.5");
assertNotNull(o.toString());
assertNull(o.getResult());
o.setResult(new ExtendedResult(new LDAPResult(o.getMessageID(), ResultCode.SUCCESS)));
assertNotNull(o.getResult());
assertNotNull(o.toString());
}
use of com.unboundid.ldap.protocol.ExtendedRequestProtocolOp in project ldapsdk by pingidentity.
the class ManageCertificatesServerCertificateCollector method run.
/**
* Performs the core processing for this thread. It will establish a TCP
* connection to the specified server, optionally perform the LDAP StartTLS
* operation, and initiate TLS negotiation so that the server's certificate
* chain can be
*/
@Override()
public void run() {
// Establish a non-secure connection to the target server.
final String hostPort = hostname + ':' + port;
if (verbose) {
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_CONNECTING.get(hostPort));
}
final Socket nonSecureSocket;
try {
nonSecureSocket = new Socket();
final InetAddress address = LDAPConnectionOptions.DEFAULT_NAME_RESOLVER.getByName(hostname);
nonSecureSocket.connect(new InetSocketAddress(address, port), 60_000);
if (verbose) {
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_CONNECTED.get());
}
} catch (final Exception e) {
Debug.debugException(e);
final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_CONNECT_FAILED.get(hostPort);
manageCertificates.err();
manageCertificates.wrapErr(0, WRAP_COLUMN, message);
e.printStackTrace(manageCertificates.getErr());
queue.offer(new CertException(message, e));
return;
}
try {
// If we should send an LDAP StartTLS extended request, then do that now.
if (useLDAPStartTLS) {
if (verbose) {
manageCertificates.out();
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_SENDING_START_TLS.get());
}
final LDAPMessage startTLSRequestMessage = new LDAPMessage(1, new ExtendedRequestProtocolOp(StartTLSExtendedRequest.STARTTLS_REQUEST_OID, null));
try {
nonSecureSocket.getOutputStream().write(startTLSRequestMessage.encode().encode());
nonSecureSocket.getOutputStream().flush();
final ASN1StreamReader asn1Reader = new ASN1StreamReader(nonSecureSocket.getInputStream());
final LDAPMessage startTLSResponseMessage = LDAPMessage.readFrom(asn1Reader, true);
if (startTLSResponseMessage == null) {
// This could happen if the server terminated the connection for
// some reason (e.g., it's not an LDAP server, or the user specified
// an already-secure port).
final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_FAILED.get();
manageCertificates.wrapErr(0, WRAP_COLUMN, message);
queue.offer(new CertException(message));
return;
}
final ExtendedResponseProtocolOp startTLSResponse = startTLSResponseMessage.getExtendedResponseProtocolOp();
if (startTLSResponse.getResultCode() == ResultCode.SUCCESS_INT_VALUE) {
if (verbose) {
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_SUCCESSFUL.get());
}
} else {
final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_FAILED.get();
manageCertificates.wrapErr(0, WRAP_COLUMN, message);
final String[] referralURLArray = startTLSResponse.getReferralURLs().toArray(StaticUtils.NO_STRINGS);
final Control[] responseControlArray = startTLSResponseMessage.getControls().toArray(StaticUtils.NO_CONTROLS);
final ExtendedResult extendedResult = new ExtendedResult(startTLSRequestMessage.getMessageID(), ResultCode.valueOf(startTLSResponse.getResultCode()), startTLSResponse.getDiagnosticMessage(), startTLSResponse.getMatchedDN(), referralURLArray, startTLSResponse.getResponseOID(), startTLSResponse.getResponseValue(), responseControlArray);
for (final String line : ResultUtils.formatResult(extendedResult, false, 0, WRAP_COLUMN)) {
manageCertificates.err(line);
}
queue.offer(new CertException(message));
return;
}
} catch (final Exception e) {
final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_START_TLS_FAILED.get();
manageCertificates.wrapErr(0, WRAP_COLUMN, message);
e.printStackTrace(manageCertificates.getErr());
queue.offer(new CertException(message));
return;
}
}
// Convert the non-secure Socket to an SSLSocket and begin TLS
// negotiation.
final SSLSocket sslSocket;
try {
if (verbose) {
manageCertificates.out();
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_BEGINNING_TLS_NEGOTIATION.get());
}
final SSLUtil sslUtil = new SSLUtil(this);
sslSocket = (SSLSocket) sslUtil.createSSLSocketFactory().createSocket(nonSecureSocket, hostname, port, true);
sslSocket.startHandshake();
sslSocket.setSoTimeout(1000);
} catch (final Exception e) {
Debug.debugException(e);
final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_ERROR_STARTING_TLS_NEGOTIATION.get();
manageCertificates.wrapErr(0, WRAP_COLUMN, message);
e.printStackTrace(manageCertificates.getErr());
queue.offer(new CertException(message, e));
return;
}
try {
final long stopWaitingTime = System.currentTimeMillis() + 60_000L;
while ((System.currentTimeMillis() < stopWaitingTime) && (!gotCertificateChain)) {
try {
final int bytesRead = sslSocket.getInputStream().read();
if ((bytesRead < 0) && gotCertificateChain) {
// to the queue, so we don't need to add anything here.
return;
}
} catch (final Exception e) {
Debug.debugException(e);
}
}
if (!gotCertificateChain) {
// If we have gotten here, then it should mean that we timed out
// without having gotten the certificate chain.
final String message = ERR_MANAGE_CERTS_CERT_COLLECTOR_NO_CERT_CHAIN_RECEIVED.get(hostPort);
manageCertificates.wrapErr(0, WRAP_COLUMN, message);
queue.offer(new CertException(message));
return;
}
if (verbose) {
final SSLSession sslSession = sslSocket.getSession();
final String negotiatedProtocol = sslSession.getProtocol();
if (negotiatedProtocol != null) {
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_NEGOTIATED_TLS_PROTOCOL.get(negotiatedProtocol));
}
final String negotiatedCipherSuite = sslSession.getCipherSuite();
if (negotiatedCipherSuite != null) {
manageCertificates.wrapOut(0, WRAP_COLUMN, INFO_MANAGE_CERTS_CERT_COLLECTOR_NEGOTIATED_TLS_SUITE.get(negotiatedCipherSuite));
}
}
} finally {
try {
sslSocket.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
} finally {
try {
nonSecureSocket.close();
} catch (final Exception e) {
Debug.debugException(e);
}
}
}
use of com.unboundid.ldap.protocol.ExtendedRequestProtocolOp in project ldapsdk by pingidentity.
the class CannedResponseRequestHandlerTestCase method testDefaultConstructor.
/**
* Tests the behavior of the request handler with the default configuration.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testDefaultConstructor() throws Exception {
final CannedResponseRequestHandler handler = new CannedResponseRequestHandler().newInstance(null);
LDAPMessage m = handler.processAddRequest(1, new AddRequestProtocolOp("dc=example,dc=com", Arrays.asList(new Attribute("objectClass", "top", "domain"), new Attribute("dc", "example"))), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 1);
assertTrue(m.getProtocolOp() instanceof AddResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processBindRequest(2, new BindRequestProtocolOp("uid=admin,dc=example,dc=com", "password"), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 2);
assertTrue(m.getProtocolOp() instanceof BindResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processCompareRequest(3, new CompareRequestProtocolOp("dc=example,dc=com", "objectClass", new ASN1OctetString("top")), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 3);
assertTrue(m.getProtocolOp() instanceof CompareResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processDeleteRequest(4, new DeleteRequestProtocolOp("dc=example,dc=com"), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 4);
assertTrue(m.getProtocolOp() instanceof DeleteResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processExtendedRequest(5, new ExtendedRequestProtocolOp("1.2.3.4", null), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 5);
assertTrue(m.getProtocolOp() instanceof ExtendedResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processModifyRequest(6, new ModifyRequestProtocolOp("dc=example,dc=com", Arrays.asList(new Modification(ModificationType.REPLACE, "description", "foo"))), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 6);
assertTrue(m.getProtocolOp() instanceof ModifyResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processModifyDNRequest(6, new ModifyDNRequestProtocolOp("ou=People,dc=example,dc=com", "ou=Users", true, null), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 6);
assertTrue(m.getProtocolOp() instanceof ModifyDNResponseProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
m = handler.processSearchRequest(7, new SearchRequestProtocolOp("dc=example,dc=com", SearchScope.SUB, DereferencePolicy.NEVER, 0, 0, false, Filter.createEqualityFilter("uid", "test"), Arrays.<String>asList()), Collections.<Control>emptyList());
assertNotNull(m);
assertEquals(m.getMessageID(), 7);
assertTrue(m.getProtocolOp() instanceof SearchResultDoneProtocolOp);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
}
Aggregations