blob: cc4946645f2249bb17c7e8cf638d95318af7e69c [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
17import org.eclipse.jgit.http.server.glue.MetaServlet;
Dave Borowitz03397692012-12-18 17:11:16 -080018import org.eclipse.jgit.lib.Config;
Dave Borowitz9de65952012-08-13 16:09:45 -070019import org.eclipse.jgit.transport.resolver.RepositoryResolver;
20
21import java.util.Enumeration;
22
Dave Borowitz03397692012-12-18 17:11:16 -080023import javax.annotation.Nullable;
Dave Borowitz9de65952012-08-13 16:09:45 -070024import javax.servlet.Filter;
25import javax.servlet.FilterConfig;
26import javax.servlet.ServletConfig;
27import javax.servlet.ServletContext;
28import javax.servlet.ServletException;
29import javax.servlet.http.HttpServlet;
30import javax.servlet.http.HttpServletRequest;
31
32/**
33 * Servlet to serve Gitiles.
34 * <p>
35 * This servlet can either be constructed manually with its dependencies, or
36 * configured to use default implementations for the Gitiles interfaces. To
37 * configure the defaults, you must provide a single init parameter
38 * "configPath", which is the path to a git config file containing additional
39 * configuration.
40 */
41public class GitilesServlet extends MetaServlet {
Chad Horohoead23f142012-11-12 09:45:39 -080042 private static final long serialVersionUID = 1L;
43
Dave Borowitz9de65952012-08-13 16:09:45 -070044 /** The prefix from which static resources are served. */
45 public static final String STATIC_PREFIX = "/+static/";
46
Dave Borowitz03397692012-12-18 17:11:16 -080047 public GitilesServlet(
David Pursehouse49164f62013-10-02 18:13:33 +090048 Config config,
Dave Borowitz03397692012-12-18 17:11:16 -080049 @Nullable Renderer renderer,
50 @Nullable GitilesUrls urls,
51 @Nullable GitilesAccess.Factory accessFactory,
52 @Nullable RepositoryResolver<HttpServletRequest> resolver,
Dave Borowitz14ce8282012-12-20 14:08:25 -080053 @Nullable VisibilityCache visibilityCache,
Dave Borowitzd91bdf72013-01-10 20:07:32 -080054 @Nullable TimeCache timeCache,
Dave Borowitz86462df2014-02-03 14:23:57 -080055 @Nullable BlameCache blameCache,
Dave Borowitzd91bdf72013-01-10 20:07:32 -080056 @Nullable GitwebRedirectFilter gitwebRedirect) {
Dave Borowitz14ce8282012-12-20 14:08:25 -080057 super(new GitilesFilter(
Dave Borowitz86462df2014-02-03 14:23:57 -080058 config, renderer, urls, accessFactory, resolver, visibilityCache, timeCache, blameCache,
Dave Borowitzd91bdf72013-01-10 20:07:32 -080059 gitwebRedirect));
Dave Borowitz9de65952012-08-13 16:09:45 -070060 }
61
62 public GitilesServlet() {
63 super(new GitilesFilter());
64 }
65
66 @Override
67 protected GitilesFilter getDelegateFilter() {
68 return (GitilesFilter) super.getDelegateFilter();
69 }
70
71 @Override
72 public void init(final ServletConfig config) throws ServletException {
73 getDelegateFilter().init(new FilterConfig() {
74 @Override
75 public String getFilterName() {
76 return getDelegateFilter().getClass().getName();
77 }
78
79 @Override
80 public String getInitParameter(String name) {
81 return config.getInitParameter(name);
82 }
83
84 @SuppressWarnings("rawtypes")
85 @Override
86 public Enumeration getInitParameterNames() {
87 return config.getInitParameterNames();
88 }
89
90 @Override
91 public ServletContext getServletContext() {
92 return config.getServletContext();
93 }
94 });
95 }
96
97 /**
98 * Add a custom filter for a view.
99 * <p>
100 * Must be called before initializing the servlet.
101 *
102 * @param view view type.
103 * @param filter filter.
104 */
105 public void addFilter(GitilesView.Type view, Filter filter) {
106 getDelegateFilter().addFilter(view, filter);
107 }
108
109 /**
110 * Set a custom handler for a view.
111 * <p>
112 * Must be called before initializing the servlet.
113 *
114 * @param view view type.
115 * @param handler handler.
116 */
117 public void setHandler(GitilesView.Type view, HttpServlet handler) {
118 getDelegateFilter().setHandler(view, handler);
119 }
120
121 public BaseServlet getDefaultHandler(GitilesView.Type view) {
122 return getDelegateFilter().getDefaultHandler(view);
123 }
124}