9
|
1 package de.mpiwg.itgroup.nimanager.cone;
|
|
2
|
|
3 import java.io.FileWriter;
|
|
4 import java.io.IOException;
|
|
5 import java.util.Hashtable;
|
|
6
|
|
7 import javax.naming.NamingEnumeration;
|
|
8 import javax.naming.NamingException;
|
|
9 import javax.naming.directory.Attribute;
|
|
10 import javax.naming.directory.Attributes;
|
|
11 import javax.naming.directory.BasicAttributes;
|
|
12 import javax.naming.directory.DirContext;
|
|
13 import javax.naming.directory.InitialDirContext;
|
|
14 import javax.naming.directory.SearchControls;
|
|
15 import javax.naming.directory.SearchResult;
|
|
16
|
|
17 public class LdapToRDFExporter {
|
|
18
|
|
19 String MPIWG_PERSON_BASE="http://entities.mpiwg-berlin.mpg.de/persons/mpiwg:%s";
|
|
20 String MPIWG_PERSON_TYPE="http://ontologies.mpiwg-berlin.mpg.de/general/MPIWGPerson";
|
|
21 //String MPIWG_PERSON_ONTOLOGY_URL="http://ontologies.mpiwg-berlin.mpg.de/general/MPIWGPerson";
|
|
22 String RDF_TYPE="http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
|
|
23 String MAIL_URL="";
|
|
24 String FIRST_NAME_URL="http://xmlns.com/foaf/0.1/givenName";
|
|
25 String SAME_AS="";
|
|
26 String LAST_NAME_URL="http://xmlns.com/foaf/0.1/familyName";
|
|
27 String FULLNAME_URL="http://xmlns.com/foaf/0.1/name";
|
|
28 String GRAPH="http://entities.mpiwg-berlin.mpg.de/graphs/MPIWGMembers.rdf";
|
|
29 String TRIPLE_FMT_lit="<%s><%s>\"%s\" .\n";
|
|
30 String TRIPLE_FMT_res="<%s><%s><%s>.\n";
|
|
31
|
|
32 private FileWriter outFileWriter;
|
|
33
|
|
34
|
|
35 public LdapToRDFExporter(String filename) throws IOException{
|
|
36 outFileWriter = new FileWriter(filename);
|
|
37
|
|
38 }
|
|
39
|
|
40 public void close() throws IOException{
|
|
41 outFileWriter.close();
|
|
42 }
|
|
43
|
|
44 /**
|
|
45 * Hole den vollen Benutzernamen aus dem LDAP
|
|
46 * @return
|
|
47 */
|
|
48 public void getUserNamesFromLdap() {
|
|
49
|
|
50 Hashtable<String,String> env = new Hashtable<String,String>();
|
|
51 String sp = "com.sun.jndi.ldap.LdapCtxFactory";
|
|
52 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, sp);
|
|
53
|
|
54 String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";//TODO should go into config file
|
|
55 env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
|
|
56
|
|
57 DirContext dctx;
|
|
58 try {
|
|
59 dctx = new InitialDirContext(env);
|
|
60 } catch (NamingException e1) {
|
|
61 // TODO Auto-generated catch block
|
|
62 e1.printStackTrace();
|
|
63 return;
|
|
64 }
|
|
65
|
|
66 String base = "ou=People";
|
|
67
|
|
68 SearchControls sc = new SearchControls();
|
|
69 String[] attributeFilter = { "cn", "mail" };
|
|
70 sc.setReturningAttributes(attributeFilter);
|
|
71 sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
|
|
72
|
|
73 //String filter = "(uid="+creator+")";
|
|
74 String filter=null;
|
|
75
|
|
76 try {
|
|
77 Attributes attrs2 = new BasicAttributes();
|
|
78 attrs2.put("mail", null);
|
|
79 NamingEnumeration<SearchResult> results = dctx.search(base, attrs2);
|
|
80
|
|
81 while (results.hasMore()) {
|
|
82 SearchResult sr = (SearchResult) results.next();
|
|
83 javax.naming.directory.Attributes attrs = sr.getAttributes();
|
|
84
|
|
85 Attribute sn = attrs.get("sn");
|
|
86 Attribute givenName = attrs.get("givenName");
|
|
87 Attribute mail = attrs.get("mail");
|
|
88 Attribute cn = attrs.get("cn");
|
|
89 Attribute uid = attrs.get("uid");
|
|
90
|
|
91 String uid_string = (String)uid.get();
|
|
92 if(uid_string.contains(" ")){
|
|
93 System.err.println("WRONG UID IN LDAP:"+uid_string);
|
|
94 continue;
|
|
95 }
|
|
96
|
|
97 String entity_iden=String.format(MPIWG_PERSON_BASE, uid.get());
|
|
98 outFileWriter.write(String.format(TRIPLE_FMT_res,entity_iden,RDF_TYPE,MPIWG_PERSON_TYPE));
|
|
99 outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,LAST_NAME_URL,sn.get()));
|
|
100 outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,FIRST_NAME_URL,givenName.get()));
|
|
101 outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,FULLNAME_URL,cn.get()));
|
|
102 //outFileWriter.write(String.format(TRIPLE_FMT_lit,entity_iden,MAIL_URL,mail.get()));
|
|
103
|
|
104
|
|
105 }
|
|
106 } catch (NamingException e) {
|
|
107 // TODO Auto-generated catch block
|
|
108 e.printStackTrace();
|
|
109 } catch (IOException e) {
|
|
110 // TODO Auto-generated catch block
|
|
111 e.printStackTrace();
|
|
112 }
|
|
113
|
|
114 try {
|
|
115 dctx.close();
|
|
116 } catch (NamingException e) {
|
|
117 // TODO Auto-generated catch block
|
|
118 e.printStackTrace();
|
|
119 }
|
|
120 return;
|
|
121 }
|
|
122 /**
|
|
123 * @param args
|
|
124 */
|
|
125 public static void main(String[] args) {
|
|
126 try {
|
|
127 LdapToRDFExporter le = new LdapToRDFExporter("/tmp/ldp.n3t");
|
|
128 le.getUserNamesFromLdap();
|
|
129 le.close();
|
|
130 } catch (IOException e) {
|
|
131 // TODO Auto-generated catch block
|
|
132 e.printStackTrace();
|
|
133 }
|
|
134
|
|
135 }
|
|
136
|
|
137 }
|