changeset 8:5dba58db2f55

cone
author dwinter
date Mon, 02 Jan 2012 18:13:35 +0100
parents e0aca5ffc4df
children 866503140b34
files .classpath src/de/mpiwg/itgroup/nimanager/cone/ConeEmailMatcher.java src/de/mpiwg/itgroup/nimanager/cone/Mail2ConeService.java
diffstat 3 files changed, 292 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.classpath	Mon Jan 02 12:43:49 2012 +0100
+++ b/.classpath	Mon Jan 02 18:13:35 2012 +0100
@@ -20,6 +20,10 @@
 	<classpathentry exported="true" kind="lib" path="libs/virtjdbc3.jar"/>
 	<classpathentry exported="true" kind="lib" path="libs/virtjdbc3ssl.jar"/>
 	<classpathentry kind="lib" path="libs/commons-lang-2.3.jar"/>
+	<classpathentry kind="lib" path="libs/httpcomponents-client-4.0-beta1/lib/httpclient-4.0-beta1.jar"/>
+	<classpathentry kind="lib" path="libs/httpcomponents-client-4.0-beta1/lib/httpmime-4.0-beta1.jar"/>
+	<classpathentry kind="lib" path="libs/httpcomponents-core-4.0-beta3/lib/httpcore-4.0-beta3.jar"/>
+	<classpathentry kind="lib" path="libs/httpcomponents-core-4.0-beta3/lib/httpcore-nio-4.0-beta3.jar"/>
 	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/protege"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/protege-core"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/mpiwg/itgroup/nimanager/cone/ConeEmailMatcher.java	Mon Jan 02 18:13:35 2012 +0100
@@ -0,0 +1,248 @@
+package de.mpiwg.itgroup.nimanager.cone;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.text.Normalizer;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+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;
+
+
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.BasicHttpEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicHeader;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+import org.restlet.resource.Get;
+
+public class ConeEmailMatcher {
+
+	public Map<String,String> name2mail = new HashMap<String,String>();
+	public Map<String,String> mail2name = new HashMap<String,String>();
+	public Map<String,String> cone2name = new HashMap<String,String>();
+	public Map<String,String> name2Cone = new HashMap<String,String>();
+	public Map<String,String>  mail2cone = new HashMap<String,String>();
+	
+	public static String CONEURL="http://pubman.mpiwg-berlin.mpg.de/cone/persons/all?format=json";
+
+	/**
+	 * Hole den vollen Benutzernamen aus dem LDAP
+	 * @param creator
+	 * @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");
+				
+				//retString+=String.format("%s; %s; %s\n", sn,givenName,mail);
+				
+				mail2name.put(mail.get().toString(), sn.get().toString()+", "+givenName.get().toString());
+				String name = sn.get().toString()+", "+givenName.get().toString();
+				name2mail.put(name,mail.get().toString());
+			}
+		} catch (NamingException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+		try {
+			dctx.close();
+		} catch (NamingException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		return;
+	}
+	
+	public void getUserNamesFromCone(){
+		HttpGet httpget = new HttpGet(CONEURL);
+	  	DefaultHttpClient httpclient = new DefaultHttpClient();
+	 
+	    BasicHttpEntity entity= new BasicHttpEntity();  
+	    
+	    httpget.setHeader(new BasicHeader("Content-Type","application/json"));
+	  
+	    HttpResponse response;
+		try {
+			response = httpclient.execute(httpget);
+		} catch (ClientProtocolException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return;
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return;
+		}
+	    HttpEntity resEntity = response.getEntity();
+	    
+	    try {
+	    	
+	    	
+			JSONArray ja = new JSONArray(convertStreamToString(resEntity.getContent()));
+		
+			for (int i=0; i<ja.length();i++){
+				JSONObject jo = ja.getJSONObject(i);
+				cone2name.put((String)jo.get("id"), (String)jo.get("value"));
+				name2Cone.put(asciiName((String)jo.get("value")), (String)jo.get("id"));
+			}
+		} catch (IllegalStateException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (JSONException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	  
+	}
+	
+	void createMail2Cone() {
+		for(String key: mail2name.keySet()){
+			String coneID = name2Cone.get(mail2name.get(key));
+			if (coneID==null){
+				String name=mail2name.get(key);
+				String newName=name2Cone.get(name);
+				coneID = name2Cone.get(newName);
+			} 
+			mail2cone.put(key,coneID);
+			}
+		}
+		
+	private String asciiName(String string) {
+		HashMap<Character,String> uml = new HashMap<Character, String>();
+		uml.put('ä', "ae");
+		uml.put('ö', "oe");
+		uml.put('ü', "ue");
+		uml.put('ß', "ss");
+		uml.put('Ä', "Ae");
+		uml.put('Ö', "Oe");
+		uml.put('Ü', "Ue");
+		String newString="";
+		for(Character x: string.toCharArray()){
+			String repl;
+			if(uml.containsKey(x)){
+				repl=uml.get(x);
+			} else {
+				repl=String.valueOf(x);
+			}
+			newString=newString+repl;
+		}
+		string=newString;
+		
+		
+		string = Normalizer.normalize(string, Normalizer.Form.NFD);
+		string=string.replaceAll("[^\\p{ASCII}]", "");
+		
+		return string;
+	}
+
+	
+	
+	   public static String convertStreamToString(InputStream is) {
+	        
+	        BufferedReader reader;
+			try {
+				reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
+			} catch (UnsupportedEncodingException e1) {
+				// TODO Auto-generated catch block
+				e1.printStackTrace();
+				return null;
+			}
+	        StringBuilder sb = new StringBuilder();
+	 
+	        String line = null;
+	        try {
+	            while ((line = reader.readLine()) != null) {
+	                sb.append(line + "\n");
+	            }
+	        } catch (IOException e) {
+	            e.printStackTrace();
+	        } finally {
+	            try {
+	                is.close();
+	            } catch (IOException e) {
+	                e.printStackTrace();
+	            }
+	        }
+	 
+	        return sb.toString();
+	    }
+
+	public static void main(String args[]){
+		ConeEmailMatcher cm = new ConeEmailMatcher();
+		cm.getUserNamesFromLdap();
+		//System.out.println(cm.mail2name);
+		cm.getUserNamesFromCone();
+		//System.out.println(cm.name2Cone);
+		cm.createMail2Cone();
+		
+		for(String key: cm.mail2cone.keySet()){
+		 System.out.println(key+","+cm.mail2cone.get(key)+"\n");
+		}
+		
+		System.out.println(cm.mail2cone.get("renn@mpiwg-berlin.mpg.de"));
+	}
+
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/mpiwg/itgroup/nimanager/cone/Mail2ConeService.java	Mon Jan 02 18:13:35 2012 +0100
@@ -0,0 +1,40 @@
+package de.mpiwg.itgroup.nimanager.cone;
+
+import org.apache.log4j.Logger;
+import org.restlet.data.Status;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
+import org.restlet.resource.Get;
+import org.restlet.resource.ServerResource;
+
+public class Mail2ConeService extends ServerResource{
+
+	private ConeEmailMatcher cm;
+	private Logger rl = Logger.getRootLogger();
+	
+	public Mail2ConeService(){
+		cm = new ConeEmailMatcher();
+		cm.getUserNamesFromLdap();
+		cm.getUserNamesFromCone();
+		cm.createMail2Cone();
+		
+	}
+	
+	@Get
+	public Representation getMethod(Representation entity){
+		String mail = (String)getRequest().getAttributes().get("personId");
+		
+		if (!mail.contains("@")){
+			mail=mail+"@mpiwg-berlin.mpg.de";
+		}
+		
+		
+		if (cm.mail2cone.containsKey(mail)){
+			String retString=cm.mail2cone.get(mail);
+			return new StringRepresentation(retString);
+		}  else {
+			setStatus(Status.CLIENT_ERROR_NOT_FOUND);
+			return null;
+		}
+	}
+}