diff src/de/mpiwg/itgroup/annotationManager/drupal/AnnotationHandler.java @ 5:0be9d53a6967

editor for annotations
author dwinter
date Tue, 13 Dec 2011 17:43:46 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/mpiwg/itgroup/annotationManager/drupal/AnnotationHandler.java	Tue Dec 13 17:43:46 2011 +0100
@@ -0,0 +1,190 @@
+package de.mpiwg.itgroup.annotationManager.drupal;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+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.apache.log4j.Logger;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+import org.restlet.Context;
+
+public class AnnotationHandler {
+
+	//
+	public  String drupalURL;
+	
+	private Logger logger = Logger.getRootLogger();
+
+	public String drupalRestURL;
+	
+	public AnnotationHandler(String drupalURL){
+		this.drupalURL=drupalURL;
+		drupalRestURL = drupalURL+"/rest";	
+		
+	};
+	
+	public JSONObject createAnnotation(String title,String annot,String user, String password) throws UnknowUserException{	
+	  	DefaultHttpClient httpclient = new DefaultHttpClient();
+	    JSONObject uo;
+		try {
+			uo = new JSONObject();
+			uo.put("username", user);
+			uo.put("password", password);		
+		} catch (JSONException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return null;
+		}
+		
+	    HttpEntity ent = postJSON(httpclient, uo, "/user/login");
+	    
+	  
+		   
+	    try {
+	    	String retString =convertStreamToString(ent.getContent());
+			if (retString==null){
+				throw new UnknowUserException();
+			}
+				
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	    JSONObject annotObject=null;
+	    
+	    try {
+			annotObject = new JSONObject();
+			annotObject.put("type", "page");
+			
+			
+			JSONObject body = new JSONObject();
+			JSONObject content = new JSONObject();
+			
+			content.put("value", annot);
+			content.put("format", "full_html");
+			//content.put("safe_value", "<p>HELLO</p>");
+			
+			JSONObject zw = new JSONObject();
+			zw.put("0", content);
+			
+			body.put("und", zw);
+			body.put("name", user);
+			annotObject.put("body", body);
+			annotObject.put("title", title);
+			
+		} catch (JSONException e) {
+			// TODO: handle exception
+		}
+		
+		ent =postJSON(httpclient,annotObject,"/node");
+		
+		try {
+			String retString =convertStreamToString(ent.getContent());
+			JSONObject retOB= new JSONObject(retString);
+			
+			
+			retOB.put("node_uri",drupalURL+"/node/"+retOB.get("nid"));
+			return retOB;
+		} catch (IllegalStateException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		} catch (JSONException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	    
+		return null;
+	}
+
+	protected HttpEntity postJSON(DefaultHttpClient httpclient, JSONObject uo,String command) {
+		HttpPost httppost = new HttpPost(drupalRestURL+command);
+	    System.out.println("executing request" + httppost.getRequestLine());    
+	
+	    BasicHttpEntity entity= new BasicHttpEntity();  
+	    try {
+			entity.setContent(convertStringToStream(uo.toString()));
+		} catch (UnsupportedEncodingException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	  
+	    httppost.setEntity(entity);
+	    entity.setContentType(new BasicHeader("Content-Type","application/json"));
+	    
+	    HttpResponse response;
+		try {
+			response = httpclient.execute(httppost);
+		} catch (ClientProtocolException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return null;
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			return null;
+		}
+	    HttpEntity resEntity = response.getEntity();
+	    
+	    return resEntity;
+	  
+	}
+
+    /** converts a stream to a string
+     * @param string
+     * @return
+     * @throws UnsupportedEncodingException
+     */
+    public static InputStream convertStringToStream(String string) throws UnsupportedEncodingException{
+    	return new ByteArrayInputStream(string.getBytes("utf-8"));
+    }
+    
+    /**
+     * 
+         * To convert the InputStream to String we use the BufferedReader.readLine()
+         * method. We iterate until the BufferedReader return null which means
+         * there's no more data to read. Each line will appended to a StringBuilder
+         * and returned as String.
+         
+     * @param is
+     * @return
+     */
+    public static String convertStreamToString(InputStream is) {
+        
+        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+        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();
+    }
+
+}