Modifying status codes for v2 service errors

This commit is contained in:
Martin Stockhammer 2020-11-12 23:15:59 +01:00
parent fa2cce07a9
commit f8c739739d
3 changed files with 61 additions and 3 deletions

View File

@ -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();
}
}

View File

@ -242,7 +242,7 @@ public class DefaultUserService
catch ( UserNotFoundException e ) catch ( UserNotFoundException e )
{ {
//ignore we just want to prevent non human readable error message from backend :-) //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 ) catch ( UserManagerException e )
{ {
@ -281,7 +281,7 @@ public class DefaultUserService
try try
{ {
u = userManager.updateUser( u ); u = userManager.updateUser( u );
log.debug( "user {} created", u.getUsername( ) ); log.debug( "User {} created", u.getUsername( ) );
} }
catch ( UserNotFoundException e ) catch ( UserNotFoundException e )
{ {
@ -301,6 +301,7 @@ public class DefaultUserService
} }
catch ( UserManagerException e ) catch ( UserManagerException e )
{ {
log.error( "UserManagerException: {}", e.getMessage( ), e );
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_UNKNOWN, e.getMessage( ) ) ); throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_UNKNOWN, e.getMessage( ) ) );
} }
return result; return result;

View File

@ -104,7 +104,7 @@
<ref bean="bearerAuthInterceptor#rest"/> <ref bean="bearerAuthInterceptor#rest"/>
<ref bean="permissionInterceptor#rest"/> <ref bean="permissionInterceptor#rest"/>
<ref bean="redbackServiceExceptionMapper"/> <ref bean="redbackServiceExceptionMapper"/>
<ref bean="passwordRuleViolationExceptionMapper"/> <ref bean="v2.passwordRuleViolationExceptionMapper"/>
<ref bean="requestValidationInterceptor#rest" /> <ref bean="requestValidationInterceptor#rest" />
<ref bean="threadLocalUserCleaner#rest"/> <ref bean="threadLocalUserCleaner#rest"/>
</jaxrs:providers> </jaxrs:providers>