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>
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
<inherits name='com.google.gerrit.UserAgent'/>
|
<inherits name='com.google.gerrit.UserAgent'/>
|
||||||
<inherits name='org.spearce.jgit.JGit'/>
|
<inherits name='org.spearce.jgit.JGit'/>
|
||||||
<stylesheet src='gerrit.css' />
|
<stylesheet src='gerrit.css' />
|
||||||
|
<stylesheet src='prettify20090521/prettify.css' />
|
||||||
<extend-property name="locale" values="en"/>
|
<extend-property name="locale" values="en"/>
|
||||||
|
|
||||||
<entry-point class='com.google.gerrit.client.Gerrit'/>
|
<entry-point class='com.google.gerrit.client.Gerrit'/>
|
||||||
|
87
src/main/java/com/google/gerrit/server/PrettifyServlet.java
Normal file
87
src/main/java/com/google/gerrit/server/PrettifyServlet.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -5,6 +5,7 @@
|
|||||||
<script id="gerrit_gerritconfig"></script>
|
<script id="gerrit_gerritconfig"></script>
|
||||||
<script id="gerrit_myaccount"></script>
|
<script id="gerrit_myaccount"></script>
|
||||||
<script id="gerrit_module" type="text/javascript" language="javascript" src="gerrit/gerrit.nocache.js"></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>
|
<style id="gerrit_sitecss" type="text/css"></style>
|
||||||
<link rel="icon" type="image/gif" href="favicon.ico" />
|
<link rel="icon" type="image/gif" href="favicon.ico" />
|
||||||
</head>
|
</head>
|
||||||
|
@@ -38,6 +38,16 @@
|
|||||||
<url-pattern>/Gerrit/*</url-pattern>
|
<url-pattern>/Gerrit/*</url-pattern>
|
||||||
</servlet-mapping>
|
</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>
|
||||||
<servlet-name>Login</servlet-name>
|
<servlet-name>Login</servlet-name>
|
||||||
<servlet-class>com.google.gerrit.server.OpenIdLoginServlet</servlet-class>
|
<servlet-class>com.google.gerrit.server.OpenIdLoginServlet</servlet-class>
|
||||||
|
Reference in New Issue
Block a user