| 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 | |
| 17 | import static com.google.common.base.Preconditions.checkNotNull; |
| 18 | |
| 19 | import com.google.common.base.Charsets; |
| 20 | import com.google.common.base.Function; |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 21 | import com.google.common.collect.FluentIterable; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableMap; |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Iterables; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Maps; |
| 26 | import com.google.template.soy.tofu.SoyTofu; |
| 27 | |
| 28 | import java.io.File; |
| 29 | import java.io.IOException; |
| 30 | import java.net.MalformedURLException; |
| 31 | import java.net.URL; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | |
| 35 | import javax.servlet.http.HttpServletResponse; |
| 36 | |
| 37 | /** Renderer for Soy templates used by Gitiles. */ |
| 38 | public abstract class Renderer { |
| 39 | private static final List<String> SOY_FILENAMES = ImmutableList.of( |
| Dave Borowitz | 6ec0c87 | 2014-01-29 13:59:37 -0800 | [diff] [blame] | 40 | "BlameDetail.soy", |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 41 | "Common.soy", |
| 42 | "DiffDetail.soy", |
| 43 | "HostIndex.soy", |
| 44 | "LogDetail.soy", |
| 45 | "ObjectDetail.soy", |
| 46 | "PathDetail.soy", |
| Dave Borowitz | 209d0aa | 2012-12-28 14:28:53 -0800 | [diff] [blame] | 47 | "RefList.soy", |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 48 | "RevisionDetail.soy", |
| 49 | "RepositoryIndex.soy"); |
| 50 | |
| 51 | public static final Map<String, String> STATIC_URL_GLOBALS = ImmutableMap.of( |
| 52 | "gitiles.CSS_URL", "gitiles.css", |
| Dave Borowitz | ddc0efc | 2014-04-27 19:12:47 -0600 | [diff] [blame] | 53 | "gitiles.PRETTIFY_CSS_URL", "prettify/prettify.css"); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 54 | |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 55 | protected static class FileUrlMapper implements Function<String, URL> { |
| 56 | private final String prefix; |
| 57 | |
| 58 | protected FileUrlMapper() { |
| 59 | this(""); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 60 | } |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 61 | |
| 62 | protected FileUrlMapper(String prefix) { |
| 63 | this.prefix = checkNotNull(prefix, "prefix"); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public URL apply(String filename) { |
| 68 | if (filename == null) { |
| 69 | return null; |
| 70 | } |
| 71 | try { |
| 72 | return new File(prefix + filename).toURI().toURL(); |
| 73 | } catch (MalformedURLException e) { |
| 74 | throw new IllegalArgumentException(e); |
| 75 | } |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | protected ImmutableList<URL> templates; |
| 80 | protected ImmutableMap<String, String> globals; |
| 81 | |
| 82 | protected Renderer(Function<String, URL> resourceMapper, Map<String, String> globals, |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 83 | String staticPrefix, Iterable<URL> customTemplates, String siteTitle) { |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 84 | checkNotNull(staticPrefix, "staticPrefix"); |
| Dave Borowitz | 76bbefd | 2014-03-11 16:57:45 -0700 | [diff] [blame] | 85 | Iterable<URL> allTemplates = FluentIterable.from(SOY_FILENAMES).transform(resourceMapper); |
| 86 | templates = ImmutableList.copyOf(Iterables.concat(allTemplates, customTemplates)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 87 | |
| 88 | Map<String, String> allGlobals = Maps.newHashMap(); |
| 89 | for (Map.Entry<String, String> e : STATIC_URL_GLOBALS.entrySet()) { |
| 90 | allGlobals.put(e.getKey(), staticPrefix + e.getValue()); |
| 91 | } |
| Chad Horohoe | 2a28d62 | 2012-11-12 11:56:59 -0800 | [diff] [blame] | 92 | allGlobals.put("gitiles.SITE_TITLE", siteTitle); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 93 | allGlobals.putAll(globals); |
| 94 | this.globals = ImmutableMap.copyOf(allGlobals); |
| 95 | } |
| 96 | |
| 97 | public void render(HttpServletResponse res, String templateName) throws IOException { |
| 98 | render(res, templateName, ImmutableMap.<String, Object> of()); |
| 99 | } |
| 100 | |
| 101 | public void render(HttpServletResponse res, String templateName, Map<String, ?> soyData) |
| 102 | throws IOException { |
| 103 | res.setContentType("text/html"); |
| 104 | res.setCharacterEncoding("UTF-8"); |
| 105 | byte[] data = newRenderer(templateName).setData(soyData).render().getBytes(Charsets.UTF_8); |
| 106 | res.setContentLength(data.length); |
| 107 | res.getOutputStream().write(data); |
| 108 | } |
| 109 | |
| 110 | SoyTofu.Renderer newRenderer(String templateName) { |
| 111 | return getTofu().newRenderer(templateName); |
| 112 | } |
| 113 | |
| 114 | protected abstract SoyTofu getTofu(); |
| 115 | } |