View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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   * A simplistic implementation of {@link AbstractContextFactory}.
38   * This class simply extends {@link AbstractContextFactory} and leaves all
39   * abstract event listener methods as empty.
40   *
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   * @version $Rev: 679219 $
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          // check to make sure we have access to the specified dn in provider URL
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 }