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