Merge: doc: fixed some typos and other misc. corrections
[nit.git] / lib / android / http_request.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Android implementation of `app:http_request`
16 module http_request is
17 android_manifest """<uses-permission android:name="android.permission.INTERNET" />"""
18 end
19
20 intrude import app::http_request
21 import ui
22
23 in "Java" `{
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.impl.client.DefaultHttpClient;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.StatusLine;
29 import java.io.ByteArrayOutputStream;
30 `}
31
32 redef class App
33 redef fun run_on_ui_thread(task)
34 do
35 if app.activities.not_empty then
36 app.native_activity.run_on_ui_thread task
37 else
38 # There is no UI, it must be a service, run on the caller thread
39 task.main
40 end
41 end
42 end
43
44 redef class Text
45
46 redef fun http_get
47 do
48 jni_env.push_local_frame 8
49 var juri = self.to_java_string
50 var jrep = java_http_get(juri)
51
52 assert not jrep.is_java_null
53
54 var res
55 if jrep.is_exception then
56 jrep = jrep.as_exception
57
58 # If the top exception doesn't have a message, get it from its causes.
59 var msg = null
60 var cause: JavaThrowable = jrep
61 loop
62 var jmsg = cause.message
63 if not jmsg.is_java_null then
64 msg = jmsg.to_s
65 break
66 else
67 cause = cause.cause
68 if cause.is_java_null then break
69 end
70 end
71 if msg == null then msg = jrep.to_s
72
73 res = new HttpRequestResult(null, new IOError(msg))
74 else if jrep.is_http_response then
75 jrep = jrep.as_http_response
76 res = new HttpRequestResult(jrep.content.to_s, null, jrep.status_code)
77 else abort
78
79 jni_env.pop_local_frame
80 return res
81 end
82 end
83
84 redef class AsyncHttpRequest
85
86 redef fun main
87 do
88 var res = super
89 jvm.detach_current_thread
90 return res
91 end
92 end
93
94 redef class JavaObject
95 private fun is_exception: Bool in "Java" `{ return self instanceof Exception; `}
96 private fun as_exception: JavaException in "Java" `{ return (Exception)self; `}
97
98 private fun is_http_response: Bool in "Java" `{ return self instanceof HttpResponse; `}
99 private fun as_http_response: JavaHttpResponse in "Java" `{ return (HttpResponse)self; `}
100 end
101
102 private fun java_http_get(uri: JavaString): JavaObject in "Java" `{
103 try {
104 DefaultHttpClient client = new DefaultHttpClient();
105 HttpGet get = new HttpGet(uri);
106 return client.execute(get);
107 } catch (Exception ex) {
108 ex.printStackTrace();
109 return ex;
110 }
111 `}
112
113 private extern class JavaHttpResponse in "Java" `{ org.apache.http.HttpResponse `}
114 super JavaObject
115
116 fun status_code: Int in "Java" `{ return self.getStatusLine().getStatusCode(); `}
117
118 fun content: JavaString in "Java" `{
119 try {
120 ByteArrayOutputStream out = new ByteArrayOutputStream();
121 self.getEntity().writeTo(out);
122 out.close();
123 return out.toString();
124 } catch (Exception ex) {
125 ex.printStackTrace();
126 return "";
127 }
128 `}
129 end
130
131 # Force linearization of print
132 #
133 # TODO prioritize `android::log`
134 redef fun print(object) do super