Merge "LFS protocol support"

This commit is contained in:
Dave Borowitz 2016-03-18 12:59:32 +00:00 committed by Gerrit Code Review
commit 45ad6673c9
5 changed files with 290 additions and 0 deletions

View File

@ -2867,6 +2867,17 @@ are specified in the link:#container[container section]:
javaOptions = -Dcom.sun.jndi.ldap.connect.pool.timeout=300000
----
[[lfs]]
=== Section lfs
[[lfs.plugin]]lfs.plugin::
+
The name of a plugin which serves the LFS protocol on the
`<project-name>/info/lfs/objects/batch` endpoint. When not configured Gerrit
will respond with `501 Not Implemented` on LFS protocol requests.
+
By default unset.
[[log]]
=== Section log

View File

@ -2063,6 +2063,60 @@ ProjectWebLinks will appear in the project list in the
BranchWebLinks will appear in the branch list in the last column.
[[lfs-extension]]
== LFS Storage Plugins
Gerrit provides an extension point that enables development of LFS (Large File
Storage) storage plugins. Gerrit core exposes the default LFS protocol endpoint
`<project-name>/info/lfs/objects/batch` and forwards the requests to the configured
link:config-gerrit.html#lfs[lfs.plugin] plugin which implements the LFS protocol.
By exposing the default LFS endpoint, the git-lfs client can be used without
any configuration.
[source, java]
----
/** Provide an LFS protocol implementation */
import org.eclipse.jgit.lfs.server.LargeFileRepository;
import org.eclipse.jgit.lfs.server.LfsProtocolServlet;
@Singleton
public class LfsApiServlet extends LfsProtocolServlet {
private static final long serialVersionUID = 1L;
private final S3LargeFileRepository repository;
@Inject
LfsApiServlet(S3LargeFileRepository repository) {
this.repository = repository;
}
@Override
protected LargeFileRepository getLargeFileRepository() {
return repository;
}
}
/** Register the LfsApiServlet to listen on the default LFS protocol endpoint */
import static com.google.gerrit.httpd.plugins.LfsPluginServlet.URL_REGEX;
import com.google.gerrit.httpd.plugins.HttpPluginModule;
public class HttpModule extends HttpPluginModule {
@Override
protected void configureServlets() {
serveRegex(URL_REGEX).with(LfsApiServlet.class);
}
}
/** Provide an implementation of the LargeFileRepository */
import org.eclipse.jgit.lfs.server.s3.S3Repository;
public class S3LargeFileRepository extends S3Repository {
...
}
----
[[documentation]]
== Documentation

View File

@ -32,6 +32,9 @@ public class HttpPluginModule extends ServletModule {
bind(HttpPluginServlet.class);
serveRegex("^/(?:a/)?plugins/(.*)?$").with(HttpPluginServlet.class);
bind(LfsPluginServlet.class);
serveRegex(LfsPluginServlet.URL_REGEX).with(LfsPluginServlet.class);
bind(StartPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(HttpPluginServlet.class);
@ -40,6 +43,14 @@ public class HttpPluginModule extends ServletModule {
.annotatedWith(UniqueAnnotations.create())
.to(HttpPluginServlet.class);
bind(StartPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(LfsPluginServlet.class);
bind(ReloadPluginListener.class)
.annotatedWith(UniqueAnnotations.create())
.to(LfsPluginServlet.class);
bind(HttpModuleGenerator.class)
.to(HttpAutoRegisterModuleGenerator.class);

View File

@ -0,0 +1,157 @@
// 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 javax.servlet.http.HttpServletResponse.SC_NOT_IMPLEMENTED;
import com.google.common.collect.Lists;
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 org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
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;
@Singleton
public class LfsPluginServlet extends HttpServlet
implements StartPluginListener, ReloadPluginListener {
private static final long serialVersionUID = 1L;
private static final Logger log
= LoggerFactory.getLogger(LfsPluginServlet.class);
public static final String URL_REGEX =
"^(?:/a)?(?:/p/|/)(.+)(?:/info/lfs/objects/batch)$";
private List<Plugin> pending = Lists.newArrayList();
private final String pluginName;
private GuiceFilter filter;
@Inject
LfsPluginServlet(@GerritServerConfig Config cfg) {
this.pluginName = cfg.getString("lfs", null, "plugin");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
if (filter == null) {
CacheHeaders.setNotCacheable(res);
res.sendError(SC_NOT_IMPLEMENTED);
return;
}
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest req, ServletResponse res)
throws IOException {
Resource.NOT_FOUND.send((HttpServletRequest) req, (HttpServletResponse) res);
}
};
if (filter != null) {
filter.doFilter(req, res, chain);
} else {
chain.doFilter(req, res);
}
}
@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 install(Plugin plugin) {
if (!plugin.getName().equals(pluginName)) {
return;
}
filter = load(plugin);
plugin.add(new RegistrationHandle() {
@Override
public void remove() {
filter = null;
}
});
}
private GuiceFilter load(Plugin plugin) {
if (plugin.getHttpInjector() != null) {
final String name = plugin.getName();
final GuiceFilter filter;
try {
filter = plugin.getHttpInjector().getInstance(GuiceFilter.class);
} catch (RuntimeException e) {
log.warn(String.format("Plugin %s cannot load GuiceFilter", name), e);
return null;
}
try {
ServletContext ctx =
PluginServletContext.create(plugin, "/");
filter.init(new WrappedFilterConfig(ctx));
} catch (ServletException e) {
log.warn(String.format("Plugin %s failed to initialize HTTP", name), e);
return null;
}
plugin.add(new RegistrationHandle() {
@Override
public void remove() {
filter.destroy();
}
});
return filter;
}
return null;
}
}

View File

@ -0,0 +1,57 @@
// Copyright (C) 2016 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.common.truth.Truth.assertThat;
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LfsPluginServletTest {
@Test
public void noLfsEndPoint_noMatch() {
Pattern p = Pattern.compile(LfsPluginServlet.URL_REGEX);
doesNotMatch(p, "/foo");
doesNotMatch(p, "/a/foo");
doesNotMatch(p, "/p/foo");
doesNotMatch(p, "/a/p/foo");
doesNotMatch(p, "/info/lfs/objects/batch");
doesNotMatch(p, "/info/lfs/objects/batch/foo");
}
@Test
public void matchingLfsEndpoint_projectNameCaptured() {
Pattern p = Pattern.compile(LfsPluginServlet.URL_REGEX);
matches(p, "/foo/bar/info/lfs/objects/batch", "foo/bar");
matches(p, "/a/foo/bar/info/lfs/objects/batch", "foo/bar");
matches(p, "/p/foo/bar/info/lfs/objects/batch", "foo/bar");
matches(p, "/a/p/foo/bar/info/lfs/objects/batch", "foo/bar");
}
private void doesNotMatch(Pattern p, String input) {
Matcher m = p.matcher(input);
assertThat(m.matches()).isFalse();
}
private void matches(Pattern p, String input, String expectedProjectName) {
Matcher m = p.matcher(input);
assertThat(m.matches()).isTrue();
assertThat(m.group(1)).isEqualTo(expectedProjectName);
}
}