KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > authentication > ldap > LDAPPersonExportSource


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.authentication.ldap;
18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.naming.NamingEnumeration JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.directory.Attribute JavaDoc;
30 import javax.naming.directory.Attributes JavaDoc;
31 import javax.naming.directory.InitialDirContext JavaDoc;
32 import javax.naming.directory.SearchControls JavaDoc;
33 import javax.naming.directory.SearchResult JavaDoc;
34
35 import org.alfresco.model.ContentModel;
36 import org.alfresco.repo.importer.ExportSource;
37 import org.alfresco.repo.importer.ExportSourceImporterException;
38 import org.alfresco.service.cmr.security.PersonService;
39 import org.alfresco.service.namespace.NamespaceService;
40 import org.alfresco.service.namespace.QName;
41 import org.alfresco.util.ApplicationContextHelper;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.dom4j.io.OutputFormat;
45 import org.dom4j.io.XMLWriter;
46 import org.springframework.context.ApplicationContext;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.helpers.AttributesImpl JavaDoc;
49
50 public class LDAPPersonExportSource implements ExportSource
51 {
52     private static Log s_logger = LogFactory.getLog(LDAPPersonExportSource.class);
53     
54     private String JavaDoc personQuery = "(objectclass=inetOrgPerson)";
55
56     private String JavaDoc searchBase;
57
58     private String JavaDoc userIdAttributeName;
59
60     private LDAPInitialDirContextFactory ldapInitialContextFactory;
61
62     private PersonService personService;
63
64     private Map JavaDoc<String JavaDoc, String JavaDoc> attributeMapping;
65
66     private NamespaceService namespaceService;
67
68     private String JavaDoc defaultHomeFolder;
69
70     public LDAPPersonExportSource()
71     {
72         super();
73     }
74
75     public void setPersonQuery(String JavaDoc personQuery)
76     {
77         this.personQuery = personQuery;
78     }
79
80     public void setSearchBase(String JavaDoc searchBase)
81     {
82         this.searchBase = searchBase;
83     }
84
85     public void setUserIdAttributeName(String JavaDoc userIdAttributeName)
86     {
87         this.userIdAttributeName = userIdAttributeName;
88     }
89
90     public void setLDAPInitialDirContextFactory(LDAPInitialDirContextFactory ldapInitialDirContextFactory)
91     {
92         this.ldapInitialContextFactory = ldapInitialDirContextFactory;
93     }
94
95     public void setPersonService(PersonService personService)
96     {
97         this.personService = personService;
98     }
99
100     public void setDefaultHomeFolder(String JavaDoc defaultHomeFolder)
101     {
102         this.defaultHomeFolder = defaultHomeFolder;
103     }
104
105     public void setNamespaceService(NamespaceService namespaceService)
106     {
107         this.namespaceService = namespaceService;
108     }
109
110     public void setAttributeMapping(Map JavaDoc<String JavaDoc, String JavaDoc> attributeMapping)
111     {
112         this.attributeMapping = attributeMapping;
113     }
114
115     public void generateExport(XMLWriter writer)
116     {
117         QName nodeUUID = QName.createQName("sys:node-uuid", namespaceService);
118
119         Collection JavaDoc<String JavaDoc> prefixes = namespaceService.getPrefixes();
120         QName childQName = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, "childName", namespaceService);
121
122         try
123         {
124             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
125             attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, childQName.getLocalName(), childQName
126                     .toPrefixString(), null, ContentModel.TYPE_PERSON.toPrefixString(namespaceService));
127
128             writer.startDocument();
129
130             for (String JavaDoc prefix : prefixes)
131             {
132                 if (!prefix.equals("xml"))
133                 {
134                     String JavaDoc uri = namespaceService.getNamespaceURI(prefix);
135                     writer.startPrefixMapping(prefix, uri);
136                 }
137             }
138
139             writer.startElement(NamespaceService.REPOSITORY_VIEW_PREFIX, "view",
140                     NamespaceService.REPOSITORY_VIEW_PREFIX + ":" + "view", new AttributesImpl JavaDoc());
141
142             InitialDirContext JavaDoc ctx = null;
143             try
144             {
145                 ctx = ldapInitialContextFactory.getDefaultIntialDirContext();
146
147                 // Authentication has been successful.
148
// Set the current user, they are now authenticated.
149

150                 SearchControls JavaDoc userSearchCtls = new SearchControls JavaDoc();
151                 userSearchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
152                 System.out.println("COUNT "+userSearchCtls.getCountLimit());
153                 System.out.println("TIME "+userSearchCtls.getTimeLimit());
154                 userSearchCtls.setCountLimit(Integer.MAX_VALUE);
155                 
156                 NamingEnumeration JavaDoc searchResults = ctx.search(searchBase, personQuery, userSearchCtls);
157                 while (searchResults.hasMoreElements())
158                 {
159                     SearchResult JavaDoc result = (SearchResult JavaDoc) searchResults.next();
160                     Attributes JavaDoc attributes = result.getAttributes();
161                     Attribute JavaDoc uidAttribute = attributes.get(userIdAttributeName);
162                     String JavaDoc uid = (String JavaDoc) uidAttribute.get(0);
163
164                     if(s_logger.isDebugEnabled())
165                     {
166                         s_logger.debug("Adding user for "+uid);
167                     }
168                     System.out.println("User "+uid);
169                     
170                     writer.startElement(ContentModel.TYPE_PERSON.getNamespaceURI(), ContentModel.TYPE_PERSON
171                             .getLocalName(), ContentModel.TYPE_PERSON.toPrefixString(namespaceService), attrs);
172
173                     // permissions
174

175                     // owner
176

177                     writer.startElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE
178                             .getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService),
179                             new AttributesImpl JavaDoc());
180
181                     writer.endElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE
182                             .getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService));
183
184                     writer.startElement(ContentModel.PROP_OWNER.getNamespaceURI(), ContentModel.PROP_OWNER
185                             .getLocalName(), ContentModel.PROP_OWNER.toPrefixString(namespaceService),
186                             new AttributesImpl JavaDoc());
187
188                     writer.characters(uid.toCharArray(), 0, uid.length());
189
190                     writer.endElement(ContentModel.PROP_OWNER.getNamespaceURI(),
191                             ContentModel.PROP_OWNER.getLocalName(), ContentModel.PROP_OWNER
192                                     .toPrefixString(namespaceService));
193
194                     for (String JavaDoc key : attributeMapping.keySet())
195                     {
196                         QName keyQName = QName.createQName(key, namespaceService);
197
198                         writer.startElement(keyQName.getNamespaceURI(), keyQName.getLocalName(), keyQName
199                                 .toPrefixString(namespaceService), new AttributesImpl JavaDoc());
200
201                         // cater for null
202
String JavaDoc attribute = attributeMapping.get(key);
203                         if (attribute != null)
204                         {
205                             String JavaDoc value = (String JavaDoc) attributes.get(attribute).get(0);
206                             if (value != null)
207                             {
208                                 writer.characters(value.toCharArray(), 0, value.length());
209                             }
210                         }
211
212                         writer.endElement(keyQName.getNamespaceURI(), keyQName.getLocalName(), keyQName
213                                 .toPrefixString(namespaceService));
214                     }
215
216                     // Default home folder
217

218                     if (!(attributeMapping.keySet().contains(ContentModel.PROP_HOMEFOLDER.toString()) || attributeMapping
219                             .keySet().contains(ContentModel.PROP_HOMEFOLDER.toPrefixString(namespaceService))))
220                     {
221                         // Only if we are creating the person for the first time
222
if (!personService.personExists(uid))
223                         {
224                             writer.startElement(ContentModel.PROP_HOMEFOLDER.getNamespaceURI(),
225                                     ContentModel.PROP_HOMEFOLDER.getLocalName(), ContentModel.PROP_HOMEFOLDER
226                                             .toPrefixString(namespaceService), new AttributesImpl JavaDoc());
227
228                             if (defaultHomeFolder != null)
229                             {
230                                 writer.characters(defaultHomeFolder.toCharArray(), 0, defaultHomeFolder.length());
231                             }
232
233                             writer.endElement(ContentModel.PROP_HOMEFOLDER.getNamespaceURI(),
234                                     ContentModel.PROP_HOMEFOLDER.getLocalName(), ContentModel.PROP_HOMEFOLDER
235                                             .toPrefixString(namespaceService));
236                         }
237                     }
238
239                     if (personService.personExists(uid))
240                     {
241                         String JavaDoc uguid = personService.getPerson(uid).getId();
242
243                         writer.startElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID
244                                 .toPrefixString(namespaceService), new AttributesImpl JavaDoc());
245
246                         writer.characters(uguid.toCharArray(), 0, uguid.length());
247
248                         writer.endElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID
249                                 .toPrefixString(namespaceService));
250                     }
251                     writer.endElement(ContentModel.TYPE_PERSON.getNamespaceURI(), ContentModel.TYPE_PERSON
252                             .getLocalName(), ContentModel.TYPE_PERSON.toPrefixString(namespaceService));
253
254                 }
255
256             }
257             catch (NamingException JavaDoc e)
258             {
259                 throw new ExportSourceImporterException("Failed to import people.", e);
260             }
261             finally
262             {
263                 if (ctx != null)
264                 {
265                     try
266                     {
267                         ctx.close();
268                     }
269                     catch (NamingException JavaDoc e)
270                     {
271                         throw new ExportSourceImporterException("Failed to import people.", e);
272                     }
273                 }
274             }
275
276             for (String JavaDoc prefix : prefixes)
277             {
278                 if (!prefix.equals("xml"))
279                 {
280                     writer.endPrefixMapping(prefix);
281                 }
282             }
283
284             writer.endElement(NamespaceService.REPOSITORY_VIEW_PREFIX, "view", NamespaceService.REPOSITORY_VIEW_PREFIX
285                     + ":" + "view");
286
287             writer.endDocument();
288         }
289         catch (SAXException JavaDoc e)
290         {
291             throw new ExportSourceImporterException("Failed to create file for import.", e);
292         }
293     }
294
295     public static void main(String JavaDoc[] args) throws IOException JavaDoc
296     {
297         ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
298         ExportSource source = (ExportSource) ctx.getBean("ldapPeopleExportSource");
299
300         File JavaDoc file = new File JavaDoc(args[0]);
301         Writer JavaDoc writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
302         XMLWriter xmlWriter = createXMLExporter(writer);
303         source.generateExport(xmlWriter);
304         xmlWriter.close();
305
306     }
307
308     private static XMLWriter createXMLExporter(Writer JavaDoc writer)
309     {
310         // Define output format
311
OutputFormat format = OutputFormat.createPrettyPrint();
312         format.setNewLineAfterDeclaration(false);
313         format.setIndentSize(3);
314         format.setEncoding("UTF-8");
315
316         // Construct an XML Exporter
317

318         XMLWriter xmlWriter = new XMLWriter(writer, format);
319         return xmlWriter;
320     }
321 }
Popular Tags