blob: c92579d821f1dece6e444fab86680b85b3247d1a [file] [log] [blame]
Dave Borowitz9de65952012-08-13 16:09:45 -07001// Copyright 2012 Google Inc. All Rights Reserved.
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
15package com.google.gitiles;
16
17import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
18
19import com.google.common.base.Charsets;
20import com.google.common.collect.ImmutableMap;
21import com.google.common.collect.Maps;
22import com.google.common.net.HttpHeaders;
23
24import org.joda.time.Instant;
25
26import java.io.IOException;
27import java.util.Map;
28
29import javax.servlet.http.HttpServlet;
30import javax.servlet.http.HttpServletRequest;
31import javax.servlet.http.HttpServletResponse;
32
33/** Base servlet class for Gitiles servlets that serve Soy templates. */
34public abstract class BaseServlet extends HttpServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080035 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070036 private static final String DATA_ATTRIBUTE = BaseServlet.class.getName() + "/Data";
37
38 static void setNotCacheable(HttpServletResponse res) {
39 res.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
40 res.setHeader(HttpHeaders.PRAGMA, "no-cache");
41 res.setHeader(HttpHeaders.EXPIRES, "Fri, 01 Jan 1990 00:00:00 GMT");
42 res.setDateHeader(HttpHeaders.DATE, new Instant().getMillis());
43 }
44
45 public static BaseServlet notFoundServlet() {
46 return new BaseServlet(null) {
Chad Horohoead23f142012-11-12 09:45:39 -080047 private static final long serialVersionUID = 1L;
Dave Borowitz9de65952012-08-13 16:09:45 -070048 @Override
49 public void service(HttpServletRequest req, HttpServletResponse res) {
50 res.setStatus(SC_NOT_FOUND);
51 }
52 };
53 }
54
55 public static Map<String, String> menuEntry(String text, String url) {
56 if (url != null) {
57 return ImmutableMap.of("text", text, "url", url);
58 } else {
59 return ImmutableMap.of("text", text);
60 }
61 }
62
63 protected static Map<String, Object> getData(HttpServletRequest req) {
64 @SuppressWarnings("unchecked")
65 Map<String, Object> data = (Map<String, Object>) req.getAttribute(DATA_ATTRIBUTE);
66 if (data == null) {
67 data = Maps.newHashMap();
68 req.setAttribute(DATA_ATTRIBUTE, data);
69 }
70 return data;
71 }
72
73 protected final Renderer renderer;
74
75 protected BaseServlet(Renderer renderer) {
76 this.renderer = renderer;
77 }
78
79 /**
80 * Put a value into a request's Soy data map.
81 * <p>
82 * This method is intended to support a composition pattern whereby a
83 * {@link BaseServlet} is wrapped in a different {@link HttpServlet} that can
84 * update its data map.
85 *
86 * @param req in-progress request.
87 * @param key key.
88 * @param value Soy data value.
89 */
90 public void put(HttpServletRequest req, String key, Object value) {
91 getData(req).put(key, value);
92 }
93
94 protected void render(HttpServletRequest req, HttpServletResponse res, String templateName,
95 Map<String, ?> soyData) throws IOException {
96 try {
97 res.setContentType(FormatType.HTML.getMimeType());
98 res.setCharacterEncoding(Charsets.UTF_8.name());
99 setCacheHeaders(req, res);
100
101 Map<String, Object> allData = getData(req);
102 allData.putAll(soyData);
103 GitilesView view = ViewFilter.getView(req);
104 if (!allData.containsKey("repositoryName") && view.getRepositoryName() != null) {
105 allData.put("repositoryName", view.getRepositoryName());
106 }
107 if (!allData.containsKey("breadcrumbs")) {
108 allData.put("breadcrumbs", view.getBreadcrumbs());
109 }
110
111 res.setStatus(HttpServletResponse.SC_OK);
112 renderer.render(res, templateName, allData);
113 } finally {
114 req.removeAttribute(DATA_ATTRIBUTE);
115 }
116 }
117
118 protected void setCacheHeaders(HttpServletRequest req, HttpServletResponse res) {
119 setNotCacheable(res);
120 }
121}