You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@archiva.apache.org by ma...@apache.org on 2020/11/12 22:16:08 UTC
[archiva-redback-core] branch master updated: Modifying status
codes for v2 service errors
This is an automated email from the ASF dual-hosted git repository.
martin_s pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/archiva-redback-core.git
The following commit(s) were added to refs/heads/master by this push:
new f8c7397 Modifying status codes for v2 service errors
f8c7397 is described below
commit f8c739739d5a1594736c1b979741c51b2cf97b1f
Author: Martin Stockhammer <ma...@apache.org>
AuthorDate: Thu Nov 12 23:15:59 2020 +0100
Modifying status codes for v2 service errors
---
.../v2/PasswordRuleViolationExceptionMapper.java | 57 ++++++++++++++++++++++
.../rest/services/v2/DefaultUserService.java | 5 +-
.../src/main/resources/META-INF/spring-context.xml | 2 +-
3 files changed, 61 insertions(+), 3 deletions(-)
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/v2/PasswordRuleViolationExceptionMapper.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/v2/PasswordRuleViolationExceptionMapper.java
new file mode 100644
index 0000000..8fb2d1e
--- /dev/null
+++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/interceptors/v2/PasswordRuleViolationExceptionMapper.java
@@ -0,0 +1,57 @@
+package org.apache.archiva.redback.rest.services.interceptors.v2;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.redback.policy.PasswordRuleViolationException;
+import org.apache.archiva.redback.policy.PasswordRuleViolations;
+import org.apache.archiva.redback.rest.api.model.ErrorMessage;
+import org.apache.archiva.redback.rest.api.model.RedbackRestError;
+import org.springframework.stereotype.Service;
+
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This implementation returns a 422 status code.
+ *
+ * @author Olivier Lamy
+ * @since 3.0
+ */
+@Provider
+@Service( "v2.passwordRuleViolationExceptionMapper" )
+public class PasswordRuleViolationExceptionMapper
+ implements ExceptionMapper<PasswordRuleViolationException>
+{
+ public Response toResponse( PasswordRuleViolationException e )
+ {
+ RedbackRestError restError = new RedbackRestError();
+
+ List<ErrorMessage> errorMessages = new ArrayList<ErrorMessage>( e.getViolations().getViolations().size() );
+ for ( PasswordRuleViolations.MessageReference messageReference : e.getViolations().getViolations() )
+ {
+ errorMessages.add( new ErrorMessage( messageReference.getKey(), messageReference.getArgs() ) );
+ }
+ restError.setErrorMessages( errorMessages );
+ Response.ResponseBuilder responseBuilder = Response.status( 422 ).entity( restError );
+ return responseBuilder.build();
+ }
+}
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java
index 8e7518f..b465809 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java
+++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java
@@ -242,7 +242,7 @@ public class DefaultUserService
catch ( UserNotFoundException e )
{
//ignore we just want to prevent non human readable error message from backend :-)
- log.debug( "user {} not exists", user.getUserId( ) );
+ log.debug( "User {} does not exist", user.getUserId( ) );
}
catch ( UserManagerException e )
{
@@ -281,7 +281,7 @@ public class DefaultUserService
try
{
u = userManager.updateUser( u );
- log.debug( "user {} created", u.getUsername( ) );
+ log.debug( "User {} created", u.getUsername( ) );
}
catch ( UserNotFoundException e )
{
@@ -301,6 +301,7 @@ public class DefaultUserService
}
catch ( UserManagerException e )
{
+ log.error( "UserManagerException: {}", e.getMessage( ), e );
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_UNKNOWN, e.getMessage( ) ) );
}
return result;
diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml b/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
index c014be2..5041bc4 100644
--- a/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
+++ b/redback-integrations/redback-rest/redback-rest-services/src/main/resources/META-INF/spring-context.xml
@@ -104,7 +104,7 @@
<ref bean="bearerAuthInterceptor#rest"/>
<ref bean="permissionInterceptor#rest"/>
<ref bean="redbackServiceExceptionMapper"/>
- <ref bean="passwordRuleViolationExceptionMapper"/>
+ <ref bean="v2.passwordRuleViolationExceptionMapper"/>
<ref bean="requestValidationInterceptor#rest" />
<ref bean="threadLocalUserCleaner#rest"/>
</jaxrs:providers>