| 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 | 6ed598f | 2014-04-17 10:09:50 -0700 | [diff] [blame] | 17 | import com.google.gitiles.blame.BlameCache; |
| 18 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 19 | import org.eclipse.jgit.http.server.glue.MetaServlet; |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 20 | import org.eclipse.jgit.lib.Config; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 21 | import org.eclipse.jgit.transport.resolver.RepositoryResolver; |
| 22 | |
| 23 | import java.util.Enumeration; |
| 24 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 25 | import javax.annotation.Nullable; |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 26 | import javax.servlet.Filter; |
| 27 | import javax.servlet.FilterConfig; |
| 28 | import javax.servlet.ServletConfig; |
| 29 | import javax.servlet.ServletContext; |
| 30 | import javax.servlet.ServletException; |
| 31 | import javax.servlet.http.HttpServlet; |
| 32 | import javax.servlet.http.HttpServletRequest; |
| 33 | |
| 34 | /** |
| 35 | * Servlet to serve Gitiles. |
| 36 | * <p> |
| 37 | * This servlet can either be constructed manually with its dependencies, or |
| 38 | * configured to use default implementations for the Gitiles interfaces. To |
| 39 | * configure the defaults, you must provide a single init parameter |
| 40 | * "configPath", which is the path to a git config file containing additional |
| 41 | * configuration. |
| 42 | */ |
| 43 | public class GitilesServlet extends MetaServlet { |
| Chad Horohoe | ad23f14 | 2012-11-12 09:45:39 -0800 | [diff] [blame] | 44 | private static final long serialVersionUID = 1L; |
| 45 | |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 46 | /** The prefix from which static resources are served. */ |
| 47 | public static final String STATIC_PREFIX = "/+static/"; |
| 48 | |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 49 | public GitilesServlet( |
| David Pursehouse | 49164f6 | 2013-10-02 18:13:33 +0900 | [diff] [blame] | 50 | Config config, |
| Dave Borowitz | 0339769 | 2012-12-18 17:11:16 -0800 | [diff] [blame] | 51 | @Nullable Renderer renderer, |
| 52 | @Nullable GitilesUrls urls, |
| 53 | @Nullable GitilesAccess.Factory accessFactory, |
| 54 | @Nullable RepositoryResolver<HttpServletRequest> resolver, |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 55 | @Nullable VisibilityCache visibilityCache, |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 56 | @Nullable TimeCache timeCache, |
| Dave Borowitz | 86462df | 2014-02-03 14:23:57 -0800 | [diff] [blame] | 57 | @Nullable BlameCache blameCache, |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 58 | @Nullable GitwebRedirectFilter gitwebRedirect) { |
| Dave Borowitz | 14ce828 | 2012-12-20 14:08:25 -0800 | [diff] [blame] | 59 | super(new GitilesFilter( |
| Dave Borowitz | 86462df | 2014-02-03 14:23:57 -0800 | [diff] [blame] | 60 | config, renderer, urls, accessFactory, resolver, visibilityCache, timeCache, blameCache, |
| Dave Borowitz | d91bdf7 | 2013-01-10 20:07:32 -0800 | [diff] [blame] | 61 | gitwebRedirect)); |
| Dave Borowitz | 9de6595 | 2012-08-13 16:09:45 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public GitilesServlet() { |
| 65 | super(new GitilesFilter()); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected GitilesFilter getDelegateFilter() { |
| 70 | return (GitilesFilter) super.getDelegateFilter(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void init(final ServletConfig config) throws ServletException { |
| 75 | getDelegateFilter().init(new FilterConfig() { |
| 76 | @Override |
| 77 | public String getFilterName() { |
| 78 | return getDelegateFilter().getClass().getName(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String getInitParameter(String name) { |
| 83 | return config.getInitParameter(name); |
| 84 | } |
| 85 | |
| 86 | @SuppressWarnings("rawtypes") |
| 87 | @Override |
| 88 | public Enumeration getInitParameterNames() { |
| 89 | return config.getInitParameterNames(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public ServletContext getServletContext() { |
| 94 | return config.getServletContext(); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add a custom filter for a view. |
| 101 | * <p> |
| 102 | * Must be called before initializing the servlet. |
| 103 | * |
| 104 | * @param view view type. |
| 105 | * @param filter filter. |
| 106 | */ |
| 107 | public void addFilter(GitilesView.Type view, Filter filter) { |
| 108 | getDelegateFilter().addFilter(view, filter); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Set a custom handler for a view. |
| 113 | * <p> |
| 114 | * Must be called before initializing the servlet. |
| 115 | * |
| 116 | * @param view view type. |
| 117 | * @param handler handler. |
| 118 | */ |
| 119 | public void setHandler(GitilesView.Type view, HttpServlet handler) { |
| 120 | getDelegateFilter().setHandler(view, handler); |
| 121 | } |
| 122 | |
| 123 | public BaseServlet getDefaultHandler(GitilesView.Type view) { |
| 124 | return getDelegateFilter().getDefaultHandler(view); |
| 125 | } |
| 126 | } |