2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.dew;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.OutputStream;
24 import java.io.Writer;
25 import java.util.List;
26 import java.util.logging.Logger;
27 import javax.tools.Diagnostic;
28 import javax.tools.JavaFileObject;
29 import org.glassfish.grizzly.http.Method;
30 import org.glassfish.grizzly.http.server.HttpHandler;
31 import org.glassfish.grizzly.http.server.Request;
32 import org.glassfish.grizzly.http.server.Response;
33 import org.glassfish.grizzly.http.util.HttpStatus;
34 import org.json.JSONObject;
35 import org.json.JSONTokener;
41 public class Dew extends HttpHandler {
42 private String html = "";
46 public void service(Request request, Response response) throws Exception {
48 if ( request.getMethod() == Method.POST ) {
49 InputStream is = request.getInputStream();
50 JSONTokener tok = new JSONTokener(new InputStreamReader(is));
51 JSONObject obj = new JSONObject(tok);
52 String tmpHtml = obj.getString("html");
53 String tmpJava = obj.getString("java");
55 Compile res = Compile.create(tmpHtml, tmpJava);
56 List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
60 response.getOutputStream().write("[]".getBytes());
61 response.setStatus(HttpStatus.OK_200);
63 response.getOutputStream().write(("[errors:'" + err + "']").getBytes());
64 response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
70 String r = request.getHttpHandlerPath();
71 if (r == null || r.equals("/")) {
74 if (r.equals("/result.html")) {
75 response.setContentType("text/html");
76 response.getOutputBuffer().write(html);
77 response.setStatus(HttpStatus.OK_200);
81 if (r.startsWith("/")) {
84 if (r.startsWith("classes/")) {
86 byte[] arr = data == null ? null : data.get(r);
89 response.setDetailMessage("No data for " + r + " yet!");
92 try (Writer w = response.getWriter()) {
93 response.setContentType("text/javascript");
95 for (int i = 0; i < arr.length; i++) {
109 w.append(Integer.toString(b));
116 if (r.endsWith(".html") || r.endsWith(".xhtml")) {
117 response.setContentType("text/html");
119 OutputStream os = response.getOutputStream();
120 try (InputStream is = Dew.class.getResourceAsStream(r) ) {
121 copyStream(is, os, request.getRequestURL().toString() );
122 } catch (IOException ex) {
123 response.setDetailMessage(ex.getLocalizedMessage());
125 response.setStatus(404);
128 private static final Logger LOG = Logger.getLogger(Dew.class.getName());
130 static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {