blob: 56f4c6162c25e8ecf564150e2ca5f7985023e5c8 [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
David Ostrovsky84286b72017-08-19 16:25:20 +020017import com.google.gitiles.blame.cache.BlameCache;
Dave Borowitz9de65952012-08-13 16:09:45 -070018import java.util.Enumeration;
Dave Borowitz03397692012-12-18 17:11:16 -080019import javax.annotation.Nullable;
Dave Borowitz9de65952012-08-13 16:09:45 -070020import javax.servlet.Filter;
21import javax.servlet.FilterConfig;
22import javax.servlet.ServletConfig;
23import javax.servlet.ServletContext;
24import javax.servlet.ServletException;
25import javax.servlet.http.HttpServlet;
26import javax.servlet.http.HttpServletRequest;
Dave Borowitz3b744b12016-08-19 16:11:10 -040027import org.eclipse.jgit.http.server.glue.MetaServlet;
28import org.eclipse.jgit.lib.Config;
29import org.eclipse.jgit.transport.resolver.RepositoryResolver;
Dave Borowitz9de65952012-08-13 16:09:45 -070030
31/**
32 * Servlet to serve Gitiles.
Dave Borowitz40255d52016-08-19 16:16:22 -040033 *
34 * <p>This servlet can either be constructed manually with its dependencies, or configured to use
35 * default implementations for the Gitiles interfaces. To configure the defaults, you must provide a
36 * single init parameter "configPath", which is the path to a git config file containing additional
Dave Borowitz9de65952012-08-13 16:09:45 -070037 * configuration.
38 */
39public class GitilesServlet extends MetaServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080040 private static final long serialVersionUID = 1L;
41
Dave Borowitz9de65952012-08-13 16:09:45 -070042 /** The prefix from which static resources are served. */
43 public static final String STATIC_PREFIX = "/+static/";
44
Dave Borowitz03397692012-12-18 17:11:16 -080045 public GitilesServlet(
David Pursehouse49164f62013-10-02 18:13:33 +090046 Config config,
Dave Borowitz03397692012-12-18 17:11:16 -080047 @Nullable Renderer renderer,
48 @Nullable GitilesUrls urls,
49 @Nullable GitilesAccess.Factory accessFactory,
50 @Nullable RepositoryResolver<HttpServletRequest> resolver,
Dave Borowitz14ce8282012-12-20 14:08:25 -080051 @Nullable VisibilityCache visibilityCache,
Dave Borowitzd91bdf72013-01-10 20:07:32 -080052 @Nullable TimeCache timeCache,
Dave Borowitz86462df2014-02-03 14:23:57 -080053 @Nullable BlameCache blameCache,
Dave Borowitzd91bdf72013-01-10 20:07:32 -080054 @Nullable GitwebRedirectFilter gitwebRedirect) {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020055 super(
56 new GitilesFilter(
57 config,
58 renderer,
59 urls,
60 accessFactory,
61 resolver,
62 visibilityCache,
63 timeCache,
64 blameCache,
65 gitwebRedirect));
Dave Borowitz9de65952012-08-13 16:09:45 -070066 }
67
68 public GitilesServlet() {
69 super(new GitilesFilter());
70 }
71
72 @Override
73 protected GitilesFilter getDelegateFilter() {
74 return (GitilesFilter) super.getDelegateFilter();
75 }
76
77 @Override
78 public void init(final ServletConfig config) throws ServletException {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020079 getDelegateFilter()
80 .init(
81 new FilterConfig() {
82 @Override
83 public String getFilterName() {
84 return getDelegateFilter().getClass().getName();
85 }
Dave Borowitz9de65952012-08-13 16:09:45 -070086
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020087 @Override
88 public String getInitParameter(String name) {
89 return config.getInitParameter(name);
90 }
Dave Borowitz9de65952012-08-13 16:09:45 -070091
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020092 @SuppressWarnings("rawtypes")
93 @Override
94 public Enumeration getInitParameterNames() {
95 return config.getInitParameterNames();
96 }
Dave Borowitz9de65952012-08-13 16:09:45 -070097
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020098 @Override
99 public ServletContext getServletContext() {
100 return config.getServletContext();
101 }
102 });
Dave Borowitz9de65952012-08-13 16:09:45 -0700103 }
104
105 /**
106 * Add a custom filter for a view.
Dave Borowitz40255d52016-08-19 16:16:22 -0400107 *
108 * <p>Must be called before initializing the servlet.
Dave Borowitz9de65952012-08-13 16:09:45 -0700109 *
110 * @param view view type.
111 * @param filter filter.
112 */
113 public void addFilter(GitilesView.Type view, Filter filter) {
114 getDelegateFilter().addFilter(view, filter);
115 }
116
117 /**
118 * Set a custom handler for a view.
Dave Borowitz40255d52016-08-19 16:16:22 -0400119 *
120 * <p>Must be called before initializing the servlet.
Dave Borowitz9de65952012-08-13 16:09:45 -0700121 *
122 * @param view view type.
123 * @param handler handler.
124 */
125 public void setHandler(GitilesView.Type view, HttpServlet handler) {
126 getDelegateFilter().setHandler(view, handler);
127 }
128
129 public BaseServlet getDefaultHandler(GitilesView.Type view) {
130 return getDelegateFilter().getDefaultHandler(view);
131 }
132}