1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.ldap.client.api;
21
22
23 import static org.apache.directory.api.ldap.model.message.ResultCodeEnum.processResponse;
24
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.apache.directory.api.ldap.codec.api.LdapApiService;
28 import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
29 import org.apache.directory.api.ldap.model.exception.LdapException;
30 import org.apache.directory.api.ldap.model.message.BindRequest;
31 import org.apache.directory.api.ldap.model.message.BindRequestImpl;
32 import org.apache.directory.api.ldap.model.message.BindResponse;
33 import org.apache.directory.api.ldap.model.message.Control;
34 import org.apache.directory.api.ldap.model.name.Dn;
35 import org.apache.directory.api.ldap.model.schema.SchemaManager;
36 import org.apache.directory.api.util.Strings;
37 import org.apache.mina.core.service.IoHandlerAdapter;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42
43
44
45
46
47
48 public abstract class AbstractLdapConnection extends IoHandlerAdapter implements LdapConnection
49 {
50
51 private static final Logger LOG = LoggerFactory.getLogger( AbstractLdapConnection.class );
52
53
54 protected SchemaManager schemaManager;
55
56
57 protected AtomicInteger messageId;
58
59
60 protected LdapApiService codec;
61
62
63
64
65
66 protected AbstractLdapConnection()
67 {
68 this( LdapApiServiceFactory.getSingleton() );
69 }
70
71 protected AbstractLdapConnection( LdapApiService codec )
72 {
73 messageId = new AtomicInteger( 0 );
74 this.codec = codec;
75 }
76
77
78
79
80
81 @Override
82 public void bind( Dn name ) throws LdapException
83 {
84 byte[] credBytes = Strings.EMPTY_BYTES;
85
86 BindRequest bindRequest = new BindRequestImpl();
87 bindRequest.setDn( name );
88 bindRequest.setCredentials( credBytes );
89
90 BindResponse bindResponse = bind( bindRequest );
91
92 processResponse( bindResponse );
93 }
94
95
96
97
98
99 @Override
100 public void bind( String name ) throws LdapException
101 {
102 LOG.debug( "Bind request : {}", name );
103
104 bind( new Dn( schemaManager, name ), null );
105 }
106
107
108
109
110
111 @Override
112 public void bind( String name, String credentials ) throws LdapException
113 {
114 bind( new Dn( schemaManager, name ), credentials );
115 }
116
117
118
119
120
121 @Override
122 public void bind( Dn name, String credentials ) throws LdapException
123 {
124 byte[] credBytes = credentials == null ? Strings.EMPTY_BYTES : Strings.getBytesUtf8( credentials );
125
126 BindRequest bindRequest = new BindRequestImpl();
127 bindRequest.setDn( name );
128 bindRequest.setCredentials( credBytes );
129
130 BindResponse bindResponse = bind( bindRequest );
131
132 processResponse( bindResponse );
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146 protected BindRequest createBindRequest( String name, byte[] credentials, String saslMechanism, Control... controls )
147 throws LdapException
148 {
149
150 BindRequest bindRequest = new BindRequestImpl();
151
152
153 bindRequest.setVersion3( true );
154
155
156 bindRequest.setName( name );
157
158
159 if ( Strings.isEmpty( saslMechanism ) )
160 {
161
162 bindRequest.setSimple( true );
163 bindRequest.setCredentials( credentials );
164 }
165 else
166 {
167
168 bindRequest.setSimple( false );
169 bindRequest.setCredentials( credentials );
170 bindRequest.setSaslMechanism( saslMechanism );
171 }
172
173
174 if ( ( controls != null ) && ( controls.length != 0 ) )
175 {
176 bindRequest.addAllControls( controls );
177 }
178
179 return bindRequest;
180 }
181 }