Removing spaces at the end of lines and providing support for http.header substitution
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.launcher;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.Closeable;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InterruptedIOException;
27 import java.io.OutputStream;
28 import java.io.Reader;
29 import java.io.UnsupportedEncodingException;
30 import java.io.Writer;
31 import java.net.JarURLConnection;
33 import java.net.URISyntaxException;
35 import java.net.URLConnection;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Enumeration;
39 import java.util.HashSet;
40 import java.util.LinkedHashSet;
41 import java.util.List;
43 import java.util.Properties;
45 import java.util.concurrent.BlockingQueue;
46 import java.util.concurrent.Callable;
47 import java.util.concurrent.CountDownLatch;
48 import java.util.concurrent.LinkedBlockingQueue;
49 import java.util.concurrent.TimeUnit;
50 import java.util.jar.Attributes;
51 import java.util.jar.Manifest;
52 import java.util.logging.ConsoleHandler;
53 import java.util.logging.Level;
54 import java.util.logging.Logger;
55 import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
56 import org.glassfish.grizzly.PortRange;
57 import org.glassfish.grizzly.http.server.HttpHandler;
58 import org.glassfish.grizzly.http.server.HttpServer;
59 import org.glassfish.grizzly.http.server.NetworkListener;
60 import org.glassfish.grizzly.http.server.Request;
61 import org.glassfish.grizzly.http.server.Response;
62 import org.glassfish.grizzly.http.server.ServerConfiguration;
63 import org.glassfish.grizzly.http.server.StaticHttpHandler;
64 import org.glassfish.grizzly.http.util.Header;
65 import org.glassfish.grizzly.http.util.HttpStatus;
66 import org.glassfish.grizzly.websockets.WebSocket;
67 import org.glassfish.grizzly.websockets.WebSocketAddOn;
68 import org.glassfish.grizzly.websockets.WebSocketApplication;
69 import org.glassfish.grizzly.websockets.WebSocketEngine;
72 * Lightweight server to launch Bck2Brwsr applications and tests.
73 * Supports execution in native browser as well as Java's internal
76 abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable<HttpServer> {
77 static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName());
78 private static final InvocationContext END = new InvocationContext(null, null, null);
79 private final Set<ClassLoader> loaders = new LinkedHashSet<ClassLoader>();
80 private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<InvocationContext>();
82 private final Res resources = new Res();
83 private final String cmd;
84 private Object[] brwsr;
85 private HttpServer server;
86 private CountDownLatch wait;
88 public BaseHTTPLauncher(String cmd) {
90 addClassLoader(BaseHTTPLauncher.class.getClassLoader());
95 InvocationContext runMethod(InvocationContext c) throws IOException {
96 loaders.add(c.clazz.getClassLoader());
100 } catch (InterruptedException ex) {
101 throw new IOException(ex);
106 public void setTimeout(long ms) {
110 public void addClassLoader(ClassLoader url) {
111 this.loaders.add(url);
114 ClassLoader[] loaders() {
115 return loaders.toArray(new ClassLoader[loaders.size()]);
118 public void showURL(String startpage) throws IOException {
119 if (!startpage.startsWith("/")) {
120 startpage = "/" + startpage;
122 HttpServer s = initServer(".", true, "");
123 int last = startpage.lastIndexOf('/');
124 String prefix = startpage.substring(0, last);
125 String simpleName = startpage.substring(last);
126 s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
129 launchServerAndBrwsr(s, simpleName);
130 } catch (Exception ex) {
131 throw new IOException(ex);
135 void showDirectory(File dir, String startpage, boolean addClasses) throws IOException {
136 if (!startpage.startsWith("/")) {
137 startpage = "/" + startpage;
139 String prefix = null;
140 if (!new File(dir, "bck2brwsr.js").exists()) {
141 int last = startpage.lastIndexOf('/');
143 prefix = startpage.substring(0, last);
146 HttpServer s = initServer(dir.getPath(), addClasses, prefix);
148 launchServerAndBrwsr(s, startpage);
149 } catch (Exception ex) {
150 throw new IOException(ex);
155 public void initialize() throws IOException {
158 } catch (InterruptedException ex) {
159 final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
162 } catch (Exception ex) {
163 if (ex instanceof IOException) {
164 throw (IOException)ex;
166 if (ex instanceof RuntimeException) {
167 throw (RuntimeException)ex;
169 throw new IOException(ex);
173 private HttpServer initServer(String path, boolean addClasses, String vmPrefix) throws IOException {
174 HttpServer s = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
176 ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
177 setPoolName("Fx/Bck2 Brwsr").
180 ThreadPoolConfig oneKernel = ThreadPoolConfig.defaultConfig().copy().
181 setPoolName("Kernel Fx/Bck2").
184 for (NetworkListener nl : s.getListeners()) {
185 nl.getTransport().setWorkerThreadPoolConfig(fewThreads);
186 nl.getTransport().setKernelThreadPoolConfig(oneKernel);
189 final ServerConfiguration conf = s.getServerConfiguration();
190 VMAndPages vm = new VMAndPages();
191 conf.addHttpHandler(vm, "/");
192 if (vmPrefix != null) {
193 vm.registerVM(vmPrefix + "/bck2brwsr.js");
199 conf.addHttpHandler(new Classes(resources), "/classes/");
201 final WebSocketAddOn addon = new WebSocketAddOn();
202 for (NetworkListener listener : s.getListeners()) {
203 listener.registerAddOn(addon);
205 Logger l = Logger.getLogger("org.glassfish.grizzly.http.server.HttpHandler");
206 l.setLevel(Level.FINE);
207 l.setUseParentHandlers(false);
208 ConsoleHandler ch = new ConsoleHandler();
209 ch.setLevel(Level.ALL);
214 private static int resourcesCount;
215 private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
216 wait = new CountDownLatch(1);
217 server = initServer(".", true, "");
218 final ServerConfiguration conf = server.getServerConfiguration();
220 class DynamicResourceHandler extends HttpHandler {
221 private final InvocationContext ic;
222 DynamicResourceHandler delegate;
223 public DynamicResourceHandler(InvocationContext ic) {
225 for (Resource r : ic.resources) {
226 conf.addHttpHandler(this, r.httpPath);
230 public void close(DynamicResourceHandler del) {
231 conf.removeHttpHandler(this);
236 public void service(Request request, Response response) throws Exception {
237 if (delegate != null) {
238 delegate.service(request, response);
242 if ("/dynamic".equals(request.getRequestURI())) {
243 boolean webSocket = false;
244 String mimeType = request.getParameter("mimeType");
245 List<String> params = new ArrayList<String>();
246 for (int i = 0; ; i++) {
247 String p = request.getParameter("param" + i);
252 if ("protocol:ws".equals(p)) {
256 final String cnt = request.getParameter("content");
257 String mangle = cnt.replace("%20", " ").replace("%0A", "\n");
258 ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8"));
260 final Resource res = new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()]));
262 url = registerWebSocket(res);
264 url = registerResource(res);
266 response.setHeader(Header.CacheControl, "no-cache");
267 response.setHeader(Header.Pragma, "no-cache");
268 response.getWriter().write(url.toString());
269 response.getWriter().write("\n");
273 for (Resource r : ic.resources) {
274 if (r.httpPath.equals(request.getRequestURI())) {
275 LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
276 response.setContentType(r.httpType);
277 response.setHeader(Header.CacheControl, "no-cache");
278 response.setHeader(Header.Pragma, "no-cache");
279 r.httpContent.reset();
280 String[] params = null;
281 if (r.parameters.length != 0) {
282 params = new String[r.parameters.length];
283 for (int i = 0; i < r.parameters.length; i++) {
284 params[i] = request.getParameter(r.parameters[i]);
285 if (params[i] == null) {
286 if ("http.method".equals(r.parameters[i])) {
287 params[i] = request.getMethod().toString();
288 } else if ("http.requestBody".equals(r.parameters[i])) {
289 Reader rdr = request.getReader();
290 StringBuilder sb = new StringBuilder();
298 params[i] = sb.toString();
299 } else if (r.parameters[i].startsWith("http.header.")) {
300 params[i] = request.getHeader(r.parameters[i].substring(12));
303 if (params[i] == null) {
309 copyStream(r.httpContent, response.getOutputStream(), null, params);
314 private URI registerWebSocket(Resource r) {
315 WebSocketEngine.getEngine().register("", r.httpPath, new WS(r));
316 return pageURL("ws", server, r.httpPath);
319 private URI registerResource(Resource r) {
320 if (!ic.resources.contains(r)) {
322 conf.addHttpHandler(this, r.httpPath);
324 return pageURL("http", server, r.httpPath);
328 conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
330 conf.addHttpHandler(new HttpHandler() {
332 List<InvocationContext> cases = new ArrayList<InvocationContext>();
333 DynamicResourceHandler prev;
335 public void service(Request request, Response response) throws Exception {
336 String id = request.getParameter("request");
337 String value = request.getParameter("result");
338 if (value != null && value.indexOf((char)0xC5) != -1) {
339 value = toUTF8(value);
343 InvocationContext mi = null;
346 if (id != null && value != null) {
347 LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
348 value = decodeURL(value);
349 int indx = Integer.parseInt(id);
350 cases.get(indx).result(value, null);
351 if (++indx < cases.size()) {
352 mi = cases.get(indx);
353 LOG.log(Level.INFO, "Re-executing case {0}", indx);
357 if (!cases.isEmpty()) {
358 LOG.info("Re-executing test cases");
369 response.getWriter().write("");
372 LOG.log(Level.INFO, "End of data reached. Exiting.");
375 final DynamicResourceHandler newRH = new DynamicResourceHandler(mi);
380 conf.addHttpHandler(prev, "/dynamic");
383 final String cn = mi.clazz.getName();
384 final String mn = mi.methodName;
385 LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
386 response.getWriter().write("{"
387 + "className: '" + cn + "', "
388 + "methodName: '" + mn + "', "
389 + "request: " + caseNmbr
391 if (mi.html != null) {
392 response.getWriter().write(", html: '");
393 response.getWriter().write(encodeJSON(mi.html));
394 response.getWriter().write("'");
396 response.getWriter().write("}");
400 this.brwsr = launchServerAndBrwsr(server, "/execute");
403 private static String encodeJSON(String in) {
404 StringBuilder sb = new StringBuilder();
405 for (int i = 0; i < in.length(); i++) {
406 char ch = in.charAt(i);
407 if (ch < 32 || ch == '\'' || ch == '"') {
409 String hs = "0000" + Integer.toHexString(ch);
410 hs = hs.substring(hs.length() - 4);
416 return sb.toString();
420 public void shutdown() throws IOException {
423 int prev = methods.size();
425 if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
428 } catch (InterruptedException ex) {
429 throw new IOException(ex);
431 if (prev == methods.size()) {
434 "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
440 "Timeout, but tests got from {0} to {1}. Trying again.",
441 new Object[]{prev, methods.size()}
444 stopServerAndBrwsr(server, brwsr);
447 static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
453 if (ch == '$' && params.length > 0) {
454 int cnt = is.read() - '0';
455 if (baseURL != null && cnt == 'U' - '0') {
456 os.write(baseURL.getBytes("UTF-8"));
458 if (cnt >= 0 && cnt < params.length) {
459 os.write(params[cnt].getBytes("UTF-8"));
471 private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
473 URI uri = pageURL("http", server, page);
474 return showBrwsr(uri);
476 private static String toUTF8(String value) throws UnsupportedEncodingException {
477 byte[] arr = new byte[value.length()];
478 for (int i = 0; i < arr.length; i++) {
479 arr[i] = (byte)value.charAt(i);
481 return new String(arr, "UTF-8");
484 private static String decodeURL(String s) {
486 int pos = s.indexOf('%');
490 int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
491 s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
495 private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
499 Process process = (Process)brwsr[0];
502 InputStream stdout = process.getInputStream();
503 InputStream stderr = process.getErrorStream();
504 drain("StdOut", stdout);
505 drain("StdErr", stderr);
509 res = process.waitFor();
510 } catch (InterruptedException ex) {
511 throw new IOException(ex);
513 LOG.log(Level.INFO, "Exit code: {0}", res);
515 deleteTree((File)brwsr[1]);
518 private static void drain(String name, InputStream is) throws IOException {
519 int av = is.available();
521 StringBuilder sb = new StringBuilder();
522 sb.append("v== ").append(name).append(" ==v\n");
524 sb.append((char)is.read());
526 sb.append("\n^== ").append(name).append(" ==^");
527 LOG.log(Level.INFO, sb.toString());
531 private void deleteTree(File file) {
535 File[] arr = file.listFiles();
545 public HttpServer call() throws Exception {
550 public void close() throws IOException {
554 protected Object[] showBrwsr(URI uri) throws IOException {
555 LOG.log(Level.INFO, "Showing {0}", uri);
558 LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
559 System.getProperty("java.vm.name"),
560 System.getProperty("java.vm.vendor"),
561 System.getProperty("java.vm.version"),
563 java.awt.Desktop.getDesktop().browse(uri);
564 LOG.log(Level.INFO, "Desktop.browse successfully finished");
566 } catch (UnsupportedOperationException ex) {
567 LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
568 LOG.log(Level.FINE, null, ex);
569 } catch (IOException ex) {
570 LOG.log(Level.INFO, "Desktop.browse failed: {0}", ex.getMessage());
571 LOG.log(Level.FINE, null, ex);
575 String cmdName = cmd == null ? "xdg-open" : cmd;
577 cmdName, uri.toString()
579 LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
580 final Process process = Runtime.getRuntime().exec(cmdArr);
581 return new Object[] { process, null };
585 abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
586 abstract String harnessResource();
587 Object compileJar(URL jar, URL precompiled) throws IOException {
590 String compileFromClassPath(URL f, Res loader) throws IOException {
594 private static URI pageURL(String protocol, HttpServer server, final String page) {
595 NetworkListener listener = server.getListeners().iterator().next();
596 int port = listener.getPort();
598 return new URI(protocol + "://localhost:" + port + page);
599 } catch (URISyntaxException ex) {
600 throw new IllegalStateException(ex);
605 private final Set<URL> ignore = new HashSet<URL>();
607 Object compileJar(URL jarURL) throws IOException {
608 List<String[]> libraries = new ArrayList<String[]>();
609 for (ClassLoader loader : loaders) {
610 Enumeration<URL> en = loader.getResources("META-INF/MANIFEST.MF");
611 while (en.hasMoreElements()) {
612 URL e = en.nextElement();
613 Manifest mf = new Manifest(e.openStream());
614 for (Map.Entry<String, Attributes> entrySet : mf.getEntries().entrySet()) {
615 String key = entrySet.getKey();
616 Attributes attr = entrySet.getValue();
618 final String a = attr.getValue("Bck2BrwsrArtifactId");
619 final String g = attr.getValue("Bck2BrwsrGroupId");
620 final String v = attr.getValue("Bck2BrwsrVersion");
621 final String d = attr.getValue("Bck2BrwsrDebug");
623 if (g != null && a != null && v != null && "true".equals(d)) {
624 libraries.add(new String[] {
631 URL precompiled = null;
632 for (ClassLoader loader : loaders) {
633 for (String[] lib : libraries) {
634 final String res = "META-INF/maven/" + lib[1] + "/" + lib[0] + "/pom.properties";
635 URL props = loader.getResource(res);
637 URLConnection c = props.openConnection();
638 Properties load = new Properties();
639 final InputStream is = c.getInputStream();
642 if (lib[2].equals(load.getProperty("version"))) {
643 if (c instanceof JarURLConnection) {
644 final URL definedInURL = ((JarURLConnection)c).getJarFileURL();
645 if (definedInURL.equals(jarURL)) {
646 precompiled = loader.getResource(lib[3]);
653 Object ret = BaseHTTPLauncher.this.compileJar(jarURL, precompiled);
657 String compileFromClassPath(URL f) throws IOException {
658 return BaseHTTPLauncher.this.compileFromClassPath(f, this);
660 public URL get(String resource, int skip) throws IOException {
661 if (!resource.endsWith(".class")) {
662 return getResource(resource, skip);
665 for (ClassLoader l : loaders) {
666 Enumeration<URL> en = l.getResources(resource);
667 while (en.hasMoreElements()) {
668 u = en.nextElement();
669 if (u.toExternalForm().matches("^.*emul.*rt\\.jar.*$")) {
675 if (u.toExternalForm().contains("rt.jar")) {
676 LOG.log(Level.WARNING, "No fallback to bootclasspath for {0}", u);
681 throw new IOException("Can't find " + resource);
683 private URL getResource(String resource, int skip) throws IOException {
684 for (ClassLoader l : loaders) {
685 Enumeration<URL> en = l.getResources(resource);
686 while (en.hasMoreElements()) {
687 final URL now = en.nextElement();
688 if (now.toExternalForm().contains("sisu-inject-bean")) {
689 // certainly we don't want this resource, as that
690 // module is not compiled with target 1.6, currently
693 if (now.getProtocol().equals("jar")) {
694 JarURLConnection juc = (JarURLConnection) now.openConnection();
695 if (now.getFile().endsWith(".class") && ignore.contains(juc.getJarFileURL())) {
704 throw new IOException("Not found (anymore of) " + resource);
708 private static class Page extends HttpHandler {
709 final String resource;
710 private final String[] args;
711 private final Res res;
713 public Page(Res res, String resource, String... args) {
715 this.resource = resource;
716 this.args = args.length == 0 ? new String[] { "$0" } : args;
720 public void service(Request request, Response response) throws Exception {
721 String r = computePage(request);
722 if (r.startsWith("/")) {
725 String[] replace = {};
726 if (r.endsWith(".html")) {
727 response.setContentType("text/html");
728 LOG.info("Content type text/html");
731 if (r.endsWith(".xhtml")) {
732 response.setContentType("application/xhtml+xml");
733 LOG.info("Content type application/xhtml+xml");
736 OutputStream os = response.getOutputStream();
738 InputStream is = res.get(r, 0).openStream();
739 copyStream(is, os, request.getRequestURL().toString(), replace);
740 } catch (IOException ex) {
741 response.setDetailMessage(ex.getLocalizedMessage());
743 response.setStatus(404);
747 protected String computePage(Request request) {
750 r = request.getHttpHandlerPath();
756 private static class SubTree extends Page {
758 public SubTree(Res res, String resource, String... args) {
759 super(res, resource, args);
763 protected String computePage(Request request) {
764 return resource + request.getHttpHandlerPath();
770 private class VMAndPages extends StaticHttpHandler {
771 private String vmResource;
773 public VMAndPages() {
774 super((String[]) null);
778 public void service(Request request, Response response) throws Exception {
779 if (request.getRequestURI().equals(vmResource)) {
780 response.setCharacterEncoding("UTF-8");
781 response.setContentType("text/javascript");
782 StringBuilder sb = new StringBuilder();
783 generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
784 response.getWriter().write(sb.toString());
786 super.service(request, response);
790 private void registerVM(String vmResource) {
791 this.vmResource = vmResource;
795 private static class Classes extends HttpHandler {
796 private final Res loader;
798 public Classes(Res loader) {
799 this.loader = loader;
803 public void service(Request request, Response response) throws Exception {
804 String res = request.getHttpHandlerPath();
805 if (res.startsWith("/")) {
806 res = res.substring(1);
808 String skip = request.getParameter("skip");
809 int skipCnt = skip == null ? 0 : Integer.parseInt(skip);
810 URL url = loader.get(res, skipCnt);
811 if (url != null && !res.equals("META-INF/MANIFEST.MF")) try {
812 response.setCharacterEncoding("UTF-8");
813 if (url.getProtocol().equals("jar")) {
814 JarURLConnection juc = (JarURLConnection) url.openConnection();
817 s = loader.compileJar(juc.getJarFileURL());
818 } catch (IOException iOException) {
819 throw new IOException("Can't compile " + url.toExternalForm(), iOException);
821 if (s instanceof String) {
822 Writer w = response.getWriter();
827 if (s instanceof InputStream) {
828 copyStream((InputStream) s, response.getOutputStream(), null);
832 if (url.getProtocol().equals("file")) {
833 final String filePart = url.getFile();
834 if (filePart.endsWith(res)) {
839 filePart.substring(0, filePart.length() - res.length())
842 String s = loader.compileFromClassPath(url);
844 Writer w = response.getWriter();
850 } catch (IOException ex) {
851 LOG.log(Level.SEVERE, "Cannot handle " + res, ex);
853 InputStream is = null;
856 throw new IOException("Resource not found");
858 is = url.openStream();
859 response.setContentType("text/javascript");
860 Writer w = response.getWriter();
862 for (int i = 0;; i++) {
876 w.append(Integer.toString(b));
879 } catch (IOException ex) {
880 response.setStatus(HttpStatus.NOT_FOUND_404);
882 response.setDetailMessage(ex.getMessage());
891 private static class WS extends WebSocketApplication {
893 private final Resource r;
895 private WS(Resource r) {
900 public void onMessage(WebSocket socket, String text) {
902 r.httpContent.reset();
903 ByteArrayOutputStream out = new ByteArrayOutputStream();
904 copyStream(r.httpContent, out, null, text);
905 String s = new String(out.toByteArray(), "UTF-8");
907 } catch (IOException ex) {
908 LOG.log(Level.WARNING, null, ex);