1 17 package org.alfresco.repo.security.authentication.ldap; 18 19 import java.io.BufferedWriter ; 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.Writer ; 24 import java.util.Collection ; 25 import java.util.Map ; 26 27 import javax.naming.NamingEnumeration ; 28 import javax.naming.NamingException ; 29 import javax.naming.directory.Attribute ; 30 import javax.naming.directory.Attributes ; 31 import javax.naming.directory.InitialDirContext ; 32 import javax.naming.directory.SearchControls ; 33 import javax.naming.directory.SearchResult ; 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 ; 48 import org.xml.sax.helpers.AttributesImpl ; 49 50 public class LDAPPersonExportSource implements ExportSource 51 { 52 private static Log s_logger = LogFactory.getLog(LDAPPersonExportSource.class); 53 54 private String personQuery = "(objectclass=inetOrgPerson)"; 55 56 private String searchBase; 57 58 private String userIdAttributeName; 59 60 private LDAPInitialDirContextFactory ldapInitialContextFactory; 61 62 private PersonService personService; 63 64 private Map <String , String > attributeMapping; 65 66 private NamespaceService namespaceService; 67 68 private String defaultHomeFolder; 69 70 public LDAPPersonExportSource() 71 { 72 super(); 73 } 74 75 public void setPersonQuery(String personQuery) 76 { 77 this.personQuery = personQuery; 78 } 79 80 public void setSearchBase(String searchBase) 81 { 82 this.searchBase = searchBase; 83 } 84 85 public void setUserIdAttributeName(String 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 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 <String , String > 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 <String > prefixes = namespaceService.getPrefixes(); 120 QName childQName = QName.createQName(NamespaceService.REPOSITORY_VIEW_PREFIX, "childName", namespaceService); 121 122 try 123 { 124 AttributesImpl attrs = new AttributesImpl (); 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 prefix : prefixes) 131 { 132 if (!prefix.equals("xml")) 133 { 134 String 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 ()); 141 142 InitialDirContext ctx = null; 143 try 144 { 145 ctx = ldapInitialContextFactory.getDefaultIntialDirContext(); 146 147 150 SearchControls userSearchCtls = new SearchControls (); 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 searchResults = ctx.search(searchBase, personQuery, userSearchCtls); 157 while (searchResults.hasMoreElements()) 158 { 159 SearchResult result = (SearchResult ) searchResults.next(); 160 Attributes attributes = result.getAttributes(); 161 Attribute uidAttribute = attributes.get(userIdAttributeName); 162 String uid = (String ) 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 175 177 writer.startElement(ContentModel.ASPECT_OWNABLE.getNamespaceURI(), ContentModel.ASPECT_OWNABLE 178 .getLocalName(), ContentModel.ASPECT_OWNABLE.toPrefixString(namespaceService), 179 new AttributesImpl ()); 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 ()); 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 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 ()); 200 201 String attribute = attributeMapping.get(key); 203 if (attribute != null) 204 { 205 String value = (String ) 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 218 if (!(attributeMapping.keySet().contains(ContentModel.PROP_HOMEFOLDER.toString()) || attributeMapping 219 .keySet().contains(ContentModel.PROP_HOMEFOLDER.toPrefixString(namespaceService)))) 220 { 221 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 ()); 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 uguid = personService.getPerson(uid).getId(); 242 243 writer.startElement(nodeUUID.getNamespaceURI(), nodeUUID.getLocalName(), nodeUUID 244 .toPrefixString(namespaceService), new AttributesImpl ()); 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 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 e) 270 { 271 throw new ExportSourceImporterException("Failed to import people.", e); 272 } 273 } 274 } 275 276 for (String 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 e) 290 { 291 throw new ExportSourceImporterException("Failed to create file for import.", e); 292 } 293 } 294 295 public static void main(String [] args) throws IOException 296 { 297 ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); 298 ExportSource source = (ExportSource) ctx.getBean("ldapPeopleExportSource"); 299 300 File file = new File (args[0]); 301 Writer writer = new BufferedWriter (new FileWriter (file)); 302 XMLWriter xmlWriter = createXMLExporter(writer); 303 source.generateExport(xmlWriter); 304 xmlWriter.close(); 305 306 } 307 308 private static XMLWriter createXMLExporter(Writer writer) 309 { 310 OutputFormat format = OutputFormat.createPrettyPrint(); 312 format.setNewLineAfterDeclaration(false); 313 format.setIndentSize(3); 314 format.setEncoding("UTF-8"); 315 316 318 XMLWriter xmlWriter = new XMLWriter(writer, format); 319 return xmlWriter; 320 } 321 } | Popular Tags |