| | |
| | | * CDDL HEADER END |
| | | * |
| | | * |
| | | * Copyright 2009-2010 Sun Microsystems, Inc. |
| | | * Copyright 2010 Sun Microsystems, Inc. |
| | | */ |
| | | |
| | | package com.sun.opends.sdk.ldap; |
| | |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import java.util.concurrent.CopyOnWriteArrayList; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | |
| | | |
| | | private final AtomicInteger nextMsgID = new AtomicInteger(1); |
| | | |
| | | private boolean bindOrStartTLSInProgress = false; |
| | | private final AtomicBoolean bindOrStartTLSInProgress = |
| | | new AtomicBoolean(false); |
| | | |
| | | private final ConcurrentHashMap<Integer, AbstractLDAPFutureResultImpl<?>> pendingRequests = |
| | | new ConcurrentHashMap<Integer, AbstractLDAPFutureResultImpl<?>>(); |
| | |
| | | return new CompletedFutureResult<Void>( |
| | | ErrorResultException.wrap(connectionInvalidReason), messageID); |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | final Result errorResult = Responses.newResult( |
| | | ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | future.setResultOrError(Responses |
| | | .newResult(ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (!bindOrStartTLSInProgress.compareAndSet(false, true)) |
| | | { |
| | | future.setResultOrError(Responses.newBindResult( |
| | | ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | } |
| | | |
| | | pendingRequests.put(messageID, future); |
| | | bindOrStartTLSInProgress = true; |
| | | } |
| | | |
| | | try |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | future.setResultOrError(Responses.newCompareResult( |
| | | ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | future.setResultOrError(Responses |
| | | .newResult(ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (!bindOrStartTLSInProgress.compareAndSet(false, true)) |
| | | { |
| | | future.setResultOrError(request.getResultDecoder() |
| | | .newExtendedErrorResult(ResultCode.OPERATIONS_ERROR, "", |
| | |
| | | .newExtendedErrorResult(ResultCode.OPERATIONS_ERROR, "", |
| | | "This connection is already TLS enabled")); |
| | | } |
| | | bindOrStartTLSInProgress = true; |
| | | } |
| | | pendingRequests.put(messageID, future); |
| | | } |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | future.setResultOrError(Responses |
| | | .newResult(ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | future.setResultOrError(Responses |
| | | .newResult(ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | future.adaptErrorResult(connectionInvalidReason); |
| | | return future; |
| | | } |
| | | if (bindOrStartTLSInProgress) |
| | | if (bindOrStartTLSInProgress.get()) |
| | | { |
| | | future.setResultOrError(Responses |
| | | .newResult(ResultCode.OPERATIONS_ERROR).setDiagnosticMessage( |
| | |
| | | |
| | | void setBindOrStartTLSInProgress(final boolean state) |
| | | { |
| | | bindOrStartTLSInProgress = state; |
| | | bindOrStartTLSInProgress.set(state); |
| | | } |
| | | |
| | | |
| | | |
| | | synchronized void startTLS(final SSLContext sslContext, |
| | | final String[] protocols, final String[] cipherSuites, |
| | | final List<String> protocols, final List<String> cipherSuites, |
| | | final CompletionHandler<SSLEngine> completionHandler) throws IOException |
| | | { |
| | | if (isTLSEnabled()) |
| | |
| | | |
| | | sslEngineConfigurator = new SSLEngineConfigurator(sslContext, true, false, |
| | | false); |
| | | sslEngineConfigurator.setEnabledProtocols(protocols); |
| | | sslEngineConfigurator.setEnabledCipherSuites(cipherSuites); |
| | | sslEngineConfigurator.setEnabledProtocols(protocols.isEmpty() ? |
| | | null : protocols.toArray(new String[protocols.size()])); |
| | | sslEngineConfigurator.setEnabledCipherSuites(cipherSuites.isEmpty() ? |
| | | null : cipherSuites.toArray(new String[cipherSuites.size()])); |
| | | sslFilter = new SSLFilter(null, sslEngineConfigurator); |
| | | installFilter(sslFilter); |
| | | sslFilter.handshake(connection, completionHandler); |