author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Tue, 15 Jan 2013 22:48:17 +0100 | |
branch | dew |
changeset 462 | aa69b1387624 |
parent 461 | ccc3fd318cb1 |
child 465 | 443eb0f21a0a |
permissions | -rw-r--r-- |
phrebejk@460 | 1 |
/* |
phrebejk@460 | 2 |
* To change this template, choose Tools | Templates |
phrebejk@460 | 3 |
* and open the template in the editor. |
phrebejk@460 | 4 |
*/ |
phrebejk@460 | 5 |
package org.apidesign.bck2brwsr.dew; |
phrebejk@460 | 6 |
|
phrebejk@461 | 7 |
import java.io.ByteArrayInputStream; |
phrebejk@461 | 8 |
import java.io.ByteArrayOutputStream; |
phrebejk@460 | 9 |
import java.io.IOException; |
phrebejk@460 | 10 |
import java.io.InputStream; |
phrebejk@461 | 11 |
import java.io.InputStreamReader; |
phrebejk@460 | 12 |
import java.io.OutputStream; |
phrebejk@461 | 13 |
import java.util.logging.Logger; |
phrebejk@461 | 14 |
import org.glassfish.grizzly.http.Method; |
phrebejk@460 | 15 |
import org.glassfish.grizzly.http.server.HttpHandler; |
phrebejk@460 | 16 |
import org.glassfish.grizzly.http.server.Request; |
phrebejk@460 | 17 |
import org.glassfish.grizzly.http.server.Response; |
phrebejk@461 | 18 |
import org.glassfish.grizzly.http.util.HttpStatus; |
phrebejk@461 | 19 |
import org.json.JSONObject; |
phrebejk@461 | 20 |
import org.json.JSONTokener; |
phrebejk@460 | 21 |
|
phrebejk@460 | 22 |
/** |
phrebejk@460 | 23 |
* |
phrebejk@460 | 24 |
* @author phrebejk |
phrebejk@460 | 25 |
*/ |
phrebejk@460 | 26 |
public class Dew extends HttpHandler { |
jaroslav@462 | 27 |
private String html = "Nazdar!"; |
jaroslav@462 | 28 |
private String java = "class C {\n}\n"; |
phrebejk@460 | 29 |
|
phrebejk@460 | 30 |
@Override |
phrebejk@460 | 31 |
public void service(Request request, Response response) throws Exception { |
phrebejk@461 | 32 |
|
phrebejk@461 | 33 |
if ( request.getMethod() == Method.POST ) { |
phrebejk@461 | 34 |
InputStream is = request.getInputStream(); |
phrebejk@461 | 35 |
JSONTokener tok = new JSONTokener(new InputStreamReader(is)); |
phrebejk@461 | 36 |
JSONObject obj = new JSONObject(tok); |
phrebejk@461 | 37 |
html = obj.getString("html"); |
jaroslav@462 | 38 |
java = obj.getString("java"); |
phrebejk@461 | 39 |
LOG.info(html); |
jaroslav@462 | 40 |
LOG.info(java); |
phrebejk@461 | 41 |
|
phrebejk@461 | 42 |
response.getOutputStream().write("[]".getBytes()); |
phrebejk@461 | 43 |
response.setStatus(HttpStatus.OK_200); |
phrebejk@461 | 44 |
|
phrebejk@461 | 45 |
return; |
phrebejk@461 | 46 |
} |
phrebejk@461 | 47 |
|
phrebejk@460 | 48 |
String r = request.getHttpHandlerPath(); |
phrebejk@460 | 49 |
if (r == null || r.equals("/")) { |
phrebejk@460 | 50 |
r = "index.html"; |
phrebejk@460 | 51 |
} |
phrebejk@461 | 52 |
if (r.equals("/result.html")) { |
phrebejk@461 | 53 |
response.setContentType("text/html"); |
phrebejk@461 | 54 |
response.getOutputBuffer().write(html); |
phrebejk@461 | 55 |
response.setStatus(HttpStatus.OK_200); |
phrebejk@461 | 56 |
return; |
phrebejk@461 | 57 |
} |
phrebejk@461 | 58 |
|
phrebejk@460 | 59 |
if (r.startsWith("/")) { |
phrebejk@460 | 60 |
r = r.substring(1); |
phrebejk@460 | 61 |
} |
phrebejk@460 | 62 |
if (r.endsWith(".html") || r.endsWith(".xhtml")) { |
phrebejk@460 | 63 |
response.setContentType("text/html"); |
phrebejk@460 | 64 |
} |
phrebejk@460 | 65 |
OutputStream os = response.getOutputStream(); |
phrebejk@460 | 66 |
try (InputStream is = Dew.class.getResourceAsStream(r) ) { |
phrebejk@460 | 67 |
copyStream(is, os, request.getRequestURL().toString() ); |
phrebejk@460 | 68 |
} catch (IOException ex) { |
phrebejk@460 | 69 |
response.setDetailMessage(ex.getLocalizedMessage()); |
phrebejk@460 | 70 |
response.setError(); |
phrebejk@460 | 71 |
response.setStatus(404); |
phrebejk@460 | 72 |
} |
phrebejk@460 | 73 |
} |
phrebejk@461 | 74 |
private static final Logger LOG = Logger.getLogger(Dew.class.getName()); |
phrebejk@460 | 75 |
|
phrebejk@460 | 76 |
static void copyStream(InputStream is, OutputStream os, String baseURL) throws IOException { |
phrebejk@460 | 77 |
for (;;) { |
phrebejk@460 | 78 |
int ch = is.read(); |
phrebejk@460 | 79 |
if (ch == -1) { |
phrebejk@460 | 80 |
break; |
phrebejk@460 | 81 |
} |
phrebejk@460 | 82 |
os.write(ch); |
phrebejk@460 | 83 |
} |
phrebejk@460 | 84 |
} |
phrebejk@460 | 85 |
|
phrebejk@460 | 86 |
} |