Mercurial > hg > NamedIdentityManager
changeset 9:866503140b34
LdapToRDFExporter added.
author | dwinter |
---|---|
date | Thu, 26 Apr 2012 09:27:57 +0200 |
parents | 5dba58db2f55 |
children | 0fe0d5b39243 |
files | src/de/mpiwg/itgroup/nimanager/cone/LdapToRDFExporter.java |
diffstat | 1 files changed, 137 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/de/mpiwg/itgroup/nimanager/cone/LdapToRDFExporter.java Thu Apr 26 09:27:57 2012 +0200 @@ -0,0 +1,137 @@ +package de.mpiwg.itgroup.nimanager.cone; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.Hashtable; + +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.BasicAttributes; +import javax.naming.directory.DirContext; +import javax.naming.directory.InitialDirContext; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; + +public class LdapToRDFExporter { + + String MPIWG_PERSON_BASE="http://entities.mpiwg-berlin.mpg.de/persons/mpiwg:%s"; + String MPIWG_PERSON_TYPE="http://ontologies.mpiwg-berlin.mpg.de/general/MPIWGPerson"; + //String MPIWG_PERSON_ONTOLOGY_URL="http://ontologies.mpiwg-berlin.mpg.de/general/MPIWGPerson"; + String RDF_TYPE="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"; + String MAIL_URL=""; + String FIRST_NAME_URL="http://xmlns.com/foaf/0.1/givenName"; + String SAME_AS=""; + String LAST_NAME_URL="http://xmlns.com/foaf/0.1/familyName"; + String FULLNAME_URL="http://xmlns.com/foaf/0.1/name"; + String GRAPH="http://entities.mpiwg-berlin.mpg.de/graphs/MPIWGMembers.rdf"; + String TRIPLE_FMT_lit="<%s><%s>\"%s\" .\n"; + String TRIPLE_FMT_res="<%s><%s><%s>.\n"; + + private FileWriter outFileWriter; + + + public LdapToRDFExporter(String filename) throws IOException{ + outFileWriter = new FileWriter(filename); + + } + + public void close() throws IOException{ + outFileWriter.close(); + } + + /** + * Hole den vollen Benutzernamen aus dem LDAP + * @return + */ + public void getUserNamesFromLdap() { + + Hashtable<String,String> env = new Hashtable<String,String>(); + String sp = "com.sun.jndi.ldap.LdapCtxFactory"; + env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp); + + String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";//TODO should go into config file + env.put(javax.naming.Context.PROVIDER_URL, ldapUrl); + + DirContext dctx; + try { + dctx = new InitialDirContext(env); + } catch (NamingException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + return; + } + + String base = "ou=People"; + + SearchControls sc = new SearchControls(); + String[] attributeFilter = { "cn", "mail" }; + sc.setReturningAttributes(attributeFilter); + sc.setSearchScope(SearchControls.SUBTREE_SCOPE); + + //String filter = "(uid="+creator+")"; + String filter=null; + + try { + Attributes attrs2 = new BasicAttributes(); + attrs2.put("mail", null); + NamingEnumeration<SearchResult> results = dctx.search(base, attrs2); + + while (results.hasMore()) { + SearchResult sr = (SearchResult) results.next(); + javax.naming.directory.Attributes attrs = sr.getAttributes(); + + Attribute sn = attrs.get("sn"); + Attribute givenName = attrs.get("givenName"); + Attribute mail = attrs.get("mail"); + Attribute cn = attrs.get("cn"); + Attribute uid = attrs.get("uid"); + + String uid_string = (String)uid.get(); + if(uid_string.contains(" ")){ + System.err.println("WRONG UID IN LDAP:"+uid_string); + continue; + } + + String entity_iden=String.format(MPIWG_PERSON_BASE, uid.get()); + outFileWriter.write(String.format(TRIPLE_FMT_res,entity_iden,RDF_TYPE,MPIWG_PERSON_TYPE)); + outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,LAST_NAME_URL,sn.get())); + outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,FIRST_NAME_URL,givenName.get())); + outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,FULLNAME_URL,cn.get())); + //outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,MAIL_URL,mail.get())); + + + } + } catch (NamingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + dctx.close(); + } catch (NamingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return; + } + /** + * @param args + */ + public static void main(String[] args) { + try { + LdapToRDFExporter le = new LdapToRDFExporter("/tmp/ldp.n3t"); + le.getUserNamesFromLdap(); + le.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +}