| | |
| | | import java.util.Arrays; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import javax.net.ssl.SSLContext; |
| | | import javax.net.ssl.SSLSocketFactory; |
| | |
| | | import org.opends.server.protocols.ldap.CompareResponseProtocolOp; |
| | | import org.opends.server.protocols.ldap.DeleteRequestProtocolOp; |
| | | import org.opends.server.protocols.ldap.DeleteResponseProtocolOp; |
| | | import org.opends.server.protocols.ldap.ExtendedRequestProtocolOp; |
| | | import org.opends.server.protocols.ldap.LDAPMessage; |
| | | import org.opends.server.protocols.ldap.ModifyDNRequestProtocolOp; |
| | | import org.opends.server.protocols.ldap.ModifyDNResponseProtocolOp; |
| | |
| | | import org.opends.server.protocols.ldap.SearchRequestProtocolOp; |
| | | import org.opends.server.protocols.ldap.SearchResultDoneProtocolOp; |
| | | import org.opends.server.protocols.ldap.SearchResultEntryProtocolOp; |
| | | import org.opends.server.protocols.ldap.UnbindRequestProtocolOp; |
| | | import org.opends.server.types.LDAPException; |
| | | |
| | | /** Modeled like an SDK Connection, but implemented using the servers' ProtocolOp classes */ |
| | | @SuppressWarnings("javadoc") |
| | | public final class RemoteConnection implements Closeable |
| | | { |
| | | private final String host; |
| | | private final Socket socket; |
| | | private LDAPReader r; |
| | | private LDAPWriter w; |
| | | private int messageID; |
| | | private AtomicInteger messageID = new AtomicInteger(1); |
| | | |
| | | public RemoteConnection(String host, int port) throws Exception |
| | | { |
| | |
| | | |
| | | public RemoteConnection(String host, int port, boolean secure) throws Exception |
| | | { |
| | | this.host = host; |
| | | socket = secure ? getSslSocket(host, port) : new Socket(host, port); |
| | | r = new LDAPReader(socket); |
| | | w = new LDAPWriter(socket); |
| | |
| | | .getControls()); |
| | | } |
| | | |
| | | public LDAPMessage bind(String bindDN, String bindPassword, Control... controls) |
| | | public void bind(String bindDN, String bindPassword, Control... controls) |
| | | throws IOException, LDAPException, LdapException |
| | | { |
| | | return bind(bindDN, bindPassword.getBytes(), true, Arrays.asList(controls)); |
| | | bind(bindDN, bindPassword.getBytes(), true, Arrays.asList(controls)); |
| | | } |
| | | |
| | | private LDAPMessage bind(String bindDN, byte[] bindPassword, boolean throwOnExceptionalResultCode, |
| | |
| | | return message; |
| | | } |
| | | |
| | | public void unbind() throws IOException, LDAPException, LdapException |
| | | { |
| | | writeMessage(new UnbindRequestProtocolOp()); |
| | | } |
| | | |
| | | public LDAPMessage add(AddRequest addRequest) throws IOException, LDAPException, LdapException |
| | | { |
| | | return add(addRequest, true); |
| | |
| | | return delete(deleteRequest, true); |
| | | } |
| | | |
| | | public LDAPMessage delete(DeleteRequest deleteRequest, boolean throwOnExceptionalResultCode) throws IOException, |
| | | LDAPException, LdapException |
| | | public LDAPMessage delete(DeleteRequest deleteRequest, boolean throwOnExceptionalResultCode) |
| | | throws IOException, LDAPException, LdapException |
| | | { |
| | | writeMessage(new DeleteRequestProtocolOp(bs(deleteRequest.getName())), to(deleteRequest.getControls())); |
| | | LDAPMessage message = r.readMessage(); |
| | |
| | | return message; |
| | | } |
| | | |
| | | public LDAPMessage extendedRequest(String oid) throws IOException, LDAPException, LdapException |
| | | { |
| | | return extendedRequest(oid, null); |
| | | } |
| | | |
| | | public LDAPMessage extendedRequest(String oid, ByteString requestValue) |
| | | throws IOException, LDAPException, LdapException |
| | | { |
| | | writeMessage(new ExtendedRequestProtocolOp(oid, requestValue)); |
| | | return r.readMessage(); |
| | | } |
| | | |
| | | private ByteString bs(Object o) |
| | | { |
| | | return o != null ? ByteString.valueOfObject(o) : null; |
| | |
| | | |
| | | public void writeMessage(ProtocolOp protocolOp) throws IOException |
| | | { |
| | | writeMessage(protocolOp, null); |
| | | writeMessage(protocolOp, (List<org.opends.server.types.Control>) null); |
| | | } |
| | | |
| | | public void writeMessage(ProtocolOp protocolOp, List<org.opends.server.types.Control> controls) throws IOException |
| | | { |
| | | w.writeMessage(new LDAPMessage(++messageID, protocolOp, controls)); |
| | | w.writeMessage(new LDAPMessage(messageID.getAndIncrement(), protocolOp, controls)); |
| | | } |
| | | |
| | | public void writeMessage(ProtocolOp protocolOp, org.opends.server.types.Control control) throws IOException |
| | | { |
| | | w.writeMessage(new LDAPMessage(messageID.getAndIncrement(), protocolOp, Arrays.asList(control))); |
| | | } |
| | | |
| | | public LDAPMessage readMessage() throws IOException, LDAPException |
| | |
| | | return message; |
| | | } |
| | | |
| | | public LDAPWriter getLdapWriter() |
| | | { |
| | | return this.w; |
| | | } |
| | | |
| | | public LDAPAuthenticationHandler newLDAPAuthenticationHandler() |
| | | { |
| | | return new LDAPAuthenticationHandler(r, w, host, messageID); |
| | | } |
| | | |
| | | @Override |
| | | public void close() throws IOException |
| | | { |