launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 15 Jan 2013 22:48:17 +0100
branchdew
changeset 462 aa69b1387624
parent 461 ccc3fd318cb1
child 465 443eb0f21a0a
permissions -rw-r--r--
Trying to compile Java source via the javax.tools.ToolProvider.getSystemJavaCompiler
     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.ByteArrayOutputStream;
     9 import java.io.IOException;
    10 import java.io.InputStream;
    11 import java.io.InputStreamReader;
    12 import java.io.OutputStream;
    13 import java.util.logging.Logger;
    14 import org.glassfish.grizzly.http.Method;
    15 import org.glassfish.grizzly.http.server.HttpHandler;
    16 import org.glassfish.grizzly.http.server.Request;
    17 import org.glassfish.grizzly.http.server.Response;
    18 import org.glassfish.grizzly.http.util.HttpStatus;
    19 import org.json.JSONObject;
    20 import org.json.JSONTokener;
    21 
    22 /**
    23  *
    24  * @author phrebejk
    25  */
    26 public class Dew extends HttpHandler {
    27     private String html = "Nazdar!";
    28     private String java = "class C {\n}\n";
    29 
    30     @Override
    31     public void service(Request request, Response response) throws Exception {
    32         
    33         if ( request.getMethod() == Method.POST ) {
    34             InputStream is = request.getInputStream();
    35             JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    36             JSONObject obj = new JSONObject(tok);
    37             html = obj.getString("html");
    38             java = obj.getString("java");
    39             LOG.info(html);
    40             LOG.info(java);
    41             
    42             response.getOutputStream().write("[]".getBytes());
    43             response.setStatus(HttpStatus.OK_200);
    44             
    45             return;
    46         }
    47         
    48         String r = request.getHttpHandlerPath();
    49         if (r == null || r.equals("/")) {
    50             r = "index.html";
    51         }
    52         if (r.equals("/result.html")) {
    53             response.setContentType("text/html");
    54             response.getOutputBuffer().write(html);
    55             response.setStatus(HttpStatus.OK_200);
    56             return;
    57         }
    58         
    59         if (r.startsWith("/")) {
    60             r = r.substring(1);
    61         }
    62         if (r.endsWith(".html") || r.endsWith(".xhtml")) {
    63             response.setContentType("text/html");
    64         }
    65         OutputStream os = response.getOutputStream();
    66         try (InputStream is = Dew.class.getResourceAsStream(r) ) {
    67             copyStream(is, os, request.getRequestURL().toString() );
    68         } catch (IOException ex) {
    69             response.setDetailMessage(ex.getLocalizedMessage());
    70             response.setError();
    71             response.setStatus(404);
    72         }
    73     }
    74     private static final Logger LOG = Logger.getLogger(Dew.class.getName());
    75     
    76     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
    77         for (;;) {
    78             int ch = is.read();
    79             if (ch == -1) {
    80                 break;
    81             }
    82             os.write(ch);            
    83         }
    84     }
    85     
    86 }