1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/dew/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Wed Jan 23 13:18:46 2013 +0100
1.3 @@ -0,0 +1,131 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.dew;
1.22 +
1.23 +import java.io.ByteArrayInputStream;
1.24 +import java.io.IOException;
1.25 +import java.io.InputStream;
1.26 +import java.io.InputStreamReader;
1.27 +import java.io.OutputStream;
1.28 +import java.io.Writer;
1.29 +import java.util.List;
1.30 +import java.util.Locale;
1.31 +import java.util.Locale;
1.32 +import java.util.logging.Logger;
1.33 +import javax.tools.Diagnostic;
1.34 +import javax.tools.JavaFileObject;
1.35 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.36 +import org.glassfish.grizzly.http.Method;
1.37 +import org.glassfish.grizzly.http.server.HttpHandler;
1.38 +import org.glassfish.grizzly.http.server.Request;
1.39 +import org.glassfish.grizzly.http.server.Response;
1.40 +import org.glassfish.grizzly.http.util.HttpStatus;
1.41 +import org.json.JSONArray;
1.42 +import org.json.JSONObject;
1.43 +import org.json.JSONStringer;
1.44 +import org.json.JSONTokener;
1.45 +
1.46 +/**
1.47 + *
1.48 + * @author phrebejk
1.49 + */
1.50 +public class Dew extends HttpHandler implements Bck2Brwsr.Resources {
1.51 + private String html = "";
1.52 + private Compile data;
1.53 +
1.54 + @Override
1.55 + public void service(Request request, Response response) throws Exception {
1.56 +
1.57 + if ( request.getMethod() == Method.POST ) {
1.58 + InputStream is = request.getInputStream();
1.59 + JSONTokener tok = new JSONTokener(new InputStreamReader(is));
1.60 + JSONObject obj = new JSONObject(tok);
1.61 + String tmpHtml = obj.getString("html");
1.62 + String tmpJava = obj.getString("java");
1.63 +
1.64 + Compile res = Compile.create(tmpHtml, tmpJava);
1.65 + List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
1.66 + if (err.isEmpty()) {
1.67 + data = res;
1.68 + html = tmpHtml;
1.69 + response.getOutputStream().write("[]".getBytes());
1.70 + response.setStatus(HttpStatus.OK_200);
1.71 + } else {
1.72 +
1.73 + JSONArray errors = new JSONArray();
1.74 +
1.75 + for (Diagnostic<? extends JavaFileObject> d : err) {
1.76 + JSONObject e = new JSONObject();
1.77 + e.put("col", d.getColumnNumber());
1.78 + e.put("line", d.getLineNumber());
1.79 + e.put("kind", d.getKind().toString());
1.80 + e.put("msg", d.getMessage(Locale.ENGLISH));
1.81 + errors.put(e);
1.82 + }
1.83 +
1.84 + errors.write(response.getWriter());
1.85 + response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
1.86 + }
1.87 +
1.88 + return;
1.89 + }
1.90 +
1.91 + String r = request.getHttpHandlerPath();
1.92 + if (r == null || r.equals("/")) {
1.93 + r = "index.html";
1.94 + }
1.95 + if (r.equals("/result.html")) {
1.96 + response.setContentType("text/html");
1.97 + response.getOutputBuffer().write(html);
1.98 + response.setStatus(HttpStatus.OK_200);
1.99 + return;
1.100 + }
1.101 +
1.102 + if (r.startsWith("/")) {
1.103 + r = r.substring(1);
1.104 + }
1.105 +
1.106 + if (r.endsWith(".html") || r.endsWith(".xhtml")) {
1.107 + response.setContentType("text/html");
1.108 + }
1.109 + OutputStream os = response.getOutputStream();
1.110 + try (InputStream is = Dew.class.getResourceAsStream(r) ) {
1.111 + copyStream(is, os, request.getRequestURL().toString() );
1.112 + } catch (IOException ex) {
1.113 + response.setDetailMessage(ex.getLocalizedMessage());
1.114 + response.setError();
1.115 + response.setStatus(404);
1.116 + }
1.117 + }
1.118 +
1.119 + static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
1.120 + for (;;) {
1.121 + int ch = is.read();
1.122 + if (ch == -1) {
1.123 + break;
1.124 + }
1.125 + os.write(ch);
1.126 + }
1.127 + }
1.128 +
1.129 + @Override
1.130 + public InputStream get(String r) throws IOException {
1.131 + byte[] arr = data == null ? null : data.get(r);
1.132 + return arr == null ? null : new ByteArrayInputStream(arr);
1.133 + }
1.134 +}