| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import com.google.common.base.Charsets; |
| 21 | import com.google.common.base.Function; |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 22 | import com.google.common.collect.FluentIterable; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Iterables; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Maps; |
| 27 | import com.google.template.soy.tofu.SoyTofu; |
| 28 | |
| 29 | import java.io.File; |
| 30 | import java.io.IOException; |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 31 | import java.io.OutputStream; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 32 | import java.net.MalformedURLException; |
| 33 | import java.net.URL; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | |
| 37 | import javax.servlet.http.HttpServletResponse; |
| 38 | |
| Dave Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame^] | 39 | /** |
| 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 45 | public abstract class Renderer { |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 46 | // Must match .streamingPlaceholder. |
| 47 | private static final String PLACEHOLDER = "id=\"STREAMED_OUTPUT_BLOCK\""; |
| 48 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 49 | private static final List<String> SOY_FILENAMES = ImmutableList.of( |
| Dave Borowitz | 6ec0c87 | 2014-01-29 13:59:37 -0800 | [diff] [blame] | 50 | "BlameDetail.soy", |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 51 | "Common.soy", |
| 52 | "DiffDetail.soy", |
| 53 | "HostIndex.soy", |
| 54 | "LogDetail.soy", |
| 55 | "ObjectDetail.soy", |
| 56 | "PathDetail.soy", |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 57 | "RefList.soy", |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 58 | "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 Borowitz | ddc0efc | 2014-04-27 19:12:47 -0600 | [diff] [blame] | 63 | "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 64 | |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 65 | protected static class FileUrlMapper implements Function<String, URL> { |
| 66 | private final String prefix; |
| 67 | |
| 68 | protected FileUrlMapper() { |
| 69 | this(""); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 70 | } |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 71 | |
| 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 86 | } |
| 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 Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 93 | String staticPrefix, Iterable<URL> customTemplates, String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 94 | checkNotNull(staticPrefix, "staticPrefix"); |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 95 | Iterable<URL> allTemplates = FluentIterable.from(SOY_FILENAMES).transform(resourceMapper); |
| 96 | templates = ImmutableList.copyOf(Iterables.concat(allTemplates, customTemplates)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 97 | |
| 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 Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 102 | allGlobals.put("gitiles.SITE_TITLE", siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 103 | allGlobals.putAll(globals); |
| 104 | this.globals = ImmutableMap.copyOf(allGlobals); |
| 105 | } |
| 106 | |
| Dave Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame^] | 107 | void render(HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 108 | 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 Borowitz | fc775ad | 2014-07-30 11:38:53 -0700 | [diff] [blame^] | 116 | OutputStream renderStreaming(HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 117 | throws IOException { |
| Dave Borowitz | fc2f00a | 2014-07-29 17:34:43 -0700 | [diff] [blame] | 118 | 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 Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 159 | SoyTofu.Renderer newRenderer(String templateName) { |
| 160 | return getTofu().newRenderer(templateName); |
| 161 | } |
| 162 | |
| 163 | protected abstract SoyTofu getTofu(); |
| 164 | } |