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  package org.apache.directory.server.core.integ;
20  
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  import javax.naming.InvalidNameException;
26  import javax.naming.NamingException;
27  import javax.naming.ldap.LdapContext;
28  
29  import org.apache.commons.io.FileUtils;
30  import org.apache.directory.server.constants.ServerDNConstants;
31  import org.apache.directory.server.core.DirectoryService;
32  import org.apache.directory.server.core.authn.LdapPrincipal;
33  import org.apache.directory.server.core.entry.DefaultServerEntry;
34  import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
35  import org.apache.directory.shared.ldap.entry.EntryAttribute;
36  import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
37  import org.apache.directory.shared.ldap.ldif.ChangeType;
38  import org.apache.directory.shared.ldap.ldif.LdifEntry;
39  import org.apache.directory.shared.ldap.ldif.LdifReader;
40  import org.apache.directory.shared.ldap.name.LdapDN;
41  import org.apache.directory.shared.ldap.name.Rdn;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import org.apache.directory.server.core.CoreSession;
46  import org.apache.directory.server.core.jndi.ServerLdapContext;
47  
48  
49  /**
50   * Integration test utility methods.
51   *
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   * @version $Rev$, $Date$
54   */
55  public class IntegrationUtils
56  {
57      /** The class logger */
58      private static final Logger LOG = LoggerFactory.getLogger( IntegrationUtils.class );
59  
60  
61      /**
62       * Deletes the working directory.
63       *
64       * @param wkdir the working directory to delete
65       * @throws IOException if the working directory cannot be deleted
66       */
67      public static void doDelete( File wkdir ) throws IOException
68      {
69          if ( wkdir.exists() )
70          {
71              try
72              {
73                  FileUtils.deleteDirectory( wkdir );
74              }
75              catch ( IOException e )
76              {
77                  LOG.error( "Failed to delete the working directory.", e );
78              }
79          }
80          if ( wkdir.exists() )
81          {
82              throw new IOException( "Failed to delete: " + wkdir );
83          }
84      }
85  
86  
87      /**
88       * Inject an ldif String into the server. DN must be relative to the
89       * root.
90       *
91       * @param service the directory service to use 
92       * @param ldif the ldif containing entries to add to the server.
93       * @throws NamingException if there is a problem adding the entries from the LDIF
94       */
95      public static void injectEntries( DirectoryService service, String ldif ) throws Exception
96      {
97          LdifReader reader = new LdifReader();
98          List<LdifEntry> entries = reader.parseLdif( ldif );
99  
100         for ( LdifEntry entry : entries )
101         {
102             service.getAdminSession().add( 
103                 new DefaultServerEntry( service.getRegistries(), entry.getEntry() ) ); 
104         }
105     }
106 
107 
108     public static LdifEntry getUserAddLdif() throws InvalidNameException, NamingException
109     {
110         return getUserAddLdif( "uid=akarasulu,ou=users,ou=system", "test".getBytes(), "Alex Karasulu", "Karasulu" );
111     }
112 
113 
114     public static LdapContext getContext( String principalDn, DirectoryService service, String dn )
115             throws Exception
116     {
117         if ( principalDn == null )
118         {
119             principalDn = "";
120         }
121 
122         LdapDN userDn = new LdapDN( principalDn );
123         userDn.normalize( service.getRegistries().getAttributeTypeRegistry().getNormalizerMapping() );
124         LdapPrincipal principal = new LdapPrincipal( userDn, AuthenticationLevel.SIMPLE );
125 
126         if ( dn == null )
127         {
128             dn = "";
129         }
130 
131         CoreSession session = service.getSession( principal );
132         LdapContext ctx = new ServerLdapContext( service, session, new LdapDN( dn ) );
133         return ctx;
134     }
135 
136 
137     public static CoreSession getCoreSession( String principalDn, DirectoryService service, String dn )
138         throws Exception
139     {
140         if ( principalDn == null )
141         {
142             principalDn = "";
143         }
144         
145         LdapDN userDn = new LdapDN( principalDn );
146         userDn.normalize( service.getRegistries().getAttributeTypeRegistry().getNormalizerMapping() );
147         LdapPrincipal principal = new LdapPrincipal( userDn, AuthenticationLevel.SIMPLE );
148         
149         if ( dn == null )
150         {
151             dn = "";
152         }
153         
154         CoreSession session = service.getSession( principal );
155         return session;
156     }
157 
158 
159     public static LdapContext getSystemContext( DirectoryService service ) throws Exception
160     {
161         return getContext( ServerDNConstants.ADMIN_SYSTEM_DN, service, ServerDNConstants.SYSTEM_DN );
162     }
163 
164 
165     public static LdapContext getSchemaContext( DirectoryService service ) throws Exception
166     {
167         return getContext( ServerDNConstants.ADMIN_SYSTEM_DN, service, ServerDNConstants.OU_SCHEMA_DN );
168     }
169 
170 
171     public static LdapContext getRootContext( DirectoryService service ) throws Exception
172     {
173         return getContext( ServerDNConstants.ADMIN_SYSTEM_DN, service, "" );
174     }
175 
176 
177     public static void apply( DirectoryService service, LdifEntry entry ) throws Exception
178     {
179         LdapDN dn = new LdapDN( entry.getDn() );
180         CoreSession session = service.getAdminSession();
181 
182         switch( entry.getChangeType().getChangeType() )
183         {
184             case( ChangeType.ADD_ORDINAL ):
185                 session.add( 
186                     new DefaultServerEntry( service.getRegistries(), entry.getEntry() ) ); 
187                 break;
188                 
189             case( ChangeType.DELETE_ORDINAL ):
190                 session.delete( dn );
191                 break;
192                 
193             case( ChangeType.MODDN_ORDINAL ):
194             case( ChangeType.MODRDN_ORDINAL ):
195                 Rdn newRdn = new Rdn( entry.getNewRdn() );
196             
197                 if ( entry.getNewSuperior() != null )
198                 {
199                     // It's a move. The superior have changed
200                     // Let's see if it's a rename too
201                     Rdn oldRdn = dn.getRdn();
202                     LdapDN newSuperior = new LdapDN( entry.getNewSuperior() );
203                     
204                     if ( dn.size() == 0 )
205                     {
206                         throw new IllegalStateException( "can't move the root DSE" );
207                     }
208                     else if ( oldRdn.equals( newRdn ) )
209                     {
210                         // Same rdn : it's a move
211                         session.move( dn, newSuperior );
212                     }
213                     else
214                     {
215                         // it's a move and rename 
216                         session.moveAndRename( dn, newSuperior, newRdn, entry.isDeleteOldRdn() );
217                     }
218                 }
219                 else
220                 {
221                     // it's a rename
222                     session.rename( dn, newRdn, entry.isDeleteOldRdn() );
223                 }
224                 
225                 break;
226 
227             case( ChangeType.MODIFY_ORDINAL ):
228                 session.modify( dn, entry.getModificationItems() );
229                 break;
230 
231             default:
232                 throw new IllegalStateException( "Unidentified change type value: " + entry.getChangeType() );
233         }
234     }
235 
236 
237     public static LdifEntry getUserAddLdif( String dnstr, byte[] password, String cn, String sn )
238             throws InvalidNameException, NamingException
239     {
240         LdapDN dn = new LdapDN( dnstr );
241         LdifEntry ldif = new LdifEntry();
242         ldif.setDn( dnstr );
243         ldif.setChangeType( ChangeType.Add );
244 
245         EntryAttribute attr = new DefaultClientAttribute( "objectClass", 
246             "top", "person", "organizationalPerson", "inetOrgPerson" );
247         ldif.addAttribute( attr );
248 
249         attr = new DefaultClientAttribute( "ou", "Engineering", "People" );
250         ldif.addAttribute( attr );
251 
252         String uid = ( String ) dn.getRdn().getValue();
253         ldif.putAttribute( "uid", uid );
254 
255         ldif.putAttribute( "l", "Bogusville" );
256         ldif.putAttribute( "cn", cn );
257         ldif.putAttribute( "sn", sn );
258         ldif.putAttribute( "mail", uid + "@apache.org" );
259         ldif.putAttribute( "telephoneNumber", "+1 408 555 4798" );
260         ldif.putAttribute( "facsimileTelephoneNumber", "+1 408 555 9751" );
261         ldif.putAttribute( "roomnumber", "4612" );
262         ldif.putAttribute( "userPassword", password );
263 
264         String givenName = cn.split( " " )[0];
265         ldif.putAttribute( "givenName", givenName );
266         return ldif;
267     }
268 }