Web development server initial prototype. dew
authorphrebejk
Tue, 15 Jan 2013 16:56:18 +0100
branchdew
changeset 461ccc3fd318cb1
parent 460 c0f1788183dd
child 462 aa69b1387624
Web development server initial prototype.
launcher/pom.xml
launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java
launcher/src/main/resources/org/apidesign/bck2brwsr/dew/index.html
launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js
     1.1 --- a/launcher/pom.xml	Tue Jan 15 15:56:45 2013 +0100
     1.2 +++ b/launcher/pom.xml	Tue Jan 15 16:56:18 2013 +0100
     1.3 @@ -45,5 +45,10 @@
     1.4        <artifactId>vm4brwsr</artifactId>
     1.5        <version>${project.version}</version>
     1.6      </dependency>
     1.7 +    <dependency>
     1.8 +      <groupId>org.json</groupId>
     1.9 +      <artifactId>json</artifactId>
    1.10 +      <version>20090211</version>
    1.11 +    </dependency>
    1.12    </dependencies>
    1.13  </project>
     2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java	Tue Jan 15 15:56:45 2013 +0100
     2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java	Tue Jan 15 16:56:18 2013 +0100
     2.3 @@ -4,25 +4,55 @@
     2.4   */
     2.5  package org.apidesign.bck2brwsr.dew;
     2.6  
     2.7 +import java.io.ByteArrayInputStream;
     2.8 +import java.io.ByteArrayOutputStream;
     2.9  import java.io.IOException;
    2.10  import java.io.InputStream;
    2.11 +import java.io.InputStreamReader;
    2.12  import java.io.OutputStream;
    2.13 +import java.util.logging.Logger;
    2.14 +import org.glassfish.grizzly.http.Method;
    2.15  import org.glassfish.grizzly.http.server.HttpHandler;
    2.16  import org.glassfish.grizzly.http.server.Request;
    2.17  import org.glassfish.grizzly.http.server.Response;
    2.18 +import org.glassfish.grizzly.http.util.HttpStatus;
    2.19 +import org.json.JSONObject;
    2.20 +import org.json.JSONTokener;
    2.21  
    2.22  /**
    2.23   *
    2.24   * @author phrebejk
    2.25   */
    2.26  public class Dew extends HttpHandler {
    2.27 +    private static String html = "Nazdar!";
    2.28  
    2.29      @Override
    2.30      public void service(Request request, Response response) throws Exception {
    2.31 +        
    2.32 +        if ( request.getMethod() == Method.POST ) {
    2.33 +            InputStream is = request.getInputStream();
    2.34 +            JSONTokener tok = new JSONTokener(new InputStreamReader(is));
    2.35 +            JSONObject obj = new JSONObject(tok);
    2.36 +            html = obj.getString("html");
    2.37 +            LOG.info(html);
    2.38 +            
    2.39 +            response.getOutputStream().write("[]".getBytes());
    2.40 +            response.setStatus(HttpStatus.OK_200);
    2.41 +            
    2.42 +            return;
    2.43 +        }
    2.44 +        
    2.45          String r = request.getHttpHandlerPath();
    2.46          if (r == null || r.equals("/")) {
    2.47              r = "index.html";
    2.48          }
    2.49 +        if (r.equals("/result.html")) {
    2.50 +            response.setContentType("text/html");
    2.51 +            response.getOutputBuffer().write(html);
    2.52 +            response.setStatus(HttpStatus.OK_200);
    2.53 +            return;
    2.54 +        }
    2.55 +        
    2.56          if (r.startsWith("/")) {
    2.57              r = r.substring(1);
    2.58          }
    2.59 @@ -38,6 +68,7 @@
    2.60              response.setStatus(404);
    2.61          }
    2.62      }
    2.63 +    private static final Logger LOG = Logger.getLogger(Dew.class.getName());
    2.64      
    2.65      static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
    2.66          for (;;) {
     3.1 --- a/launcher/src/main/resources/org/apidesign/bck2brwsr/dew/index.html	Tue Jan 15 15:56:45 2013 +0100
     3.2 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/dew/index.html	Tue Jan 15 16:56:18 2013 +0100
     3.3 @@ -40,11 +40,14 @@
     3.4          
     3.5          <div>&nbsp;</div>    
     3.6              
     3.7 +        <button class="btn btn-warning pull-right" ng-click="post()">Rebuild</button>
     3.8          <ul class="nav nav-tabs">
     3.9              <li ng-class="'active'"><a href="#">Result</a></li>
    3.10          </ul>    
    3.11          
    3.12 -        <iframe id="result" frameborder="0" scrolling="no" width="100%" style="height: max-content; overflow: hidden; border: 1px solid #DFDFDF;" src="">
    3.13 +        
    3.14 +        <!-- button class="btn" ng-click="reload()">Reload</button -->
    3.15 +        <iframe id="result" frameborder="0" scrolling="yes" width="100%" style="height: 1000px; overflow: auto; border: 1px solid #DFDFDF;" src="result.html">
    3.16              <p>Your browser does not support iframes.</p>
    3.17          </iframe>
    3.18              
     4.1 --- a/launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js	Tue Jan 15 15:56:45 2013 +0100
     4.2 +++ b/launcher/src/main/resources/org/apidesign/bck2brwsr/dew/js/app.js	Tue Jan 15 16:56:18 2013 +0100
     4.3 @@ -70,11 +70,21 @@
     4.4          };
     4.5  }]);
     4.6  
     4.7 -function DevCtrl( $scope ) {
     4.8 +function DevCtrl( $scope, $http ) {
     4.9      
    4.10 -    var htmlChange = function() {
    4.11 -        var frame = document.getElementById("result");
    4.12 -        frame.contentDocument.write($scope.html);        
    4.13 +    $scope.reload= function() {
    4.14 +        var frame = document.getElementById("result");        
    4.15 +        frame.src = "result.html";
    4.16 +        frame.contentDocument.location.reload(true);
    4.17 +        frame.contentWindow.location.reload();
    4.18 +    };
    4.19 +    
    4.20 +    $scope.post = function(html, java) {
    4.21 +        return $http({url: ".",
    4.22 +            method: "POST",
    4.23 +            //headers: this.headers,
    4.24 +            data: { html : $scope.html, java : $scope.java} 
    4.25 +        }).success( $scope.reload );
    4.26      };
    4.27      
    4.28      $scope.tab = "html";
    4.29 @@ -85,7 +95,7 @@
    4.30          return tab === $scope.tab ? "active" : "";
    4.31      };
    4.32      
    4.33 -    $scope.$watch( "html", htmlChange );
    4.34 +    // $scope.$watch( "html", htmlChange );
    4.35      
    4.36      
    4.37  }