launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 16 Jan 2013 11:36:19 +0100
branchdew
changeset 465 443eb0f21a0a
parent 462 aa69b1387624
child 466 d6b1f996c0d8
permissions -rw-r--r--
Trying to server real HTML and Java code
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 package org.apidesign.bck2brwsr.dew;
     6 
     7 import java.io.ByteArrayInputStream;
     8 import java.io.IOException;
     9 import java.io.InputStream;
    10 import java.io.InputStreamReader;
    11 import java.io.OutputStream;
    12 import java.util.List;
    13 import java.util.logging.Logger;
    14 import javax.tools.Diagnostic;
    15 import javax.tools.JavaFileObject;
    16 import org.glassfish.grizzly.http.Method;
    17 import org.glassfish.grizzly.http.server.HttpHandler;
    18 import org.glassfish.grizzly.http.server.Request;
    19 import org.glassfish.grizzly.http.server.Response;
    20 import org.glassfish.grizzly.http.util.HttpStatus;
    21 import org.json.JSONObject;
    22 import org.json.JSONTokener;
    23 
    24 /**
    25  *
    26  * @author phrebejk
    27  */
    28 public class Dew extends HttpHandler {
    29     private String html = "<html><body>\n"
    30         + " <button id='btn'>Hello!</button>\n"
    31         + " <hr/>\n"
    32         + "\n"
    33         + "\n"
    34         + "\n"
    35         + " <script src=\"/bck2brwsr.js\"></script>\n"
    36         + " <script type=\"text/javascript\">\n"
    37         + "   function ldCls(res) {\n"
    38         + "     var request = new XMLHttpRequest();\n"
    39         + "     request.open('GET', '/dew/classes/' + res, false);\n"
    40         + "     request.send();\n"
    41         + "     var arr = eval('(' + request.responseText + ')');\n"
    42         + "     return arr;\n"
    43         + "   }\n"
    44         + " //  var vm = new bck2brwsr(ldCls);\n"
    45         + " //  vm.loadClass('bck2brwsr.demo.Index');\n"
    46         + " </script>\n"
    47         + "</body></html>\n";
    48     private String java = "package bck2brwsr.demo;\n"
    49                 + "import org.apidesign.bck2brwsr.htmlpage.api.*;\n"
    50             + "@Page(xhtml=\"index.html\", className=\"Index\")\n"
    51             + "class X {\n"
    52             + "   @OnClick(id=\"btn\") static void clcs() {\n"
    53             + "     Index.BTN.setDisabled(true);\n"
    54             + "   }\n"
    55             + "}\n";
    56     private Compile data;
    57 
    58     @Override
    59     public void service(Request request, Response response) throws Exception {
    60         
    61         if ( request.getMethod() == Method.POST ) {
    62             InputStream is = request.getInputStream();
    63             JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    64             JSONObject obj = new JSONObject(tok);
    65             html = obj.getString("html");
    66             java = obj.getString("java");
    67             
    68             Compile res = Compile.create(html, java);
    69             List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
    70             if (err.isEmpty()) {
    71                 data = res;
    72                 response.getOutputStream().write("[]".getBytes());
    73                 response.setStatus(HttpStatus.OK_200);
    74             } else {
    75                 response.getOutputStream().write(("[errors:'" + err + "']").getBytes());
    76                 response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
    77             }
    78             
    79             return;
    80         }
    81         
    82         String r = request.getHttpHandlerPath();
    83         if (r == null || r.equals("/")) {
    84             r = "index.html";
    85         }
    86         if (r.equals("/result.html")) {
    87             response.setContentType("text/html");
    88             response.getOutputBuffer().write(html);
    89             response.setStatus(HttpStatus.OK_200);
    90             return;
    91         }
    92         
    93         if (r.startsWith("/")) {
    94             r = r.substring(1);
    95         }
    96         if (r.startsWith("classes/")) {
    97             if (data == null) {
    98                 //data = Compile.create(html, java);
    99             }
   100             r = r.substring(8);
   101             byte[] is = data == null ? null : data.get(r);
   102             if (is == null) {
   103                 is = new byte[0];
   104             }
   105             OutputStream os = response.getOutputStream();
   106             copyStream(new ByteArrayInputStream(is), os, request.getRequestURL().toString() );
   107             return;
   108         }
   109         
   110         if (r.endsWith(".html") || r.endsWith(".xhtml")) {
   111             response.setContentType("text/html");
   112         }
   113         OutputStream os = response.getOutputStream();
   114         try (InputStream is = Dew.class.getResourceAsStream(r) ) {
   115             copyStream(is, os, request.getRequestURL().toString() );
   116         } catch (IOException ex) {
   117             response.setDetailMessage(ex.getLocalizedMessage());
   118             response.setError();
   119             response.setStatus(404);
   120         }
   121     }
   122     private static final Logger LOG = Logger.getLogger(Dew.class.getName());
   123     
   124     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
   125         for (;;) {
   126             int ch = is.read();
   127             if (ch == -1) {
   128                 break;
   129             }
   130             os.write(ch);            
   131         }
   132     }
   133     
   134 }