summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2009-07-18 16:33:48 -0700
committerShawn O. Pearce <sop@google.com>2009-07-18 17:07:12 -0700
commita5317ec8b477f9ad4a09a958e42d5180349b22f0 (patch)
tree0ea56a0253c575697bed1c7295dfc9255f63ce50
parent56fc9e3d951b0886c4781a5c8623dbc3da824f30 (diff)
Load Google prettify JavaScript into client
We now load the Google prettify JavaScript code into the browser as part of the Gerrit bootstrap. The script is created with a defer tag, so it can load in parallel with other page resources. We also use a custom servlet to send all of the JavaScript files combined as one stream, with a very long cache time of 1 year. This should permit browsers to only load the highlighting library once very few months, as it is somewhat large (~30 KiB). Bug: GERRIT-228 Signed-off-by: Shawn O. Pearce <sop@google.com>
-rw-r--r--src/main/java/com/google/gerrit/Gerrit.gwt.xml1
-rw-r--r--src/main/java/com/google/gerrit/server/PrettifyServlet.java87
-rw-r--r--src/main/webapp/WEB-INF/Gerrit.html1
-rw-r--r--src/main/webapp/WEB-INF/web.xml10
4 files changed, 99 insertions, 0 deletions
diff --git a/src/main/java/com/google/gerrit/Gerrit.gwt.xml b/src/main/java/com/google/gerrit/Gerrit.gwt.xml
index d31b894e15..ca73692464 100644
--- a/src/main/java/com/google/gerrit/Gerrit.gwt.xml
+++ b/src/main/java/com/google/gerrit/Gerrit.gwt.xml
@@ -26,6 +26,7 @@
<inherits name='com.google.gerrit.UserAgent'/>
<inherits name='org.spearce.jgit.JGit'/>
<stylesheet src='gerrit.css' />
+ <stylesheet src='prettify20090521/prettify.css' />
<extend-property name="locale" values="en"/>
<entry-point class='com.google.gerrit.client.Gerrit'/>
diff --git a/src/main/java/com/google/gerrit/server/PrettifyServlet.java b/src/main/java/com/google/gerrit/server/PrettifyServlet.java
new file mode 100644
index 0000000000..1ba5a9e03b
--- /dev/null
+++ b/src/main/java/com/google/gerrit/server/PrettifyServlet.java
@@ -0,0 +1,87 @@
+// Copyright (C) 2009 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.server;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public class PrettifyServlet extends HttpServlet {
+ private static final String VERSION = "20090521";
+
+ private byte[] content;
+
+ @Override
+ public void init(ServletConfig config) throws ServletException {
+ super.init(config);
+
+ final String myDir = "/gerrit/prettify" + VERSION + "/";
+ final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ load(buffer, myDir + "prettify.js");
+ for (Object p : getServletContext().getResourcePaths(myDir)) {
+ String name = (String) p;
+ if (name.startsWith(myDir + "lang-") && name.endsWith(".js")) {
+ load(buffer, name);
+ }
+ }
+ content = buffer.toByteArray();
+ }
+
+ private void load(final OutputStream buffer, final String path) {
+ final InputStream in = getServletContext().getResourceAsStream(path);
+ if (in != null) {
+ try {
+ final byte[] tmp = new byte[4096];
+ int cnt;
+ while ((cnt = in.read(tmp)) > 0) {
+ buffer.write(tmp, 0, cnt);
+ }
+ buffer.write(';');
+ buffer.write('\n');
+ in.close();
+ } catch (IOException e) {
+ getServletContext().log("Cannot read " + path, e);
+ }
+ }
+ }
+
+ @Override
+ protected void doGet(final HttpServletRequest req,
+ final HttpServletResponse rsp) throws IOException {
+ final String want = req.getPathInfo();
+ if (want.equals("/" + VERSION + ".js")) {
+ final long now = System.currentTimeMillis();
+ rsp.setHeader("Cache-Control", "max-age=31536000,public");
+ rsp.setDateHeader("Expires", now + 31536000000L);
+ rsp.setDateHeader("Date", now);
+ rsp.setContentType("application/x-javascript");
+ rsp.setContentLength(content.length);
+ rsp.getOutputStream().write(content);
+ } else {
+ rsp.setHeader("Expires", "Fri, 01 Jan 1980 00:00:00 GMT");
+ rsp.setHeader("Pragma", "no-cache");
+ rsp.setHeader("Cache-Control", "no-cache, must-revalidate");
+ rsp.setDateHeader("Date", System.currentTimeMillis());
+ rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
+ }
+ }
+}
diff --git a/src/main/webapp/WEB-INF/Gerrit.html b/src/main/webapp/WEB-INF/Gerrit.html
index 424cbb02a0..7ef5731777 100644
--- a/src/main/webapp/WEB-INF/Gerrit.html
+++ b/src/main/webapp/WEB-INF/Gerrit.html
@@ -5,6 +5,7 @@
<script id="gerrit_gerritconfig"></script>
<script id="gerrit_myaccount"></script>
<script id="gerrit_module" type="text/javascript" language="javascript" src="gerrit/gerrit.nocache.js"></script>
+ <script src="prettify/20090521.js" type="text/javascript" language="javascript" defer="true"></script>
<style id="gerrit_sitecss" type="text/css"></style>
<link rel="icon" type="image/gif" href="favicon.ico" />
</head>
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml
index 0b6fdbd2a1..134f918df5 100644
--- a/src/main/webapp/WEB-INF/web.xml
+++ b/src/main/webapp/WEB-INF/web.xml
@@ -39,6 +39,16 @@
</servlet-mapping>
<servlet>
+ <servlet-name>Prettify</servlet-name>
+ <servlet-class>com.google.gerrit.server.PrettifyServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>Prettify</servlet-name>
+ <url-pattern>/prettify/*</url-pattern>
+ </servlet-mapping>
+
+ <servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.google.gerrit.server.OpenIdLoginServlet</servlet-class>
<load-on-startup>1</load-on-startup>