1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Dew.java Tue Jan 15 15:56:45 2013 +0100
1.3 @@ -0,0 +1,52 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +package org.apidesign.bck2brwsr.dew;
1.9 +
1.10 +import java.io.IOException;
1.11 +import java.io.InputStream;
1.12 +import java.io.OutputStream;
1.13 +import org.glassfish.grizzly.http.server.HttpHandler;
1.14 +import org.glassfish.grizzly.http.server.Request;
1.15 +import org.glassfish.grizzly.http.server.Response;
1.16 +
1.17 +/**
1.18 + *
1.19 + * @author phrebejk
1.20 + */
1.21 +public class Dew extends HttpHandler {
1.22 +
1.23 + @Override
1.24 + public void service(Request request, Response response) throws Exception {
1.25 + String r = request.getHttpHandlerPath();
1.26 + if (r == null || r.equals("/")) {
1.27 + r = "index.html";
1.28 + }
1.29 + if (r.startsWith("/")) {
1.30 + r = r.substring(1);
1.31 + }
1.32 + if (r.endsWith(".html") || r.endsWith(".xhtml")) {
1.33 + response.setContentType("text/html");
1.34 + }
1.35 + OutputStream os = response.getOutputStream();
1.36 + try (InputStream is = Dew.class.getResourceAsStream(r) ) {
1.37 + copyStream(is, os, request.getRequestURL().toString() );
1.38 + } catch (IOException ex) {
1.39 + response.setDetailMessage(ex.getLocalizedMessage());
1.40 + response.setError();
1.41 + response.setStatus(404);
1.42 + }
1.43 + }
1.44 +
1.45 + static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException {
1.46 + for (;;) {
1.47 + int ch = is.read();
1.48 + if (ch == -1) {
1.49 + break;
1.50 + }
1.51 + os.write(ch);
1.52 + }
1.53 + }
1.54 +
1.55 +}