-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpinHttpImpl.java
77 lines (70 loc) · 2.62 KB
/
SpinHttpImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package wit_spin_http;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import wit_wasi_outbound_http.WasiOutboundHttp;
import wit_wasi_outbound_http.WasiOutboundHttp.HttpError;
import wit_wasi_outbound_http.WasiOutboundHttp.Method;
import wit_wasi_outbound_http.WasiOutboundHttp.Request;
import wit_wasi_outbound_http.WasiOutboundHttp.Response;
import wit_wasi_outbound_http.WasiOutboundHttp.Result;
public class SpinHttpImpl {
/**
* Interpret the word after the last '/' in the request URI as an English
* word and respond with any Spanish translations found for that word.
*
* If no word is specified, respond with text retrieved from the URL
* specified by the "URL" environment variable.
*/
public static SpinHttp.Response handleHttpRequest(SpinHttp.Request request) {
int status = 404;
String body = "Disculpe, no entiendo.\n";
String contentType = "text/plain;charset=UTF-8";
String query = request.uri.substring(request.uri.lastIndexOf('/') + 1);
if (query.isEmpty()) {
String url = System.getenv("URL");
Result<Response, HttpError> result =
WasiOutboundHttp.request(
new Request(Method.GET, url, new ArrayList<>(), new ArrayList<>(), null));
if (result.tag == Result.OK) {
status = 200;
Response response = result.getOk();
if (response.status == (short) 200) {
body = "Por tu consideración, un poema:\n\n" + new String(response.body, UTF_8) + "\n";
} else {
body = "Una respuesta inesperada: " + url + ": " + response.status + "\n";
}
} else {
status = 500;
body = "Problema con " + url + ": " + result.getErr() + "\n";
}
} else {
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(new FileInputStream("/en-es.csv"), UTF_8)); ) {
String line;
while ((line = reader.readLine()) != null) {
int comma = line.indexOf(",");
if (comma > 0 && line.substring(0, comma).equals(query)) {
status = 200;
body = line.substring(comma + 1) + "\n";
break;
}
}
} catch (IOException e) {
status = 500;
body = "No puedo leer el archivo: " + e + "\n";
}
}
return new SpinHttp.Response(
(short) status,
new ArrayList<>() {
{
add(new SpinHttp.Tuple2<>("content-type", contentType));
}
},
body.getBytes(UTF_8));
}
}