From d31090bcd4b893978aca1c1e7969a322b7d91817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Mon, 10 Dec 2018 18:06:13 +0100 Subject: [PATCH 1/5] Introduce generic BadRequestException --- .../java/sonia/scm/BadRequestException.java | 9 +++++++++ .../sonia/scm/NotSupportedFeatureException.java | 2 +- .../user/ChangePasswordNotAllowedException.java | 4 ++-- .../scm/user/InvalidPasswordException.java | 4 ++-- .../scm/api/rest/BadRequestExceptionMapper.java | 16 ++++++++++++++++ .../v2/NotSupportedFeatureExceptionMapper.java | 17 ----------------- ...ChangePasswordNotAllowedExceptionMapper.java | 17 ----------------- .../InvalidPasswordExceptionMapper.java | 17 ----------------- .../scm/api/v2/resources/DispatcherMock.java | 6 ++---- 9 files changed, 32 insertions(+), 60 deletions(-) create mode 100644 scm-core/src/main/java/sonia/scm/BadRequestException.java create mode 100644 scm-webapp/src/main/java/sonia/scm/api/rest/BadRequestExceptionMapper.java delete mode 100644 scm-webapp/src/main/java/sonia/scm/api/v2/NotSupportedFeatureExceptionMapper.java delete mode 100644 scm-webapp/src/main/java/sonia/scm/api/v2/resources/ChangePasswordNotAllowedExceptionMapper.java delete mode 100644 scm-webapp/src/main/java/sonia/scm/api/v2/resources/InvalidPasswordExceptionMapper.java diff --git a/scm-core/src/main/java/sonia/scm/BadRequestException.java b/scm-core/src/main/java/sonia/scm/BadRequestException.java new file mode 100644 index 0000000000..544ed75a0b --- /dev/null +++ b/scm-core/src/main/java/sonia/scm/BadRequestException.java @@ -0,0 +1,9 @@ +package sonia.scm; + +import java.util.List; + +public abstract class BadRequestException extends ExceptionWithContext { + public BadRequestException(List context, String message) { + super(context, message); + } +} diff --git a/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java b/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java index daf996ee6c..bc4e1b8a4f 100644 --- a/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java +++ b/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java @@ -40,7 +40,7 @@ import java.util.Collections; * @author Sebastian Sdorra * @version 1.6 */ -public class NotSupportedFeatureException extends ExceptionWithContext { +public class NotSupportedFeatureException extends BadRequestException { private static final long serialVersionUID = 256498734456613496L; diff --git a/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java b/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java index caa35e0b88..be5eda6599 100644 --- a/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java +++ b/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java @@ -1,9 +1,9 @@ package sonia.scm.user; +import sonia.scm.BadRequestException; import sonia.scm.ContextEntry; -import sonia.scm.ExceptionWithContext; -public class ChangePasswordNotAllowedException extends ExceptionWithContext { +public class ChangePasswordNotAllowedException extends BadRequestException { private static final String CODE = "9BR7qpDAe1"; public static final String WRONG_USER_TYPE = "User of type %s are not allowed to change password"; diff --git a/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java b/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java index 93a6a7c1d1..17e24fcd9d 100644 --- a/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java +++ b/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java @@ -1,9 +1,9 @@ package sonia.scm.user; +import sonia.scm.BadRequestException; import sonia.scm.ContextEntry; -import sonia.scm.ExceptionWithContext; -public class InvalidPasswordException extends ExceptionWithContext { +public class InvalidPasswordException extends BadRequestException { private static final String CODE = "8YR7aawFW1"; diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/BadRequestExceptionMapper.java b/scm-webapp/src/main/java/sonia/scm/api/rest/BadRequestExceptionMapper.java new file mode 100644 index 0000000000..e529bc7c1a --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/BadRequestExceptionMapper.java @@ -0,0 +1,16 @@ +package sonia.scm.api.rest; + +import sonia.scm.BadRequestException; +import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper; + +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.Provider; + +@Provider +public class BadRequestExceptionMapper extends ContextualExceptionMapper { + @Inject + public BadRequestExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) { + super(BadRequestException.class, Response.Status.BAD_REQUEST, mapper); + } +} diff --git a/scm-webapp/src/main/java/sonia/scm/api/v2/NotSupportedFeatureExceptionMapper.java b/scm-webapp/src/main/java/sonia/scm/api/v2/NotSupportedFeatureExceptionMapper.java deleted file mode 100644 index 6a48663aa5..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/api/v2/NotSupportedFeatureExceptionMapper.java +++ /dev/null @@ -1,17 +0,0 @@ -package sonia.scm.api.v2; - -import sonia.scm.NotSupportedFeatureException; -import sonia.scm.api.rest.ContextualExceptionMapper; -import sonia.scm.api.v2.resources.ExceptionWithContextToErrorDtoMapper; - -import javax.inject.Inject; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; - -@Provider -public class NotSupportedFeatureExceptionMapper extends ContextualExceptionMapper { - @Inject - public NotSupportedFeatureExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) { - super(NotSupportedFeatureException.class, Response.Status.BAD_REQUEST, mapper); - } -} diff --git a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/ChangePasswordNotAllowedExceptionMapper.java b/scm-webapp/src/main/java/sonia/scm/api/v2/resources/ChangePasswordNotAllowedExceptionMapper.java deleted file mode 100644 index 18a6e6e75c..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/ChangePasswordNotAllowedExceptionMapper.java +++ /dev/null @@ -1,17 +0,0 @@ -package sonia.scm.api.v2.resources; - -import sonia.scm.api.rest.ContextualExceptionMapper; -import sonia.scm.user.ChangePasswordNotAllowedException; -import sonia.scm.user.InvalidPasswordException; - -import javax.inject.Inject; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; - -@Provider -public class ChangePasswordNotAllowedExceptionMapper extends ContextualExceptionMapper { - @Inject - public ChangePasswordNotAllowedExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) { - super(ChangePasswordNotAllowedException.class, Response.Status.BAD_REQUEST, mapper); - } -} diff --git a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/InvalidPasswordExceptionMapper.java b/scm-webapp/src/main/java/sonia/scm/api/v2/resources/InvalidPasswordExceptionMapper.java deleted file mode 100644 index 7a1d311a1c..0000000000 --- a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/InvalidPasswordExceptionMapper.java +++ /dev/null @@ -1,17 +0,0 @@ -package sonia.scm.api.v2.resources; - -import sonia.scm.api.rest.ContextualExceptionMapper; -import sonia.scm.user.InvalidPasswordException; - -import javax.inject.Inject; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; - -@Provider -public class InvalidPasswordExceptionMapper extends ContextualExceptionMapper { - - @Inject - public InvalidPasswordExceptionMapper(ExceptionWithContextToErrorDtoMapper mapper) { - super(InvalidPasswordException.class, Response.Status.BAD_REQUEST, mapper); - } -} diff --git a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/DispatcherMock.java b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/DispatcherMock.java index 9638a8aa49..fe205e88a1 100644 --- a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/DispatcherMock.java +++ b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/DispatcherMock.java @@ -4,9 +4,9 @@ import org.jboss.resteasy.core.Dispatcher; import org.jboss.resteasy.mock.MockDispatcherFactory; import sonia.scm.api.rest.AlreadyExistsExceptionMapper; import sonia.scm.api.rest.AuthorizationExceptionMapper; +import sonia.scm.api.rest.BadRequestExceptionMapper; import sonia.scm.api.rest.ConcurrentModificationExceptionMapper; import sonia.scm.api.v2.NotFoundExceptionMapper; -import sonia.scm.api.v2.NotSupportedFeatureExceptionMapper; public class DispatcherMock { public static Dispatcher createDispatcher(Object resource) { @@ -18,9 +18,7 @@ public class DispatcherMock { dispatcher.getProviderFactory().register(new ConcurrentModificationExceptionMapper(mapper)); dispatcher.getProviderFactory().registerProvider(AuthorizationExceptionMapper.class); dispatcher.getProviderFactory().register(new InternalRepositoryExceptionMapper(mapper)); - dispatcher.getProviderFactory().register(new ChangePasswordNotAllowedExceptionMapper(mapper)); - dispatcher.getProviderFactory().register(new InvalidPasswordExceptionMapper(mapper)); - dispatcher.getProviderFactory().register(new NotSupportedFeatureExceptionMapper(mapper)); + dispatcher.getProviderFactory().register(new BadRequestExceptionMapper(mapper)); return dispatcher; } } From cdf2f2e2385f783cff8b4e5ac62a6408298ace34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Tue, 11 Dec 2018 11:36:51 +0100 Subject: [PATCH 2/5] Suppress sonar issue --- .../src/main/java/sonia/scm/NotSupportedFeatureException.java | 1 + .../java/sonia/scm/user/ChangePasswordNotAllowedException.java | 1 + .../src/main/java/sonia/scm/user/InvalidPasswordException.java | 1 + 3 files changed, 3 insertions(+) diff --git a/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java b/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java index bc4e1b8a4f..3b389a7dc4 100644 --- a/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java +++ b/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java @@ -40,6 +40,7 @@ import java.util.Collections; * @author Sebastian Sdorra * @version 1.6 */ +@SuppressWarnings("squid:MaximumInheritanceDepth") // exceptions have a deep inheritance depth themselves; therefore we accept this here public class NotSupportedFeatureException extends BadRequestException { private static final long serialVersionUID = 256498734456613496L; diff --git a/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java b/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java index be5eda6599..486d392b0b 100644 --- a/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java +++ b/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java @@ -3,6 +3,7 @@ package sonia.scm.user; import sonia.scm.BadRequestException; import sonia.scm.ContextEntry; +@SuppressWarnings("squid:MaximumInheritanceDepth") // exceptions have a deep inheritance depth themselves; therefore we accept this here public class ChangePasswordNotAllowedException extends BadRequestException { private static final String CODE = "9BR7qpDAe1"; diff --git a/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java b/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java index 17e24fcd9d..6f1bfd9954 100644 --- a/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java +++ b/scm-core/src/main/java/sonia/scm/user/InvalidPasswordException.java @@ -3,6 +3,7 @@ package sonia.scm.user; import sonia.scm.BadRequestException; import sonia.scm.ContextEntry; +@SuppressWarnings("squid:MaximumInheritanceDepth") // exceptions have a deep inheritance depth themselves; therefore we accept this here public class InvalidPasswordException extends BadRequestException { private static final String CODE = "8YR7aawFW1"; From 7e2184ef61ddeb5370e6dc47fad9f7f00d25c4ba Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Thu, 13 Dec 2018 15:21:13 +0100 Subject: [PATCH 3/5] Fixed typo --- .../java/sonia/scm/user/ChangePasswordNotAllowedException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java b/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java index 486d392b0b..b0f8117e82 100644 --- a/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java +++ b/scm-core/src/main/java/sonia/scm/user/ChangePasswordNotAllowedException.java @@ -7,7 +7,7 @@ import sonia.scm.ContextEntry; public class ChangePasswordNotAllowedException extends BadRequestException { private static final String CODE = "9BR7qpDAe1"; - public static final String WRONG_USER_TYPE = "User of type %s are not allowed to change password"; + public static final String WRONG_USER_TYPE = "Users of type %s are not allowed to change password"; public ChangePasswordNotAllowedException(ContextEntry.ContextBuilder context, String type) { super(context.build(), String.format(WRONG_USER_TYPE, type)); From 0af5d3901cd7a02c75130117c7cabc2b39a916aa Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Thu, 13 Dec 2018 15:31:00 +0100 Subject: [PATCH 4/5] Renamed NotSupportedFeatureException --- ...reException.java => FeatureNotSupportedException.java} | 4 ++-- .../sonia/scm/repository/AbstractRepositoryHandler.java | 6 +++--- .../main/java/sonia/scm/repository/RepositoryHandler.java | 6 +++--- .../java/sonia/scm/repository/api/DiffCommandBuilder.java | 4 ++-- .../java/sonia/scm/repository/api/LogCommandBuilder.java | 4 ++-- .../scm/api/rest/resources/RepositoryImportResource.java | 8 ++++---- 6 files changed, 16 insertions(+), 16 deletions(-) rename scm-core/src/main/java/sonia/scm/{NotSupportedFeatureException.java => FeatureNotSupportedException.java} (95%) diff --git a/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java b/scm-core/src/main/java/sonia/scm/FeatureNotSupportedException.java similarity index 95% rename from scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java rename to scm-core/src/main/java/sonia/scm/FeatureNotSupportedException.java index 3b389a7dc4..2d64af4318 100644 --- a/scm-core/src/main/java/sonia/scm/NotSupportedFeatureException.java +++ b/scm-core/src/main/java/sonia/scm/FeatureNotSupportedException.java @@ -41,13 +41,13 @@ import java.util.Collections; * @version 1.6 */ @SuppressWarnings("squid:MaximumInheritanceDepth") // exceptions have a deep inheritance depth themselves; therefore we accept this here -public class NotSupportedFeatureException extends BadRequestException { +public class FeatureNotSupportedException extends BadRequestException { private static final long serialVersionUID = 256498734456613496L; private static final String CODE = "9SR8G0kmU1"; - public NotSupportedFeatureException(String feature) + public FeatureNotSupportedException(String feature) { super(Collections.emptyList(),createMessage(feature)); } diff --git a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java index a3e8a1da73..1e9cc3d374 100644 --- a/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java +++ b/scm-core/src/main/java/sonia/scm/repository/AbstractRepositoryHandler.java @@ -38,7 +38,7 @@ package sonia.scm.repository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import sonia.scm.NotSupportedFeatureException; +import sonia.scm.FeatureNotSupportedException; import sonia.scm.SCMContextProvider; import sonia.scm.event.ScmEventBus; @@ -167,12 +167,12 @@ public abstract class AbstractRepositoryHandler * * @return * - * @throws NotSupportedFeatureException + * @throws FeatureNotSupportedException */ @Override public ImportHandler getImportHandler() { - throw new NotSupportedFeatureException("import"); + throw new FeatureNotSupportedException("import"); } /** diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryHandler.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryHandler.java index cb19cb7f5e..aaa090827a 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryHandler.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryHandler.java @@ -36,7 +36,7 @@ package sonia.scm.repository; //~--- non-JDK imports -------------------------------------------------------- import sonia.scm.Handler; -import sonia.scm.NotSupportedFeatureException; +import sonia.scm.FeatureNotSupportedException; import sonia.scm.plugin.ExtensionPoint; /** @@ -59,9 +59,9 @@ public interface RepositoryHandler * @return {@link ImportHandler} for the repository type of this handler * @since 1.12 * - * @throws NotSupportedFeatureException + * @throws FeatureNotSupportedException */ - public ImportHandler getImportHandler() throws NotSupportedFeatureException; + public ImportHandler getImportHandler() throws FeatureNotSupportedException; /** * Returns informations about the version of the RepositoryHandler. diff --git a/scm-core/src/main/java/sonia/scm/repository/api/DiffCommandBuilder.java b/scm-core/src/main/java/sonia/scm/repository/api/DiffCommandBuilder.java index 32b633a67c..9e7094d5bf 100644 --- a/scm-core/src/main/java/sonia/scm/repository/api/DiffCommandBuilder.java +++ b/scm-core/src/main/java/sonia/scm/repository/api/DiffCommandBuilder.java @@ -38,7 +38,7 @@ package sonia.scm.repository.api; import com.google.common.base.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import sonia.scm.NotSupportedFeatureException; +import sonia.scm.FeatureNotSupportedException; import sonia.scm.repository.Feature; import sonia.scm.repository.spi.DiffCommand; import sonia.scm.repository.spi.DiffCommandRequest; @@ -203,7 +203,7 @@ public final class DiffCommandBuilder public DiffCommandBuilder setAncestorChangeset(String revision) { if (!supportedFeatures.contains(Feature.INCOMING_REVISION)) { - throw new NotSupportedFeatureException(Feature.INCOMING_REVISION.name()); + throw new FeatureNotSupportedException(Feature.INCOMING_REVISION.name()); } request.setAncestorChangeset(revision); 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 7b8e172661..917b81391f 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 @@ -39,7 +39,7 @@ import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import sonia.scm.NotSupportedFeatureException; +import sonia.scm.FeatureNotSupportedException; import sonia.scm.cache.Cache; import sonia.scm.cache.CacheManager; import sonia.scm.repository.Changeset; @@ -410,7 +410,7 @@ public final class LogCommandBuilder */ public LogCommandBuilder setAncestorChangeset(String ancestorChangeset) { if (!supportedFeatures.contains(Feature.INCOMING_REVISION)) { - throw new NotSupportedFeatureException(Feature.INCOMING_REVISION.name()); + throw new FeatureNotSupportedException(Feature.INCOMING_REVISION.name()); } request.setAncestorChangeset(ancestorChangeset); return this; diff --git a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryImportResource.java b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryImportResource.java index 6bf6c8e803..64b20fc10c 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryImportResource.java +++ b/scm-webapp/src/main/java/sonia/scm/api/rest/resources/RepositoryImportResource.java @@ -46,7 +46,7 @@ import org.apache.shiro.SecurityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sonia.scm.NotFoundException; -import sonia.scm.NotSupportedFeatureException; +import sonia.scm.FeatureNotSupportedException; import sonia.scm.Type; import sonia.scm.api.rest.RestActionUploadResult; import sonia.scm.api.v2.resources.RepositoryResource; @@ -394,7 +394,7 @@ public class RepositoryImportResource response = Response.ok(result).build(); } - catch (NotSupportedFeatureException ex) + catch (FeatureNotSupportedException ex) { logger .warn( @@ -609,7 +609,7 @@ public class RepositoryImportResource types.add(t); } } - catch (NotSupportedFeatureException ex) + catch (FeatureNotSupportedException ex) { if (logger.isTraceEnabled()) { @@ -711,7 +711,7 @@ public class RepositoryImportResource } } } - catch (NotSupportedFeatureException ex) + catch (FeatureNotSupportedException ex) { throw new WebApplicationException(ex, Response.Status.BAD_REQUEST); } From de2c594fafc670edc208ad59223038aca0cf0e6c Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Fri, 14 Dec 2018 08:09:50 +0000 Subject: [PATCH 5/5] Close branch feature/bad_request