launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
branchdew
changeset 461 ccc3fd318cb1
parent 460 c0f1788183dd
child 462 aa69b1387624
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java	Tue Jan 15 15:56:45 2013 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java	Tue Jan 15 16:56:18 2013 +0100
     1.3 @@ -4,25 +4,55 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.dew;
     1.6  
     1.7 +import java.io.ByteArrayInputStream;
     1.8 +import java.io.ByteArrayOutputStream;
     1.9  import java.io.IOException;
    1.10  import java.io.InputStream;
    1.11 +import java.io.InputStreamReader;
    1.12  import java.io.OutputStream;
    1.13 +import java.util.logging.Logger;
    1.14 +import org.glassfish.grizzly.http.Method;
    1.15  import org.glassfish.grizzly.http.server.HttpHandler;
    1.16  import org.glassfish.grizzly.http.server.Request;
    1.17  import org.glassfish.grizzly.http.server.Response;
    1.18 +import org.glassfish.grizzly.http.util.HttpStatus;
    1.19 +import org.json.JSONObject;
    1.20 +import org.json.JSONTokener;
    1.21  
    1.22  /**
    1.23   *
    1.24   * @author phrebejk
    1.25   */
    1.26  public class Dew extends HttpHandler {
    1.27 +    private static String html = "Nazdar!";
    1.28  
    1.29      @Override
    1.30      public void service(Request request, Response response) throws Exception {
    1.31 +        
    1.32 +        if ( request.getMethod() == Method.POST ) {
    1.33 +            InputStream is = request.getInputStream();
    1.34 +            JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    1.35 +            JSONObject obj = new JSONObject(tok);
    1.36 +            html = obj.getString("html");
    1.37 +            LOG.info(html);
    1.38 +            
    1.39 +            response.getOutputStream().write("[]".getBytes());
    1.40 +            response.setStatus(HttpStatus.OK_200);
    1.41 +            
    1.42 +            return;
    1.43 +        }
    1.44 +        
    1.45          String r = request.getHttpHandlerPath();
    1.46          if (r == null || r.equals("/")) {
    1.47              r = "index.html";
    1.48          }
    1.49 +        if (r.equals("/result.html")) {
    1.50 +            response.setContentType("text/html");
    1.51 +            response.getOutputBuffer().write(html);
    1.52 +            response.setStatus(HttpStatus.OK_200);
    1.53 +            return;
    1.54 +        }
    1.55 +        
    1.56          if (r.startsWith("/")) {
    1.57              r = r.substring(1);
    1.58          }
    1.59 @@ -38,6 +68,7 @@
    1.60              response.setStatus(404);
    1.61          }
    1.62      }
    1.63 +    private static final Logger LOG = Logger.getLogger(Dew.class.getName());
    1.64      
    1.65      static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
    1.66          for (;;) {