1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-ws-tyrus/src/test/java/org/apidesign/html/wstyrus/TyrusDynamicHTTP.java Sun Aug 25 14:40:16 2013 +0200
1.3 @@ -0,0 +1,238 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.wstyrus;
1.25 +
1.26 +import java.io.ByteArrayInputStream;
1.27 +import java.io.ByteArrayOutputStream;
1.28 +import java.io.IOException;
1.29 +import java.io.InputStream;
1.30 +import java.io.OutputStream;
1.31 +import java.io.Reader;
1.32 +import java.net.URI;
1.33 +import java.net.URISyntaxException;
1.34 +import java.util.ArrayList;
1.35 +import java.util.List;
1.36 +import java.util.logging.Level;
1.37 +import java.util.logging.Logger;
1.38 +import org.glassfish.grizzly.PortRange;
1.39 +import org.glassfish.grizzly.http.server.HttpHandler;
1.40 +import org.glassfish.grizzly.http.server.HttpServer;
1.41 +import org.glassfish.grizzly.http.server.NetworkListener;
1.42 +import org.glassfish.grizzly.http.server.Request;
1.43 +import org.glassfish.grizzly.http.server.Response;
1.44 +import org.glassfish.grizzly.http.server.ServerConfiguration;
1.45 +import org.glassfish.grizzly.websockets.WebSocket;
1.46 +import org.glassfish.grizzly.websockets.WebSocketAddOn;
1.47 +import org.glassfish.grizzly.websockets.WebSocketApplication;
1.48 +import org.glassfish.grizzly.websockets.WebSocketEngine;
1.49 +
1.50 +/**
1.51 + *
1.52 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.53 + */
1.54 +final class TyrusDynamicHTTP extends HttpHandler {
1.55 + private static int resourcesCount;
1.56 + private static List<Resource> resources;
1.57 + private static ServerConfiguration conf;
1.58 + private static HttpServer server;
1.59 +
1.60 + private TyrusDynamicHTTP() {
1.61 + }
1.62 +
1.63 + static URI initServer() throws Exception {
1.64 + server = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
1.65 + final WebSocketAddOn addon = new WebSocketAddOn();
1.66 + for (NetworkListener listener : server.getListeners()) {
1.67 + listener.registerAddOn(addon);
1.68 + }
1.69 + resources = new ArrayList<Resource>();
1.70 +
1.71 + conf = server.getServerConfiguration();
1.72 + final TyrusDynamicHTTP dh = new TyrusDynamicHTTP();
1.73 +
1.74 + conf.addHttpHandler(dh, "/");
1.75 +
1.76 + server.start();
1.77 +
1.78 + return pageURL("http", server, "/test.html");
1.79 + }
1.80 +
1.81 + @Override
1.82 + public void service(Request request, Response response) throws Exception {
1.83 + if ("/test.html".equals(request.getRequestURI())) {
1.84 + response.setContentType("text/html");
1.85 + final InputStream is = TyrusDynamicHTTP.class.getResourceAsStream("test.html");
1.86 + copyStream(is, response.getOutputStream(), null);
1.87 + return;
1.88 + }
1.89 + if ("/dynamic".equals(request.getRequestURI())) {
1.90 + String mimeType = request.getParameter("mimeType");
1.91 + List<String> params = new ArrayList<String>();
1.92 + boolean webSocket = false;
1.93 + for (int i = 0;; i++) {
1.94 + String p = request.getParameter("param" + i);
1.95 + if (p == null) {
1.96 + break;
1.97 + }
1.98 + if ("protocol:ws".equals(p)) {
1.99 + webSocket = true;
1.100 + continue;
1.101 + }
1.102 + params.add(p);
1.103 + }
1.104 + final String cnt = request.getParameter("content");
1.105 + String mangle = cnt.replace("%20", " ").replace("%0A", "\n");
1.106 + ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8"));
1.107 + URI url;
1.108 + final Resource res = new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()]));
1.109 + if (webSocket) {
1.110 + url = registerWebSocket(res);
1.111 + } else {
1.112 + url = registerResource(res);
1.113 + }
1.114 + response.getWriter().write(url.toString());
1.115 + response.getWriter().write("\n");
1.116 + return;
1.117 + }
1.118 +
1.119 + for (Resource r : resources) {
1.120 + if (r.httpPath.equals(request.getRequestURI())) {
1.121 + response.setContentType(r.httpType);
1.122 + r.httpContent.reset();
1.123 + String[] params = null;
1.124 + if (r.parameters.length != 0) {
1.125 + params = new String[r.parameters.length];
1.126 + for (int i = 0; i < r.parameters.length; i++) {
1.127 + params[i] = request.getParameter(r.parameters[i]);
1.128 + if (params[i] == null) {
1.129 + if ("http.method".equals(r.parameters[i])) {
1.130 + params[i] = request.getMethod().toString();
1.131 + } else if ("http.requestBody".equals(r.parameters[i])) {
1.132 + Reader rdr = request.getReader();
1.133 + StringBuilder sb = new StringBuilder();
1.134 + for (;;) {
1.135 + int ch = rdr.read();
1.136 + if (ch == -1) {
1.137 + break;
1.138 + }
1.139 + sb.append((char) ch);
1.140 + }
1.141 + params[i] = sb.toString();
1.142 + }
1.143 + }
1.144 + if (params[i] == null) {
1.145 + params[i] = "null";
1.146 + }
1.147 + }
1.148 + }
1.149 +
1.150 + copyStream(r.httpContent, response.getOutputStream(), null, params);
1.151 + }
1.152 + }
1.153 + }
1.154 +
1.155 + private URI registerWebSocket(Resource r) {
1.156 + WebSocketEngine.getEngine().register("", r.httpPath, new WS(r));
1.157 + return pageURL("ws", server, r.httpPath);
1.158 + }
1.159 +
1.160 + private URI registerResource(Resource r) {
1.161 + if (!resources.contains(r)) {
1.162 + resources.add(r);
1.163 + conf.addHttpHandler(this, r.httpPath);
1.164 + }
1.165 + return pageURL("http", server, r.httpPath);
1.166 + }
1.167 +
1.168 + private static URI pageURL(String proto, HttpServer server, final String page) {
1.169 + NetworkListener listener = server.getListeners().iterator().next();
1.170 + int port = listener.getPort();
1.171 + try {
1.172 + return new URI(proto + "://localhost:" + port + page);
1.173 + } catch (URISyntaxException ex) {
1.174 + throw new IllegalStateException(ex);
1.175 + }
1.176 + }
1.177 +
1.178 + static final class Resource {
1.179 +
1.180 + final InputStream httpContent;
1.181 + final String httpType;
1.182 + final String httpPath;
1.183 + final String[] parameters;
1.184 +
1.185 + Resource(InputStream httpContent, String httpType, String httpPath,
1.186 + String[] parameters) {
1.187 + httpContent.mark(Integer.MAX_VALUE);
1.188 + this.httpContent = httpContent;
1.189 + this.httpType = httpType;
1.190 + this.httpPath = httpPath;
1.191 + this.parameters = parameters;
1.192 + }
1.193 + }
1.194 +
1.195 + static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
1.196 + for (;;) {
1.197 + int ch = is.read();
1.198 + if (ch == -1) {
1.199 + break;
1.200 + }
1.201 + if (ch == '$' && params.length > 0) {
1.202 + int cnt = is.read() - '0';
1.203 + if (baseURL != null && cnt == 'U' - '0') {
1.204 + os.write(baseURL.getBytes("UTF-8"));
1.205 + } else {
1.206 + if (cnt >= 0 && cnt < params.length) {
1.207 + os.write(params[cnt].getBytes("UTF-8"));
1.208 + } else {
1.209 + os.write('$');
1.210 + os.write(cnt + '0');
1.211 + }
1.212 + }
1.213 + } else {
1.214 + os.write(ch);
1.215 + }
1.216 + }
1.217 + }
1.218 +
1.219 + private static class WS extends WebSocketApplication {
1.220 + private final Resource r;
1.221 +
1.222 + private WS(Resource r) {
1.223 + this.r = r;
1.224 + }
1.225 +
1.226 + @Override
1.227 + public void onMessage(WebSocket socket, String text) {
1.228 + try {
1.229 + r.httpContent.reset();
1.230 + ByteArrayOutputStream out = new ByteArrayOutputStream();
1.231 + copyStream(r.httpContent, out, null, text);
1.232 + String s = new String(out.toByteArray(), "UTF-8");
1.233 + socket.send(s);
1.234 + } catch (IOException ex) {
1.235 + LOG.log(Level.WARNING, null, ex);
1.236 + }
1.237 + }
1.238 + private static final Logger LOG = Logger.getLogger(WS.class.getName());
1.239 +
1.240 + }
1.241 +}