mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
introducing new ExtensionPoint for repository path matching
The new ExtensionPoint was introduced to remove the tight coupling between the DefaultRepositoryManager and the GitRepositoryHandler. Git has now its own RepositoryPathMatcher which allow the matching of repository with .git or without .git extension.
This commit is contained in:
@@ -35,7 +35,6 @@ package sonia.scm.repository;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
@@ -110,6 +109,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
||||
* @param repositoryListenersProvider
|
||||
* @param repositoryHooksProvider
|
||||
* @param preProcessorUtil
|
||||
* @param repositoryMatcher
|
||||
*/
|
||||
@Inject
|
||||
public DefaultRepositoryManager(ScmConfiguration configuration,
|
||||
@@ -117,7 +117,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
||||
RepositoryDAO repositoryDAO, Set<RepositoryHandler> handlerSet,
|
||||
Provider<Set<RepositoryListener>> repositoryListenersProvider,
|
||||
Provider<Set<RepositoryHook>> repositoryHooksProvider,
|
||||
PreProcessorUtil preProcessorUtil)
|
||||
PreProcessorUtil preProcessorUtil, RepositoryMatcher repositoryMatcher)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.keyGenerator = keyGenerator;
|
||||
@@ -125,6 +125,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
||||
this.repositoryListenersProvider = repositoryListenersProvider;
|
||||
this.repositoryHooksProvider = repositoryHooksProvider;
|
||||
this.preProcessorUtil = preProcessorUtil;
|
||||
this.repositoryMatcher = repositoryMatcher;
|
||||
|
||||
//J-
|
||||
ThreadFactory factory = new ThreadFactoryBuilder()
|
||||
@@ -703,7 +704,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
||||
|
||||
for (Repository r : repositories)
|
||||
{
|
||||
if (type.equals(r.getType()) && isNameMatching(r, uri))
|
||||
if (repositoryMatcher.matches(r, type, uri))
|
||||
{
|
||||
assertIsReader(r);
|
||||
repository = r.clone();
|
||||
@@ -972,51 +973,6 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks whether or not the provided path belongs to the provided repository.
|
||||
*
|
||||
* @param repository The repository to be tested.
|
||||
* @param path The path that might be part of the repository.
|
||||
* @return Returns <code>true</code> if path belongs to the repository. Returns <code>false</code> otherwise.
|
||||
*/
|
||||
private boolean isNameMatching(Repository repository, String path) {
|
||||
return isNameMatching(repository.getType(), repository.getName(), path);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks whether or not the provided path belongs to the provided repository.
|
||||
*
|
||||
* @param repositoryType The type of the repository being tested.
|
||||
* @param repositoryName The name of the repository being tested.
|
||||
* @param path The path that might be part of the repository.
|
||||
* @return Returns <code>true</code> if path belongs to the repository. Returns <code>false</code> otherwise.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
boolean isNameMatching(String repositoryType, String repositoryName, String path) {
|
||||
boolean result = false;
|
||||
|
||||
if (path.startsWith(repositoryName)) {
|
||||
|
||||
String pathPart = path.substring(repositoryName.length());
|
||||
|
||||
//TODO: this introduces a strong coupling to the git plugin. This can be resolved with a "Repository Matcher" API.
|
||||
//ausformulieren, ticketId weg
|
||||
if (GitRepositoryHandler.TYPE_NAME.equals(repositoryType)) {
|
||||
|
||||
//git repository may also be named <<repo-name>>.git by convention
|
||||
if (pathPart.startsWith(GitRepositoryHandler.DOT_GIT)) {
|
||||
//if this is the case, just also cut it away
|
||||
pathPart = pathPart.substring(GitRepositoryHandler.DOT_GIT.length());
|
||||
}
|
||||
}
|
||||
|
||||
result = Util.isEmpty(pathPart) || pathPart.startsWith(HttpUtil.SEPARATOR_PATH);
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
@@ -1072,4 +1028,6 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager
|
||||
|
||||
/** Field description */
|
||||
private Set<Type> types;
|
||||
|
||||
private RepositoryMatcher repositoryMatcher;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.inject.Inject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
import sonia.scm.util.Util;
|
||||
|
||||
/**
|
||||
* RepositoryMatcher is able to check if a repository matches the requested path.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.54
|
||||
*/
|
||||
public final class RepositoryMatcher {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RepositoryMatcher.class);
|
||||
|
||||
private static final RepositoryPathMatcher DEFAULT_PATH_MATCHER = new DefaultRepositoryPathMatcher();
|
||||
|
||||
private final Map<String, RepositoryPathMatcher> pathMatchers;
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param pathMatchers injected set of {@link RepositoryPathMatcher}.
|
||||
*/
|
||||
@Inject
|
||||
public RepositoryMatcher(Set<RepositoryPathMatcher> pathMatchers) {
|
||||
this.pathMatchers = Maps.newHashMap();
|
||||
for ( RepositoryPathMatcher pathMatcher : pathMatchers ) {
|
||||
LOG.info("register custom repository path matcher for type {}", pathMatcher.getType());
|
||||
this.pathMatchers.put(pathMatcher.getType(), pathMatcher);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} is the repository matches the type and the name matches the requested path.
|
||||
*
|
||||
* @param repository repository
|
||||
* @param type type of repository
|
||||
* @param path requested path without context and without type information
|
||||
*
|
||||
* @return {@code true} is the repository matches
|
||||
*/
|
||||
public boolean matches(Repository repository, String type, String path) {
|
||||
return type.equals(repository.getType()) && isPathMatching(repository, path);
|
||||
}
|
||||
|
||||
private boolean isPathMatching(Repository repository, String path) {
|
||||
return getPathMatcherForType(repository.getType()).isPathMatching(repository, path);
|
||||
}
|
||||
|
||||
private RepositoryPathMatcher getPathMatcherForType(String type) {
|
||||
RepositoryPathMatcher pathMatcher = pathMatchers.get(type);
|
||||
if (pathMatcher == null) {
|
||||
pathMatcher = DEFAULT_PATH_MATCHER;
|
||||
}
|
||||
return pathMatcher;
|
||||
}
|
||||
|
||||
private static class DefaultRepositoryPathMatcher implements RepositoryPathMatcher {
|
||||
|
||||
@Override
|
||||
public boolean isPathMatching(Repository repository, String path) {
|
||||
String name = repository.getName();
|
||||
|
||||
if (path.startsWith(name)) {
|
||||
String sub = path.substring(name.length());
|
||||
|
||||
return Util.isEmpty(sub) || sub.startsWith(HttpUtil.SEPARATOR_PATH);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "any";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user