jaroslav@468
|
1 |
/**
|
jaroslav@468
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@468
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@468
|
4 |
*
|
jaroslav@468
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@468
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@468
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@468
|
8 |
*
|
jaroslav@468
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@468
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@468
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@468
|
12 |
* GNU General Public License for more details.
|
jaroslav@468
|
13 |
*
|
jaroslav@468
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@468
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@468
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
phrebejk@460
|
17 |
*/
|
phrebejk@460
|
18 |
package org.apidesign.bck2brwsr.dew;
|
phrebejk@460
|
19 |
|
jaroslav@471
|
20 |
import java.io.ByteArrayInputStream;
|
phrebejk@460
|
21 |
import java.io.IOException;
|
phrebejk@460
|
22 |
import java.io.InputStream;
|
phrebejk@461
|
23 |
import java.io.InputStreamReader;
|
phrebejk@460
|
24 |
import java.io.OutputStream;
|
jaroslav@466
|
25 |
import java.io.Writer;
|
jaroslav@465
|
26 |
import java.util.List;
|
phrebejk@541
|
27 |
import java.util.Locale;
|
phrebejk@541
|
28 |
import java.util.Locale;
|
phrebejk@461
|
29 |
import java.util.logging.Logger;
|
jaroslav@465
|
30 |
import javax.tools.Diagnostic;
|
jaroslav@465
|
31 |
import javax.tools.JavaFileObject;
|
jaroslav@471
|
32 |
import org.apidesign.vm4brwsr.Bck2Brwsr;
|
phrebejk@461
|
33 |
import org.glassfish.grizzly.http.Method;
|
phrebejk@460
|
34 |
import org.glassfish.grizzly.http.server.HttpHandler;
|
phrebejk@460
|
35 |
import org.glassfish.grizzly.http.server.Request;
|
phrebejk@460
|
36 |
import org.glassfish.grizzly.http.server.Response;
|
phrebejk@461
|
37 |
import org.glassfish.grizzly.http.util.HttpStatus;
|
phrebejk@541
|
38 |
import org.json.JSONArray;
|
phrebejk@461
|
39 |
import org.json.JSONObject;
|
phrebejk@541
|
40 |
import org.json.JSONStringer;
|
phrebejk@461
|
41 |
import org.json.JSONTokener;
|
phrebejk@460
|
42 |
|
phrebejk@460
|
43 |
/**
|
phrebejk@460
|
44 |
*
|
phrebejk@460
|
45 |
* @author phrebejk
|
phrebejk@460
|
46 |
*/
|
jaroslav@471
|
47 |
public class Dew extends HttpHandler implements Bck2Brwsr.Resources {
|
jaroslav@466
|
48 |
private String html = "";
|
jaroslav@465
|
49 |
private Compile data;
|
phrebejk@460
|
50 |
|
phrebejk@460
|
51 |
@Override
|
phrebejk@460
|
52 |
public void service(Request request, Response response) throws Exception {
|
phrebejk@461
|
53 |
|
phrebejk@461
|
54 |
if ( request.getMethod() == Method.POST ) {
|
phrebejk@461
|
55 |
InputStream is = request.getInputStream();
|
phrebejk@461
|
56 |
JSONTokener tok = new JSONTokener(new InputStreamReader(is));
|
phrebejk@461
|
57 |
JSONObject obj = new JSONObject(tok);
|
jaroslav@466
|
58 |
String tmpHtml = obj.getString("html");
|
jaroslav@466
|
59 |
String tmpJava = obj.getString("java");
|
phrebejk@461
|
60 |
|
jaroslav@466
|
61 |
Compile res = Compile.create(tmpHtml, tmpJava);
|
jaroslav@465
|
62 |
List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
|
jaroslav@465
|
63 |
if (err.isEmpty()) {
|
jaroslav@465
|
64 |
data = res;
|
jaroslav@466
|
65 |
html = tmpHtml;
|
jaroslav@465
|
66 |
response.getOutputStream().write("[]".getBytes());
|
jaroslav@465
|
67 |
response.setStatus(HttpStatus.OK_200);
|
jaroslav@465
|
68 |
} else {
|
phrebejk@541
|
69 |
|
phrebejk@541
|
70 |
JSONArray errors = new JSONArray();
|
phrebejk@541
|
71 |
|
phrebejk@541
|
72 |
for (Diagnostic<? extends JavaFileObject> d : err) {
|
phrebejk@541
|
73 |
JSONObject e = new JSONObject();
|
phrebejk@541
|
74 |
e.put("col", d.getColumnNumber());
|
phrebejk@541
|
75 |
e.put("line", d.getLineNumber());
|
phrebejk@541
|
76 |
e.put("kind", d.getKind().toString());
|
phrebejk@541
|
77 |
e.put("msg", d.getMessage(Locale.ENGLISH));
|
phrebejk@541
|
78 |
errors.put(e);
|
phrebejk@541
|
79 |
}
|
phrebejk@541
|
80 |
|
phrebejk@541
|
81 |
errors.write(response.getWriter());
|
jaroslav@465
|
82 |
response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
|
jaroslav@465
|
83 |
}
|
phrebejk@461
|
84 |
|
phrebejk@461
|
85 |
return;
|
phrebejk@461
|
86 |
}
|
phrebejk@461
|
87 |
|
phrebejk@460
|
88 |
String r = request.getHttpHandlerPath();
|
phrebejk@460
|
89 |
if (r == null || r.equals("/")) {
|
phrebejk@460
|
90 |
r = "index.html";
|
phrebejk@460
|
91 |
}
|
phrebejk@461
|
92 |
if (r.equals("/result.html")) {
|
phrebejk@461
|
93 |
response.setContentType("text/html");
|
phrebejk@461
|
94 |
response.getOutputBuffer().write(html);
|
phrebejk@461
|
95 |
response.setStatus(HttpStatus.OK_200);
|
phrebejk@461
|
96 |
return;
|
phrebejk@461
|
97 |
}
|
phrebejk@461
|
98 |
|
phrebejk@460
|
99 |
if (r.startsWith("/")) {
|
phrebejk@460
|
100 |
r = r.substring(1);
|
phrebejk@460
|
101 |
}
|
jaroslav@465
|
102 |
|
phrebejk@460
|
103 |
if (r.endsWith(".html") || r.endsWith(".xhtml")) {
|
phrebejk@460
|
104 |
response.setContentType("text/html");
|
phrebejk@460
|
105 |
}
|
phrebejk@460
|
106 |
OutputStream os = response.getOutputStream();
|
phrebejk@460
|
107 |
try (InputStream is = Dew.class.getResourceAsStream(r) ) {
|
phrebejk@460
|
108 |
copyStream(is, os, request.getRequestURL().toString() );
|
phrebejk@460
|
109 |
} catch (IOException ex) {
|
phrebejk@460
|
110 |
response.setDetailMessage(ex.getLocalizedMessage());
|
phrebejk@460
|
111 |
response.setError();
|
phrebejk@460
|
112 |
response.setStatus(404);
|
phrebejk@460
|
113 |
}
|
phrebejk@460
|
114 |
}
|
phrebejk@460
|
115 |
|
phrebejk@460
|
116 |
static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
|
phrebejk@460
|
117 |
for (;;) {
|
phrebejk@460
|
118 |
int ch = is.read();
|
phrebejk@460
|
119 |
if (ch == -1) {
|
phrebejk@460
|
120 |
break;
|
phrebejk@460
|
121 |
}
|
phrebejk@460
|
122 |
os.write(ch);
|
phrebejk@460
|
123 |
}
|
phrebejk@460
|
124 |
}
|
jaroslav@471
|
125 |
|
jaroslav@471
|
126 |
@Override
|
jaroslav@471
|
127 |
public InputStream get(String r) throws IOException {
|
jaroslav@471
|
128 |
byte[] arr = data == null ? null : data.get(r);
|
jaroslav@471
|
129 |
return arr == null ? null : new ByteArrayInputStream(arr);
|
jaroslav@471
|
130 |
}
|
phrebejk@460
|
131 |
}
|