dew/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 13:18:46 +0100
branchdew
changeset 544 08ffdc3938e7
parent 541 launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java@927a5f9fa430
child 546 79e5a4aae48d
permissions -rw-r--r--
Moving Development Environment for Web to own project
     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.Locale;
    28 import java.util.Locale;
    29 import java.util.logging.Logger;
    30 import javax.tools.Diagnostic;
    31 import javax.tools.JavaFileObject;
    32 import org.apidesign.vm4brwsr.Bck2Brwsr;
    33 import org.glassfish.grizzly.http.Method;
    34 import org.glassfish.grizzly.http.server.HttpHandler;
    35 import org.glassfish.grizzly.http.server.Request;
    36 import org.glassfish.grizzly.http.server.Response;
    37 import org.glassfish.grizzly.http.util.HttpStatus;
    38 import org.json.JSONArray;
    39 import org.json.JSONObject;
    40 import org.json.JSONStringer;
    41 import org.json.JSONTokener;
    42 
    43 /**
    44  *
    45  * @author phrebejk
    46  */
    47 public class Dew extends HttpHandler implements Bck2Brwsr.Resources {
    48     private String html = "";
    49     private Compile data;
    50 
    51     @Override
    52     public void service(Request request, Response response) throws Exception {
    53         
    54         if ( request.getMethod() == Method.POST ) {
    55             InputStream is = request.getInputStream();
    56             JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    57             JSONObject obj = new JSONObject(tok);
    58             String tmpHtml = obj.getString("html");
    59             String tmpJava = obj.getString("java");
    60             
    61             Compile res = Compile.create(tmpHtml, tmpJava);
    62             List<Diagnostic<? extends JavaFileObject>> err = res.getErrors();
    63             if (err.isEmpty()) {
    64                 data = res;
    65                 html = tmpHtml;
    66                 response.getOutputStream().write("[]".getBytes());
    67                 response.setStatus(HttpStatus.OK_200);
    68             } else {
    69                 
    70                 JSONArray errors = new JSONArray();
    71                 
    72                 for (Diagnostic<? extends JavaFileObject> d : err) {
    73                     JSONObject e = new JSONObject();
    74                     e.put("col", d.getColumnNumber());
    75                     e.put("line", d.getLineNumber());
    76                     e.put("kind", d.getKind().toString());
    77                     e.put("msg", d.getMessage(Locale.ENGLISH));
    78                     errors.put(e);
    79                 }
    80                 
    81                 errors.write(response.getWriter());                
    82                 response.setStatus(HttpStatus.PRECONDITION_FAILED_412);
    83             }
    84             
    85             return;
    86         }
    87         
    88         String r = request.getHttpHandlerPath();
    89         if (r == null || r.equals("/")) {
    90             r = "index.html";
    91         }
    92         if (r.equals("/result.html")) {
    93             response.setContentType("text/html");
    94             response.getOutputBuffer().write(html);
    95             response.setStatus(HttpStatus.OK_200);
    96             return;
    97         }
    98         
    99         if (r.startsWith("/")) {
   100             r = r.substring(1);
   101         }
   102         
   103         if (r.endsWith(".html") || r.endsWith(".xhtml")) {
   104             response.setContentType("text/html");
   105         }
   106         OutputStream os = response.getOutputStream();
   107         try (InputStream is = Dew.class.getResourceAsStream(r) ) {
   108             copyStream(is, os, request.getRequestURL().toString() );
   109         } catch (IOException ex) {
   110             response.setDetailMessage(ex.getLocalizedMessage());
   111             response.setError();
   112             response.setStatus(404);
   113         }
   114     }
   115     
   116     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
   117         for (;;) {
   118             int ch = is.read();
   119             if (ch == -1) {
   120                 break;
   121             }
   122             os.write(ch);            
   123         }
   124     }
   125 
   126     @Override
   127     public InputStream get(String r) throws IOException {
   128         byte[] arr = data == null ? null : data.get(r);
   129         return arr == null ? null : new ByteArrayInputStream(arr);
   130     }
   131 }