launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
author phrebejk
Tue, 15 Jan 2013 16:56:18 +0100
branchdew
changeset 461 ccc3fd318cb1
parent 460 c0f1788183dd
child 462 aa69b1387624
permissions -rw-r--r--
Web development server initial prototype.
     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 static String html = "Nazdar!";
    28 
    29     @Override
    30     public void service(Request request, Response response) throws Exception {
    31         
    32         if ( request.getMethod() == Method.POST ) {
    33             InputStream is = request.getInputStream();
    34             JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    35             JSONObject obj = new JSONObject(tok);
    36             html = obj.getString("html");
    37             LOG.info(html);
    38             
    39             response.getOutputStream().write("[]".getBytes());
    40             response.setStatus(HttpStatus.OK_200);
    41             
    42             return;
    43         }
    44         
    45         String r = request.getHttpHandlerPath();
    46         if (r == null || r.equals("/")) {
    47             r = "index.html";
    48         }
    49         if (r.equals("/result.html")) {
    50             response.setContentType("text/html");
    51             response.getOutputBuffer().write(html);
    52             response.setStatus(HttpStatus.OK_200);
    53             return;
    54         }
    55         
    56         if (r.startsWith("/")) {
    57             r = r.substring(1);
    58         }
    59         if (r.endsWith(".html") || r.endsWith(".xhtml")) {
    60             response.setContentType("text/html");
    61         }
    62         OutputStream os = response.getOutputStream();
    63         try (InputStream is = Dew.class.getResourceAsStream(r) ) {
    64             copyStream(is, os, request.getRequestURL().toString() );
    65         } catch (IOException ex) {
    66             response.setDetailMessage(ex.getLocalizedMessage());
    67             response.setError();
    68             response.setStatus(404);
    69         }
    70     }
    71     private static final Logger LOG = Logger.getLogger(Dew.class.getName());
    72     
    73     static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
    74         for (;;) {
    75             int ch = is.read();
    76             if (ch == -1) {
    77                 break;
    78             }
    79             os.write(ch);            
    80         }
    81     }
    82     
    83 }