phrebejk@460: /* phrebejk@460: * To change this template, choose Tools | Templates phrebejk@460: * and open the template in the editor. phrebejk@460: */ phrebejk@460: package org.apidesign.bck2brwsr.dew; phrebejk@460: phrebejk@460: import java.io.IOException; phrebejk@460: import java.io.InputStream; phrebejk@460: import java.io.OutputStream; phrebejk@460: import org.glassfish.grizzly.http.server.HttpHandler; phrebejk@460: import org.glassfish.grizzly.http.server.Request; phrebejk@460: import org.glassfish.grizzly.http.server.Response; phrebejk@460: phrebejk@460: /** phrebejk@460: * phrebejk@460: * @author phrebejk phrebejk@460: */ phrebejk@460: public class Dew extends HttpHandler { phrebejk@460: phrebejk@460: @Override phrebejk@460: public void service(Request request, Response response) throws Exception { phrebejk@460: String r = request.getHttpHandlerPath(); phrebejk@460: if (r == null || r.equals("/")) { phrebejk@460: r = "index.html"; phrebejk@460: } phrebejk@460: if (r.startsWith("/")) { phrebejk@460: r = r.substring(1); phrebejk@460: } phrebejk@460: if (r.endsWith(".html") || r.endsWith(".xhtml")) { phrebejk@460: response.setContentType("text/html"); phrebejk@460: } phrebejk@460: OutputStream os = response.getOutputStream(); phrebejk@460: try (InputStream is = Dew.class.getResourceAsStream(r) ) { phrebejk@460: copyStream(is, os, request.getRequestURL().toString() ); phrebejk@460: } catch (IOException ex) { phrebejk@460: response.setDetailMessage(ex.getLocalizedMessage()); phrebejk@460: response.setError(); phrebejk@460: response.setStatus(404); phrebejk@460: } phrebejk@460: } phrebejk@460: phrebejk@460: static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException { phrebejk@460: for (;;) { phrebejk@460: int ch = is.read(); phrebejk@460: if (ch == -1) { phrebejk@460: break; phrebejk@460: } phrebejk@460: os.write(ch); phrebejk@460: } phrebejk@460: } phrebejk@460: phrebejk@460: }