Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd.raw; 16 : 17 : import com.google.gerrit.httpd.HtmlDomUtil; 18 : import com.google.gerrit.util.http.CacheHeaders; 19 : import com.google.gerrit.util.http.RequestUtil; 20 : import com.google.inject.Inject; 21 : import com.google.inject.Singleton; 22 : import java.io.FileNotFoundException; 23 : import java.io.IOException; 24 : import java.io.OutputStream; 25 : import javax.servlet.http.HttpServlet; 26 : import javax.servlet.http.HttpServletRequest; 27 : import javax.servlet.http.HttpServletResponse; 28 : 29 : /** 30 : * Redirects from {@code /Gerrit#foo} to {@code /#foo} in JavaScript. 31 : * 32 : * <p>This redirect exists to convert the older /Gerrit URL into the more modern URL format which 33 : * does not use a servlet name for the host page. We cannot do the redirect here in the server side, 34 : * as it would lose any history token that appears in the URL. Instead we send an HTML page which 35 : * instructs the browser to replace the URL, but preserve the history token. 36 : */ 37 : @Singleton 38 : public class LegacyGerritServlet extends HttpServlet { 39 : private static final long serialVersionUID = 1L; 40 : 41 : private final byte[] raw; 42 : private final byte[] compressed; 43 : 44 : @Inject 45 0 : LegacyGerritServlet() throws IOException { 46 0 : final String pageName = "LegacyGerrit.html"; 47 0 : final String doc = HtmlDomUtil.readFile(getClass(), pageName); 48 0 : if (doc == null) { 49 0 : throw new FileNotFoundException("No " + pageName + " in webapp"); 50 : } 51 : 52 0 : raw = doc.getBytes(HtmlDomUtil.ENC); 53 0 : compressed = HtmlDomUtil.compress(raw); 54 0 : } 55 : 56 : @Override 57 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 58 : final byte[] tosend; 59 0 : if (RequestUtil.acceptsGzipEncoding(req)) { 60 0 : rsp.setHeader("Content-Encoding", "gzip"); 61 0 : tosend = compressed; 62 : } else { 63 0 : tosend = raw; 64 : } 65 : 66 0 : CacheHeaders.setNotCacheable(rsp); 67 0 : rsp.setContentType("text/html"); 68 0 : rsp.setCharacterEncoding(HtmlDomUtil.ENC.name()); 69 0 : rsp.setContentLength(tosend.length); 70 0 : try (OutputStream out = rsp.getOutputStream()) { 71 0 : out.write(tosend); 72 : } 73 0 : } 74 : }