launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 16 Jan 2013 12:28:56 +0100
branchdew
changeset 468 a7ff47e054f3
parent 466 d6b1f996c0d8
child 471 3f71a3364367
permissions -rw-r--r--
Fixing license
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.dew;
    19 
    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;
    36 
    37 /**
    38  *
    39  * @author phrebejk
    40  */
    41 public class Dew extends HttpHandler {
    42     private String html = "";
    43     private Compile data;
    44 
    45     @Override
    46     public void service(Request request, Response response) throws Exception {
    47         
    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");
    54             
    55             Compile res = Compile.create(tmpHtml, tmpJava);
    56             List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
    57             if (err.isEmpty()) {
    58                 data = res;
    59                 html = tmpHtml;
    60                 response.getOutputStream().write("[]".getBytes());
    61                 response.setStatus(HttpStatus.OK_200);
    62             } else {
    63                 response.getOutputStream().write(("[errors:'" + err + "']").getBytes());
    64                 response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
    65             }
    66             
    67             return;
    68         }
    69         
    70         String r = request.getHttpHandlerPath();
    71         if (r == null || r.equals("/")) {
    72             r = "index.html";
    73         }
    74         if (r.equals("/result.html")) {
    75             response.setContentType("text/html");
    76             response.getOutputBuffer().write(html);
    77             response.setStatus(HttpStatus.OK_200);
    78             return;
    79         }
    80         
    81         if (r.startsWith("/")) {
    82             r = r.substring(1);
    83         }
    84         if (r.startsWith("classes/")) {
    85             r = r.substring(8);
    86             byte[] arr = data == null ? null : data.get(r);
    87             if (arr == null) {
    88                 response.setError();
    89                 response.setDetailMessage("No data for " + r + " yet!");
    90                 return;
    91             }
    92             try (Writer w = response.getWriter()) {
    93                 response.setContentType("text/javascript");
    94                 w.append("[");
    95                 for (int i = 0; i < arr.length; i++) {
    96                     int b = arr[i];
    97                     if (b == -1) {
    98                         break;
    99                     }
   100                     if (i > 0) {
   101                         w.append(", ");
   102                     }
   103                     if (i % 20 == 0) {
   104                         w.write("\n");
   105                     }
   106                     if (b > 127) {
   107                         b = b - 256;
   108                     }
   109                     w.append(Integer.toString(b));
   110                 }
   111                 w.append("\n]");
   112             }
   113             return;
   114         }
   115         
   116         if (r.endsWith(".html") || r.endsWith(".xhtml")) {
   117             response.setContentType("text/html");
   118         }
   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());
   124             response.setError();
   125             response.setStatus(404);
   126         }
   127     }
   128     private static final Logger LOG = Logger.getLogger(Dew.class.getName());
   129     
   130     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
   131         for (;;) {
   132             int ch = is.read();
   133             if (ch == -1) {
   134                 break;
   135             }
   136             os.write(ch);            
   137         }
   138     }
   139     
   140 }