diff --git a/scm-core/src/main/java/sonia/scm/ArgumentIsInvalidException.java b/scm-core/src/main/java/sonia/scm/ArgumentIsInvalidException.java
deleted file mode 100644
index 727e9a8160..0000000000
--- a/scm-core/src/main/java/sonia/scm/ArgumentIsInvalidException.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/**
- * 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;
-
-/**
- *
- * @author Sebastian Sdorra
- * @since 1.17
- */
-public class ArgumentIsInvalidException extends IllegalStateException
-{
-
- /**
- * Constructs ...
- *
- */
- public ArgumentIsInvalidException()
- {
- super();
- }
-
- /**
- * Constructs ...
- *
- *
- * @param s
- */
- public ArgumentIsInvalidException(String s)
- {
- super(s);
- }
-
- /**
- * Constructs ...
- *
- *
- * @param cause
- */
- public ArgumentIsInvalidException(Throwable cause)
- {
- super(cause);
- }
-
- /**
- * Constructs ...
- *
- *
- * @param message
- * @param cause
- */
- public ArgumentIsInvalidException(String message, Throwable cause)
- {
- super(message, cause);
- }
-}
diff --git a/scm-core/src/main/java/sonia/scm/api/v2/resources/LinkBuilder.java b/scm-core/src/main/java/sonia/scm/api/v2/resources/LinkBuilder.java
index 6f6831b058..0797134c9f 100644
--- a/scm-core/src/main/java/sonia/scm/api/v2/resources/LinkBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/api/v2/resources/LinkBuilder.java
@@ -3,7 +3,6 @@ package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableList;
import javax.ws.rs.core.UriBuilder;
-import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.Arrays;
@@ -14,7 +13,7 @@ import java.util.Arrays;
* builder for each method.
*
*
- * LinkBuilder builder = new LinkBuilder(uriInfo, MainResource.class, SubResource.class);
+ * LinkBuilder builder = new LinkBuilder(pathInfo, MainResource.class, SubResource.class);
* Link link = builder
* .method("sub")
* .parameters("param")
@@ -25,16 +24,16 @@ import java.util.Arrays;
*/
@SuppressWarnings("WeakerAccess") // Non-public will result in IllegalAccessError for plugins
public class LinkBuilder {
- private final UriInfo uriInfo;
+ private final ScmPathInfo pathInfo;
private final Class[] classes;
private final ImmutableList calls;
- public LinkBuilder(UriInfo uriInfo, Class... classes) {
- this(uriInfo, classes, ImmutableList.of());
+ public LinkBuilder(ScmPathInfo pathInfo, Class... classes) {
+ this(pathInfo, classes, ImmutableList.of());
}
- private LinkBuilder(UriInfo uriInfo, Class[] classes, ImmutableList calls) {
- this.uriInfo = uriInfo;
+ private LinkBuilder(ScmPathInfo pathInfo, Class[] classes, ImmutableList calls) {
+ this.pathInfo = pathInfo;
this.classes = classes;
this.calls = calls;
}
@@ -51,7 +50,7 @@ public class LinkBuilder {
throw new IllegalStateException("not enough methods for all classes");
}
- URI baseUri = uriInfo.getBaseUri();
+ URI baseUri = pathInfo.getApiRestUri();
URI relativeUri = createRelativeUri();
return baseUri.resolve(relativeUri);
}
@@ -61,7 +60,7 @@ public class LinkBuilder {
}
private LinkBuilder add(String method, String[] parameters) {
- return new LinkBuilder(uriInfo, classes, appendNewCall(method, parameters));
+ return new LinkBuilder(pathInfo, classes, appendNewCall(method, parameters));
}
private ImmutableList appendNewCall(String method, String[] parameters) {
diff --git a/scm-core/src/main/java/sonia/scm/api/v2/resources/ScmPathInfo.java b/scm-core/src/main/java/sonia/scm/api/v2/resources/ScmPathInfo.java
new file mode 100644
index 0000000000..fa975520c1
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/api/v2/resources/ScmPathInfo.java
@@ -0,0 +1,14 @@
+package sonia.scm.api.v2.resources;
+
+import java.net.URI;
+
+public interface ScmPathInfo {
+
+ String REST_API_PATH = "/api/rest";
+
+ URI getApiRestUri();
+
+ default URI getRootUri() {
+ return getApiRestUri().resolve("../..");
+ }
+}
diff --git a/scm-core/src/main/java/sonia/scm/api/v2/resources/ScmPathInfoStore.java b/scm-core/src/main/java/sonia/scm/api/v2/resources/ScmPathInfoStore.java
new file mode 100644
index 0000000000..c88bd4a2b5
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/api/v2/resources/ScmPathInfoStore.java
@@ -0,0 +1,18 @@
+package sonia.scm.api.v2.resources;
+
+public class ScmPathInfoStore {
+
+ private ScmPathInfo pathInfo;
+
+ public ScmPathInfo get() {
+ return pathInfo;
+ }
+
+ public void set(ScmPathInfo info) {
+ if (this.pathInfo != null) {
+ throw new IllegalStateException("UriInfo already set");
+ }
+ this.pathInfo = info;
+ }
+
+}
diff --git a/scm-core/src/main/java/sonia/scm/api/v2/resources/UriInfoStore.java b/scm-core/src/main/java/sonia/scm/api/v2/resources/UriInfoStore.java
deleted file mode 100644
index 2f61383cfd..0000000000
--- a/scm-core/src/main/java/sonia/scm/api/v2/resources/UriInfoStore.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package sonia.scm.api.v2.resources;
-
-import javax.ws.rs.core.UriInfo;
-
-public class UriInfoStore {
-
- private UriInfo uriInfo;
-
- public UriInfo get() {
- return uriInfo;
- }
-
- public void set(UriInfo uriInfo) {
- if (this.uriInfo != null) {
- throw new IllegalStateException("UriInfo already set");
- }
- this.uriInfo = uriInfo;
- }
-}
diff --git a/scm-core/src/main/java/sonia/scm/filter/Filters.java b/scm-core/src/main/java/sonia/scm/filter/Filters.java
index b6a45811bc..b1f5ea47cf 100644
--- a/scm-core/src/main/java/sonia/scm/filter/Filters.java
+++ b/scm-core/src/main/java/sonia/scm/filter/Filters.java
@@ -31,6 +31,8 @@
package sonia.scm.filter;
+import static sonia.scm.api.v2.resources.ScmPathInfo.REST_API_PATH;
+
/**
* Useful constants for filter implementations.
*
@@ -44,26 +46,26 @@ public final class Filters
public static final String PATTERN_ALL = "/*";
/** Field description */
- public static final String PATTERN_CONFIG = "/api/rest/config*";
+ public static final String PATTERN_CONFIG = REST_API_PATH + "/config*";
/** Field description */
public static final String PATTERN_DEBUG = "/debug.html";
/** Field description */
- public static final String PATTERN_GROUPS = "/api/rest/groups*";
+ public static final String PATTERN_GROUPS = REST_API_PATH + "/groups*";
/** Field description */
- public static final String PATTERN_PLUGINS = "/api/rest/plugins*";
+ public static final String PATTERN_PLUGINS = REST_API_PATH + "/plugins*";
/** Field description */
public static final String PATTERN_RESOURCE_REGEX =
"^/(?:resources|api|plugins|index)[\\./].*(?:html|\\.css|\\.js|\\.xml|\\.json|\\.txt)";
/** Field description */
- public static final String PATTERN_RESTAPI = "/api/rest/*";
+ public static final String PATTERN_RESTAPI = REST_API_PATH + "/*";
/** Field description */
- public static final String PATTERN_USERS = "/api/rest/users*";
+ public static final String PATTERN_USERS = REST_API_PATH + "/users*";
/** authentication priority */
public static final int PRIORITY_AUTHENTICATION = 5000;
diff --git a/scm-core/src/main/java/sonia/scm/repository/EscapeUtil.java b/scm-core/src/main/java/sonia/scm/repository/EscapeUtil.java
deleted file mode 100644
index 979ea0056d..0000000000
--- a/scm-core/src/main/java/sonia/scm/repository/EscapeUtil.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/**
- * 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;
-
-//~--- non-JDK imports --------------------------------------------------------
-
-import com.google.common.collect.Lists;
-import org.apache.commons.lang.StringEscapeUtils;
-import sonia.scm.util.Util;
-
-import java.util.List;
-
-//~--- JDK imports ------------------------------------------------------------
-
-/**
- *
- * @author Sebastian Sdorra
- * @since 1.15
- */
-public final class EscapeUtil
-{
-
- /**
- * Constructs ...
- *
- */
- private EscapeUtil() {}
-
- //~--- methods --------------------------------------------------------------
-
- /**
- * Method description
- *
- *
- * @param result
- */
- public static void escape(BrowserResult result)
- {
- result.setBranch(escape(result.getBranch()));
- result.setTag(escape(result.getTag()));
-
- for (FileObject fo : result)
- {
- escape(fo);
- }
- }
-
- /**
- * Method description
- *
- *
- * @param result
- * @since 1.17
- */
- public static void escape(BlameResult result)
- {
- for (BlameLine line : result.getBlameLines())
- {
- escape(line);
- }
- }
-
- /**
- * Method description
- *
- *
- * @param line
- * @since 1.17
- */
- public static void escape(BlameLine line)
- {
- line.setDescription(escape(line.getDescription()));
- escape(line.getAuthor());
- }
-
- /**
- * Method description
- *
- *
- * @param fo
- */
- public static void escape(FileObject fo)
- {
- fo.setDescription(escape(fo.getDescription()));
- fo.setName(fo.getName());
- fo.setPath(fo.getPath());
- }
-
- /**
- * Method description
- *
- *
- * @param changeset
- */
- public static void escape(Changeset changeset)
- {
- changeset.setDescription(escape(changeset.getDescription()));
- escape(changeset.getAuthor());
- changeset.setBranches(escapeList(changeset.getBranches()));
- changeset.setTags(escapeList(changeset.getTags()));
- }
-
- /**
- * Method description
- *
- *
- * @param person
- * @since 1.17
- */
- public static void escape(Person person)
- {
- if (person != null)
- {
- person.setName(escape(person.getName()));
- person.setMail(escape(person.getMail()));
- }
- }
-
- /**
- * Method description
- *
- *
- * @param result
- */
- public static void escape(ChangesetPagingResult result)
- {
- for (Changeset c : result)
- {
- escape(c);
- }
- }
-
- /**
- * Method description
- *
- *
- * @param value
- *
- * @return
- */
- public static String escape(String value)
- {
- return StringEscapeUtils.escapeHtml(value);
- }
-
- /**
- * Method description
- *
- *
- * @param values
- *
- * @return
- */
- public static List escapeList(List values)
- {
- if (Util.isNotEmpty(values))
- {
- List newList = Lists.newArrayList();
-
- for (String v : values)
- {
- newList.add(StringEscapeUtils.escapeHtml(v));
- }
-
- values = newList;
- }
-
- return values;
- }
-
- public static void escape(Modifications modifications) {
- modifications.setAdded(escapeList(modifications.getAdded()));
- modifications.setModified(escapeList(modifications.getModified()));
- modifications.setRemoved(escapeList(modifications.getRemoved()));
- }
-}
diff --git a/scm-core/src/main/java/sonia/scm/repository/PreProcessorUtil.java b/scm-core/src/main/java/sonia/scm/repository/PreProcessorUtil.java
index 98730c2039..2a1d9c0340 100644
--- a/scm-core/src/main/java/sonia/scm/repository/PreProcessorUtil.java
+++ b/scm-core/src/main/java/sonia/scm/repository/PreProcessorUtil.java
@@ -111,7 +111,6 @@ public class PreProcessorUtil
blameLine.getLineNumber(), repository.getName());
}
- EscapeUtil.escape(blameLine);
handlePreProcess(repository,blameLine,blameLinePreProcessorFactorySet, blameLinePreProcessorSet);
}
@@ -123,22 +122,6 @@ public class PreProcessorUtil
* @param blameResult
*/
public void prepareForReturn(Repository repository, BlameResult blameResult)
- {
- prepareForReturn(repository, blameResult, true);
- }
-
- /**
- * Method description
- *
- *
- * @param repository
- * @param blameResult
- * @param escape
- *
- * @since 1.35
- */
- public void prepareForReturn(Repository repository, BlameResult blameResult,
- boolean escape)
{
if (logger.isTraceEnabled())
{
@@ -146,10 +129,6 @@ public class PreProcessorUtil
repository.getName());
}
- if (escape)
- {
- EscapeUtil.escape(blameResult);
- }
handlePreProcessForIterable(repository, blameResult.getBlameLines(),blameLinePreProcessorFactorySet, blameLinePreProcessorSet);
}
@@ -162,32 +141,12 @@ public class PreProcessorUtil
*/
public void prepareForReturn(Repository repository, Changeset changeset)
{
- prepareForReturn(repository, changeset, true);
- }
-
- /**
- * Method description
- *
- *
- * @param repository
- * @param changeset
- * @param escape
- *
- * @since 1.35
- */
- public void prepareForReturn(Repository repository, Changeset changeset, boolean escape){
logger.trace("prepare changeset {} of repository {} for return", changeset.getId(), repository.getName());
- if (escape) {
- EscapeUtil.escape(changeset);
- }
handlePreProcess(repository, changeset, changesetPreProcessorFactorySet, changesetPreProcessorSet);
}
- public void prepareForReturn(Repository repository, Modifications modifications, boolean escape) {
+ public void prepareForReturn(Repository repository, Modifications modifications) {
logger.trace("prepare modifications {} of repository {} for return", modifications, repository.getName());
- if (escape) {
- EscapeUtil.escape(modifications);
- }
handlePreProcess(repository, modifications, modificationsPreProcessorFactorySet, modificationsPreProcessorSet);
}
@@ -199,22 +158,6 @@ public class PreProcessorUtil
* @param result
*/
public void prepareForReturn(Repository repository, BrowserResult result)
- {
- prepareForReturn(repository, result, true);
- }
-
- /**
- * Method description
- *
- *
- * @param repository
- * @param result
- * @param escape
- *
- * @since 1.35
- */
- public void prepareForReturn(Repository repository, BrowserResult result,
- boolean escape)
{
if (logger.isTraceEnabled())
{
@@ -222,10 +165,6 @@ public class PreProcessorUtil
repository.getName());
}
- if (escape)
- {
- EscapeUtil.escape(result);
- }
handlePreProcessForIterable(repository, result,fileObjectPreProcessorFactorySet, fileObjectPreProcessorSet);
}
@@ -235,12 +174,8 @@ public class PreProcessorUtil
*
* @param repository
* @param result
- * @param escape
- *
- * @since 1.35
*/
- public void prepareForReturn(Repository repository,
- ChangesetPagingResult result, boolean escape)
+ public void prepareForReturn(Repository repository, ChangesetPagingResult result)
{
if (logger.isTraceEnabled())
{
@@ -248,27 +183,9 @@ public class PreProcessorUtil
repository.getName());
}
- if (escape)
- {
- EscapeUtil.escape(result);
- }
handlePreProcessForIterable(repository,result,changesetPreProcessorFactorySet, changesetPreProcessorSet);
}
- /**
- * Method description
- *
- *
- * @param repository
- * @param result
- */
- public void prepareForReturn(Repository repository,
- ChangesetPagingResult result)
- {
- prepareForReturn(repository, result, true);
- }
-
-
private , P extends PreProcessor> void handlePreProcess(Repository repository, T processedObject,
Collection factories,
Collection
preProcessors) {
diff --git a/scm-core/src/main/java/sonia/scm/repository/Repository.java b/scm-core/src/main/java/sonia/scm/repository/Repository.java
index 0d8c6a6af8..cad36f2d88 100644
--- a/scm-core/src/main/java/sonia/scm/repository/Repository.java
+++ b/scm-core/src/main/java/sonia/scm/repository/Repository.java
@@ -40,7 +40,6 @@ import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import sonia.scm.BasicPropertiesAware;
import sonia.scm.ModelObject;
-import sonia.scm.util.HttpUtil;
import sonia.scm.util.Util;
import sonia.scm.util.ValidationUtil;
@@ -349,17 +348,6 @@ public class Repository extends BasicPropertiesAware implements ModelObject, Per
// do not copy health check results
}
- /**
- * Creates the url of the repository.
- *
- * @param baseUrl base url of the server including the context path
- * @return url of the repository
- * @since 1.17
- */
- public String createUrl(String baseUrl) {
- return HttpUtil.concatenate(baseUrl, type, namespace, name);
- }
-
/**
* Returns true if the {@link Repository} is the same as the obj argument.
*
diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java
index 2c4d958d8d..1e2fdccf42 100644
--- a/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java
+++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryManager.java
@@ -38,7 +38,6 @@ package sonia.scm.repository;
import sonia.scm.AlreadyExistsException;
import sonia.scm.TypeManager;
-import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Collection;
@@ -99,29 +98,6 @@ public interface RepositoryManager
*/
public Collection getConfiguredTypes();
- /**
- * Returns the {@link Repository} associated to the request uri.
- *
- *
- * @param request the current http request
- *
- * @return associated to the request uri
- * @since 1.9
- */
- public Repository getFromRequest(HttpServletRequest request);
-
- /**
- * Returns the {@link Repository} associated to the request uri.
- *
- *
- *
- * @param uri request uri without context path
- *
- * @return associated to the request uri
- * @since 1.9
- */
- public Repository getFromUri(String uri);
-
/**
* Returns a {@link RepositoryHandler} by the given type (hg, git, svn ...).
*
diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryManagerDecorator.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryManagerDecorator.java
index aa4117af34..87df960da7 100644
--- a/scm-core/src/main/java/sonia/scm/repository/RepositoryManagerDecorator.java
+++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryManagerDecorator.java
@@ -39,7 +39,6 @@ import sonia.scm.AlreadyExistsException;
import sonia.scm.ManagerDecorator;
import sonia.scm.Type;
-import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Collection;
@@ -120,34 +119,6 @@ public class RepositoryManagerDecorator
return decorated;
}
- /**
- * {@inheritDoc}
- *
- *
- * @param request
- *
- * @return
- */
- @Override
- public Repository getFromRequest(HttpServletRequest request)
- {
- return decorated.getFromRequest(request);
- }
-
- /**
- * {@inheritDoc}
- *
- *
- * @param uri
- *
- * @return
- */
- @Override
- public Repository getFromUri(String uri)
- {
- return decorated.getFromUri(uri);
- }
-
/**
* {@inheritDoc}
*
diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryNotFoundException.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryNotFoundException.java
index 070221117a..9dd866daa4 100644
--- a/scm-core/src/main/java/sonia/scm/repository/RepositoryNotFoundException.java
+++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryNotFoundException.java
@@ -44,8 +44,8 @@ import sonia.scm.NotFoundException;
public class RepositoryNotFoundException extends NotFoundException
{
- /** Field description */
private static final long serialVersionUID = -6583078808900520166L;
+ private static final String TYPE_REPOSITORY = "repository";
//~--- constructors ---------------------------------------------------------
@@ -55,10 +55,14 @@ public class RepositoryNotFoundException extends NotFoundException
*
*/
public RepositoryNotFoundException(Repository repository) {
- super("repository", repository.getName() + "/" + repository.getNamespace());
+ super(TYPE_REPOSITORY, repository.getName() + "/" + repository.getNamespace());
}
public RepositoryNotFoundException(String repositoryId) {
- super("repository", repositoryId);
+ super(TYPE_REPOSITORY, repositoryId);
+ }
+
+ public RepositoryNotFoundException(NamespaceAndName namespaceAndName) {
+ super(TYPE_REPOSITORY, namespaceAndName.toString());
}
}
diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryProvider.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryProvider.java
index 1a5300ad21..cea8574d14 100644
--- a/scm-core/src/main/java/sonia/scm/repository/RepositoryProvider.java
+++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryProvider.java
@@ -6,13 +6,13 @@
* 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.
+ * 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.
+ * 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.
+ * 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
@@ -26,35 +26,21 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://bitbucket.org/sdorra/scm-manager
- *
*/
-
package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import com.google.inject.throwingproviders.CheckedProvider;
-import sonia.scm.security.ScmSecurityException;
-
/**
*
* @author Sebastian Sdorra
* @since 1.10
*/
-public interface RepositoryProvider extends CheckedProvider
-{
-
- /**
- * Method description
- *
- *
- * @return
- *
- * @throws ScmSecurityException
- */
+public interface RepositoryProvider extends CheckedProvider {
@Override
- public Repository get() throws ScmSecurityException;
+ Repository get();
}
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java
index 5a34345dce..f55abd4598 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/BlameCommandBuilder.java
@@ -185,7 +185,7 @@ public final class BlameCommandBuilder
if (!disablePreProcessors && (result != null))
{
- preProcessorUtil.prepareForReturn(repository, result, !disableEscaping);
+ preProcessorUtil.prepareForReturn(repository, result);
}
return result;
@@ -210,24 +210,6 @@ public final class BlameCommandBuilder
return this;
}
- /**
- * Disable html escaping for the returned blame lines. By default all
- * blame lines are html escaped.
- *
- *
- * @param disableEscaping true to disable the html escaping
- *
- * @return {@code this}
- *
- * @since 1.35
- */
- public BlameCommandBuilder setDisableEscaping(boolean disableEscaping)
- {
- this.disableEscaping = disableEscaping;
-
- return this;
- }
-
/**
* Disable the execution of pre processors.
*
@@ -362,9 +344,6 @@ public final class BlameCommandBuilder
/** the cache */
private final Cache cache;
- /** disable escaping */
- private boolean disableEscaping = false;
-
/** disable change */
private boolean disableCache = false;
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java
index eeae45b893..fe39aa0a05 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/BrowseCommandBuilder.java
@@ -179,7 +179,7 @@ public final class BrowseCommandBuilder
if (!disablePreProcessors && (result != null))
{
- preProcessorUtil.prepareForReturn(repository, result, !disableEscaping);
+ preProcessorUtil.prepareForReturn(repository, result);
List fileObjects = result.getFiles();
@@ -212,24 +212,6 @@ public final class BrowseCommandBuilder
return this;
}
- /**
- * Disable html escaping for the returned file objects. By default all
- * file objects are html escaped.
- *
- *
- * @param disableEscaping true to disable the html escaping
- *
- * @return {@code this}
- *
- * @since 1.35
- */
- public BrowseCommandBuilder setDisableEscaping(boolean disableEscaping)
- {
- this.disableEscaping = disableEscaping;
-
- return this;
- }
-
/**
* Disabling the last commit means that every call to
* {@link FileObject#getDescription()} and
@@ -433,9 +415,6 @@ public final class BrowseCommandBuilder
/** cache */
private final Cache cache;
- /** disable escaping */
- private boolean disableEscaping = false;
-
/** disables the cache */
private boolean disableCache = false;
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/HookChangesetBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/HookChangesetBuilder.java
index c06e227d5f..fe43fee038 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/HookChangesetBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/HookChangesetBuilder.java
@@ -132,8 +132,7 @@ public final class HookChangesetBuilder
try
{
copy = DeepCopy.copy(c);
- preProcessorUtil.prepareForReturn(repository, copy,
- !disableEscaping);
+ preProcessorUtil.prepareForReturn(repository, copy);
}
catch (IOException ex)
{
@@ -156,24 +155,6 @@ public final class HookChangesetBuilder
//~--- set methods ----------------------------------------------------------
- /**
- * Disable html escaping for the returned changesets. By default all
- * changesets are html escaped.
- *
- *
- * @param disableEscaping true to disable the html escaping
- *
- * @return {@code this}
- *
- * @since 1.35
- */
- public HookChangesetBuilder setDisableEscaping(boolean disableEscaping)
- {
- this.disableEscaping = disableEscaping;
-
- return this;
- }
-
/**
* Disable the execution of pre processors.
*
@@ -192,9 +173,6 @@ public final class HookChangesetBuilder
//~--- fields ---------------------------------------------------------------
- /** disable escaping */
- private boolean disableEscaping = false;
-
/** disable pre processors marker */
private boolean disablePreProcessors = false;
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java
index 1450cf687a..9c782a781b 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/LogCommandBuilder.java
@@ -264,7 +264,7 @@ public final class LogCommandBuilder
if (!disablePreProcessors && (cpr != null))
{
- preProcessorUtil.prepareForReturn(repository, cpr, !disableEscaping);
+ preProcessorUtil.prepareForReturn(repository, cpr);
}
return cpr;
@@ -306,24 +306,6 @@ public final class LogCommandBuilder
return this;
}
- /**
- * Disable html escaping for the returned changesets. By default all
- * changesets are html escaped.
- *
- *
- * @param disableEscaping true to disable the html escaping
- *
- * @return {@code this}
- *
- * @since 1.35
- */
- public LogCommandBuilder setDisableEscaping(boolean disableEscaping)
- {
- this.disableEscaping = disableEscaping;
-
- return this;
- }
-
/**
* Disable the execution of pre processors.
*
@@ -545,9 +527,6 @@ public final class LogCommandBuilder
/** repository to query */
private final Repository repository;
- /** disable escaping */
- private boolean disableEscaping = false;
-
/** disable cache */
private boolean disableCache = false;
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/ModificationsCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/ModificationsCommandBuilder.java
index d81088bd33..6459b47cd5 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/ModificationsCommandBuilder.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/ModificationsCommandBuilder.java
@@ -43,9 +43,6 @@ public final class ModificationsCommandBuilder {
private final PreProcessorUtil preProcessorUtil;
- @Setter
- private boolean disableEscaping = false;
-
@Setter
private boolean disableCache = false;
@@ -90,7 +87,7 @@ public final class ModificationsCommandBuilder {
}
}
if (!disablePreProcessors && (modifications != null)) {
- preProcessorUtil.prepareForReturn(repository, modifications, !disableEscaping);
+ preProcessorUtil.prepareForReturn(repository, modifications);
}
return modifications;
}
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java
index 9eb7427a4f..bdd6e4b320 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryService.java
@@ -31,6 +31,7 @@
package sonia.scm.repository.api;
+import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.cache.CacheManager;
@@ -42,6 +43,8 @@ import sonia.scm.repository.spi.RepositoryServiceProvider;
import java.io.Closeable;
import java.io.IOException;
+import java.util.Set;
+import java.util.stream.Stream;
/**
* From the {@link RepositoryService} it is possible to access all commands for
@@ -78,30 +81,32 @@ import java.io.IOException;
* @apiviz.uses sonia.scm.repository.api.UnbundleCommandBuilder
* @since 1.17
*/
+@Slf4j
public final class RepositoryService implements Closeable {
- private CacheManager cacheManager;
- private PreProcessorUtil preProcessorUtil;
- private RepositoryServiceProvider provider;
- private Repository repository;
- private static final Logger logger =
- LoggerFactory.getLogger(RepositoryService.class);
+
+ private static final Logger logger = LoggerFactory.getLogger(RepositoryService.class);
+
+ private final CacheManager cacheManager;
+ private final PreProcessorUtil preProcessorUtil;
+ private final RepositoryServiceProvider provider;
+ private final Repository repository;
+ private final Set protocolProviders;
/**
* Constructs a new {@link RepositoryService}. This constructor should only
* be called from the {@link RepositoryServiceFactory}.
- *
- * @param cacheManager cache manager
+ * @param cacheManager cache manager
* @param provider implementation for {@link RepositoryServiceProvider}
* @param repository the repository
- * @param preProcessorUtil
*/
RepositoryService(CacheManager cacheManager,
- RepositoryServiceProvider provider, Repository repository,
- PreProcessorUtil preProcessorUtil) {
+ RepositoryServiceProvider provider, Repository repository,
+ PreProcessorUtil preProcessorUtil, Set protocolProviders) {
this.cacheManager = cacheManager;
this.provider = provider;
this.repository = repository;
this.preProcessorUtil = preProcessorUtil;
+ this.protocolProviders = protocolProviders;
}
/**
@@ -125,7 +130,7 @@ public final class RepositoryService implements Closeable {
try {
provider.close();
} catch (IOException ex) {
- logger.error("Could not close repository service provider", ex);
+ log.error("Could not close repository service provider", ex);
}
}
@@ -138,7 +143,7 @@ public final class RepositoryService implements Closeable {
*/
public BlameCommandBuilder getBlameCommand() {
logger.debug("create blame command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new BlameCommandBuilder(cacheManager, provider.getBlameCommand(),
repository, preProcessorUtil);
@@ -153,7 +158,7 @@ public final class RepositoryService implements Closeable {
*/
public BranchesCommandBuilder getBranchesCommand() {
logger.debug("create branches command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new BranchesCommandBuilder(cacheManager,
provider.getBranchesCommand(), repository);
@@ -168,7 +173,7 @@ public final class RepositoryService implements Closeable {
*/
public BrowseCommandBuilder getBrowseCommand() {
logger.debug("create browse command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new BrowseCommandBuilder(cacheManager, provider.getBrowseCommand(),
repository, preProcessorUtil);
@@ -184,7 +189,7 @@ public final class RepositoryService implements Closeable {
*/
public BundleCommandBuilder getBundleCommand() {
logger.debug("create bundle command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new BundleCommandBuilder(provider.getBundleCommand(), repository);
}
@@ -198,7 +203,7 @@ public final class RepositoryService implements Closeable {
*/
public CatCommandBuilder getCatCommand() {
logger.debug("create cat command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new CatCommandBuilder(provider.getCatCommand());
}
@@ -213,7 +218,7 @@ public final class RepositoryService implements Closeable {
*/
public DiffCommandBuilder getDiffCommand() {
logger.debug("create diff command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new DiffCommandBuilder(provider.getDiffCommand());
}
@@ -229,7 +234,7 @@ public final class RepositoryService implements Closeable {
*/
public IncomingCommandBuilder getIncomingCommand() {
logger.debug("create incoming command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new IncomingCommandBuilder(cacheManager,
provider.getIncomingCommand(), repository, preProcessorUtil);
@@ -244,7 +249,7 @@ public final class RepositoryService implements Closeable {
*/
public LogCommandBuilder getLogCommand() {
logger.debug("create log command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new LogCommandBuilder(cacheManager, provider.getLogCommand(),
repository, preProcessorUtil);
@@ -258,7 +263,7 @@ public final class RepositoryService implements Closeable {
* by the implementation of the repository service provider.
*/
public ModificationsCommandBuilder getModificationsCommand() {
- logger.debug("create modifications command for repository {}",repository.getNamespaceAndName());
+ logger.debug("create modifications command for repository {}", repository.getNamespaceAndName());
return new ModificationsCommandBuilder(provider.getModificationsCommand(),repository, cacheManager.getCache(ModificationsCommandBuilder.CACHE_NAME), preProcessorUtil);
}
@@ -272,7 +277,7 @@ public final class RepositoryService implements Closeable {
*/
public OutgoingCommandBuilder getOutgoingCommand() {
logger.debug("create outgoing command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new OutgoingCommandBuilder(cacheManager,
provider.getOutgoingCommand(), repository, preProcessorUtil);
@@ -288,7 +293,7 @@ public final class RepositoryService implements Closeable {
*/
public PullCommandBuilder getPullCommand() {
logger.debug("create pull command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new PullCommandBuilder(provider.getPullCommand(), repository);
}
@@ -303,7 +308,7 @@ public final class RepositoryService implements Closeable {
*/
public PushCommandBuilder getPushCommand() {
logger.debug("create push command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new PushCommandBuilder(provider.getPushCommand());
}
@@ -326,7 +331,7 @@ public final class RepositoryService implements Closeable {
*/
public TagsCommandBuilder getTagsCommand() {
logger.debug("create tags command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new TagsCommandBuilder(cacheManager, provider.getTagsCommand(),
repository);
@@ -342,7 +347,7 @@ public final class RepositoryService implements Closeable {
*/
public UnbundleCommandBuilder getUnbundleCommand() {
logger.debug("create unbundle command for repository {}",
- repository.getName());
+ repository.getNamespaceAndName());
return new UnbundleCommandBuilder(provider.getUnbundleCommand(),
repository);
@@ -369,5 +374,20 @@ public final class RepositoryService implements Closeable {
return provider.getSupportedFeatures().contains(feature);
}
+ public Stream getSupportedProtocols() {
+ return protocolProviders.stream()
+ .filter(protocolProvider -> protocolProvider.getType().equals(getRepository().getType()))
+ .map(this::createProviderInstanceForRepository);
+ }
+ private T createProviderInstanceForRepository(ScmProtocolProvider protocolProvider) {
+ return protocolProvider.get(repository);
+ }
+
+ public T getProtocol(Class clazz) {
+ return this.getSupportedProtocols()
+ .filter(scmProtocol -> clazz.isAssignableFrom(scmProtocol.getClass()))
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException(String.format("no implementation for %s and repository type %s", clazz.getName(),getRepository().getType())));
+ }
}
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java
index 627e57a797..fbb1ee6b58 100644
--- a/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java
+++ b/scm-core/src/main/java/sonia/scm/repository/api/RepositoryServiceFactory.java
@@ -137,13 +137,15 @@ public final class RepositoryServiceFactory
@Inject
public RepositoryServiceFactory(ScmConfiguration configuration,
CacheManager cacheManager, RepositoryManager repositoryManager,
- Set resolvers, PreProcessorUtil preProcessorUtil)
+ Set resolvers, PreProcessorUtil preProcessorUtil,
+ Set protocolProviders)
{
this.configuration = configuration;
this.cacheManager = cacheManager;
this.repositoryManager = repositoryManager;
this.resolvers = resolvers;
this.preProcessorUtil = preProcessorUtil;
+ this.protocolProviders = protocolProviders;
ScmEventBus.getInstance().register(new CacheClearHook(cacheManager));
}
@@ -208,9 +210,7 @@ public final class RepositoryServiceFactory
if (repository == null)
{
- String msg = "could not find a repository with namespace/name " + namespaceAndName;
-
- throw new RepositoryNotFoundException(msg);
+ throw new RepositoryNotFoundException(namespaceAndName);
}
return create(repository);
@@ -254,7 +254,7 @@ public final class RepositoryServiceFactory
}
service = new RepositoryService(cacheManager, provider, repository,
- preProcessorUtil);
+ preProcessorUtil, protocolProviders);
break;
}
@@ -369,4 +369,6 @@ public final class RepositoryServiceFactory
/** service resolvers */
private final Set resolvers;
+
+ private Set protocolProviders;
}
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/ScmProtocol.java b/scm-core/src/main/java/sonia/scm/repository/api/ScmProtocol.java
new file mode 100644
index 0000000000..c987510491
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/api/ScmProtocol.java
@@ -0,0 +1,19 @@
+package sonia.scm.repository.api;
+
+/**
+ * An ScmProtocol represents a concrete protocol provided by the SCM-Manager instance
+ * to interact with a repository depending on its type. There may be multiple protocols
+ * available for a repository type (eg. http and ssh).
+ */
+public interface ScmProtocol {
+
+ /**
+ * The type of the concrete protocol, eg. "http" or "ssh".
+ */
+ String getType();
+
+ /**
+ * The URL to access the repository providing this protocol.
+ */
+ String getUrl();
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/api/ScmProtocolProvider.java b/scm-core/src/main/java/sonia/scm/repository/api/ScmProtocolProvider.java
new file mode 100644
index 0000000000..597826676d
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/api/ScmProtocolProvider.java
@@ -0,0 +1,12 @@
+package sonia.scm.repository.api;
+
+import sonia.scm.plugin.ExtensionPoint;
+import sonia.scm.repository.Repository;
+
+@ExtensionPoint(multi = true)
+public interface ScmProtocolProvider {
+
+ String getType();
+
+ T get(Repository repository);
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/HttpScmProtocol.java b/scm-core/src/main/java/sonia/scm/repository/spi/HttpScmProtocol.java
new file mode 100644
index 0000000000..b933abf559
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/HttpScmProtocol.java
@@ -0,0 +1,38 @@
+package sonia.scm.repository.spi;
+
+import sonia.scm.repository.Repository;
+import sonia.scm.repository.api.ScmProtocol;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URI;
+
+public abstract class HttpScmProtocol implements ScmProtocol {
+
+ private final Repository repository;
+ private final String basePath;
+
+ public HttpScmProtocol(Repository repository, String basePath) {
+ this.repository = repository;
+ this.basePath = basePath;
+ }
+
+ @Override
+ public String getType() {
+ return "http";
+ }
+
+ @Override
+ public String getUrl() {
+ return URI.create(basePath + "/").resolve(String.format("repo/%s/%s", repository.getNamespace(), repository.getName())).toASCIIString();
+ }
+
+ public final void serve(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws ServletException, IOException {
+ serve(request, response, repository, config);
+ }
+
+ protected abstract void serve(HttpServletRequest request, HttpServletResponse response, Repository repository, ServletConfig config) throws ServletException, IOException;
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/InitializingHttpScmProtocolWrapper.java b/scm-core/src/main/java/sonia/scm/repository/spi/InitializingHttpScmProtocolWrapper.java
new file mode 100644
index 0000000000..c1b7229036
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/InitializingHttpScmProtocolWrapper.java
@@ -0,0 +1,91 @@
+package sonia.scm.repository.spi;
+
+import lombok.extern.slf4j.Slf4j;
+import sonia.scm.api.v2.resources.ScmPathInfoStore;
+import sonia.scm.config.ScmConfiguration;
+import sonia.scm.repository.Repository;
+import sonia.scm.repository.api.ScmProtocolProvider;
+
+import javax.inject.Provider;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Optional;
+
+import static java.util.Optional.empty;
+import static java.util.Optional.of;
+
+@Slf4j
+public abstract class InitializingHttpScmProtocolWrapper implements ScmProtocolProvider {
+
+ private final Provider extends ScmProviderHttpServlet> delegateProvider;
+ private final Provider pathInfoStore;
+ private final ScmConfiguration scmConfiguration;
+
+ private volatile boolean isInitialized = false;
+
+
+ protected InitializingHttpScmProtocolWrapper(Provider extends ScmProviderHttpServlet> delegateProvider, Provider pathInfoStore, ScmConfiguration scmConfiguration) {
+ this.delegateProvider = delegateProvider;
+ this.pathInfoStore = pathInfoStore;
+ this.scmConfiguration = scmConfiguration;
+ }
+
+ protected void initializeServlet(ServletConfig config, ScmProviderHttpServlet httpServlet) throws ServletException {
+ httpServlet.init(config);
+ }
+
+ @Override
+ public HttpScmProtocol get(Repository repository) {
+ if (!repository.getType().equals(getType())) {
+ throw new IllegalArgumentException(String.format("cannot handle repository with type %s with protocol for type %s", repository.getType(), getType()));
+ }
+ return new ProtocolWrapper(repository, computeBasePath());
+ }
+
+ private String computeBasePath() {
+ return getPathFromScmPathInfoIfAvailable().orElse(getPathFromConfiguration());
+ }
+
+ private Optional getPathFromScmPathInfoIfAvailable() {
+ try {
+ ScmPathInfoStore scmPathInfoStore = pathInfoStore.get();
+ if (scmPathInfoStore != null && scmPathInfoStore.get() != null) {
+ return of(scmPathInfoStore.get().getRootUri().toASCIIString());
+ }
+ } catch (Exception e) {
+ log.debug("could not get ScmPathInfoStore from context", e);
+ }
+ return empty();
+ }
+
+ private String getPathFromConfiguration() {
+ log.debug("using base path from configuration: {}", scmConfiguration.getBaseUrl());
+ return scmConfiguration.getBaseUrl();
+ }
+
+ private class ProtocolWrapper extends HttpScmProtocol {
+
+ public ProtocolWrapper(Repository repository, String basePath) {
+ super(repository, basePath);
+ }
+
+ @Override
+ protected void serve(HttpServletRequest request, HttpServletResponse response, Repository repository, ServletConfig config) throws ServletException, IOException {
+ if (!isInitialized) {
+ synchronized (InitializingHttpScmProtocolWrapper.this) {
+ if (!isInitialized) {
+ ScmProviderHttpServlet httpServlet = delegateProvider.get();
+ initializeServlet(config, httpServlet);
+ isInitialized = true;
+ }
+ }
+ }
+
+ delegateProvider.get().service(request, response, repository);
+ }
+
+ }
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java b/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java
index 2f436836cd..c66c56c0f1 100644
--- a/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/RepositoryServiceProvider.java
@@ -33,8 +33,6 @@
package sonia.scm.repository.spi;
-//~--- non-JDK imports --------------------------------------------------------
-
import sonia.scm.repository.Feature;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.CommandNotSupportedException;
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServlet.java b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServlet.java
new file mode 100644
index 0000000000..3a9dad52d6
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServlet.java
@@ -0,0 +1,16 @@
+package sonia.scm.repository.spi;
+
+import sonia.scm.repository.Repository;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public interface ScmProviderHttpServlet {
+
+ void service(HttpServletRequest request, HttpServletResponse response, Repository repository) throws ServletException, IOException;
+
+ void init(ServletConfig config) throws ServletException;
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletDecorator.java b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletDecorator.java
new file mode 100644
index 0000000000..c5dd55d277
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletDecorator.java
@@ -0,0 +1,28 @@
+package sonia.scm.repository.spi;
+
+import sonia.scm.repository.Repository;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class ScmProviderHttpServletDecorator implements ScmProviderHttpServlet {
+
+ private final ScmProviderHttpServlet object;
+
+ public ScmProviderHttpServletDecorator(ScmProviderHttpServlet object) {
+ this.object = object;
+ }
+
+ @Override
+ public void service(HttpServletRequest request, HttpServletResponse response, Repository repository) throws ServletException, IOException {
+ object.service(request, response, repository);
+ }
+
+ @Override
+ public void init(ServletConfig config) throws ServletException {
+ object.init(config);
+ }
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletDecoratorFactory.java b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletDecoratorFactory.java
new file mode 100644
index 0000000000..531a25e91d
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletDecoratorFactory.java
@@ -0,0 +1,15 @@
+package sonia.scm.repository.spi;
+
+import sonia.scm.DecoratorFactory;
+import sonia.scm.plugin.ExtensionPoint;
+
+@ExtensionPoint
+public interface ScmProviderHttpServletDecoratorFactory extends DecoratorFactory {
+ /**
+ * Has to return true if this factory provides a decorator for the given scm type (eg. "git", "hg" or
+ * "svn").
+ * @param type The current scm type this factory can provide a decorator for.
+ * @return true when the provided decorator should be used for the given scm type.
+ */
+ boolean handlesScmType(String type);
+}
diff --git a/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletProvider.java b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletProvider.java
new file mode 100644
index 0000000000..3793d4b935
--- /dev/null
+++ b/scm-core/src/main/java/sonia/scm/repository/spi/ScmProviderHttpServletProvider.java
@@ -0,0 +1,33 @@
+package sonia.scm.repository.spi;
+
+import com.google.inject.Inject;
+import sonia.scm.util.Decorators;
+
+import javax.inject.Provider;
+import java.util.List;
+import java.util.Set;
+
+import static java.util.stream.Collectors.toList;
+
+public abstract class ScmProviderHttpServletProvider implements Provider {
+
+ @Inject(optional = true)
+ private Set decoratorFactories;
+
+ private final String type;
+
+ protected ScmProviderHttpServletProvider(String type) {
+ this.type = type;
+ }
+
+ @Override
+ public ScmProviderHttpServlet get() {
+ return Decorators.decorate(getRootServlet(), getDecoratorsForType());
+ }
+
+ private List getDecoratorsForType() {
+ return decoratorFactories.stream().filter(d -> d.handlesScmType(type)).collect(toList());
+ }
+
+ protected abstract ScmProviderHttpServlet getRootServlet();
+}
diff --git a/scm-webapp/src/main/java/sonia/scm/util/Decorators.java b/scm-core/src/main/java/sonia/scm/util/Decorators.java
similarity index 99%
rename from scm-webapp/src/main/java/sonia/scm/util/Decorators.java
rename to scm-core/src/main/java/sonia/scm/util/Decorators.java
index 41c75660a9..6465631d03 100644
--- a/scm-webapp/src/main/java/sonia/scm/util/Decorators.java
+++ b/scm-core/src/main/java/sonia/scm/util/Decorators.java
@@ -37,7 +37,6 @@ package sonia.scm.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import sonia.scm.DecoratorFactory;
/**
diff --git a/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java b/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java
index dd3c82e800..328494a626 100644
--- a/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java
+++ b/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java
@@ -33,39 +33,32 @@
package sonia.scm.web.filter;
-//~--- non-JDK imports --------------------------------------------------------
-
-import com.google.common.base.Splitter;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import sonia.scm.ArgumentIsInvalidException;
import sonia.scm.SCMContext;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryPermissions;
+import sonia.scm.repository.spi.ScmProviderHttpServlet;
+import sonia.scm.repository.spi.ScmProviderHttpServletDecorator;
import sonia.scm.security.Role;
import sonia.scm.security.ScmSecurityException;
import sonia.scm.util.HttpUtil;
-import sonia.scm.util.Util;
-import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.util.Iterator;
-
-//~--- JDK imports ------------------------------------------------------------
/**
* Abstract http filter to check repository permissions.
*
* @author Sebastian Sdorra
*/
-public abstract class PermissionFilter extends HttpFilter
+public abstract class PermissionFilter extends ScmProviderHttpServletDecorator
{
/** the logger for PermissionFilter */
@@ -81,23 +74,14 @@ public abstract class PermissionFilter extends HttpFilter
*
* @since 1.21
*/
- public PermissionFilter(ScmConfiguration configuration)
+ protected PermissionFilter(ScmConfiguration configuration, ScmProviderHttpServlet delegate)
{
+ super(delegate);
this.configuration = configuration;
}
//~--- get methods ----------------------------------------------------------
- /**
- * Returns the requested repository.
- *
- *
- * @param request current http request
- *
- * @return requested repository
- */
- protected abstract Repository getRepository(HttpServletRequest request);
-
/**
* Returns true if the current request is a write request.
*
@@ -117,66 +101,38 @@ public abstract class PermissionFilter extends HttpFilter
*
* @param request http request
* @param response http response
- * @param chain filter chain
*
* @throws IOException
* @throws ServletException
*/
@Override
- protected void doFilter(HttpServletRequest request,
- HttpServletResponse response, FilterChain chain)
+ public void service(HttpServletRequest request,
+ HttpServletResponse response, Repository repository)
throws IOException, ServletException
{
Subject subject = SecurityUtils.getSubject();
try
{
- Repository repository = getRepository(request);
+ boolean writeRequest = isWriteRequest(request);
- if (repository != null)
+ if (hasPermission(repository, writeRequest))
{
- boolean writeRequest = isWriteRequest(request);
+ logger.trace("{} access to repository {} for user {} granted",
+ getActionAsString(writeRequest), repository.getName(),
+ getUserName(subject));
- if (hasPermission(repository, writeRequest))
- {
- logger.trace("{} access to repository {} for user {} granted",
- getActionAsString(writeRequest), repository.getName(),
- getUserName(subject));
-
- chain.doFilter(request, response);
- }
- else
- {
- logger.info("{} access to repository {} for user {} denied",
- getActionAsString(writeRequest), repository.getName(),
- getUserName(subject));
-
- sendAccessDenied(request, response, subject);
- }
+ super.service(request, response, repository);
}
else
{
- logger.debug("repository not found");
+ logger.info("{} access to repository {} for user {} denied",
+ getActionAsString(writeRequest), repository.getName(),
+ getUserName(subject));
- response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ sendAccessDenied(request, response, subject);
}
}
- catch (ArgumentIsInvalidException ex)
- {
- if (logger.isTraceEnabled())
- {
- logger.trace(
- "wrong request at ".concat(request.getRequestURI()).concat(
- " send redirect"), ex);
- }
- else if (logger.isWarnEnabled())
- {
- logger.warn("wrong request at {} send redirect",
- request.getRequestURI());
- }
-
- response.sendRedirect(getRepositoryRootHelpUrl(request));
- }
catch (ScmSecurityException | AuthorizationException ex)
{
logger.warn("user " + subject.getPrincipal() + " has not enough permissions", ex);
@@ -217,29 +173,6 @@ public abstract class PermissionFilter extends HttpFilter
HttpUtil.sendUnauthorized(response, configuration.getRealmDescription());
}
- /**
- * Extracts the type of the repositroy from url.
- *
- *
- * @param request http request
- *
- * @return type of repository
- */
- private String extractType(HttpServletRequest request)
- {
- Iterator it = Splitter.on(
- HttpUtil.SEPARATOR_PATH).omitEmptyStrings().split(
- request.getRequestURI()).iterator();
- String type = it.next();
-
- if (Util.isNotEmpty(request.getContextPath()))
- {
- type = it.next();
- }
-
- return type;
- }
-
/**
* Send access denied to the servlet response.
*
@@ -280,25 +213,6 @@ public abstract class PermissionFilter extends HttpFilter
: "read";
}
- /**
- * Returns the repository root help url.
- *
- *
- * @param request current http request
- *
- * @return repository root help url
- */
- private String getRepositoryRootHelpUrl(HttpServletRequest request)
- {
- String type = extractType(request);
- String helpUrl = HttpUtil.getCompleteUrl(request,
- "/api/rest/help/repository-root/");
-
- helpUrl = helpUrl.concat(type).concat(".html");
-
- return helpUrl;
- }
-
/**
* Returns the username from the given subject or anonymous.
*
diff --git a/scm-core/src/main/java/sonia/scm/web/filter/ProviderPermissionFilter.java b/scm-core/src/main/java/sonia/scm/web/filter/ProviderPermissionFilter.java
deleted file mode 100644
index ea0d90c915..0000000000
--- a/scm-core/src/main/java/sonia/scm/web/filter/ProviderPermissionFilter.java
+++ /dev/null
@@ -1,118 +0,0 @@
-/**
- * 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.web.filter;
-
-//~--- non-JDK imports --------------------------------------------------------
-
-import com.google.common.base.Throwables;
-import com.google.inject.ProvisionException;
-
-import org.apache.shiro.authz.AuthorizationException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import sonia.scm.config.ScmConfiguration;
-import sonia.scm.repository.Repository;
-import sonia.scm.repository.RepositoryProvider;
-
-//~--- JDK imports ------------------------------------------------------------
-
-import javax.servlet.http.HttpServletRequest;
-
-/**
- *
- * @author Sebastian Sdorra
- * @since 1.9
- */
-public abstract class ProviderPermissionFilter extends PermissionFilter
-{
-
- /**
- * the logger for ProviderPermissionFilter
- */
- private static final Logger logger =
- LoggerFactory.getLogger(ProviderPermissionFilter.class);
-
- //~--- constructors ---------------------------------------------------------
-
- /**
- * Constructs ...
- *
- *
- * @param configuration
- * @param repositoryProvider
- * @since 1.21
- */
- public ProviderPermissionFilter(ScmConfiguration configuration,
- RepositoryProvider repositoryProvider)
- {
- super(configuration);
- this.repositoryProvider = repositoryProvider;
- }
-
- //~--- get methods ----------------------------------------------------------
-
- /**
- * Method description
- *
- *
- * @param request
- *
- * @return
- */
- @Override
- protected Repository getRepository(HttpServletRequest request)
- {
- Repository repository = null;
-
- try
- {
- repository = repositoryProvider.get();
- }
- catch (ProvisionException ex)
- {
- Throwables.propagateIfPossible(ex.getCause(),
- IllegalStateException.class, AuthorizationException.class);
- logger.error("could not get repository from request", ex);
- }
-
- return repository;
- }
-
- //~--- fields ---------------------------------------------------------------
-
- /** Field description */
- private final RepositoryProvider repositoryProvider;
-}
diff --git a/scm-core/src/test/java/sonia/scm/repository/RepositoryTest.java b/scm-core/src/test/java/sonia/scm/repository/RepositoryTest.java
deleted file mode 100644
index f13f4cbc67..0000000000
--- a/scm-core/src/test/java/sonia/scm/repository/RepositoryTest.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/**
- * 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 org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-/**
- *
- * @author Sebastian Sdorra
- */
-public class RepositoryTest
-{
-
- /**
- * Method description
- *
- */
- @Test
- public void testCreateUrl()
- {
- Repository repository = new Repository("123", "hg", "test", "repo");
-
- assertEquals("http://localhost:8080/scm/hg/test/repo",
- repository.createUrl("http://localhost:8080/scm"));
- assertEquals("http://localhost:8080/scm/hg/test/repo",
- repository.createUrl("http://localhost:8080/scm/"));
- }
-}
diff --git a/scm-core/src/test/java/sonia/scm/repository/api/RepositoryServiceTest.java b/scm-core/src/test/java/sonia/scm/repository/api/RepositoryServiceTest.java
new file mode 100644
index 0000000000..2ceafc19bb
--- /dev/null
+++ b/scm-core/src/test/java/sonia/scm/repository/api/RepositoryServiceTest.java
@@ -0,0 +1,74 @@
+package sonia.scm.repository.api;
+
+import org.junit.Test;
+import sonia.scm.repository.Repository;
+import sonia.scm.repository.spi.HttpScmProtocol;
+import sonia.scm.repository.spi.RepositoryServiceProvider;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Collections;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.assertj.core.util.IterableUtil.sizeOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+
+public class RepositoryServiceTest {
+
+ private final RepositoryServiceProvider provider = mock(RepositoryServiceProvider.class);
+ private final Repository repository = new Repository("", "git", "space", "repo");
+
+ @Test
+ public void shouldReturnMatchingProtocolsFromProvider() {
+ RepositoryService repositoryService = new RepositoryService(null, provider, repository, null, Collections.singleton(new DummyScmProtocolProvider()));
+ Stream supportedProtocols = repositoryService.getSupportedProtocols();
+
+ assertThat(sizeOf(supportedProtocols.collect(Collectors.toList()))).isEqualTo(1);
+ }
+
+ @Test
+ public void shouldFindKnownProtocol() {
+ RepositoryService repositoryService = new RepositoryService(null, provider, repository, null, Collections.singleton(new DummyScmProtocolProvider()));
+
+ HttpScmProtocol protocol = repositoryService.getProtocol(HttpScmProtocol.class);
+
+ assertThat(protocol).isNotNull();
+ }
+
+ @Test
+ public void shouldFailForUnknownProtocol() {
+ RepositoryService repositoryService = new RepositoryService(null, provider, repository, null, Collections.singleton(new DummyScmProtocolProvider()));
+
+ assertThrows(IllegalArgumentException.class, () -> {
+ repositoryService.getProtocol(UnknownScmProtocol.class);
+ });
+ }
+
+ private static class DummyHttpProtocol extends HttpScmProtocol {
+ public DummyHttpProtocol(Repository repository) {
+ super(repository, "");
+ }
+
+ @Override
+ public void serve(HttpServletRequest request, HttpServletResponse response, Repository repository, ServletConfig config) {
+ }
+ }
+
+ private static class DummyScmProtocolProvider implements ScmProtocolProvider {
+ @Override
+ public String getType() {
+ return "git";
+ }
+
+ @Override
+ public ScmProtocol get(Repository repository) {
+ return new DummyHttpProtocol(repository);
+ }
+ }
+
+ private interface UnknownScmProtocol extends ScmProtocol {}
+}
diff --git a/scm-core/src/test/java/sonia/scm/repository/spi/HttpScmProtocolTest.java b/scm-core/src/test/java/sonia/scm/repository/spi/HttpScmProtocolTest.java
new file mode 100644
index 0000000000..1fd772fee3
--- /dev/null
+++ b/scm-core/src/test/java/sonia/scm/repository/spi/HttpScmProtocolTest.java
@@ -0,0 +1,40 @@
+package sonia.scm.repository.spi;
+
+import org.junit.jupiter.api.DynamicTest;
+import org.junit.jupiter.api.TestFactory;
+import sonia.scm.repository.Repository;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class HttpScmProtocolTest {
+
+ @TestFactory
+ Stream shouldCreateCorrectUrlsWithContextPath() {
+ return Stream.of("http://localhost/scm", "http://localhost/scm/")
+ .map(url -> assertResultingUrl(url, "http://localhost/scm/repo/space/name"));
+ }
+
+ @TestFactory
+ Stream shouldCreateCorrectUrlsWithPort() {
+ return Stream.of("http://localhost:8080", "http://localhost:8080/")
+ .map(url -> assertResultingUrl(url, "http://localhost:8080/repo/space/name"));
+ }
+
+ DynamicTest assertResultingUrl(String baseUrl, String expectedUrl) {
+ String actualUrl = createInstanceOfHttpScmProtocol(baseUrl).getUrl();
+ return DynamicTest.dynamicTest(baseUrl + " -> " + expectedUrl, () -> assertThat(actualUrl).isEqualTo(expectedUrl));
+ }
+
+ private HttpScmProtocol createInstanceOfHttpScmProtocol(String baseUrl) {
+ return new HttpScmProtocol(new Repository("", "", "space", "name"), baseUrl) {
+ @Override
+ protected void serve(HttpServletRequest request, HttpServletResponse response, Repository repository, ServletConfig config) {
+ }
+ };
+ }
+}
diff --git a/scm-core/src/test/java/sonia/scm/repository/spi/InitializingHttpScmProtocolWrapperTest.java b/scm-core/src/test/java/sonia/scm/repository/spi/InitializingHttpScmProtocolWrapperTest.java
new file mode 100644
index 0000000000..8c910f92a9
--- /dev/null
+++ b/scm-core/src/test/java/sonia/scm/repository/spi/InitializingHttpScmProtocolWrapperTest.java
@@ -0,0 +1,120 @@
+package sonia.scm.repository.spi;
+
+import com.google.inject.ProvisionException;
+import com.google.inject.util.Providers;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.stubbing.OngoingStubbing;
+import sonia.scm.api.v2.resources.ScmPathInfo;
+import sonia.scm.api.v2.resources.ScmPathInfoStore;
+import sonia.scm.config.ScmConfiguration;
+import sonia.scm.repository.Repository;
+
+import javax.inject.Provider;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URI;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+public class InitializingHttpScmProtocolWrapperTest {
+
+ private static final Repository REPOSITORY = new Repository("", "git", "space", "name");
+
+ @Mock
+ private ScmProviderHttpServlet delegateServlet;
+ @Mock
+ private ScmPathInfoStore pathInfoStore;
+ @Mock
+ private ScmConfiguration scmConfiguration;
+ private Provider pathInfoStoreProvider;
+
+ @Mock
+ private HttpServletRequest request;
+ @Mock
+ private HttpServletResponse response;
+ @Mock
+ private ServletConfig servletConfig;
+
+ private InitializingHttpScmProtocolWrapper wrapper;
+
+ @Before
+ public void init() {
+ initMocks(this);
+ pathInfoStoreProvider = mock(Provider.class);
+ when(pathInfoStoreProvider.get()).thenReturn(pathInfoStore);
+
+ wrapper = new InitializingHttpScmProtocolWrapper(Providers.of(this.delegateServlet), pathInfoStoreProvider, scmConfiguration) {
+ @Override
+ public String getType() {
+ return "git";
+ }
+ };
+ when(scmConfiguration.getBaseUrl()).thenReturn("http://example.com/scm");
+ }
+
+ @Test
+ public void shouldUsePathFromPathInfo() {
+ mockSetPathInfo();
+
+ HttpScmProtocol httpScmProtocol = wrapper.get(REPOSITORY);
+
+ assertEquals("http://example.com/scm/repo/space/name", httpScmProtocol.getUrl());
+ }
+
+ @Test
+ public void shouldUseConfigurationWhenPathInfoNotSet() {
+ HttpScmProtocol httpScmProtocol = wrapper.get(REPOSITORY);
+
+ assertEquals("http://example.com/scm/repo/space/name", httpScmProtocol.getUrl());
+ }
+
+ @Test
+ public void shouldUseConfigurationWhenNotInRequestScope() {
+ when(pathInfoStoreProvider.get()).thenThrow(new ProvisionException("test"));
+
+ HttpScmProtocol httpScmProtocol = wrapper.get(REPOSITORY);
+
+ assertEquals("http://example.com/scm/repo/space/name", httpScmProtocol.getUrl());
+ }
+
+ @Test
+ public void shouldInitializeAndDelegateRequestThroughFilter() throws ServletException, IOException {
+ HttpScmProtocol httpScmProtocol = wrapper.get(REPOSITORY);
+
+ httpScmProtocol.serve(request, response, servletConfig);
+
+ verify(delegateServlet).init(servletConfig);
+ verify(delegateServlet).service(request, response, REPOSITORY);
+ }
+
+ @Test
+ public void shouldInitializeOnlyOnce() throws ServletException, IOException {
+ HttpScmProtocol httpScmProtocol = wrapper.get(REPOSITORY);
+
+ httpScmProtocol.serve(request, response, servletConfig);
+ httpScmProtocol.serve(request, response, servletConfig);
+
+ verify(delegateServlet, times(1)).init(servletConfig);
+ verify(delegateServlet, times(2)).service(request, response, REPOSITORY);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldFailForIllegalScmType() {
+ HttpScmProtocol httpScmProtocol = wrapper.get(new Repository("", "other", "space", "name"));
+ }
+
+ private OngoingStubbing mockSetPathInfo() {
+ return when(pathInfoStore.get()).thenReturn(() -> URI.create("http://example.com/scm/api/rest/"));
+ }
+
+}
diff --git a/scm-core/src/test/java/sonia/scm/web/filter/PermissionFilterTest.java b/scm-core/src/test/java/sonia/scm/web/filter/PermissionFilterTest.java
new file mode 100644
index 0000000000..9fa65d51b8
--- /dev/null
+++ b/scm-core/src/test/java/sonia/scm/web/filter/PermissionFilterTest.java
@@ -0,0 +1,74 @@
+package sonia.scm.web.filter;
+
+import com.github.sdorra.shiro.ShiroRule;
+import com.github.sdorra.shiro.SubjectAware;
+import org.junit.Rule;
+import org.junit.Test;
+import sonia.scm.config.ScmConfiguration;
+import sonia.scm.repository.Repository;
+import sonia.scm.repository.spi.ScmProviderHttpServlet;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+@SubjectAware(configuration = "classpath:sonia/scm/shiro.ini")
+public class PermissionFilterTest {
+
+ public static final Repository REPOSITORY = new Repository("1", "git", "space", "name");
+
+ @Rule
+ public final ShiroRule shiroRule = new ShiroRule();
+
+ private final ScmProviderHttpServlet delegateServlet = mock(ScmProviderHttpServlet.class);
+
+ private final PermissionFilter permissionFilter = new PermissionFilter(new ScmConfiguration(), delegateServlet) {
+ @Override
+ protected boolean isWriteRequest(HttpServletRequest request) {
+ return writeRequest;
+ }
+ };
+
+ private final HttpServletRequest request = mock(HttpServletRequest.class);
+ private final HttpServletResponse response = mock(HttpServletResponse.class);
+
+ private boolean writeRequest = false;
+
+ @Test
+ @SubjectAware(username = "reader", password = "secret")
+ public void shouldPassForReaderOnReadRequest() throws IOException, ServletException {
+ writeRequest = false;
+
+ permissionFilter.service(request, response, REPOSITORY);
+
+ verify(delegateServlet).service(request, response, REPOSITORY);
+ }
+
+ @Test
+ @SubjectAware(username = "reader", password = "secret")
+ public void shouldBlockForReaderOnWriteRequest() throws IOException, ServletException {
+ writeRequest = true;
+
+ permissionFilter.service(request, response, REPOSITORY);
+
+ verify(response).sendError(eq(401), anyString());
+ verify(delegateServlet, never()).service(request, response, REPOSITORY);
+ }
+
+ @Test
+ @SubjectAware(username = "writer", password = "secret")
+ public void shouldPassForWriterOnWriteRequest() throws IOException, ServletException {
+ writeRequest = true;
+
+ permissionFilter.service(request, response, REPOSITORY);
+
+ verify(delegateServlet).service(request, response, REPOSITORY);
+ }
+}
diff --git a/scm-core/src/test/resources/sonia/scm/shiro.ini b/scm-core/src/test/resources/sonia/scm/shiro.ini
index e87c81b097..fbdd35ba50 100644
--- a/scm-core/src/test/resources/sonia/scm/shiro.ini
+++ b/scm-core/src/test/resources/sonia/scm/shiro.ini
@@ -1,6 +1,12 @@
[users]
trillian = secret, user
+admin = secret, admin
+writer = secret, repo_write
+reader = secret, repo_read
+unpriv = secret
[roles]
admin = *
-user = something:*
\ No newline at end of file
+user = something:*
+repo_read = "repository:read:1"
+repo_write = "repository:push:1"
diff --git a/scm-it/src/test/java/sonia/scm/it/RepositoryUtil.java b/scm-it/src/test/java/sonia/scm/it/RepositoryUtil.java
index 96b92a0bed..2340588fe1 100644
--- a/scm-it/src/test/java/sonia/scm/it/RepositoryUtil.java
+++ b/scm-it/src/test/java/sonia/scm/it/RepositoryUtil.java
@@ -31,7 +31,7 @@ public class RepositoryUtil {
static RepositoryClient createRepositoryClient(String repositoryType, File folder, String username, String password) throws IOException {
String httpProtocolUrl = TestData.callRepository(username, password, repositoryType, HttpStatus.SC_OK)
.extract()
- .path("_links.httpProtocol.href");
+ .path("_links.protocol.find{it.name=='http'}.href");
return REPOSITORY_CLIENT_FACTORY.create(repositoryType, httpProtocolUrl, username, password, folder);
}
diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/api/v2/resources/GitConfigToGitConfigDtoMapper.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/api/v2/resources/GitConfigToGitConfigDtoMapper.java
index 7163497487..7607b31faf 100644
--- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/api/v2/resources/GitConfigToGitConfigDtoMapper.java
+++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/api/v2/resources/GitConfigToGitConfigDtoMapper.java
@@ -18,7 +18,7 @@ import static de.otto.edison.hal.Links.linkingTo;
public abstract class GitConfigToGitConfigDtoMapper extends BaseMapper {
@Inject
- private UriInfoStore uriInfoStore;
+ private ScmPathInfoStore scmPathInfoStore;
@AfterMapping
void appendLinks(GitConfig config, @MappingTarget GitConfigDto target) {
@@ -30,12 +30,12 @@ public abstract class GitConfigToGitConfigDtoMapper extends BaseMapper webTokenGenerators)
- {
- super(configuration, webTokenGenerators);
- }
-}
diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java
index 1f07753f1e..e38f26a309 100644
--- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java
+++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilter.java
@@ -33,38 +33,24 @@
package sonia.scm.web;
-//~--- non-JDK imports --------------------------------------------------------
-
import com.google.common.annotations.VisibleForTesting;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-
import org.eclipse.jgit.http.server.GitSmartHttpTools;
-
import sonia.scm.ClientMessages;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.repository.GitUtil;
-import sonia.scm.repository.RepositoryProvider;
-import sonia.scm.web.filter.ProviderPermissionFilter;
-
-//~--- JDK imports ------------------------------------------------------------
-
-import java.io.IOException;
+import sonia.scm.repository.spi.ScmProviderHttpServlet;
+import sonia.scm.web.filter.PermissionFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import sonia.scm.Priority;
-import sonia.scm.filter.Filters;
-import sonia.scm.filter.WebElement;
+import java.io.IOException;
/**
* GitPermissionFilter decides if a git request requires write or read privileges.
*
* @author Sebastian Sdorra
*/
-@Priority(Filters.PRIORITY_AUTHORIZATION)
-@WebElement(value = GitServletModule.PATTERN_GIT)
-public class GitPermissionFilter extends ProviderPermissionFilter
+public class GitPermissionFilter extends PermissionFilter
{
private static final String PARAMETER_SERVICE = "service";
@@ -83,11 +69,9 @@ public class GitPermissionFilter extends ProviderPermissionFilter
* Constructs a new instance of the GitPermissionFilter.
*
* @param configuration scm main configuration
- * @param repositoryProvider repository provider
*/
- @Inject
- public GitPermissionFilter(ScmConfiguration configuration, RepositoryProvider repositoryProvider) {
- super(configuration, repositoryProvider);
+ public GitPermissionFilter(ScmConfiguration configuration, ScmProviderHttpServlet delegate) {
+ super(configuration, delegate);
}
@Override
@@ -103,7 +87,7 @@ public class GitPermissionFilter extends ProviderPermissionFilter
}
@Override
- protected boolean isWriteRequest(HttpServletRequest request) {
+ public boolean isWriteRequest(HttpServletRequest request) {
return isReceivePackRequest(request) ||
isReceiveServiceRequest(request) ||
isLfsFileUpload(request);
diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilterFactory.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilterFactory.java
new file mode 100644
index 0000000000..c358da5fb1
--- /dev/null
+++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitPermissionFilterFactory.java
@@ -0,0 +1,30 @@
+package sonia.scm.web;
+
+import sonia.scm.config.ScmConfiguration;
+import sonia.scm.plugin.Extension;
+import sonia.scm.repository.GitRepositoryHandler;
+import sonia.scm.repository.spi.ScmProviderHttpServlet;
+import sonia.scm.repository.spi.ScmProviderHttpServletDecoratorFactory;
+
+import javax.inject.Inject;
+
+@Extension
+public class GitPermissionFilterFactory implements ScmProviderHttpServletDecoratorFactory {
+
+ private final ScmConfiguration configuration;
+
+ @Inject
+ public GitPermissionFilterFactory(ScmConfiguration configuration) {
+ this.configuration = configuration;
+ }
+
+ @Override
+ public boolean handlesScmType(String type) {
+ return GitRepositoryHandler.TYPE_NAME.equals(type);
+ }
+
+ @Override
+ public ScmProviderHttpServlet createDecorator(ScmProviderHttpServlet delegate) {
+ return new GitPermissionFilter(configuration, delegate);
+ }
+}
diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitRepositoryResolver.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitRepositoryResolver.java
index 76e742a71a..7f04bb3a54 100644
--- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitRepositoryResolver.java
+++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitRepositoryResolver.java
@@ -125,8 +125,9 @@ public class GitRepositoryResolver implements RepositoryResolver uriInfoStore, ScmConfiguration scmConfiguration) {
+ super(servletProvider, uriInfoStore, scmConfiguration);
+ }
+
+ @Override
+ public String getType() {
+ return GitRepositoryHandler.TYPE_NAME;
+ }
+}
diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitServletModule.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitServletModule.java
index bdad103c15..e731e01a62 100644
--- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitServletModule.java
+++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/GitServletModule.java
@@ -51,18 +51,6 @@ import sonia.scm.web.lfs.LfsBlobStoreFactory;
public class GitServletModule extends ServletModule
{
- public static final String GIT_PATH = "/git";
-
- /** Field description */
- public static final String PATTERN_GIT = GIT_PATH + "/*";
-
-
- //~--- methods --------------------------------------------------------------
-
- /**
- * Method description
- *
- */
@Override
protected void configureServlets()
{
@@ -75,8 +63,5 @@ public class GitServletModule extends ServletModule
bind(GitConfigDtoToGitConfigMapper.class).to(Mappers.getMapper(GitConfigDtoToGitConfigMapper.class).getClass());
bind(GitConfigToGitConfigDtoMapper.class).to(Mappers.getMapper(GitConfigToGitConfigDtoMapper.class).getClass());
-
- // serlvelts and filters
- serve(PATTERN_GIT).with(ScmGitServlet.class);
}
}
diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/ScmGitServlet.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/ScmGitServlet.java
index 5612a64652..2701764607 100644
--- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/ScmGitServlet.java
+++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/web/ScmGitServlet.java
@@ -33,8 +33,6 @@
package sonia.scm.web;
-//~--- non-JDK imports --------------------------------------------------------
-
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -42,8 +40,8 @@ import org.eclipse.jgit.http.server.GitServlet;
import org.eclipse.jgit.lfs.lib.Constants;
import org.slf4j.Logger;
import sonia.scm.repository.Repository;
-import sonia.scm.repository.RepositoryProvider;
import sonia.scm.repository.RepositoryRequestListenerUtil;
+import sonia.scm.repository.spi.ScmProviderHttpServlet;
import sonia.scm.util.HttpUtil;
import sonia.scm.web.lfs.servlet.LfsServletFactory;
@@ -57,19 +55,18 @@ import java.util.regex.Pattern;
import static org.eclipse.jgit.lfs.lib.Constants.CONTENT_TYPE_GIT_LFS_JSON;
import static org.slf4j.LoggerFactory.getLogger;
-//~--- JDK imports ------------------------------------------------------------
-
/**
*
* @author Sebastian Sdorra
*/
@Singleton
-public class ScmGitServlet extends GitServlet
+public class ScmGitServlet extends GitServlet implements ScmProviderHttpServlet
{
- /** Field description */
+ public static final String REPO_PATH = "/repo";
+
public static final Pattern REGEX_GITHTTPBACKEND = Pattern.compile(
- "(?x)^/git/(.*/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\\.(pack|idx))|git-(upload|receive)-pack))$"
+ "(?x)^/repo/(.*/(HEAD|info/refs|objects/(info/[^/]+|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\\.(pack|idx))|git-(upload|receive)-pack))$"
);
/** Field description */
@@ -88,7 +85,6 @@ public class ScmGitServlet extends GitServlet
* @param repositoryResolver
* @param receivePackFactory
* @param repositoryViewer
- * @param repositoryProvider
* @param repositoryRequestListenerUtil
* @param lfsServletFactory
*/
@@ -96,11 +92,9 @@ public class ScmGitServlet extends GitServlet
public ScmGitServlet(GitRepositoryResolver repositoryResolver,
GitReceivePackFactory receivePackFactory,
GitRepositoryViewer repositoryViewer,
- RepositoryProvider repositoryProvider,
RepositoryRequestListenerUtil repositoryRequestListenerUtil,
LfsServletFactory lfsServletFactory)
{
- this.repositoryProvider = repositoryProvider;
this.repositoryViewer = repositoryViewer;
this.repositoryRequestListenerUtil = repositoryRequestListenerUtil;
this.lfsServletFactory = lfsServletFactory;
@@ -122,44 +116,9 @@ public class ScmGitServlet extends GitServlet
* @throws ServletException
*/
@Override
- protected void service(HttpServletRequest request,
- HttpServletResponse response)
+ public void service(HttpServletRequest request, HttpServletResponse response, Repository repository)
throws ServletException, IOException
{
- Repository repository = repositoryProvider.get();
- if (repository != null) {
- handleRequest(request, response, repository);
- } else {
- // logger
- response.sendError(HttpServletResponse.SC_NOT_FOUND);
- }
- }
-
- /**
- * Decides the type request being currently made and delegates it accordingly.
- *
- *
Batch API:
- *
- *
used to provide the client with information on how handle the large files of a repository.
- *
response contains the information where to perform the actual upload and download of the large objects.
- *
- *
Transfer API:
- *
- *
receives and provides the actual large objects (resolves the pointer placed in the file of the working copy).
- *
invoked only after the Batch API has been questioned about what to do with the large files
- *
- *
Regular Git Http API:
- *
- *
regular git http wire protocol, use by normal git clients.