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