Introduce default error object with context for not found exceptions

This commit is contained in:
René Pfeuffer
2018-10-18 13:12:16 +02:00
parent a56aeca8d2
commit b74fb814b8
61 changed files with 395 additions and 1553 deletions

View File

@@ -0,0 +1,27 @@
package sonia.scm;
import sonia.scm.util.AssertUtil;
public class ContextEntry {
private final String type;
private final String id;
ContextEntry(Class type, String id) {
this(type.getSimpleName(), id);
}
ContextEntry(String type, String id) {
AssertUtil.assertIsNotEmpty(type);
AssertUtil.assertIsNotEmpty(id);
this.type = type;
this.id = id;
}
public String getType () {
return type;
}
public String getId () {
return id;
}
}

View File

@@ -1,10 +1,72 @@
package sonia.scm;
import sonia.scm.repository.Repository;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static java.util.Collections.unmodifiableList;
import static java.util.stream.Collectors.joining;
public class NotFoundException extends RuntimeException {
public NotFoundException(String type, String id) {
super(type + " with id '" + id + "' not found");
private final List<ContextEntry> context;
public NotFoundException(Class type, String id) {
this.context = Collections.singletonList(new ContextEntry(type, id));
}
public NotFoundException() {
public NotFoundException(String type, String id) {
this.context = Collections.singletonList(new ContextEntry(type, id));
}
private NotFoundException(List<ContextEntry> context) {
this.context = context;
}
public static NotFoundExceptionBuilder notFound(Class type, String id) {
NotFoundExceptionBuilder builder = new NotFoundExceptionBuilder();
return builder.in(type, id);
}
public static NotFoundExceptionBuilder notFound(String type, String id) {
NotFoundExceptionBuilder builder = new NotFoundExceptionBuilder();
return builder.in(type, id);
}
public List<ContextEntry> getContext() {
return unmodifiableList(context);
}
@Override
public String getMessage() {
return context.stream()
.map(c -> c.getType().toLowerCase() + " with id " + c.getId())
.collect(joining(" in ", "could not find ", ""));
}
public static class NotFoundExceptionBuilder {
private final List<ContextEntry> context = new LinkedList<>();
public NotFoundExceptionBuilder in(Repository repository) {
this.in(Repository.class, repository.getNamespaceAndName().logString());
return this;
}
public NotFoundExceptionBuilder in(Class type, String id) {
this.context.add(new ContextEntry(type, id));
return this;
}
public NotFoundExceptionBuilder in(String type, String id) {
this.context.add(new ContextEntry(type, id));
return this;
}
public NotFoundException build() {
return new NotFoundException(context);
}
}
}

View File

@@ -25,9 +25,13 @@ public class NamespaceAndName implements Comparable<NamespaceAndName> {
return name;
}
public String logString() {
return getNamespace() + "/" + getName();
}
@Override
public String toString() {
return getNamespace() + "/" + getName();
return logString();
}
@Override

View File

@@ -1,84 +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 sonia.scm.NotFoundException;
import sonia.scm.util.Util;
/**
* Signals that the specified path could be found.
*
* @author Sebastian Sdorra
*/
public class PathNotFoundException extends NotFoundException
{
/** Field description */
private static final long serialVersionUID = 4629690181172951809L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs a new {@link PathNotFoundException}
* with the specified path.
*
*
* @param path path which could not be found
*/
public PathNotFoundException(String path)
{
super("path", Util.nonNull(path));
this.path = Util.nonNull(path);
}
//~--- get methods ----------------------------------------------------------
/**
* Return the path which could not be found.
*
*
* @return path which could not be found
*/
public String getPath()
{
return path;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String path;
}

View File

@@ -45,7 +45,7 @@ public class RepositoryNotFoundException extends NotFoundException
{
private static final long serialVersionUID = -6583078808900520166L;
private static final String TYPE_REPOSITORY = "repository";
private static final String TYPE_REPOSITORY = "Repository";
//~--- constructors ---------------------------------------------------------
@@ -55,7 +55,7 @@ public class RepositoryNotFoundException extends NotFoundException
*
*/
public RepositoryNotFoundException(Repository repository) {
super(TYPE_REPOSITORY, repository.getName() + "/" + repository.getNamespace());
super(Repository.class, repository.getNamespaceAndName().logString());
}
public RepositoryNotFoundException(String repositoryId) {
@@ -63,6 +63,6 @@ public class RepositoryNotFoundException extends NotFoundException
}
public RepositoryNotFoundException(NamespaceAndName namespaceAndName) {
super(TYPE_REPOSITORY, namespaceAndName.toString());
super(Repository.class, namespaceAndName.logString());
}
}

View File

@@ -1,83 +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 sonia.scm.NotFoundException;
import sonia.scm.util.Util;
/**
* Signals that the specified revision could be found.
*
* @author Sebastian Sdorra
*/
public class RevisionNotFoundException extends NotFoundException {
/** Field description */
private static final long serialVersionUID = -5594008535358811998L;
//~--- constructors ---------------------------------------------------------
/**
* Constructs a new {@link RevisionNotFoundException}
* with the specified revision.
*
*
* @param revision revision which could not be found
*/
public RevisionNotFoundException(String revision)
{
super("revision", revision);
this.revision = Util.nonNull(revision);
}
//~--- get methods ----------------------------------------------------------
/**
* Return the revision which could not be found.
*
*
* @return revision which could not be found
*/
public String getRevision()
{
return revision;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String revision;
}

View File

@@ -46,7 +46,6 @@ import sonia.scm.repository.FileObjectNameComparator;
import sonia.scm.repository.PreProcessorUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryCacheKey;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.spi.BrowseCommand;
import sonia.scm.repository.spi.BrowseCommandRequest;
@@ -138,7 +137,7 @@ public final class BrowseCommandBuilder
*
* @throws IOException
*/
public BrowserResult getBrowserResult() throws IOException, RevisionNotFoundException {
public BrowserResult getBrowserResult() throws IOException {
BrowserResult result = null;
if (disableCache)

View File

@@ -37,9 +37,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.spi.CatCommand;
import sonia.scm.repository.spi.CatCommandRequest;
import sonia.scm.util.IOUtil;
@@ -107,7 +105,7 @@ public final class CatCommandBuilder
* @param outputStream output stream for the content
* @param path file path
*/
public void retriveContent(OutputStream outputStream, String path) throws IOException, PathNotFoundException, RevisionNotFoundException {
public void retriveContent(OutputStream outputStream, String path) throws IOException {
getCatResult(outputStream, path);
}
@@ -116,7 +114,7 @@ public final class CatCommandBuilder
*
* @param path file path
*/
public InputStream getStream(String path) throws IOException, PathNotFoundException, RevisionNotFoundException {
public InputStream getStream(String path) throws IOException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(path),
"path is required");
@@ -139,7 +137,7 @@ public final class CatCommandBuilder
*
* @throws IOException
*/
public String getContent(String path) throws IOException, PathNotFoundException, RevisionNotFoundException {
public String getContent(String path) throws IOException {
String content = null;
ByteArrayOutputStream baos = null;
@@ -186,7 +184,7 @@ public final class CatCommandBuilder
* @throws IOException
*/
private void getCatResult(OutputStream outputStream, String path)
throws IOException, PathNotFoundException, RevisionNotFoundException {
throws IOException {
Preconditions.checkNotNull(outputStream, "OutputStream is required");
Preconditions.checkArgument(!Strings.isNullOrEmpty(path),
"path is required");

View File

@@ -38,7 +38,6 @@ package sonia.scm.repository.api;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.spi.DiffCommand;
import sonia.scm.repository.spi.DiffCommandRequest;
import sonia.scm.util.IOUtil;
@@ -104,7 +103,7 @@ public final class DiffCommandBuilder
*
* @throws IOException
*/
public DiffCommandBuilder retriveContent(OutputStream outputStream) throws IOException, RevisionNotFoundException {
public DiffCommandBuilder retriveContent(OutputStream outputStream) throws IOException {
getDiffResult(outputStream);
return this;
@@ -119,7 +118,7 @@ public final class DiffCommandBuilder
*
* @throws IOException
*/
public String getContent() throws IOException, RevisionNotFoundException {
public String getContent() throws IOException {
String content = null;
ByteArrayOutputStream baos = null;
@@ -199,7 +198,7 @@ public final class DiffCommandBuilder
*
* @throws IOException
*/
private void getDiffResult(OutputStream outputStream) throws IOException, RevisionNotFoundException {
private void getDiffResult(OutputStream outputStream) throws IOException {
Preconditions.checkNotNull(outputStream, "OutputStream is required");
Preconditions.checkArgument(request.isValid(),
"path and/or revision is required");

View File

@@ -46,7 +46,6 @@ import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.PreProcessorUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryCacheKey;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.spi.LogCommand;
import sonia.scm.repository.spi.LogCommandRequest;
@@ -165,7 +164,7 @@ public final class LogCommandBuilder
*
* @throws IOException
*/
public Changeset getChangeset(String id) throws IOException, RevisionNotFoundException {
public Changeset getChangeset(String id) throws IOException {
Changeset changeset;
if (disableCache)
@@ -224,7 +223,7 @@ public final class LogCommandBuilder
*
* @throws IOException
*/
public ChangesetPagingResult getChangesets() throws IOException, RevisionNotFoundException {
public ChangesetPagingResult getChangesets() throws IOException {
ChangesetPagingResult cpr;
if (disableCache)

View File

@@ -13,7 +13,6 @@ import sonia.scm.repository.Modifications;
import sonia.scm.repository.PreProcessorUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryCacheKey;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.spi.ModificationsCommand;
import sonia.scm.repository.spi.ModificationsCommandRequest;
@@ -67,7 +66,7 @@ public final class ModificationsCommandBuilder {
return this;
}
public Modifications getModifications() throws IOException, RevisionNotFoundException {
public Modifications getModifications() throws IOException {
Modifications modifications;
if (disableCache) {
log.info("Get modifications for {} with disabled cache", request);

View File

@@ -152,37 +152,6 @@ public final class RepositoryServiceFactory
//~--- methods --------------------------------------------------------------
/**
* Creates a new RepositoryService for the given repository.
*
*
* @param repositoryId id of the repository
*
* @return a implementation of RepositoryService
* for the given type of repository
*
* @throws RepositoryNotFoundException if no repository
* with the given id is available
* @throws RepositoryServiceNotFoundException if no repository service
* implementation for this kind of repository is available
* @throws IllegalArgumentException if the repository id is null or empty
* @throws ScmSecurityException if current user has not read permissions
* for that repository
*/
public RepositoryService create(String repositoryId) throws RepositoryNotFoundException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(repositoryId),
"a non empty repositoryId is required");
Repository repository = repositoryManager.get(repositoryId);
if (repository == null)
{
throw new RepositoryNotFoundException(repositoryId);
}
return create(repository);
}
/**
* Creates a new RepositoryService for the given repository.
*

View File

@@ -36,7 +36,6 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
@@ -60,4 +59,5 @@ public interface BrowseCommand
*
* @throws IOException
*/
BrowserResult getBrowserResult(BrowseCommandRequest request) throws IOException, RevisionNotFoundException;}
BrowserResult getBrowserResult(BrowseCommandRequest request) throws IOException;
}

View File

@@ -33,9 +33,6 @@
package sonia.scm.repository.spi;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -47,7 +44,7 @@ import java.io.OutputStream;
*/
public interface CatCommand {
void getCatResult(CatCommandRequest request, OutputStream output) throws IOException, RevisionNotFoundException, PathNotFoundException;
void getCatResult(CatCommandRequest request, OutputStream output) throws IOException;
InputStream getCatResultStream(CatCommandRequest request) throws IOException, RevisionNotFoundException, PathNotFoundException;
InputStream getCatResultStream(CatCommandRequest request) throws IOException;
}

View File

@@ -33,8 +33,6 @@
package sonia.scm.repository.spi;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
@@ -56,5 +54,5 @@ public interface DiffCommand
* @throws IOException
* @throws RuntimeException
*/
public void getDiffResult(DiffCommandRequest request, OutputStream output) throws IOException, RevisionNotFoundException;
public void getDiffResult(DiffCommandRequest request, OutputStream output) throws IOException;
}

View File

@@ -37,7 +37,6 @@ package sonia.scm.repository.spi;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
@@ -50,7 +49,7 @@ import java.io.IOException;
*/
public interface LogCommand {
Changeset getChangeset(String id) throws IOException, RevisionNotFoundException;
Changeset getChangeset(String id) throws IOException;
ChangesetPagingResult getChangesets(LogCommandRequest request) throws IOException, RevisionNotFoundException;
ChangesetPagingResult getChangesets(LogCommandRequest request) throws IOException;
}

View File

@@ -32,7 +32,6 @@
package sonia.scm.repository.spi;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
@@ -46,8 +45,8 @@ import java.io.IOException;
*/
public interface ModificationsCommand {
Modifications getModifications(String revision) throws IOException, RevisionNotFoundException;
Modifications getModifications(String revision) throws IOException;
Modifications getModifications(ModificationsCommandRequest request) throws IOException, RevisionNotFoundException;
Modifications getModifications(ModificationsCommandRequest request) throws IOException;
}

View File

@@ -44,6 +44,7 @@ public class VndMediaType {
public static final String ME = PREFIX + "me" + SUFFIX;
public static final String SOURCE = PREFIX + "source" + SUFFIX;
public static final String ERROR_TYPE = PREFIX + "error" + SUFFIX;
private VndMediaType() {
}

View File

@@ -50,13 +50,12 @@ import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.GitSubModuleParser;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SubRepository;
import sonia.scm.util.Util;
@@ -103,7 +102,7 @@ public class GitBrowseCommand extends AbstractGitCommand
@Override
@SuppressWarnings("unchecked")
public BrowserResult getBrowserResult(BrowseCommandRequest request)
throws IOException, RevisionNotFoundException {
throws IOException {
logger.debug("try to create browse result for {}", request);
BrowserResult result;
@@ -157,7 +156,7 @@ public class GitBrowseCommand extends AbstractGitCommand
*/
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
throws IOException, RevisionNotFoundException {
throws IOException {
FileObject file;
try
@@ -267,7 +266,7 @@ public class GitBrowseCommand extends AbstractGitCommand
private BrowserResult getResult(org.eclipse.jgit.lib.Repository repo,
BrowseCommandRequest request, ObjectId revId)
throws IOException, RevisionNotFoundException {
throws IOException {
BrowserResult result = null;
RevWalk revWalk = null;
TreeWalk treeWalk = null;
@@ -364,7 +363,7 @@ public class GitBrowseCommand extends AbstractGitCommand
private Map<String,
SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
ObjectId revision)
throws IOException, RevisionNotFoundException {
throws IOException {
if (logger.isDebugEnabled())
{
logger.debug("read submodules of {} at {}", repository.getName(),
@@ -378,7 +377,7 @@ public class GitBrowseCommand extends AbstractGitCommand
PATH_MODULES, baos);
subRepositories = GitSubModuleParser.parse(baos.toString());
}
catch (PathNotFoundException ex)
catch (NotFoundException ex)
{
logger.trace("could not find .gitmodules", ex);
subRepositories = Collections.EMPTY_MAP;
@@ -389,7 +388,7 @@ public class GitBrowseCommand extends AbstractGitCommand
private SubRepository getSubRepository(org.eclipse.jgit.lib.Repository repo,
ObjectId revId, String path)
throws IOException, RevisionNotFoundException {
throws IOException {
Map<String, SubRepository> subRepositories = subrepositoryCache.get(revId);
if (subRepositories == null)

View File

@@ -44,9 +44,8 @@ import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.util.Util;
import java.io.Closeable;
@@ -65,7 +64,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
}
@Override
public void getCatResult(CatCommandRequest request, OutputStream output) throws IOException, PathNotFoundException, RevisionNotFoundException {
public void getCatResult(CatCommandRequest request, OutputStream output) throws IOException {
logger.debug("try to read content for {}", request);
try (ClosableObjectLoaderContainer closableObjectLoaderContainer = getLoader(request)) {
closableObjectLoaderContainer.objectLoader.copyTo(output);
@@ -73,24 +72,24 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
}
@Override
public InputStream getCatResultStream(CatCommandRequest request) throws IOException, PathNotFoundException, RevisionNotFoundException {
public InputStream getCatResultStream(CatCommandRequest request) throws IOException {
logger.debug("try to read content for {}", request);
return new InputStreamWrapper(getLoader(request));
}
void getContent(org.eclipse.jgit.lib.Repository repo, ObjectId revId, String path, OutputStream output) throws IOException, PathNotFoundException, RevisionNotFoundException {
void getContent(org.eclipse.jgit.lib.Repository repo, ObjectId revId, String path, OutputStream output) throws IOException {
try (ClosableObjectLoaderContainer closableObjectLoaderContainer = getLoader(repo, revId, path)) {
closableObjectLoaderContainer.objectLoader.copyTo(output);
}
}
private ClosableObjectLoaderContainer getLoader(CatCommandRequest request) throws IOException, PathNotFoundException, RevisionNotFoundException {
private ClosableObjectLoaderContainer getLoader(CatCommandRequest request) throws IOException {
org.eclipse.jgit.lib.Repository repo = open();
ObjectId revId = getCommitOrDefault(repo, request.getRevision());
return getLoader(repo, revId, request.getPath());
}
private ClosableObjectLoaderContainer getLoader(Repository repo, ObjectId revId, String path) throws IOException, PathNotFoundException, RevisionNotFoundException {
private ClosableObjectLoaderContainer getLoader(Repository repo, ObjectId revId, String path) throws IOException {
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.setRecursive(Util.nonNull(path).contains("/"));
@@ -102,7 +101,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
try {
entry = revWalk.parseCommit(revId);
} catch (MissingObjectException e) {
throw new RevisionNotFoundException(revId.getName());
throw NotFoundException.notFound("Revision", revId.getName()).in(repository).build();
}
RevTree revTree = entry.getTree();
@@ -120,7 +119,7 @@ public class GitCatCommand extends AbstractGitCommand implements CatCommand {
return new ClosableObjectLoaderContainer(loader, treeWalk, revWalk);
} else {
throw new PathNotFoundException(path);
throw NotFoundException.notFound("Path", path).in("Revision", revId.getName()).in(repository).build();
}
}

View File

@@ -48,12 +48,12 @@ import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.GitChangesetConverter;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.util.IOUtil;
import java.io.IOException;
@@ -85,7 +85,6 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
*
* @param context
* @param repository
* @param repositoryDirectory
*/
GitLogCommand(GitContext context, sonia.scm.repository.Repository repository)
{
@@ -162,7 +161,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
*/
@Override
@SuppressWarnings("unchecked")
public ChangesetPagingResult getChangesets(LogCommandRequest request) throws RevisionNotFoundException {
public ChangesetPagingResult getChangesets(LogCommandRequest request) {
if (logger.isDebugEnabled()) {
logger.debug("fetch changesets for request: {}", request);
}
@@ -249,7 +248,7 @@ public class GitLogCommand extends AbstractGitCommand implements LogCommand
}
catch (MissingObjectException e)
{
throw new RevisionNotFoundException(e.getObjectId().name());
throw NotFoundException.notFound("Revision", e.getObjectId().getName()).in(repository).build();
}
catch (Exception ex)
{

View File

@@ -39,7 +39,6 @@ import org.junit.Test;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
import java.util.List;
@@ -63,7 +62,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
* Test browse command with default branch.
*/
@Test
public void testDefaultBranch() throws IOException, RevisionNotFoundException {
public void testDefaultBranch() throws IOException {
// without default branch, the repository head should be used
BrowserResult result = createCommand().getBrowserResult(new BrowseCommandRequest());
assertNotNull(result);
@@ -93,7 +92,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
}
@Test
public void testBrowse() throws IOException, RevisionNotFoundException {
public void testBrowse() throws IOException {
BrowserResult result =
createCommand().getBrowserResult(new BrowseCommandRequest());
@@ -134,7 +133,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
}
@Test
public void testBrowseSubDirectory() throws IOException, RevisionNotFoundException {
public void testBrowseSubDirectory() throws IOException {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setPath("c");
@@ -181,7 +180,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase
}
@Test
public void testRecusive() throws IOException, RevisionNotFoundException {
public void testRecusive() throws IOException {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setRecursive(true);

View File

@@ -32,10 +32,13 @@
package sonia.scm.repository.spi;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import sonia.scm.NotFoundException;
import sonia.scm.repository.GitConstants;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -52,8 +55,11 @@ import static org.junit.Assert.assertEquals;
*/
public class GitCatCommandTest extends AbstractGitCommandTestBase {
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void testDefaultBranch() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testDefaultBranch() throws IOException {
// without default branch, the repository head should be used
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
@@ -66,7 +72,7 @@ public class GitCatCommandTest extends AbstractGitCommandTestBase {
}
@Test
public void testCat() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testCat() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
@@ -75,32 +81,58 @@ public class GitCatCommandTest extends AbstractGitCommandTestBase {
}
@Test
public void testSimpleCat() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testSimpleCat() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("b.txt");
assertEquals("b", execute(request));
}
@Test(expected = PathNotFoundException.class)
public void testUnknownFile() throws IOException, PathNotFoundException, RevisionNotFoundException {
@Test
public void testUnknownFile() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("unknown");
execute(request);
expectedException.expect(new BaseMatcher<Object>() {
@Override
public void describeTo(Description description) {
description.appendText("expected NotFoundException for path");
}
@Test(expected = RevisionNotFoundException.class)
public void testUnknownRevision() throws IOException, PathNotFoundException, RevisionNotFoundException {
CatCommandRequest request = new CatCommandRequest();
@Override
public boolean matches(Object item) {
return "Path".equals(((NotFoundException)item).getContext().get(0).getType());
}
});
request.setRevision("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
request.setPath("a.txt");
execute(request);
}
@Test
public void testSimpleStream() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testUnknownRevision() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setRevision("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
request.setPath("a.txt");
expectedException.expect(new BaseMatcher<Object>() {
@Override
public void describeTo(Description description) {
description.appendText("expected NotFoundException for revision");
}
@Override
public boolean matches(Object item) {
return "Revision".equals(((NotFoundException)item).getContext().get(0).getType());
}
});
execute(request);
}
@Test
public void testSimpleStream() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("b.txt");
@@ -113,7 +145,7 @@ public class GitCatCommandTest extends AbstractGitCommandTestBase {
catResultStream.close();
}
private String execute(CatCommandRequest request) throws IOException, PathNotFoundException, RevisionNotFoundException {
private String execute(CatCommandRequest request) throws IOException {
String content = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();

View File

@@ -39,7 +39,6 @@ import org.junit.Test;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
@@ -151,7 +150,7 @@ public class HgLogCommandTest extends AbstractHgCommandTestBase
}
@Test
public void testGetCommit() throws IOException, RevisionNotFoundException {
public void testGetCommit() throws IOException {
HgLogCommand command = createComamnd();
String revision = "a9bacaf1b7fa0cebfca71fed4e59ed69a6319427";
Changeset c =

View File

@@ -49,6 +49,7 @@ import org.tmatesoft.svn.core.internal.util.SVNEncodingUtil;
import org.tmatesoft.svn.core.internal.util.SVNXMLUtil;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import sonia.scm.NotFoundException;
import sonia.scm.util.HttpUtil;
import sonia.scm.util.Util;
@@ -102,7 +103,7 @@ public final class SvnUtil
//~--- methods --------------------------------------------------------------
public static long parseRevision(String v) throws RevisionNotFoundException {
public static long parseRevision(String v, Repository repository) {
long result = -1l;
if (!Strings.isNullOrEmpty(v))
@@ -113,7 +114,7 @@ public final class SvnUtil
}
catch (NumberFormatException ex)
{
throw new RevisionNotFoundException(v);
throw NotFoundException.notFound("Revision", v).in(repository).build();
}
}
@@ -339,7 +340,7 @@ public final class SvnUtil
}
}
public static long getRevisionNumber(String revision) throws RevisionNotFoundException {
public static long getRevisionNumber(String revision, Repository repository) {
// REVIEW Bei SVN wird ohne Revision die -1 genommen, was zu einem Fehler führt
long revisionNumber = -1;
@@ -351,7 +352,7 @@ public final class SvnUtil
}
catch (NumberFormatException ex)
{
throw new RevisionNotFoundException(revision);
throw NotFoundException.notFound("Revision", revision).in(repository).build();
}
}

View File

@@ -47,7 +47,6 @@ import org.tmatesoft.svn.core.io.SVNRepository;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SubRepository;
import sonia.scm.repository.SvnUtil;
import sonia.scm.util.Util;
@@ -78,9 +77,9 @@ public class SvnBrowseCommand extends AbstractSvnCommand
@Override
@SuppressWarnings("unchecked")
public BrowserResult getBrowserResult(BrowseCommandRequest request) throws RevisionNotFoundException {
public BrowserResult getBrowserResult(BrowseCommandRequest request) {
String path = request.getPath();
long revisionNumber = SvnUtil.getRevisionNumber(request.getRevision());
long revisionNumber = SvnUtil.getRevisionNumber(request.getRevision(), repository);
if (logger.isDebugEnabled()) {
logger.debug("browser repository {} in path {} at revision {}", repository.getName(), path, revisionNumber);

View File

@@ -43,10 +43,9 @@ import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.admin.SVNLookClient;
import sonia.scm.NotFoundException;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SvnUtil;
import java.io.ByteArrayInputStream;
@@ -79,7 +78,7 @@ public class SvnCatCommand extends AbstractSvnCommand implements CatCommand
//~--- get methods ----------------------------------------------------------
@Override
public void getCatResult(CatCommandRequest request, OutputStream output) throws RevisionNotFoundException, PathNotFoundException {
public void getCatResult(CatCommandRequest request, OutputStream output) {
if (logger.isDebugEnabled())
{
logger.debug("try to get content for {}", request);
@@ -96,14 +95,14 @@ public class SvnCatCommand extends AbstractSvnCommand implements CatCommand
else
{
long revisionNumber = SvnUtil.getRevisionNumber(revision);
long revisionNumber = SvnUtil.getRevisionNumber(revision, repository);
getCatFromRevision(request, output, revisionNumber);
}
}
@Override
public InputStream getCatResultStream(CatCommandRequest request) throws RevisionNotFoundException, PathNotFoundException {
public InputStream getCatResultStream(CatCommandRequest request) {
// There seems to be no method creating an input stream as a result, so
// we have no other possibility then to copy the content into a buffer and
// stream it from there.
@@ -112,7 +111,7 @@ public class SvnCatCommand extends AbstractSvnCommand implements CatCommand
return new ByteArrayInputStream(output.toByteArray());
}
private void getCatFromRevision(CatCommandRequest request, OutputStream output, long revision) throws PathNotFoundException, RevisionNotFoundException {
private void getCatFromRevision(CatCommandRequest request, OutputStream output, long revision) {
logger.debug("try to read content from revision {} and path {}", revision,
request.getPath());
@@ -129,12 +128,12 @@ public class SvnCatCommand extends AbstractSvnCommand implements CatCommand
}
}
private void handleSvnException(CatCommandRequest request, SVNException ex) throws PathNotFoundException, RevisionNotFoundException {
private void handleSvnException(CatCommandRequest request, SVNException ex) {
int svnErrorCode = ex.getErrorMessage().getErrorCode().getCode();
if (SVNErrorCode.FS_NOT_FOUND.getCode() == svnErrorCode) {
throw new PathNotFoundException(request.getPath());
throw NotFoundException.notFound("Path", request.getPath()).in("Revision", request.getRevision()).in(repository).build();
} else if (SVNErrorCode.FS_NO_SUCH_REVISION.getCode() == svnErrorCode) {
throw new RevisionNotFoundException(request.getRevision());
throw NotFoundException.notFound("Revision", request.getRevision()).in(repository).build();
} else {
throw new InternalRepositoryException("could not get content from revision", ex);
}

View File

@@ -48,7 +48,6 @@ import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SvnUtil;
import sonia.scm.repository.api.DiffFormat;
import sonia.scm.util.Util;
@@ -76,7 +75,7 @@ public class SvnDiffCommand extends AbstractSvnCommand implements DiffCommand
}
@Override
public void getDiffResult(DiffCommandRequest request, OutputStream output) throws RevisionNotFoundException {
public void getDiffResult(DiffCommandRequest request, OutputStream output) {
if (logger.isDebugEnabled())
{
logger.debug("create diff for {}", request);
@@ -111,7 +110,7 @@ public class SvnDiffCommand extends AbstractSvnCommand implements DiffCommand
diffGenerator.setDiffDeleted(true);
diffClient.setDiffGenerator(diffGenerator);
long currentRev = SvnUtil.getRevisionNumber(request.getRevision());
long currentRev = SvnUtil.getRevisionNumber(request.getRevision(), repository);
diffClient.setGitDiffFormat(request.getFormat() == DiffFormat.GIT);

View File

@@ -47,7 +47,6 @@ import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SvnUtil;
import sonia.scm.util.Util;
@@ -76,7 +75,7 @@ public class SvnLogCommand extends AbstractSvnCommand implements LogCommand
@Override
@SuppressWarnings("unchecked")
public Changeset getChangeset(String revision) throws RevisionNotFoundException {
public Changeset getChangeset(String revision) {
Changeset changeset = null;
if (logger.isDebugEnabled())
@@ -86,7 +85,7 @@ public class SvnLogCommand extends AbstractSvnCommand implements LogCommand
try
{
long revisioNumber = parseRevision(revision);
long revisioNumber = parseRevision(revision, repository);
SVNRepository repo = open();
Collection<SVNLogEntry> entries = repo.log(null, null, revisioNumber,
revisioNumber, true, true);
@@ -106,7 +105,7 @@ public class SvnLogCommand extends AbstractSvnCommand implements LogCommand
@Override
@SuppressWarnings("unchecked")
public ChangesetPagingResult getChangesets(LogCommandRequest request) throws RevisionNotFoundException {
public ChangesetPagingResult getChangesets(LogCommandRequest request) {
if (logger.isDebugEnabled())
{
logger.debug("fetch changesets for {}", request);
@@ -115,8 +114,8 @@ public class SvnLogCommand extends AbstractSvnCommand implements LogCommand
ChangesetPagingResult changesets = null;
int start = request.getPagingStart();
int limit = request.getPagingLimit();
long startRevision = parseRevision(request.getStartChangeset());
long endRevision = parseRevision(request.getEndChangeset());
long startRevision = parseRevision(request.getStartChangeset(), repository);
long endRevision = parseRevision(request.getEndChangeset(), repository);
String[] pathArray = null;
if (!Strings.isNullOrEmpty(request.getPath()))

View File

@@ -7,11 +7,9 @@ import org.tmatesoft.svn.core.io.SVNRepository;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.SvnUtil;
import sonia.scm.util.Util;
import java.io.IOException;
import java.util.Collection;
@Slf4j
@@ -24,11 +22,11 @@ public class SvnModificationsCommand extends AbstractSvnCommand implements Modif
@Override
@SuppressWarnings("unchecked")
public Modifications getModifications(String revision) throws IOException, RevisionNotFoundException {
public Modifications getModifications(String revision) {
Modifications modifications = null;
log.debug("get modifications {}", revision);
try {
long revisionNumber = SvnUtil.parseRevision(revision);
long revisionNumber = SvnUtil.parseRevision(revision, repository);
SVNRepository repo = open();
Collection<SVNLogEntry> entries = repo.log(null, null, revisionNumber,
revisionNumber, true, true);
@@ -42,7 +40,7 @@ public class SvnModificationsCommand extends AbstractSvnCommand implements Modif
}
@Override
public Modifications getModifications(ModificationsCommandRequest request) throws IOException, RevisionNotFoundException {
public Modifications getModifications(ModificationsCommandRequest request) {
return getModifications(request.getRevision());
}

View File

@@ -38,7 +38,6 @@ package sonia.scm.repository.spi;
import org.junit.Test;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
import java.util.List;
@@ -59,7 +58,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
{
@Test
public void testBrowse() throws RevisionNotFoundException {
public void testBrowse() {
List<FileObject> foList = getRootFromTip(new BrowseCommandRequest());
FileObject a = getFileObject(foList, "a.txt");
@@ -83,7 +82,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
* @throws IOException
*/
@Test
public void testBrowseSubDirectory() throws RevisionNotFoundException {
public void testBrowseSubDirectory() {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setPath("c");
@@ -130,7 +129,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testDisableLastCommit() throws RevisionNotFoundException {
public void testDisableLastCommit() {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setDisableLastCommit(true);
@@ -144,7 +143,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testRecursive() throws RevisionNotFoundException {
public void testRecursive() {
BrowseCommandRequest request = new BrowseCommandRequest();
request.setRecursive(true);
BrowserResult result = createCommand().getBrowserResult(request);
@@ -203,7 +202,7 @@ public class SvnBrowseCommandTest extends AbstractSvnCommandTestBase
return a;
}
private List<FileObject> getRootFromTip(BrowseCommandRequest request) throws RevisionNotFoundException {
private List<FileObject> getRootFromTip(BrowseCommandRequest request) {
BrowserResult result = createCommand().getBrowserResult(request);
assertNotNull(result);

View File

@@ -32,9 +32,12 @@
package sonia.scm.repository.spi;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Rule;
import org.junit.Test;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import org.junit.rules.ExpectedException;
import sonia.scm.NotFoundException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -46,8 +49,11 @@ import static org.junit.Assert.assertEquals;
public class SvnCatCommandTest extends AbstractSvnCommandTestBase {
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void testCat() throws PathNotFoundException, RevisionNotFoundException {
public void testCat() {
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
@@ -56,35 +62,59 @@ public class SvnCatCommandTest extends AbstractSvnCommandTestBase {
}
@Test
public void testSimpleCat() throws PathNotFoundException, RevisionNotFoundException {
public void testSimpleCat() {
CatCommandRequest request = new CatCommandRequest();
request.setPath("c/d.txt");
assertEquals("d", execute(request));
}
@Test(expected = PathNotFoundException.class)
public void testUnknownFile() throws PathNotFoundException, RevisionNotFoundException {
@Test
public void testUnknownFile() {
CatCommandRequest request = new CatCommandRequest();
request.setPath("unknown");
request.setRevision("1");
execute(request);
expectedException.expect(new BaseMatcher<Object>() {
@Override
public void describeTo(Description description) {
description.appendText("expected NotFoundException for path");
}
@Test(expected = RevisionNotFoundException.class)
public void testUnknownRevision() throws PathNotFoundException, RevisionNotFoundException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
request.setRevision("42");
@Override
public boolean matches(Object item) {
return "Path".equals(((NotFoundException)item).getContext().get(0).getType());
}
});
execute(request);
}
@Test
public void testSimpleStream() throws IOException, PathNotFoundException, RevisionNotFoundException {
public void testUnknownRevision() {
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
request.setRevision("42");
expectedException.expect(new BaseMatcher<Object>() {
@Override
public void describeTo(Description description) {
description.appendText("expected NotFoundException for revision");
}
@Override
public boolean matches(Object item) {
return "Revision".equals(((NotFoundException)item).getContext().get(0).getType());
}
});
execute(request);
}
@Test
public void testSimpleStream() throws IOException {
CatCommandRequest request = new CatCommandRequest();
request.setPath("a.txt");
request.setRevision("1");
@@ -98,7 +128,7 @@ public class SvnCatCommandTest extends AbstractSvnCommandTestBase {
catResultStream.close();
}
private String execute(CatCommandRequest request) throws PathNotFoundException, RevisionNotFoundException {
private String execute(CatCommandRequest request) {
String content = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();

View File

@@ -38,7 +38,6 @@ import org.junit.Test;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.RevisionNotFoundException;
import java.io.IOException;
@@ -57,7 +56,7 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase
{
@Test
public void testGetAll() throws RevisionNotFoundException {
public void testGetAll() {
ChangesetPagingResult result =
createCommand().getChangesets(new LogCommandRequest());
@@ -67,7 +66,7 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testGetAllByPath() throws RevisionNotFoundException {
public void testGetAllByPath() {
LogCommandRequest request = new LogCommandRequest();
request.setPath("a.txt");
@@ -83,7 +82,7 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testGetAllWithLimit() throws RevisionNotFoundException {
public void testGetAllWithLimit() {
LogCommandRequest request = new LogCommandRequest();
request.setPagingLimit(2);
@@ -106,7 +105,7 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testGetAllWithPaging() throws RevisionNotFoundException {
public void testGetAllWithPaging() {
LogCommandRequest request = new LogCommandRequest();
request.setPagingStart(1);
@@ -130,7 +129,7 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testGetCommit() throws RevisionNotFoundException, IOException {
public void testGetCommit() {
Changeset c = createCommand().getChangeset("3");
assertNotNull(c);
@@ -151,7 +150,7 @@ public class SvnLogCommandTest extends AbstractSvnCommandTestBase
}
@Test
public void testGetRange() throws RevisionNotFoundException {
public void testGetRange() {
LogCommandRequest request = new LogCommandRequest();
request.setStartChangeset("2");

View File

@@ -30,7 +30,7 @@ public class ManagerDaoAdapter<T extends ModelObject> {
afterUpdate.handle(notModified);
} else {
throw new NotFoundException();
throw new NotFoundException(object.getClass(), object.getId());
}
}
@@ -58,7 +58,7 @@ public class ManagerDaoAdapter<T extends ModelObject> {
dao.delete(toDelete);
afterDelete.handle(toDelete);
} else {
throw new NotFoundException();
throw new NotFoundException(toDelete.getClass(), toDelete.getId());
}
}

View File

@@ -2,8 +2,6 @@ package sonia.scm.api.rest.resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.CatCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.util.IOUtil;
@@ -34,18 +32,6 @@ public class BrowserStreamingOutput implements StreamingOutput {
public void write(OutputStream output) throws IOException {
try {
builder.retriveContent(output, path);
} catch (PathNotFoundException ex) {
if (logger.isWarnEnabled()) {
logger.warn("could not find path {}", ex.getPath());
}
throw new WebApplicationException(Response.Status.NOT_FOUND);
} catch (RevisionNotFoundException ex) {
if (logger.isWarnEnabled()) {
logger.warn("could not find revision {}", ex.getRevision());
}
throw new WebApplicationException(Response.Status.NOT_FOUND);
} finally {
IOUtil.close(repositoryService);
}

View File

@@ -37,7 +37,6 @@ package sonia.scm.api.rest.resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.util.IOUtil;
@@ -95,22 +94,6 @@ public class DiffStreamingOutput implements StreamingOutput
{
builder.retriveContent(output);
}
catch (RevisionNotFoundException ex)
{
if (logger.isWarnEnabled())
{
logger.warn("could not find revision {}", ex.getRevision());
}
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
// catch (RepositoryException ex)
// {
// logger.error("could not write content to page", ex);
//
// throw new WebApplicationException(ex,
// Response.Status.INTERNAL_SERVER_ERROR);
// }
finally
{
IOUtil.close(repositoryService);

View File

@@ -50,6 +50,7 @@ import sonia.scm.NotFoundException;
import sonia.scm.NotSupportedFeatuerException;
import sonia.scm.Type;
import sonia.scm.api.rest.RestActionUploadResult;
import sonia.scm.api.v2.resources.RepositoryResource;
import sonia.scm.repository.*;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.RepositoryService;

View File

@@ -5,6 +5,7 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.NotFoundException;
import sonia.scm.PageResult;
import sonia.scm.repository.Branch;
import sonia.scm.repository.Branches;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
@@ -105,7 +106,7 @@ public class BranchRootResource {
.stream()
.anyMatch(branch -> branchName.equals(branch.getName()));
if (!branchExists){
throw new NotFoundException("branch", branchName);
throw NotFoundException.notFound(Branch.class, branchName).in(Repository.class, namespace + "/" + name).build();
}
Repository repository = repositoryService.getRepository();
RepositoryPermissions.read(repository).check();

View File

@@ -11,8 +11,6 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RepositoryPermissions;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.LogCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.web.VndMediaType;
@@ -56,7 +54,7 @@ public class ChangesetRootResource {
@Produces(VndMediaType.CHANGESET_COLLECTION)
@TypeHint(CollectionDto.class)
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name, @DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("pageSize") int pageSize) throws IOException, RevisionNotFoundException, RepositoryNotFoundException {
@DefaultValue("10") @QueryParam("pageSize") int pageSize) throws IOException {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
Repository repository = repositoryService.getRepository();
RepositoryPermissions.read(repository).check();
@@ -89,7 +87,7 @@ public class ChangesetRootResource {
@Produces(VndMediaType.CHANGESET)
@TypeHint(ChangesetDto.class)
@Path("{id}")
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("id") String id) throws IOException, RevisionNotFoundException, RepositoryNotFoundException {
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("id") String id) throws IOException {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
Repository repository = repositoryService.getRepository();
RepositoryPermissions.read(repository).check();

View File

@@ -6,10 +6,9 @@ import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.util.IOUtil;
@@ -64,8 +63,8 @@ public class ContentResource {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
Response.ResponseBuilder responseBuilder = Response.ok(stream);
return createContentHeader(namespace, name, revision, path, repositoryService, responseBuilder);
} catch (RepositoryNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
} catch (NotFoundException e) {
LOG.debug(e.getMessage());
return Response.status(Status.NOT_FOUND).build();
}
}
@@ -75,14 +74,8 @@ public class ContentResource {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
repositoryService.getCatCommand().setRevision(revision).retriveContent(os, path);
os.close();
} catch (RepositoryNotFoundException e) {
LOG.debug("repository {}/{} not found", path, namespace, name, e);
throw new WebApplicationException(Status.NOT_FOUND);
} catch (PathNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
throw new WebApplicationException(Status.NOT_FOUND);
} catch (RevisionNotFoundException e) {
LOG.debug("revision '{}' not found in repository {}/{}", revision, namespace, name, e);
} catch (NotFoundException e) {
LOG.debug(e.getMessage());
throw new WebApplicationException(Status.NOT_FOUND);
}
};
@@ -111,8 +104,8 @@ public class ContentResource {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
Response.ResponseBuilder responseBuilder = Response.ok();
return createContentHeader(namespace, name, revision, path, repositoryService, responseBuilder);
} catch (RepositoryNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
} catch (NotFoundException e) {
LOG.debug(e.getMessage());
return Response.status(Status.NOT_FOUND).build();
}
}
@@ -120,12 +113,6 @@ public class ContentResource {
private Response createContentHeader(String namespace, String name, String revision, String path, RepositoryService repositoryService, Response.ResponseBuilder responseBuilder) {
try {
appendContentHeader(path, getHead(revision, path, repositoryService), responseBuilder);
} catch (PathNotFoundException e) {
LOG.debug("path '{}' not found in repository {}/{}", path, namespace, name, e);
return Response.status(Status.NOT_FOUND).build();
} catch (RevisionNotFoundException e) {
LOG.debug("revision '{}' not found in repository {}/{}", revision, namespace, name, e);
return Response.status(Status.NOT_FOUND).build();
} catch (IOException e) {
LOG.info("error reading repository resource {} from {}/{}", path, namespace, name, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
@@ -139,7 +126,7 @@ public class ContentResource {
contentType.getLanguage().ifPresent(language -> responseBuilder.header("Language", language));
}
private byte[] getHead(String revision, String path, RepositoryService repositoryService) throws IOException, PathNotFoundException, RevisionNotFoundException {
private byte[] getHead(String revision, String path, RepositoryService repositoryService) throws IOException {
InputStream stream = repositoryService.getCatCommand().setRevision(revision).getStream(path);
try {
byte[] buffer = new byte[HEAD_BUFFER_SIZE];

View File

@@ -4,7 +4,6 @@ import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.util.HttpUtil;
@@ -15,7 +14,6 @@ import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
@@ -54,13 +52,9 @@ public class DiffRootResource {
HttpUtil.checkForCRLFInjection(revision);
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
StreamingOutput responseEntry = output -> {
try {
repositoryService.getDiffCommand()
.setRevision(revision)
.retriveContent(output);
} catch (RevisionNotFoundException e) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
};
return Response.ok(responseEntry)
.header(HEADER_CONTENT_DISPOSITION, HttpUtil.createContentDispositionAttachmentHeader(String.format("%s-%s.diff", name, revision)))

View File

@@ -0,0 +1,34 @@
package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import sonia.scm.ContextEntry;
import sonia.scm.NotFoundException;
import java.util.List;
@Getter
public class ErrorDto {
private final String transactionId;
private final String errorCode;
private final List<ContextEntry> context;
private final String message;
@JsonInclude(JsonInclude.Include.NON_NULL)
private final String url;
private ErrorDto(String transactionId, String errorCode, List<ContextEntry> context, String message) {
this(transactionId, errorCode, context, message, null);
}
private ErrorDto(String transactionId, String errorCode, List<ContextEntry> context, String message, String url) {
this.transactionId = transactionId;
this.errorCode = errorCode;
this.context = context;
this.message = message;
this.url = url;
}
static ErrorDto from(NotFoundException notFoundException) {
return new ErrorDto("todo", "todo", notFoundException.getContext(), notFoundException.getMessage());
}
}

View File

@@ -11,7 +11,6 @@ import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.web.VndMediaType;
@@ -51,8 +50,6 @@ public class FileHistoryRootResource {
* @param pageSize pagination
* @return all changesets related to the given file starting with the given revision
* @throws IOException on io error
* @throws RevisionNotFoundException on missing revision
* @throws RepositoryNotFoundException on missing repository
*/
@GET
@Path("{revision}/{path: .*}")
@@ -69,7 +66,7 @@ public class FileHistoryRootResource {
@PathParam("revision") String revision,
@PathParam("path") String path,
@DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("10") @QueryParam("pageSize") int pageSize) throws IOException, RevisionNotFoundException, RepositoryNotFoundException {
@DefaultValue("10") @QueryParam("pageSize") int pageSize) throws IOException {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
log.info("Get changesets of the file {} and revision {}", path, revision);
Repository repository = repositoryService.getRepository();

View File

@@ -5,6 +5,7 @@ import sonia.scm.AlreadyExistsException;
import sonia.scm.ConcurrentModificationException;
import sonia.scm.Manager;
import sonia.scm.ModelObject;
import sonia.scm.NotFoundException;
import sonia.scm.PageResult;
import javax.ws.rs.core.Response;
@@ -22,6 +23,7 @@ class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
DTO extends HalRepresentation> {
private final Manager<MODEL_OBJECT> manager;
private final String type;
private final SingleResourceManagerAdapter<MODEL_OBJECT, DTO> singleAdapter;
private final CollectionResourceManagerAdapter<MODEL_OBJECT, DTO> collectionAdapter;
@@ -30,6 +32,7 @@ class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
this.manager = manager;
singleAdapter = new SingleResourceManagerAdapter<>(manager, type);
collectionAdapter = new CollectionResourceManagerAdapter<>(manager, type);
this.type = type.getSimpleName();
}
Response get(String id, Function<MODEL_OBJECT, DTO> mapToDto) {
@@ -56,8 +59,8 @@ class IdResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
return singleAdapter.delete(id);
}
private Supplier<Optional<MODEL_OBJECT>> loadBy(String id) {
return () -> Optional.ofNullable(manager.get(id));
private Supplier<MODEL_OBJECT> loadBy(String id) {
return () -> Optional.ofNullable(manager.get(id)).orElseThrow(() -> new NotFoundException(type, id));
}
private Predicate<MODEL_OBJECT> idStaysTheSame(String id) {

View File

@@ -3,11 +3,8 @@ package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.Modifications;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.web.VndMediaType;
@@ -46,7 +43,7 @@ public class ModificationsRootResource {
@Produces(VndMediaType.MODIFICATIONS)
@TypeHint(ModificationsDto.class)
@Path("{revision}")
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision) throws IOException, RevisionNotFoundException, RepositoryNotFoundException , InternalRepositoryException {
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision) throws IOException {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
Modifications modifications = repositoryService.getModificationsCommand()
.revision(revision)

View File

@@ -32,18 +32,20 @@ package sonia.scm.api.v2.resources;
import sonia.scm.NotFoundException;
import sonia.scm.api.rest.StatusExceptionMapper;
import sonia.scm.web.VndMediaType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
* @since 2.0.0
*/
@Provider
public class NotFoundExceptionMapper extends StatusExceptionMapper<NotFoundException> {
public NotFoundExceptionMapper() {
super(NotFoundException.class, Response.Status.NOT_FOUND);
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
return Response.status(Response.Status.NOT_FOUND).entity(ErrorDto.from(exception)).type(VndMediaType.ERROR_TYPE).build();
}
}

View File

@@ -109,7 +109,7 @@ public class PermissionRootResource {
.filter(filterPermission(permissionName))
.map(permission -> modelToDtoMapper.map(permission, repository))
.findFirst()
.orElseThrow(NotFoundException::new)
.orElseThrow(() -> NotFoundException.notFound(Permission.class, namespace).in(Repository.class, namespace + "/" + name).build())
).build();
}
@@ -164,7 +164,7 @@ public class PermissionRootResource {
RepositoryPermissions.permissionWrite(repository).check();
String extractedPermissionName = getPermissionName(permissionName);
if (!isPermissionExist(new PermissionDto(extractedPermissionName, isGroupPermission(permissionName)), repository)) {
throw new NotFoundException("permission", extractedPermissionName);
throw NotFoundException.notFound(Permission.class, namespace).in(Repository.class, namespace + "/" + name).build();
}
permission.setGroupPermission(isGroupPermission(permissionName));
if (!extractedPermissionName.equals(permission.getName())) {
@@ -174,7 +174,7 @@ public class PermissionRootResource {
.stream()
.filter(filterPermission(permissionName))
.findFirst()
.orElseThrow(NotFoundException::new);
.orElseThrow(() -> NotFoundException.notFound(Permission.class, namespace).in(Repository.class, namespace + "/" + name).build());
dtoToModelMapper.modify(existingPermission, permission);
manager.modify(repository);
log.info("the permission with name: {} is updated.", permissionName);

View File

@@ -203,8 +203,8 @@ public class RepositoryResource {
}
}
private Supplier<Optional<Repository>> loadBy(String namespace, String name) {
return () -> Optional.ofNullable(manager.get(new NamespaceAndName(namespace, name)));
private Supplier<Repository> loadBy(String namespace, String name) {
return () -> Optional.ofNullable(manager.get(new NamespaceAndName(namespace, name))).orElseThrow(() -> new NotFoundException(Repository.class, namespace + "/" + name));
}
private Predicate<Repository> nameAndNamespaceStaysTheSame(String namespace, String name) {

View File

@@ -38,7 +38,10 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
this(manager, type, e -> Optional.empty());
}
SingleResourceManagerAdapter(Manager<MODEL_OBJECT> manager, Class<MODEL_OBJECT> type, Function<Throwable, Optional<Response>> errorHandler) {
SingleResourceManagerAdapter(
Manager<MODEL_OBJECT> manager,
Class<MODEL_OBJECT> type,
Function<Throwable, Optional<Response>> errorHandler) {
super(manager, type);
this.errorHandler = errorHandler;
}
@@ -47,25 +50,16 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
* Reads the model object for the given id, transforms it to a dto and returns a corresponding http response.
* This handles all corner cases, eg. no matching object for the id or missing privileges.
*/
Response get(Supplier<Optional<MODEL_OBJECT>> reader, Function<MODEL_OBJECT, DTO> mapToDto) {
return reader.get()
.map(mapToDto)
.map(Response::ok)
.map(Response.ResponseBuilder::build)
.orElseThrow(NotFoundException::new);
}
public Response update(Supplier<Optional<MODEL_OBJECT>> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey, Consumer<MODEL_OBJECT> checker) throws NotFoundException, ConcurrentModificationException {
MODEL_OBJECT existingModelObject = reader.get().orElseThrow(NotFoundException::new);
checker.accept(existingModelObject);
return update(reader,applyChanges,hasSameKey);
Response get(Supplier<MODEL_OBJECT> reader, Function<MODEL_OBJECT, DTO> mapToDto) {
return Response.ok(mapToDto.apply(reader.get())).build();
}
/**
* Update the model object for the given id according to the given function and returns a corresponding http response.
* This handles all corner cases, eg. no matching object for the id or missing privileges.
*/
public Response update(Supplier<Optional<MODEL_OBJECT>> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey) throws NotFoundException, ConcurrentModificationException {
MODEL_OBJECT existingModelObject = reader.get().orElseThrow(NotFoundException::new);
Response update(Supplier<MODEL_OBJECT> reader, Function<MODEL_OBJECT, MODEL_OBJECT> applyChanges, Predicate<MODEL_OBJECT> hasSameKey) throws ConcurrentModificationException {
MODEL_OBJECT existingModelObject = reader.get();
MODEL_OBJECT changedModelObject = applyChanges.apply(existingModelObject);
if (!hasSameKey.test(changedModelObject)) {
return Response.status(BAD_REQUEST).entity("illegal change of id").build();
@@ -81,11 +75,13 @@ class SingleResourceManagerAdapter<MODEL_OBJECT extends ModelObject,
&& (updated.getLastModified() == null || existing.getLastModified() > updated.getLastModified());
}
public Response delete(Supplier<Optional<MODEL_OBJECT>> reader) {
return reader.get()
.map(MODEL_OBJECT::getId)
.map(this::delete)
.orElse(null);
public Response delete(Supplier<MODEL_OBJECT> reader) {
try {
return delete(reader.get().getId());
} catch (NotFoundException e) {
// due to idempotency of delete this does not matter here.
return null;
}
}
@Override

View File

@@ -1,10 +1,8 @@
package sonia.scm.api.v2.resources;
import sonia.scm.NotFoundException;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.BrowseCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -33,14 +31,14 @@ public class SourceRootResource {
@GET
@Produces(VndMediaType.SOURCE)
@Path("")
public Response getAllWithoutRevision(@PathParam("namespace") String namespace, @PathParam("name") String name) throws RevisionNotFoundException, RepositoryNotFoundException, IOException {
public Response getAllWithoutRevision(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException {
return getSource(namespace, name, "/", null);
}
@GET
@Produces(VndMediaType.SOURCE)
@Path("{revision}")
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision) throws RevisionNotFoundException, RepositoryNotFoundException, IOException {
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision) throws IOException {
return getSource(namespace, name, "/", revision);
}
@@ -51,7 +49,7 @@ public class SourceRootResource {
return getSource(namespace, name, path, revision);
}
private Response getSource(String namespace, String repoName, String path, String revision) throws IOException, RevisionNotFoundException, RepositoryNotFoundException {
private Response getSource(String namespace, String repoName, String path, String revision) throws IOException {
NamespaceAndName namespaceAndName = new NamespaceAndName(namespace, repoName);
try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
BrowseCommandBuilder browseCommand = repositoryService.getBrowseCommand();

View File

@@ -1,7 +0,0 @@
package sonia.scm.api.v2.resources;
import sonia.scm.NotFoundException;
public class TagNotFoundException extends NotFoundException {
}

View File

@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
@@ -47,7 +48,7 @@ public class TagRootResource {
})
@Produces(VndMediaType.TAG_COLLECTION)
@TypeHint(CollectionDto.class)
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException, RepositoryNotFoundException {
public Response getAll(@PathParam("namespace") String namespace, @PathParam("name") String name) throws IOException {
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
Tags tags = getTags(repositoryService);
if (tags != null && tags.getTags() != null) {
@@ -72,7 +73,7 @@ public class TagRootResource {
@Produces(VndMediaType.TAG)
@TypeHint(TagDto.class)
@Path("{tagName}")
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("tagName") String tagName) throws IOException, RepositoryNotFoundException, TagNotFoundException {
public Response get(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("tagName") String tagName) throws IOException {
NamespaceAndName namespaceAndName = new NamespaceAndName(namespace, name);
try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
Tags tags = getTags(repositoryService);
@@ -80,7 +81,7 @@ public class TagRootResource {
Tag tag = tags.getTags().stream()
.filter(t -> tagName.equals(t.getName()))
.findFirst()
.orElseThrow(TagNotFoundException::new);
.orElseThrow(() -> createNotFoundException(namespace, name, tagName));
return Response.ok(tagToTagDtoMapper.map(tag, namespaceAndName)).build();
} else {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
@@ -90,6 +91,10 @@ public class TagRootResource {
}
}
private NotFoundException createNotFoundException(String namespace, String name, String tagName) {
return NotFoundException.notFound("Tag", tagName).in("Repository", namespace + "/" + name).build();
}
private Tags getTags(RepositoryService repositoryService) throws IOException {
Repository repository = repositoryService.getRepository();
RepositoryPermissions.read(repository).check();

View File

@@ -172,7 +172,7 @@ public class DefaultGroupManager extends AbstractGroupManager
if (fresh == null)
{
throw new NotFoundException("group", group.getId());
throw new NotFoundException(Group.class, group.getId());
}
fresh.copyProperties(group);

View File

@@ -219,7 +219,7 @@ public class DefaultUserManager extends AbstractUserManager
if (fresh == null)
{
throw new NotFoundException();
throw new NotFoundException(User.class, user.getName());
}
fresh.copyProperties(user);
@@ -419,7 +419,7 @@ public class DefaultUserManager extends AbstractUserManager
public void overwritePassword(String userId, String newPassword) {
User user = get(userId);
if (user == null) {
throw new NotFoundException();
throw new NotFoundException(User.class, userId);
}
if (!isTypeDefault(user)) {
throw new ChangePasswordNotAllowedException(user.getType());

View File

@@ -8,8 +8,8 @@ import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.PathNotFoundException;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.api.CatCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
@@ -59,7 +59,7 @@ public class ContentResourceTest {
// defaults for unknown things
doThrow(new RepositoryNotFoundException("x")).when(repositoryServiceFactory).create(not(eq(existingNamespaceAndName)));
doThrow(new PathNotFoundException("x")).when(catCommand).getStream(any());
doThrow(new NotFoundException("Test", "X")).when(catCommand).getStream(any());
}
@Test
@@ -175,7 +175,7 @@ public class ContentResourceTest {
}
@Override
public void close() throws IOException {
public void close() {
closed = true;
}

View File

@@ -17,11 +17,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -62,7 +62,7 @@ public class DiffResourceTest extends RepositoryTestBase {
@Before
public void prepareEnvironment() throws Exception {
public void prepareEnvironment() {
diffRootResource = new DiffRootResource(serviceFactory);
super.diffRootResource = Providers.of(diffRootResource);
dispatcher.getRegistry().addSingletonResource(getRepositoryRootResource());
@@ -108,7 +108,7 @@ public class DiffResourceTest extends RepositoryTestBase {
@Test
public void shouldGet404OnMissingRepository() throws URISyntaxException, RepositoryNotFoundException {
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(RepositoryNotFoundException.class);
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(DIFF_URL + "revision")
.accept(VndMediaType.DIFF);
@@ -120,20 +120,22 @@ public class DiffResourceTest extends RepositoryTestBase {
@Test
public void shouldGet404OnMissingRevision() throws Exception {
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
when(diffCommandBuilder.retriveContent(any())).thenThrow(RevisionNotFoundException.class);
when(diffCommandBuilder.retriveContent(any())).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(DIFF_URL + "revision")
.accept(VndMediaType.DIFF);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertEquals(404, response.getStatus());
}
@Test
public void shouldGet400OnCrlfInjection() throws Exception {
when(diffCommandBuilder.setRevision(anyString())).thenReturn(diffCommandBuilder);
when(diffCommandBuilder.retriveContent(any())).thenThrow(RevisionNotFoundException.class);
when(diffCommandBuilder.retriveContent(any())).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(DIFF_URL + "ny%0D%0ASet-cookie:%20Tamper=3079675143472450634")

View File

@@ -18,6 +18,7 @@ import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.NotFoundException;
import sonia.scm.api.rest.AuthorizationExceptionMapper;
import sonia.scm.repository.Changeset;
import sonia.scm.repository.ChangesetPagingResult;
@@ -26,7 +27,6 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Person;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.LogCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -79,7 +79,7 @@ public class FileHistoryResourceTest extends RepositoryTestBase {
@Before
public void prepareEnvironment() throws Exception {
public void prepareEnvironment() {
fileHistoryCollectionToDtoMapper = new FileHistoryCollectionToDtoMapper(changesetToChangesetDtoMapper, resourceLinks);
fileHistoryRootResource = new FileHistoryRootResource(serviceFactory, fileHistoryCollectionToDtoMapper);
super.fileHistoryRootResource = Providers.of(fileHistoryRootResource);
@@ -134,7 +134,7 @@ public class FileHistoryResourceTest extends RepositoryTestBase {
@Test
public void shouldGet404OnMissingRepository() throws URISyntaxException, RepositoryNotFoundException {
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(RepositoryNotFoundException.class);
when(serviceFactory.create(any(NamespaceAndName.class))).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(FILE_HISTORY_URL + "revision/a.txt")
.accept(VndMediaType.CHANGESET_COLLECTION);
@@ -152,7 +152,7 @@ public class FileHistoryResourceTest extends RepositoryTestBase {
when(logCommandBuilder.setPagingLimit(anyInt())).thenReturn(logCommandBuilder);
when(logCommandBuilder.setStartChangeset(eq(id))).thenReturn(logCommandBuilder);
when(logCommandBuilder.setPath(eq(path))).thenReturn(logCommandBuilder);
when(logCommandBuilder.getChangesets()).thenThrow(RevisionNotFoundException.class);
when(logCommandBuilder.getChangesets()).thenThrow(new NotFoundException("Text", "x"));
MockHttpRequest request = MockHttpRequest
.get(FILE_HISTORY_URL + id + "/" + path)

View File

@@ -14,7 +14,6 @@ import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryNotFoundException;
import sonia.scm.repository.RevisionNotFoundException;
import sonia.scm.repository.api.BrowseCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
@@ -69,7 +68,7 @@ public class SourceRootResourceTest extends RepositoryTestBase {
}
@Test
public void shouldReturnSources() throws URISyntaxException, IOException, RevisionNotFoundException {
public void shouldReturnSources() throws URISyntaxException, IOException {
BrowserResult result = createBrowserResult();
when(browseCommandBuilder.getBrowserResult()).thenReturn(result);
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo/sources");
@@ -83,7 +82,7 @@ public class SourceRootResourceTest extends RepositoryTestBase {
@Test
public void shouldReturn404IfRepoNotFound() throws URISyntaxException, RepositoryNotFoundException {
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(RepositoryNotFoundException.class);
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(new RepositoryNotFoundException("abc"));
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "idont/exist/sources");
MockHttpResponse response = new MockHttpResponse();
@@ -92,7 +91,7 @@ public class SourceRootResourceTest extends RepositoryTestBase {
}
@Test
public void shouldGetResultForSingleFile() throws URISyntaxException, IOException, RevisionNotFoundException {
public void shouldGetResultForSingleFile() throws URISyntaxException, IOException {
BrowserResult browserResult = new BrowserResult();
browserResult.setRevision("revision");
FileObject fileObject = new FileObject();
@@ -111,7 +110,7 @@ public class SourceRootResourceTest extends RepositoryTestBase {
@Test
public void shouldGet404ForSingleFileIfRepoNotFound() throws URISyntaxException, RepositoryNotFoundException {
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(RepositoryNotFoundException.class);
when(serviceFactory.create(new NamespaceAndName("idont", "exist"))).thenThrow(new RepositoryNotFoundException("abc"));
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "idont/exist/sources/revision/fileabc");
MockHttpResponse response = new MockHttpResponse();

View File

@@ -185,7 +185,7 @@ public class UserRootResourceTest {
.content(content.getBytes());
MockHttpResponse response = new MockHttpResponse();
doThrow(NotFoundException.class).when(userManager).overwritePassword(any(), any());
doThrow(new NotFoundException("Test", "x")).when(userManager).overwritePassword(any(), any());
dispatcher.invoke(request, response);

View File

@@ -63,7 +63,7 @@ public class HttpProtocolServletTest {
when(userAgentParser.parse(request)).thenReturn(userAgent);
when(userAgent.isBrowser()).thenReturn(false);
NamespaceAndName existingRepo = new NamespaceAndName("space", "repo");
when(serviceFactory.create(not(eq(existingRepo)))).thenThrow(RepositoryNotFoundException.class);
when(serviceFactory.create(not(eq(existingRepo)))).thenThrow(new RepositoryNotFoundException("x"));
when(serviceFactory.create(existingRepo)).thenReturn(repositoryService);
when(requestProvider.get()).thenReturn(httpServletRequest);
}