1 package org.apache.archiva.rest.api.services.v2;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 import org.apache.archiva.repository.Repository;
21 import org.apache.archiva.repository.validation.ValidationResponse;
22 import org.apache.archiva.rest.api.model.v2.ValidationError;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
34 * @author Martin Stockhammer <martin_s@apache.org>
36 public class ValidationException extends ArchivaRestServiceException
38 public static final int DEFAULT_CODE = 422;
40 public static final ErrorMessage DEFAULT_MESSAGE = new ErrorMessage( ErrorKeys.VALIDATION_ERROR );
42 private List<ValidationError> validationErrors;
44 public ValidationException( )
46 super( DEFAULT_MESSAGE, DEFAULT_CODE );
49 public ValidationException( int errorCode )
51 super( DEFAULT_MESSAGE, errorCode );
54 public ValidationException( List<ValidationError> errors )
56 super( DEFAULT_MESSAGE, DEFAULT_CODE );
57 this.validationErrors = errors;
60 public static ValidationException of( List<org.apache.archiva.repository.validation.ValidationError> errorList )
62 return new ValidationException( errorList.stream( ).map( ValidationError::of ).collect( Collectors.toList( ) ) );
65 public static ValidationException of( Map<String, List<org.apache.archiva.repository.validation.ValidationError>> errorMap )
67 return new ValidationException( errorMap.entrySet( ).stream( )
68 .flatMap( v -> v.getValue( ).stream( ).map( k -> ValidationError.of(v.getKey(), k)))
69 .collect( Collectors.toList( ) ) );
72 public static <R extends Repository> ValidationException of( ValidationResponse<R> result )
74 if ( result.isValid( ) )
76 return new ValidationException( );
80 return new ValidationException( result.getResult( ).entrySet( ).stream( ).flatMap(
81 v -> v.getValue( ).stream( ).map( e -> ValidationError.of( v.getKey( ), e ) ) ).collect( Collectors.toList( ) ) );
85 public List<ValidationError> getValidationErrors( )
87 return validationErrors == null ? Collections.emptyList( ) : validationErrors;
90 public void setValidationErrors( List<ValidationError> validationErrors )
92 this.validationErrors = validationErrors;
95 public void addValidationError( ValidationError error )
97 if ( this.validationErrors == null )
99 this.validationErrors = new ArrayList<>( );
101 this.validationErrors.add( error );