1.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Sat Oct 12 09:05:08 2013 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,138 +0,0 @@
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.util.List;
1.29 -import java.util.Locale;
1.30 -import javax.tools.Diagnostic;
1.31 -import javax.tools.JavaFileObject;
1.32 -import org.apidesign.vm4brwsr.Bck2Brwsr;
1.33 -import org.glassfish.grizzly.http.Method;
1.34 -import org.glassfish.grizzly.http.server.HttpHandler;
1.35 -import org.glassfish.grizzly.http.server.HttpServer;
1.36 -import org.glassfish.grizzly.http.server.Request;
1.37 -import org.glassfish.grizzly.http.server.Response;
1.38 -import org.glassfish.grizzly.http.util.HttpStatus;
1.39 -import org.json.JSONArray;
1.40 -import org.json.JSONObject;
1.41 -import org.json.JSONTokener;
1.42 -
1.43 -/**
1.44 - *
1.45 - * @author phrebejk
1.46 - */
1.47 -final class Dew extends HttpHandler implements Bck2Brwsr.Resources {
1.48 - private Compile data;
1.49 -
1.50 - public static void main(String... args) throws Exception {
1.51 - DewLauncher l = new DewLauncher(null);
1.52 - l.addClassLoader(DewLauncher.class.getClassLoader());
1.53 - final Dew dew = new Dew();
1.54 - HttpServer s = l.initServer(dew);
1.55 - s.getServerConfiguration().addHttpHandler(dew, "/dew/");
1.56 - l.launchServerAndBrwsr(s, "/dew/");
1.57 - System.in.read();
1.58 - }
1.59 -
1.60 - @Override
1.61 - public void service(Request request, Response response) throws Exception {
1.62 -
1.63 - if ( request.getMethod() == Method.POST ) {
1.64 - InputStream is = request.getInputStream();
1.65 - JSONTokener tok = new JSONTokener(new InputStreamReader(is));
1.66 - JSONObject obj = new JSONObject(tok);
1.67 - String tmpHtml = obj.getString("html");
1.68 - String tmpJava = obj.getString("java");
1.69 -
1.70 - Compile res = Compile.create(tmpHtml, tmpJava);
1.71 - List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
1.72 - if (err.isEmpty()) {
1.73 - data = res;
1.74 - response.getOutputStream().write("[]".getBytes());
1.75 - response.setStatus(HttpStatus.OK_200);
1.76 - } else {
1.77 -
1.78 - JSONArray errors = new JSONArray();
1.79 -
1.80 - for (Diagnostic<? extends JavaFileObject> d : err) {
1.81 - JSONObject e = new JSONObject();
1.82 - e.put("col", d.getColumnNumber());
1.83 - e.put("line", d.getLineNumber());
1.84 - e.put("kind", d.getKind().toString());
1.85 - e.put("msg", d.getMessage(Locale.ENGLISH));
1.86 - errors.put(e);
1.87 - }
1.88 -
1.89 - errors.write(response.getWriter());
1.90 - response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
1.91 - }
1.92 -
1.93 - return;
1.94 - }
1.95 -
1.96 - String r = request.getHttpHandlerPath();
1.97 - if (r == null || r.equals("/")) {
1.98 - r = "index.html";
1.99 - }
1.100 - if (r.equals("/result.html")) {
1.101 - response.setContentType("text/html");
1.102 - if (data != null) {
1.103 - response.getOutputBuffer().write(data.getHtml());
1.104 - }
1.105 - response.setStatus(HttpStatus.OK_200);
1.106 - return;
1.107 - }
1.108 -
1.109 - if (r.startsWith("/")) {
1.110 - r = r.substring(1);
1.111 - }
1.112 -
1.113 - if (r.endsWith(".html") || r.endsWith(".xhtml")) {
1.114 - response.setContentType("text/html");
1.115 - }
1.116 - OutputStream os = response.getOutputStream();
1.117 - try (InputStream is = Dew.class.getResourceAsStream(r) ) {
1.118 - copyStream(is, os, request.getRequestURL().toString() );
1.119 - } catch (IOException ex) {
1.120 - response.setDetailMessage(ex.getLocalizedMessage());
1.121 - response.setError();
1.122 - response.setStatus(404);
1.123 - }
1.124 - }
1.125 -
1.126 - static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
1.127 - for (;;) {
1.128 - int ch = is.read();
1.129 - if (ch == -1) {
1.130 - break;
1.131 - }
1.132 - os.write(ch);
1.133 - }
1.134 - }
1.135 -
1.136 - @Override
1.137 - public InputStream get(String r) throws IOException {
1.138 - byte[] arr = data == null ? null : data.get(r);
1.139 - return arr == null ? null : new ByteArrayInputStream(arr);
1.140 - }
1.141 -}