private void sendSourceMap( String moduleName, HttpServletRequest request, HttpServletResponse response) throws IOException { long startTime = System.currentTimeMillis(); ModuleState moduleState = modules.get(moduleName); File sourceMap = moduleState.findSourceMap(); // Stream the file, substituting the sourceroot variable with the filename. // (This is more efficient than parsing the file as JSON.) // We need to do this at runtime because we don't know what the hostname will be // until we get a request. (For example, some people run the Code Server behind // a reverse proxy to support https.) String sourceRoot = String.format( "http://%s:%d/sourcemaps/%s/", request.getServerName(), request.getServerPort(), moduleName); PageUtil.sendTemplateFile( "application/json", sourceMap, "\"" + SOURCEROOT_TEMPLATE_VARIABLE + "\"", "\"" + sourceRoot + "\"", response); long elapsedTime = System.currentTimeMillis() - startTime; logger.log( TreeLogger.WARN, "sent source map for module '" + moduleName + "' in " + elapsedTime + " ms"); }
/** * Sends an HTTP response containing Java source rendered as HTML. The lines of source that have * corresponding JavaScript will be highlighted (as determined by reading the source map). */ private void sendSourceFileAsHtml( String moduleName, String sourcePath, BufferedReader lines, HttpServletResponse response) throws IOException { ReverseSourceMap sourceMap = ReverseSourceMap.load(logger, modules.get(moduleName)); File sourceFile = new File(sourcePath); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("text/html"); HtmlWriter out = new HtmlWriter(response.getWriter()); out.startTag("html").nl(); out.startTag("head").nl(); out.startTag("title").text(sourceFile.getName() + " (GWT Code Server)").endTag("title").nl(); out.startTag("style").nl(); out.text(".unused { color: grey; }").nl(); out.text(".used { color: black; }").nl(); out.text(".title { margin-top: 0; }").nl(); out.endTag("style").nl(); out.endTag("head").nl(); out.startTag("body").nl(); out.startTag("a", "href=", ".").text(sourceFile.getParent()).endTag("a").nl(); out.startTag("h1", "class=", "title").text(sourceFile.getName()).endTag("h1").nl(); out.startTag("pre", "class=", "unused").nl(); try { int lineNumber = 1; for (String line = lines.readLine(); line != null; line = lines.readLine()) { if (sourceMap.appearsInJavaScript(sourcePath, lineNumber)) { out.startTag("span", "class=", "used").text(line).endTag("span").nl(); } else { out.text(line).nl(); } lineNumber++; } } finally { lines.close(); } out.endTag("pre").nl(); out.endTag("body").nl(); out.endTag("html").nl(); }
/** * Sends an HTTP response containing a Java source. It will be sent as plain text by default, or * as HTML if the query string is equal to "html". */ private void sendSourceFile( String moduleName, String sourcePath, String query, HttpServletResponse response) throws IOException { ModuleState moduleState = modules.get(moduleName); InputStream pageBytes = moduleState.openSourceFile(sourcePath); if (pageBytes == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); logger.log(TreeLogger.WARN, "unknown source file: " + sourcePath); return; } if (query != null && query.equals("html")) { BufferedReader reader = new BufferedReader(new InputStreamReader(pageBytes)); sendSourceFileAsHtml(moduleName, sourcePath, reader, response); } else { PageUtil.sendStream("text/plain", pageBytes, response); } }
private SourceMap loadSourceMap(String moduleName) { ModuleState moduleState = modules.get(moduleName); return SourceMap.load(moduleState.findSourceMap()); }