annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
1 package de.mpiwg.itgroup.annotationManager.drupal;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
2
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
3 import java.io.BufferedReader;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
4 import java.io.ByteArrayInputStream;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
5 import java.io.IOException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
6 import java.io.InputStream;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
7 import java.io.InputStreamReader;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
8 import java.io.UnsupportedEncodingException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
9
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
10 import org.apache.http.Header;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
11 import org.apache.http.HttpEntity;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
12 import org.apache.http.HttpResponse;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
13 import org.apache.http.client.ClientProtocolException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
14 import org.apache.http.client.methods.HttpPost;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
15 import org.apache.http.entity.BasicHttpEntity;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
16 import org.apache.http.impl.client.DefaultHttpClient;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
17 import org.apache.http.message.BasicHeader;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
18 import org.apache.log4j.Logger;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
19 import org.json.JSONException;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
20 import org.json.JSONObject;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
21 import org.json.JSONTokener;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
22 import org.restlet.Context;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
23
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
24 public class AnnotationHandler {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
25
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
26 //
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
27 public String drupalURL;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
28
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
29 private Logger logger = Logger.getRootLogger();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
30
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
31 public String drupalRestURL;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
32
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
33 public AnnotationHandler(String drupalURL){
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
34 this.drupalURL=drupalURL;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
35 drupalRestURL = drupalURL+"/rest";
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
36
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
37 };
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
38
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
39 public JSONObject createAnnotation(String title,String annot,String user, String password) throws UnknowUserException{
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
40 DefaultHttpClient httpclient = new DefaultHttpClient();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
41 JSONObject uo;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
42 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
43 uo = new JSONObject();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
44 uo.put("username", user);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
45 uo.put("password", password);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
46 } catch (JSONException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
47 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
48 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
49 return null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
50 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
51
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
52 HttpEntity ent = postJSON(httpclient, uo, "/user/login");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
53
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
54
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
55
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
56 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
57 String retString =convertStreamToString(ent.getContent());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
58 if (retString==null){
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
59 throw new UnknowUserException();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
60 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
61
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
62 } catch (IOException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
63 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
64 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
65 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
66 JSONObject annotObject=null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
67
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
68 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
69 annotObject = new JSONObject();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
70 annotObject.put("type", "page");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
71
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
72
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
73 JSONObject body = new JSONObject();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
74 JSONObject content = new JSONObject();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
75
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
76 content.put("value", annot);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
77 content.put("format", "full_html");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
78 //content.put("safe_value", "<p>HELLO</p>");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
79
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
80 JSONObject zw = new JSONObject();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
81 zw.put("0", content);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
82
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
83 body.put("und", zw);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
84 body.put("name", user);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
85 annotObject.put("body", body);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
86 annotObject.put("title", title);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
87
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
88 } catch (JSONException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
89 // TODO: handle exception
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
90 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
91
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
92 ent =postJSON(httpclient,annotObject,"/node");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
93
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
94 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
95 String retString =convertStreamToString(ent.getContent());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
96 JSONObject retOB= new JSONObject(retString);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
97
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
98
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
99 retOB.put("node_uri",drupalURL+"/node/"+retOB.get("nid"));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
100 return retOB;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
101 } catch (IllegalStateException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
102 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
103 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
104 } catch (IOException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
105 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
106 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
107 } catch (JSONException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
108 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
109 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
110 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
111
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
112 return null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
113 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
114
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
115 protected HttpEntity postJSON(DefaultHttpClient httpclient, JSONObject uo,String command) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
116 HttpPost httppost = new HttpPost(drupalRestURL+command);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
117 System.out.println("executing request" + httppost.getRequestLine());
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
118
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
119 BasicHttpEntity entity= new BasicHttpEntity();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
120 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
121 entity.setContent(convertStringToStream(uo.toString()));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
122 } catch (UnsupportedEncodingException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
123 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
124 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
125 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
126
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
127 httppost.setEntity(entity);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
128 entity.setContentType(new BasicHeader("Content-Type","application/json"));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
129
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
130 HttpResponse response;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
131 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
132 response = httpclient.execute(httppost);
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
133 } catch (ClientProtocolException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
134 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
135 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
136 return null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
137 } catch (IOException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
138 // TODO Auto-generated catch block
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
139 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
140 return null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
141 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
142 HttpEntity resEntity = response.getEntity();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
143
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
144 return resEntity;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
145
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
146 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
147
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
148 /** converts a stream to a string
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
149 * @param string
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
150 * @return
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
151 * @throws UnsupportedEncodingException
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
152 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
153 public static InputStream convertStringToStream(String string) throws UnsupportedEncodingException{
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
154 return new ByteArrayInputStream(string.getBytes("utf-8"));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
155 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
156
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
157 /**
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
158 *
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
159 * To convert the InputStream to String we use the BufferedReader.readLine()
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
160 * method. We iterate until the BufferedReader return null which means
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
161 * there's no more data to read. Each line will appended to a StringBuilder
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
162 * and returned as String.
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
163
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
164 * @param is
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
165 * @return
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
166 */
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
167 public static String convertStreamToString(InputStream is) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
168
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
169 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
170 StringBuilder sb = new StringBuilder();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
171
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
172 String line = null;
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
173 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
174 while ((line = reader.readLine()) != null) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
175 sb.append(line + "\n");
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
176 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
177 } catch (IOException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
178 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
179 } finally {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
180 try {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
181 is.close();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
182 } catch (IOException e) {
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
183 e.printStackTrace();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
184 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
185 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
186
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
187 return sb.toString();
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
188 }
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
189
0be9d53a6967 editor for annotations
dwinter
parents:
diff changeset
190 }