blob: 6965b811ab3bf20fa7536830178680bb8143d849 [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
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070017import static com.google.common.base.Preconditions.checkArgument;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import static com.google.common.base.Preconditions.checkNotNull;
19
20import com.google.common.base.Charsets;
21import com.google.common.base.Function;
Dave Borowitz76bbefd2014-03-11 16:57:45 -070022import com.google.common.collect.FluentIterable;
Dave Borowitz9de65952012-08-13 16:09:45 -070023import com.google.common.collect.ImmutableList;
24import com.google.common.collect.ImmutableMap;
Dave Borowitz76bbefd2014-03-11 16:57:45 -070025import com.google.common.collect.Iterables;
Dave Borowitz9de65952012-08-13 16:09:45 -070026import com.google.common.collect.Maps;
27import com.google.template.soy.tofu.SoyTofu;
28
29import java.io.File;
30import java.io.IOException;
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070031import java.io.OutputStream;
Dave Borowitz9de65952012-08-13 16:09:45 -070032import java.net.MalformedURLException;
33import java.net.URL;
34import java.util.List;
35import java.util.Map;
36
37import javax.servlet.http.HttpServletResponse;
38
Dave Borowitzfc775ad2014-07-30 11:38:53 -070039/**
40 * Renderer for Soy templates used by Gitiles.
41 * <p>
42 * Most callers should not use the methods in this class directly, and instead
43 * use one of the HTML methods in {@link BaseServlet}.
44 */
Dave Borowitz9de65952012-08-13 16:09:45 -070045public abstract class Renderer {
Dave Borowitzfc2f00a2014-07-29 17:34:43 -070046 // Must match .streamingPlaceholder.
47 private static final String PLACEHOLDER = "id=\"STREAMED_OUTPUT_BLOCK\"";
48
Dave Borowitz9de65952012-08-13 16:09:45 -070049 private static final List<String> SOY_FILENAMES = ImmutableList.of(
Dave Borowitz6ec0c872014-01-29 13:59:37 -080050 "BlameDetail.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070051 "Common.soy",
52 "DiffDetail.soy",
53 "HostIndex.soy",
54 "LogDetail.soy",
55 "ObjectDetail.soy",
56 "PathDetail.soy",
Dave Borowitz209d0aa2012-12-28 14:28:53 -080057 "RefList.soy",
Dave Borowitz9de65952012-08-13 16:09:45 -070058 "RevisionDetail.soy",
59 "RepositoryIndex.soy");
60
61 public static final Map<String, String> STATIC_URL_GLOBALS = ImmutableMap.of(
62 "gitiles.CSS_URL", "gitiles.css",
Dave Borowitzddc0efc2014-04-27 19:12:47 -060063 "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css");
Dave Borowitz9de65952012-08-13 16:09:45 -070064
Dave Borowitz76bbefd2014-03-11 16:57:45 -070065 protected static class FileUrlMapper implements Function<String, URL> {
66 private final String prefix;
67
68 protected FileUrlMapper() {
69 this("");
Dave Borowitz9de65952012-08-13 16:09:45 -070070 }
Dave Borowitz76bbefd2014-03-11 16:57:45 -070071
72 protected FileUrlMapper(String prefix) {
73 this.prefix = checkNotNull(prefix, "prefix");
74 }
75
76 @Override
77 public URL apply(String filename) {
78 if (filename == null) {
79 return null;
80 }
81 try {
82 return new File(prefix + filename).toURI().toURL();
83 } catch (MalformedURLException e) {
84 throw new IllegalArgumentException(e);
85 }
Dave Borowitz9de65952012-08-13 16:09:45 -070086 }
87 }
88
89 protected ImmutableList<URL> templates;
90 protected ImmutableMap<String, String> globals;
91
92 protected Renderer(Function<String, URL> resourceMapper, Map<String, String> globals,
Dave Borowitz76bbefd2014-03-11 16:57:45 -070093 String staticPrefix, Iterable<URL> customTemplates, String siteTitle) {
Dave Borowitz9de65952012-08-13 16:09:45 -070094 checkNotNull(staticPrefix, "staticPrefix");
Dave Borowitz76bbefd2014-03-11 16:57:45 -070095 Iterable<URL> allTemplates = FluentIterable.from(SOY_FILENAMES).transform(resourceMapper);
96 templates = ImmutableList.copyOf(Iterables.concat(allTemplates, customTemplates));
Dave Borowitz9de65952012-08-13 16:09:45 -070097
98 Map<String, String> allGlobals = Maps.newHashMap();
99 for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) {
100 allGlobals.put(e.getKey(), staticPrefix + e.getValue());
101 }
Chad Horohoe2a28d622012-11-12 11:56:59 -0800102 allGlobals.put("gitiles.SITE_TITLE", siteTitle);
Dave Borowitz9de65952012-08-13 16:09:45 -0700103 allGlobals.putAll(globals);
104 this.globals = ImmutableMap.copyOf(allGlobals);
105 }
106
Dave Borowitzfc775ad2014-07-30 11:38:53 -0700107 void render(HttpServletResponse res, String templateName, Map<String, ?> soyData)
Dave Borowitz9de65952012-08-13 16:09:45 -0700108 throws IOException {
109 res.setContentType("text/html");
110 res.setCharacterEncoding("UTF-8");
111 byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(Charsets.UTF_8);
112 res.setContentLength(data.length);
113 res.getOutputStream().write(data);
114 }
115
Dave Borowitzfc775ad2014-07-30 11:38:53 -0700116 OutputStream renderStreaming(HttpServletResponse res, String templateName, Map<String, ?> soyData)
117 throws IOException {
Dave Borowitzfc2f00a2014-07-29 17:34:43 -0700118 final String html = newRenderer(templateName)
119 .setData(soyData)
120 .render();
121 int id = html.indexOf(PLACEHOLDER);
122 checkArgument(id >= 0, "Template must contain %s", PLACEHOLDER);
123
124 int lt = html.lastIndexOf('<', id);
125 final int gt = html.indexOf('>', id + PLACEHOLDER.length());
126 final OutputStream out = res.getOutputStream();
127 out.write(html.substring(0, lt).getBytes(Charsets.UTF_8));
128 out.flush();
129
130 return new OutputStream() {
131 @Override
132 public void write(byte[] b) throws IOException {
133 out.write(b);
134 }
135
136 @Override
137 public void write(byte[] b, int off, int len) throws IOException {
138 out.write(b, off, len);
139 }
140
141 @Override
142 public void write(int b) throws IOException {
143 out.write(b);
144 }
145
146 @Override
147 public void flush() throws IOException {
148 out.flush();
149 }
150
151 @Override
152 public void close() throws IOException {
153 out.write(html.substring(gt + 1).getBytes(Charsets.UTF_8));
154 out.close();
155 }
156 };
157 }
158
Dave Borowitz9de65952012-08-13 16:09:45 -0700159 SoyTofu.Renderer newRenderer(String templateName) {
160 return getTofu().newRenderer(templateName);
161 }
162
163 protected abstract SoyTofu getTofu();
164}