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