001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.core.jndi;
021
022
023 import java.util.Hashtable;
024
025 import javax.naming.ConfigurationException;
026 import javax.naming.Context;
027 import javax.naming.InvalidNameException;
028 import javax.naming.NamingException;
029 import javax.naming.ldap.LdapName;
030 import javax.naming.spi.InitialContextFactory;
031
032 import org.apache.directory.server.core.CoreSession;
033 import org.apache.directory.server.core.DirectoryService;
034 import org.apache.directory.server.i18n.I18n;
035 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
036 import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
037 import org.apache.directory.shared.ldap.jndi.JndiUtils;
038 import org.apache.directory.shared.ldap.name.DN;
039 import org.apache.directory.shared.ldap.util.StringTools;
040
041
042 /**
043 * A simplistic implementation of {@link AbstractContextFactory}.
044 * This class simply extends {@link AbstractContextFactory} and leaves all
045 * abstract event listener methods as empty.
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 * @version $Rev: 925887 $
049 */
050 public class CoreContextFactory implements InitialContextFactory
051 {
052 public synchronized Context getInitialContext( Hashtable env ) throws NamingException
053 {
054 env = ( Hashtable<String, Object> ) env.clone();
055 DN principalDn = null;
056
057 try
058 {
059 principalDn = new DN( getPrincipal( env ) );
060 }
061 catch ( LdapInvalidDnException lide )
062 {
063 throw new InvalidNameException( I18n.err( I18n.ERR_733, env ) );
064 }
065
066 byte[] credential = getCredential( env );
067 String authentication = getAuthentication( env );
068 String providerUrl = getProviderUrl( env );
069
070 DirectoryService service = ( DirectoryService ) env.get( DirectoryService.JNDI_KEY );
071
072 if ( service == null )
073 {
074 throw new ConfigurationException( I18n.err( I18n.ERR_477, env ) );
075 }
076
077 if ( ! service.isStarted() )
078 {
079 return new DeadContext();
080 }
081
082 ServerLdapContext ctx = null;
083 try
084 {
085 CoreSession session = service.getSession( principalDn, credential );
086 ctx = new ServerLdapContext( service, session, new LdapName( providerUrl ) );
087 }
088 catch ( Exception e )
089 {
090 JndiUtils.wrap( e );
091 }
092
093 // check to make sure we have access to the specified dn in provider URL
094 ctx.lookup( "" );
095 return ctx;
096 }
097
098
099 public static String getProviderUrl( Hashtable<String, Object> env )
100 {
101 String providerUrl;
102 Object value;
103 value = env.get( Context.PROVIDER_URL );
104 if ( value == null )
105 {
106 value = "";
107 }
108 providerUrl = value.toString();
109
110 env.put( Context.PROVIDER_URL, providerUrl );
111
112 return providerUrl;
113 }
114
115
116 public static String getAuthentication( Hashtable<String, Object> env )
117 {
118 String authentication;
119 Object value = env.get( Context.SECURITY_AUTHENTICATION );
120 if ( value == null )
121 {
122 authentication = AuthenticationLevel.NONE.toString();
123 }
124 else
125 {
126 authentication = value.toString();
127 }
128
129 env.put( Context.SECURITY_AUTHENTICATION, authentication );
130
131 return authentication;
132 }
133
134
135 public static byte[] getCredential( Hashtable<String, Object> env ) throws javax.naming.ConfigurationException
136 {
137 byte[] credential;
138 Object value = env.get( Context.SECURITY_CREDENTIALS );
139 if ( value == null )
140 {
141 credential = null;
142 }
143 else if ( value instanceof String )
144 {
145 credential = StringTools.getBytesUtf8( (String)value );
146 }
147 else if ( value instanceof byte[] )
148 {
149 credential = ( byte[] ) value;
150 }
151 else
152 {
153 throw new javax.naming.ConfigurationException( I18n.err( I18n.ERR_478, Context.SECURITY_CREDENTIALS ) );
154 }
155
156 if ( credential != null )
157 {
158 env.put( Context.SECURITY_CREDENTIALS, credential );
159 }
160
161 return credential;
162 }
163
164
165 public static String getPrincipal( Hashtable<String,Object> env )
166 {
167 String principal;
168 Object value = env.get( Context.SECURITY_PRINCIPAL );
169 if ( value == null )
170 {
171 principal = null;
172 }
173 else
174 {
175 principal = value.toString();
176 env.put( Context.SECURITY_PRINCIPAL, principal );
177 }
178
179 return principal;
180 }
181 }