1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.jndi;
21
22
23 import java.util.Hashtable;
24
25 import javax.naming.NamingException;
26 import javax.naming.ldap.Control;
27 import javax.naming.ldap.ExtendedRequest;
28 import javax.naming.ldap.ExtendedResponse;
29 import javax.naming.ldap.LdapContext;
30
31 import org.apache.directory.server.core.CoreSession;
32 import org.apache.directory.server.core.DirectoryService;
33 import org.apache.directory.server.core.authn.LdapPrincipal;
34 import org.apache.directory.server.core.entry.ServerBinaryValue;
35 import org.apache.directory.server.core.entry.ServerStringValue;
36 import org.apache.directory.server.core.interceptor.context.CompareOperationContext;
37 import org.apache.directory.server.core.interceptor.context.UnbindOperationContext;
38 import org.apache.directory.shared.ldap.NotImplementedException;
39 import org.apache.directory.shared.ldap.entry.Value;
40 import org.apache.directory.shared.ldap.name.LdapDN;
41 import org.apache.directory.shared.ldap.schema.AttributeType;
42 import org.apache.directory.shared.ldap.util.StringTools;
43
44
45
46
47
48
49
50
51 public class ServerLdapContext extends ServerDirContext implements LdapContext
52 {
53
54
55
56
57
58
59
60 public ServerLdapContext( DirectoryService service, Hashtable<String, Object> env ) throws Exception
61 {
62 super( service, env );
63 }
64
65
66
67
68
69
70
71
72
73
74
75 public ServerLdapContext( DirectoryService service, LdapPrincipal principal, LdapDN dn ) throws Exception
76 {
77 super( service, principal, dn );
78 }
79
80
81 public ServerLdapContext( DirectoryService service, CoreSession session, LdapDN bindDn ) throws Exception
82 {
83 super( service, session, bindDn );
84 }
85
86
87
88
89
90
91 public ExtendedResponse extendedOperation( ExtendedRequest request )
92 {
93 throw new NotImplementedException();
94 }
95
96
97
98
99
100
101 public LdapContext newInstance( Control[] requestControls ) throws NamingException
102 {
103 ServerLdapContext ctx = null;
104 try
105 {
106 ctx = new ServerLdapContext( getService(), getSession().getEffectivePrincipal(), ( LdapDN ) getDn() );
107 }
108 catch ( Exception e )
109 {
110 JndiUtils.wrap( e );
111 }
112 ctx.setRequestControls( requestControls );
113 return ctx;
114 }
115
116
117
118
119
120 public void reconnect( Control[] connCtls ) throws NamingException
121 {
122 this.connectControls = connCtls;
123 }
124
125
126
127
128
129 public Control[] getConnectControls() throws NamingException
130 {
131 return this.connectControls;
132 }
133
134
135
136
137
138
139 public void setRequestControls( Control[] requestControls ) throws NamingException
140 {
141 this.requestControls = requestControls;
142 }
143
144
145
146
147
148 public Control[] getRequestControls() throws NamingException
149 {
150 return requestControls;
151 }
152
153
154
155
156
157 public Control[] getResponseControls() throws NamingException
158 {
159 return responseControls;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180 public boolean compare( LdapDN name, String oid, Object value ) throws NamingException
181 {
182 Value<?> val = null;
183
184 AttributeType attributeType = getService().getRegistries().getAttributeTypeRegistry().lookup( oid );
185
186
187 if ( attributeType.getSyntax().isHumanReadable() )
188 {
189 if ( value instanceof String )
190 {
191 val = new ServerStringValue( attributeType, (String)value );
192 }
193 else if ( value instanceof byte[] )
194 {
195 val = new ServerStringValue( attributeType, StringTools.utf8ToString( (byte[])value ) );
196 }
197 else
198 {
199 throw new NamingException( "Bad value for the OID " + oid );
200 }
201 }
202 else
203 {
204 if ( value instanceof String )
205 {
206 val = new ServerBinaryValue( attributeType, StringTools.getBytesUtf8( (String)value ) );
207 }
208 else if ( value instanceof byte[] )
209 {
210 val = new ServerBinaryValue( attributeType, (byte[])value );
211 }
212 else
213 {
214 throw new NamingException( "Bad value for the OID " + oid );
215 }
216 }
217
218
219 CompareOperationContext opCtx = new CompareOperationContext( getSession(), name, oid, val );
220 opCtx.addRequestControls( requestControls );
221
222
223 boolean result = false;
224 try
225 {
226 result = super.getDirectoryService().getOperationManager().compare( opCtx );
227 }
228 catch ( Exception e )
229 {
230 JndiUtils.wrap( e );
231 }
232
233
234 responseControls = getResponseControls();
235 requestControls = EMPTY_CONTROLS;
236 return result;
237 }
238
239
240
241
242
243
244
245
246
247
248 public void ldapUnbind() throws NamingException
249 {
250 LdapDN principalDn = getSession().getEffectivePrincipal().getJndiName();
251 UnbindOperationContext opCtx = new UnbindOperationContext( getSession() );
252 opCtx.addRequestControls( requestControls );
253 try
254 {
255 super.getDirectoryService().getOperationManager().unbind( opCtx );
256 }
257 catch ( Exception e )
258 {
259 JndiUtils.wrap( e );
260 }
261 responseControls = opCtx.getResponseControls();
262 requestControls = EMPTY_CONTROLS;
263 }
264
265
266 public ServerContext getRootContext() throws NamingException
267 {
268 ServerContext ctx = null;
269
270 try
271 {
272 ctx = new ServerLdapContext( getService(), getSession().getEffectivePrincipal(), new LdapDN() );
273 }
274 catch ( Exception e )
275 {
276 JndiUtils.wrap( e );
277 }
278
279 return ctx;
280 }
281 }