
* stable-2.15: Update git submodules Use Logger's built-in string formatting where possible Doc: Fix code example in JS API Allow some sections of the change list to overflow Fix more comparisons of current user Revert "WorkQueue: Add metrics" Fix permissions checks on Gerrit API on current user GroupCacheImpl: Fix log message when UUID is not found Update git submodules Update git submodules WorkQueue.Executor#buildMetrics: Remove MetricMaker parameter ChangeQueryBuilder: Allow ownerin predicate to be evaluated by the index Update git submodules Update git submodules Update git submodules ldap.Helper: Use local logger and make logger in LdapRealm private Remove ValidationError#createLoggerSink to avoid passing around loggers LdapLoginServlet: Improve exception handling OperatingSystemMXBeanProvider: Log exception for ReflectiveOperationException HttpPluginServlet: Don't trim leading whitespace from about.md content ProjectConfig: Don't use JGit's StringUtils to convert to lower case Do not abort indexing if < 50% projects failed Revert "AllChangesIndexer: Don't abort when failing to open repository" WorkQueue: Don't fail when queue metric already exists WorkQueue: Sanitize metric name when queue is created DropWizardMetricMaker: Introduce method to sanitize metric name VersionedAccountDestinations: Remove unused createSink(String) method ProjectBasicAuthFilter: Add comment why cause is not logged BazelBuild: Fix exception message when command was interrupted GitwebServlet: Write only one log entry for CGI errors GitwebServlet: Log unexpected errors on error level PostGpgKeys: Remove unneeded use of Joiner Remove some logs for errors that are rethrown DropWizardMetricMaker: Improve error messages for invalid arguments DropWizardMetricMaker: Improve error message when metric name is invalid AllChangesIndexer: Don't abort when failing to open repository Change-Id: I33e8a1c9498c6adecd4ffc7d8ef0cc78aeb28eea
160 lines
5.2 KiB
Java
160 lines
5.2 KiB
Java
// Copyright (C) 2015 The Android Open Source Project
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package com.google.gerrit.httpd.plugins;
|
|
|
|
import static com.google.gerrit.extensions.api.lfs.LfsDefinitions.CONTENTTYPE_VND_GIT_LFS_JSON;
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
import static javax.servlet.http.HttpServletResponse.SC_NOT_IMPLEMENTED;
|
|
|
|
import com.google.gerrit.extensions.registration.RegistrationHandle;
|
|
import com.google.gerrit.httpd.resources.Resource;
|
|
import com.google.gerrit.server.config.GerritServerConfig;
|
|
import com.google.gerrit.server.plugins.Plugin;
|
|
import com.google.gerrit.server.plugins.ReloadPluginListener;
|
|
import com.google.gerrit.server.plugins.StartPluginListener;
|
|
import com.google.gwtexpui.server.CacheHeaders;
|
|
import com.google.inject.Inject;
|
|
import com.google.inject.Singleton;
|
|
import com.google.inject.servlet.GuiceFilter;
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.Writer;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import javax.servlet.FilterChain;
|
|
import javax.servlet.ServletConfig;
|
|
import javax.servlet.ServletContext;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.ServletRequest;
|
|
import javax.servlet.ServletResponse;
|
|
import javax.servlet.http.HttpServlet;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import org.eclipse.jgit.lib.Config;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@Singleton
|
|
public class LfsPluginServlet extends HttpServlet
|
|
implements StartPluginListener, ReloadPluginListener {
|
|
private static final long serialVersionUID = 1L;
|
|
private static final Logger log = LoggerFactory.getLogger(LfsPluginServlet.class);
|
|
private static final String MESSAGE_LFS_NOT_CONFIGURED =
|
|
"{\"message\":\"No LFS plugin is configured to handle LFS requests.\"}";
|
|
|
|
private List<Plugin> pending = new ArrayList<>();
|
|
private final String pluginName;
|
|
private final FilterChain chain;
|
|
private AtomicReference<GuiceFilter> filter;
|
|
|
|
@Inject
|
|
LfsPluginServlet(@GerritServerConfig Config cfg) {
|
|
this.pluginName = cfg.getString("lfs", null, "plugin");
|
|
this.chain =
|
|
new FilterChain() {
|
|
@Override
|
|
public void doFilter(ServletRequest req, ServletResponse res) throws IOException {
|
|
Resource.NOT_FOUND.send((HttpServletRequest) req, (HttpServletResponse) res);
|
|
}
|
|
};
|
|
this.filter = new AtomicReference<>();
|
|
}
|
|
|
|
@Override
|
|
protected void service(HttpServletRequest req, HttpServletResponse res)
|
|
throws ServletException, IOException {
|
|
if (filter.get() == null) {
|
|
responseLfsNotConfigured(res);
|
|
return;
|
|
}
|
|
filter.get().doFilter(req, res, chain);
|
|
}
|
|
|
|
@Override
|
|
public synchronized void init(ServletConfig config) throws ServletException {
|
|
super.init(config);
|
|
|
|
for (Plugin plugin : pending) {
|
|
install(plugin);
|
|
}
|
|
pending = null;
|
|
}
|
|
|
|
@Override
|
|
public synchronized void onStartPlugin(Plugin plugin) {
|
|
if (pending != null) {
|
|
pending.add(plugin);
|
|
} else {
|
|
install(plugin);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onReloadPlugin(Plugin oldPlugin, Plugin newPlugin) {
|
|
install(newPlugin);
|
|
}
|
|
|
|
private void responseLfsNotConfigured(HttpServletResponse res) throws IOException {
|
|
CacheHeaders.setNotCacheable(res);
|
|
res.setContentType(CONTENTTYPE_VND_GIT_LFS_JSON);
|
|
res.setStatus(SC_NOT_IMPLEMENTED);
|
|
Writer w = new BufferedWriter(new OutputStreamWriter(res.getOutputStream(), UTF_8));
|
|
w.write(MESSAGE_LFS_NOT_CONFIGURED);
|
|
w.flush();
|
|
}
|
|
|
|
private void install(Plugin plugin) {
|
|
if (!plugin.getName().equals(pluginName)) {
|
|
return;
|
|
}
|
|
final GuiceFilter guiceFilter = load(plugin);
|
|
plugin.add(
|
|
new RegistrationHandle() {
|
|
@Override
|
|
public void remove() {
|
|
filter.compareAndSet(guiceFilter, null);
|
|
}
|
|
});
|
|
filter.set(guiceFilter);
|
|
}
|
|
|
|
private GuiceFilter load(Plugin plugin) {
|
|
if (plugin.getHttpInjector() != null) {
|
|
final String name = plugin.getName();
|
|
final GuiceFilter guiceFilter;
|
|
try {
|
|
guiceFilter = plugin.getHttpInjector().getInstance(GuiceFilter.class);
|
|
} catch (RuntimeException e) {
|
|
log.warn("Plugin {} cannot load GuiceFilter", name, e);
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
ServletContext ctx = PluginServletContext.create(plugin, "/");
|
|
guiceFilter.init(new WrappedFilterConfig(ctx));
|
|
} catch (ServletException e) {
|
|
log.warn("Plugin {} failed to initialize HTTP", name, e);
|
|
return null;
|
|
}
|
|
|
|
plugin.add(guiceFilter::destroy);
|
|
return guiceFilter;
|
|
}
|
|
return null;
|
|
}
|
|
}
|