dew/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 14:10:15 +0100
branchdew
changeset 546 79e5a4aae48d
parent 544 08ffdc3938e7
child 579 942deef87200
permissions -rw-r--r--
Keeping just the part of the server that is needed for the dew system
     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.util.List;
    26 import java.util.Locale;
    27 import javax.tools.Diagnostic;
    28 import javax.tools.JavaFileObject;
    29 import org.apidesign.vm4brwsr.Bck2Brwsr;
    30 import org.glassfish.grizzly.http.Method;
    31 import org.glassfish.grizzly.http.server.HttpHandler;
    32 import org.glassfish.grizzly.http.server.HttpServer;
    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.JSONArray;
    37 import org.json.JSONObject;
    38 import org.json.JSONTokener;
    39 
    40 /**
    41  *
    42  * @author phrebejk
    43  */
    44 final class Dew extends HttpHandler implements Bck2Brwsr.Resources {
    45     private String html = "";
    46     private Compile data;
    47 
    48     public static void main(String... args) throws Exception {
    49         DewLauncher l = new DewLauncher(null);
    50         l.addClassLoader(DewLauncher.class.getClassLoader());
    51         final Dew dew = new Dew();
    52         HttpServer s = l.initServer(dew);
    53         s.getServerConfiguration().addHttpHandler(dew, "/dew/");
    54         l.launchServerAndBrwsr(s, "/dew/");
    55         System.in.read();
    56     }
    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             String tmpHtml = obj.getString("html");
    66             String tmpJava = obj.getString("java");
    67             
    68             Compile res = Compile.create(tmpHtml, tmpJava);
    69             List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
    70             if (err.isEmpty()) {
    71                 data = res;
    72                 html = tmpHtml;
    73                 response.getOutputStream().write("[]".getBytes());
    74                 response.setStatus(HttpStatus.OK_200);
    75             } else {
    76                 
    77                 JSONArray errors = new JSONArray();
    78                 
    79                 for (Diagnostic<? extends JavaFileObject> d : err) {
    80                     JSONObject e = new JSONObject();
    81                     e.put("col", d.getColumnNumber());
    82                     e.put("line", d.getLineNumber());
    83                     e.put("kind", d.getKind().toString());
    84                     e.put("msg", d.getMessage(Locale.ENGLISH));
    85                     errors.put(e);
    86                 }
    87                 
    88                 errors.write(response.getWriter());                
    89                 response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
    90             }
    91             
    92             return;
    93         }
    94         
    95         String r = request.getHttpHandlerPath();
    96         if (r == null || r.equals("/")) {
    97             r = "index.html";
    98         }
    99         if (r.equals("/result.html")) {
   100             response.setContentType("text/html");
   101             response.getOutputBuffer().write(html);
   102             response.setStatus(HttpStatus.OK_200);
   103             return;
   104         }
   105         
   106         if (r.startsWith("/")) {
   107             r = r.substring(1);
   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     
   123     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
   124         for (;;) {
   125             int ch = is.read();
   126             if (ch == -1) {
   127                 break;
   128             }
   129             os.write(ch);            
   130         }
   131     }
   132 
   133     @Override
   134     public InputStream get(String r) throws IOException {
   135         byte[] arr = data == null ? null : data.get(r);
   136         return arr == null ? null : new ByteArrayInputStream(arr);
   137     }
   138 }