1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.directory.server.core.unit;
20
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.directory.server.core.CoreSession;
24 import org.apache.directory.server.core.entry.DefaultServerEntry;
25 import org.apache.directory.shared.ldap.entry.EntryAttribute;
26 import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
27 import org.apache.directory.shared.ldap.ldif.ChangeType;
28 import org.apache.directory.shared.ldap.ldif.LdifEntry;
29 import org.apache.directory.shared.ldap.name.LdapDN;
30 import org.apache.directory.shared.ldap.name.Rdn;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import javax.naming.InvalidNameException;
35 import javax.naming.NamingException;
36 import java.io.File;
37 import java.io.IOException;
38
39
40
41
42
43
44
45
46 public class IntegrationUtils
47 {
48 private static final Logger LOG = LoggerFactory.getLogger( IntegrationUtils.class );
49
50
51
52
53
54
55
56
57 public static void doDelete( File wkdir ) throws IOException
58 {
59 if ( wkdir.exists() )
60 {
61 try
62 {
63 FileUtils.deleteDirectory( wkdir );
64 }
65 catch ( IOException e )
66 {
67 LOG.error( "Failed to delete the working directory.", e );
68 }
69 }
70 if ( wkdir.exists() )
71 {
72 throw new IOException( "Failed to delete: " + wkdir );
73 }
74 }
75
76
77 public static LdifEntry getUserAddLdif() throws InvalidNameException, NamingException
78 {
79 return getUserAddLdif( "uid=akarasulu,ou=users,ou=system", "test".getBytes(), "Alex Karasulu", "Karasulu" );
80 }
81
82
83
84 public static void apply( CoreSession root, LdifEntry entry ) throws Exception
85 {
86 LdapDN dn = new LdapDN( entry.getDn() );
87
88 switch( entry.getChangeType().getChangeType() )
89 {
90 case( ChangeType.ADD_ORDINAL ):
91 root.add(
92 new DefaultServerEntry(
93 root.getDirectoryService().getRegistries(), entry.getEntry() ) );
94 break;
95
96 case( ChangeType.DELETE_ORDINAL ):
97 root.delete( entry.getDn() );
98 break;
99
100 case( ChangeType.MODDN_ORDINAL ):
101 case( ChangeType.MODRDN_ORDINAL ):
102 Rdn newRdn = new Rdn( entry.getNewRdn() );
103
104 if ( entry.getNewSuperior() != null )
105 {
106
107
108 Rdn oldRdn = dn.getRdn();
109 LdapDN newSuperior = new LdapDN( entry.getNewSuperior() );
110
111 if ( dn.size() == 0 )
112 {
113 throw new IllegalStateException( "can't move the root DSE" );
114 }
115 else if ( oldRdn.equals( newRdn ) )
116 {
117
118 root.move( dn, newSuperior );
119 }
120 else
121 {
122
123 root.moveAndRename( dn, newSuperior, newRdn, entry.isDeleteOldRdn() );
124 }
125 }
126 else
127 {
128
129 root.rename( dn, newRdn, entry.isDeleteOldRdn() );
130 }
131
132 break;
133
134 case( ChangeType.MODIFY_ORDINAL ):
135 root.modify( dn, entry.getModificationItems() );
136 break;
137
138 default:
139 throw new IllegalStateException( "Unidentified change type value: " + entry.getChangeType() );
140 }
141 }
142
143
144 public static LdifEntry getUserAddLdif( String dnstr, byte[] password, String cn, String sn )
145 throws InvalidNameException, NamingException
146 {
147 LdapDN dn = new LdapDN( dnstr );
148 LdifEntry ldif = new LdifEntry();
149 ldif.setDn( dnstr );
150 ldif.setChangeType( ChangeType.Add );
151
152 EntryAttribute attr = new DefaultClientAttribute( "objectClass",
153 "top", "person", "organizationalPerson", "inetOrgPerson" );
154
155 ldif.addAttribute( attr );
156
157 attr = new DefaultClientAttribute( "ou", "Engineering", "People" );
158 ldif.addAttribute( attr );
159
160 String uid = ( String ) dn.getRdn().getValue();
161 ldif.putAttribute( "uid", uid );
162
163 ldif.putAttribute( "l", "Bogusville" );
164 ldif.putAttribute( "cn", cn );
165 ldif.putAttribute( "sn", sn );
166 ldif.putAttribute( "mail", uid + "@apache.org" );
167 ldif.putAttribute( "telephoneNumber", "+1 408 555 4798" );
168 ldif.putAttribute( "facsimileTelephoneNumber", "+1 408 555 9751" );
169 ldif.putAttribute( "roomnumber", "4612" );
170 ldif.putAttribute( "userPassword", password );
171
172 String givenName = cn.split( " " )[0];
173 ldif.putAttribute( "givenName", givenName );
174 return ldif;
175 }
176 }