launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 16 Jan 2013 12:44:54 +0100
branchdew
changeset 471 3f71a3364367
parent 468 a7ff47e054f3
child 541 927a5f9fa430
permissions -rw-r--r--
Need to serve also classes from classpath
     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.ByteArrayInputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.io.InputStreamReader;
    24 import java.io.OutputStream;
    25 import java.io.Writer;
    26 import java.util.List;
    27 import java.util.logging.Logger;
    28 import javax.tools.Diagnostic;
    29 import javax.tools.JavaFileObject;
    30 import org.apidesign.vm4brwsr.Bck2Brwsr;
    31 import org.glassfish.grizzly.http.Method;
    32 import org.glassfish.grizzly.http.server.HttpHandler;
    33 import org.glassfish.grizzly.http.server.Request;
    34 import org.glassfish.grizzly.http.server.Response;
    35 import org.glassfish.grizzly.http.util.HttpStatus;
    36 import org.json.JSONObject;
    37 import org.json.JSONTokener;
    38 
    39 /**
    40  *
    41  * @author phrebejk
    42  */
    43 public class Dew extends HttpHandler implements Bck2Brwsr.Resources {
    44     private String html = "";
    45     private Compile data;
    46 
    47     @Override
    48     public void service(Request request, Response response) throws Exception {
    49         
    50         if ( request.getMethod() == Method.POST ) {
    51             InputStream is = request.getInputStream();
    52             JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    53             JSONObject obj = new JSONObject(tok);
    54             String tmpHtml = obj.getString("html");
    55             String tmpJava = obj.getString("java");
    56             
    57             Compile res = Compile.create(tmpHtml, tmpJava);
    58             List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
    59             if (err.isEmpty()) {
    60                 data = res;
    61                 html = tmpHtml;
    62                 response.getOutputStream().write("[]".getBytes());
    63                 response.setStatus(HttpStatus.OK_200);
    64             } else {
    65                 response.getOutputStream().write(("[errors:'" + err + "']").getBytes());
    66                 response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
    67             }
    68             
    69             return;
    70         }
    71         
    72         String r = request.getHttpHandlerPath();
    73         if (r == null || r.equals("/")) {
    74             r = "index.html";
    75         }
    76         if (r.equals("/result.html")) {
    77             response.setContentType("text/html");
    78             response.getOutputBuffer().write(html);
    79             response.setStatus(HttpStatus.OK_200);
    80             return;
    81         }
    82         
    83         if (r.startsWith("/")) {
    84             r = r.substring(1);
    85         }
    86         
    87         if (r.endsWith(".html") || r.endsWith(".xhtml")) {
    88             response.setContentType("text/html");
    89         }
    90         OutputStream os = response.getOutputStream();
    91         try (InputStream is = Dew.class.getResourceAsStream(r) ) {
    92             copyStream(is, os, request.getRequestURL().toString() );
    93         } catch (IOException ex) {
    94             response.setDetailMessage(ex.getLocalizedMessage());
    95             response.setError();
    96             response.setStatus(404);
    97         }
    98     }
    99     
   100     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
   101         for (;;) {
   102             int ch = is.read();
   103             if (ch == -1) {
   104                 break;
   105             }
   106             os.write(ch);            
   107         }
   108     }
   109 
   110     @Override
   111     public InputStream get(String r) throws IOException {
   112         byte[] arr = data == null ? null : data.get(r);
   113         return arr == null ? null : new ByteArrayInputStream(arr);
   114     }
   115 }