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 org.apache.directory.server.core.CoreSession;
24 import org.apache.directory.server.core.DirectoryService;
25 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
26 import org.apache.directory.shared.ldap.name.LdapDN;
27 import org.apache.directory.shared.ldap.util.StringTools;
28
29 import javax.naming.ConfigurationException;
30 import javax.naming.Context;
31 import javax.naming.NamingException;
32 import javax.naming.spi.InitialContextFactory;
33 import java.util.Hashtable;
34
35
36
37
38
39
40
41
42
43
44 public class CoreContextFactory implements InitialContextFactory
45 {
46 public synchronized Context getInitialContext( Hashtable env ) throws NamingException
47 {
48 env = ( Hashtable<String, Object> ) env.clone();
49 LdapDN principalDn = new LdapDN( getPrincipal( env ) );
50 byte[] credential = getCredential( env );
51 String authentication = getAuthentication( env );
52 String providerUrl = getProviderUrl( env );
53
54 DirectoryService service = ( DirectoryService ) env.get( DirectoryService.JNDI_KEY );
55
56 if ( service == null )
57 {
58 throw new ConfigurationException( "Cannot find directory service in environment: " + env );
59 }
60
61 if ( ! service.isStarted() )
62 {
63 return new DeadContext();
64 }
65
66 ServerLdapContext ctx = null;
67 try
68 {
69 CoreSession session = service.getSession( principalDn, credential );
70 ctx = new ServerLdapContext( service, session, new LdapDN( providerUrl ) );
71 }
72 catch ( Exception e )
73 {
74 JndiUtils.wrap( e );
75 }
76
77
78 ctx.lookup( "" );
79 return ctx;
80 }
81
82
83 public static String getProviderUrl( Hashtable<String, Object> env )
84 {
85 String providerUrl;
86 Object value;
87 value = env.get( Context.PROVIDER_URL );
88 if ( value == null )
89 {
90 value = "";
91 }
92 providerUrl = value.toString();
93
94 env.put( Context.PROVIDER_URL, providerUrl );
95
96 return providerUrl;
97 }
98
99
100 public static String getAuthentication( Hashtable<String, Object> env )
101 {
102 String authentication;
103 Object value = env.get( Context.SECURITY_AUTHENTICATION );
104 if ( value == null )
105 {
106 authentication = AuthenticationLevel.NONE.toString();
107 }
108 else
109 {
110 authentication = value.toString();
111 }
112
113 env.put( Context.SECURITY_AUTHENTICATION, authentication );
114
115 return authentication;
116 }
117
118
119 public static byte[] getCredential( Hashtable<String, Object> env ) throws javax.naming.ConfigurationException
120 {
121 byte[] credential;
122 Object value = env.get( Context.SECURITY_CREDENTIALS );
123 if ( value == null )
124 {
125 credential = null;
126 }
127 else if ( value instanceof String )
128 {
129 credential = StringTools.getBytesUtf8( (String)value );
130 }
131 else if ( value instanceof byte[] )
132 {
133 credential = ( byte[] ) value;
134 }
135 else
136 {
137 throw new javax.naming.ConfigurationException( "Can't convert '" + Context.SECURITY_CREDENTIALS + "' to byte[]." );
138 }
139
140 if ( credential != null )
141 {
142 env.put( Context.SECURITY_CREDENTIALS, credential );
143 }
144
145 return credential;
146 }
147
148
149 public static String getPrincipal( Hashtable<String,Object> env )
150 {
151 String principal;
152 Object value = env.get( Context.SECURITY_PRINCIPAL );
153 if ( value == null )
154 {
155 principal = null;
156 }
157 else
158 {
159 principal = value.toString();
160 env.put( Context.SECURITY_PRINCIPAL, principal );
161 }
162
163 return principal;
164 }
165 }