mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-07 17:06:30 +02:00
Inject repository from service provider
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package sonia.scm.repository.api;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
@@ -19,5 +17,5 @@ public interface ScmProtocol {
|
||||
/**
|
||||
* The URL to access the repository providing this protocol.
|
||||
*/
|
||||
String getUrl(Repository repository, URI baseUri);
|
||||
String getUrl(URI baseUri);
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
public abstract class DefaultHttpScmProtocol implements HttpScmProtocol {
|
||||
|
||||
private final Repository repository;
|
||||
|
||||
protected DefaultHttpScmProtocol(Repository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
}
|
||||
@@ -10,16 +10,23 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
public interface HttpScmProtocol extends ScmProtocol {
|
||||
public abstract class HttpScmProtocol implements ScmProtocol {
|
||||
|
||||
private final Repository repository;
|
||||
|
||||
public HttpScmProtocol(Repository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
default String getType() {
|
||||
public String getType() {
|
||||
return "http";
|
||||
}
|
||||
|
||||
@Override
|
||||
default String getUrl(Repository repository, URI baseUri) {
|
||||
return baseUri.resolve(URI.create("/repo" + "/" + repository.getNamespace() + "/" + repository.getName())).toASCIIString();
|
||||
public String getUrl(URI baseUri) {
|
||||
return baseUri.resolve(URI.create("repo" + "/" + repository.getNamespace() + "/" + repository.getName())).toASCIIString();
|
||||
}
|
||||
|
||||
void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws ServletException, IOException;
|
||||
public abstract void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws ServletException, IOException;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import javax.inject.Provider;
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -8,7 +10,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class InitializingHttpScmProtocolWrapper implements HttpScmProtocol {
|
||||
public abstract class InitializingHttpScmProtocolWrapper {
|
||||
|
||||
private final Provider<? extends HttpServlet> delegateProvider;
|
||||
|
||||
@@ -19,21 +21,32 @@ public abstract class InitializingHttpScmProtocolWrapper implements HttpScmProto
|
||||
this.delegateProvider = delegateProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws ServletException, IOException {
|
||||
if (!isInitialized) {
|
||||
synchronized (this) {
|
||||
if (!isInitialized) {
|
||||
HttpServlet httpServlet = delegateProvider.get();
|
||||
initializeServlet(config, httpServlet);
|
||||
isInitialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
delegateProvider.get().service(request, response);
|
||||
}
|
||||
|
||||
protected void initializeServlet(ServletConfig config, HttpServlet httpServlet) throws ServletException {
|
||||
httpServlet.init(config);
|
||||
}
|
||||
|
||||
public HttpScmProtocol get(Repository repository) {
|
||||
return new ProtocolWrapper(repository);
|
||||
}
|
||||
|
||||
private class ProtocolWrapper extends HttpScmProtocol {
|
||||
|
||||
public ProtocolWrapper(Repository repository) {
|
||||
super(repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws ServletException, IOException {
|
||||
if (!isInitialized) {
|
||||
synchronized (InitializingHttpScmProtocolWrapper.this) {
|
||||
if (!isInitialized) {
|
||||
HttpServlet httpServlet = delegateProvider.get();
|
||||
initializeServlet(config, httpServlet);
|
||||
isInitialized = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
delegateProvider.get().service(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class RepositoryServiceTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnProtocolsFromProvider() {
|
||||
when(provider.getSupportedProtocols()).thenReturn(Collections.singleton(new DummyHttpProtocol()));
|
||||
when(provider.getSupportedProtocols()).thenReturn(Collections.singleton(new DummyHttpProtocol(repository)));
|
||||
|
||||
RepositoryService repositoryService = new RepositoryService(null, provider, repository, null);
|
||||
Collection<ScmProtocol> supportedProtocols = repositoryService.getSupportedProtocols();
|
||||
@@ -43,7 +43,11 @@ public class RepositoryServiceTest {
|
||||
});
|
||||
}
|
||||
|
||||
private static class DummyHttpProtocol implements HttpScmProtocol {
|
||||
private static class DummyHttpProtocol extends HttpScmProtocol {
|
||||
public DummyHttpProtocol(Repository repository) {
|
||||
super(repository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) {
|
||||
}
|
||||
|
||||
@@ -54,10 +54,10 @@ public class GitRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
@Inject
|
||||
public GitRepositoryServiceResolver(GitRepositoryHandler handler, GitScmProtocolProviderWrapper servletProvider)
|
||||
public GitRepositoryServiceResolver(GitRepositoryHandler handler, GitScmProtocolProviderWrapper providerWrapper)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.servletProvider = servletProvider;
|
||||
this.providerWrapper = providerWrapper;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -77,7 +77,7 @@ public class GitRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
|
||||
if (TYPE.equalsIgnoreCase(repository.getType()))
|
||||
{
|
||||
provider = new GitRepositoryServiceProvider(handler, repository, servletProvider);
|
||||
provider = new GitRepositoryServiceProvider(handler, repository, providerWrapper.get(repository));
|
||||
}
|
||||
|
||||
return provider;
|
||||
@@ -87,5 +87,5 @@ public class GitRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
|
||||
/** Field description */
|
||||
private final GitRepositoryHandler handler;
|
||||
private final HttpScmProtocol servletProvider;
|
||||
private final GitScmProtocolProviderWrapper providerWrapper;
|
||||
}
|
||||
|
||||
@@ -81,16 +81,6 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param handler
|
||||
* @param hookManager
|
||||
* @param repository
|
||||
* @param httpScmProtocol
|
||||
*/
|
||||
HgRepositoryServiceProvider(HgRepositoryHandler handler,
|
||||
HgHookManager hookManager, Repository repository, HttpScmProtocol httpScmProtocol)
|
||||
{
|
||||
|
||||
@@ -65,11 +65,11 @@ public class HgRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
*/
|
||||
@Inject
|
||||
public HgRepositoryServiceResolver(HgRepositoryHandler handler,
|
||||
HgHookManager hookManager, HgScmProtocolProviderWrapper httpScmProtocol)
|
||||
HgHookManager hookManager, HgScmProtocolProviderWrapper providerWrapper)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.hookManager = hookManager;
|
||||
this.httpScmProtocol = httpScmProtocol;
|
||||
this.providerWrapper = providerWrapper;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -90,7 +90,7 @@ public class HgRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
if (TYPE.equalsIgnoreCase(repository.getType()))
|
||||
{
|
||||
provider = new HgRepositoryServiceProvider(handler, hookManager,
|
||||
repository, httpScmProtocol);
|
||||
repository, providerWrapper.get(repository));
|
||||
}
|
||||
|
||||
return provider;
|
||||
@@ -104,5 +104,5 @@ public class HgRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
/** Field description */
|
||||
private HgHookManager hookManager;
|
||||
|
||||
private final HttpScmProtocol httpScmProtocol;
|
||||
private final HgScmProtocolProviderWrapper providerWrapper;
|
||||
}
|
||||
|
||||
@@ -51,7 +51,6 @@ import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryProvider;
|
||||
import sonia.scm.repository.RepositoryRequestListenerUtil;
|
||||
import sonia.scm.repository.spi.HttpScmProtocol;
|
||||
import sonia.scm.security.CipherUtil;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
@@ -77,7 +76,7 @@ import java.util.Enumeration;
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class HgCGIServlet extends HttpServlet implements HttpScmProtocol
|
||||
public class HgCGIServlet extends HttpServlet
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
@@ -347,7 +346,7 @@ public class HgCGIServlet extends HttpServlet implements HttpScmProtocol
|
||||
return python;
|
||||
}
|
||||
|
||||
@Override
|
||||
// @Override
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) {
|
||||
service(request, response);
|
||||
}
|
||||
|
||||
@@ -58,13 +58,13 @@ public class SvnRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
*
|
||||
*
|
||||
* @param handler
|
||||
* @param httpScmProtocol
|
||||
* @param protocolWrapper
|
||||
*/
|
||||
@Inject
|
||||
public SvnRepositoryServiceResolver(SvnRepositoryHandler handler, SvnScmProtocolProviderWrapper httpScmProtocol)
|
||||
public SvnRepositoryServiceResolver(SvnRepositoryHandler handler, SvnScmProtocolProviderWrapper protocolWrapper)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.httpScmProtocol = httpScmProtocol;
|
||||
this.protocolWrapper = protocolWrapper;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -84,7 +84,7 @@ public class SvnRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
|
||||
if (TYPE.equalsIgnoreCase(repository.getType()))
|
||||
{
|
||||
provider = new SvnRepositoryServiceProvider(handler, repository, httpScmProtocol);
|
||||
provider = new SvnRepositoryServiceProvider(handler, repository, protocolWrapper.get(repository));
|
||||
}
|
||||
|
||||
return provider;
|
||||
@@ -95,5 +95,5 @@ public class SvnRepositoryServiceResolver implements RepositoryServiceResolver
|
||||
/** Field description */
|
||||
private SvnRepositoryHandler handler;
|
||||
|
||||
private final HttpScmProtocol httpScmProtocol;
|
||||
private final InitializingHttpScmProtocolWrapper protocolWrapper;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.RepositoryProvider;
|
||||
import sonia.scm.repository.RepositoryRequestListenerUtil;
|
||||
import sonia.scm.repository.SvnRepositoryHandler;
|
||||
import sonia.scm.repository.spi.HttpScmProtocol;
|
||||
import sonia.scm.util.AssertUtil;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
|
||||
@@ -63,7 +62,7 @@ import java.io.IOException;
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@Singleton
|
||||
public class SvnDAVServlet extends DAVServlet implements HttpScmProtocol
|
||||
public class SvnDAVServlet extends DAVServlet
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
@@ -285,7 +284,7 @@ public class SvnDAVServlet extends DAVServlet implements HttpScmProtocol
|
||||
private final RepositoryProvider repositoryProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
// @Override
|
||||
public void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws ServletException, IOException {
|
||||
service(request, response);
|
||||
}
|
||||
|
||||
@@ -67,6 +67,6 @@ public abstract class RepositoryToRepositoryDtoMapper extends BaseMapper<Reposit
|
||||
}
|
||||
|
||||
private Link createProtocolLink(ScmProtocol protocol, Repository repository) {
|
||||
return Link.linkBuilder("protocol", protocol.getUrl(repository, uriInfoStore.get().getBaseUri().resolve("../.."))).withName(protocol.getType()).build();
|
||||
return Link.linkBuilder("protocol", protocol.getUrl(uriInfoStore.get().getBaseUri().resolve("../.."))).withName(protocol.getType()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.repository.api.ScmProtocol;
|
||||
|
||||
import java.net.URI;
|
||||
@@ -20,7 +19,7 @@ class MockScmProtocol implements ScmProtocol {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl(Repository repository, URI baseUri) {
|
||||
public String getUrl(URI baseUri) {
|
||||
return protocol;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user