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;
32 * @author Martin Stockhammer <martin_s@apache.org>
34 public class ValidationException extends ArchivaRestServiceException
36 public static final int DEFAULT_CODE = 422;
38 public static final ErrorMessage DEFAULT_MESSAGE = new ErrorMessage( ErrorKeys.VALIDATION_ERROR );
40 private List<ValidationError> validationErrors;
42 public ValidationException( ) {
43 super( DEFAULT_MESSAGE, DEFAULT_CODE );
46 public ValidationException( int errorCode) {
47 super( DEFAULT_MESSAGE, errorCode );
50 public ValidationException( List<ValidationError> errors) {
51 super( DEFAULT_MESSAGE, DEFAULT_CODE );
52 this.validationErrors = errors;
55 public static ValidationException of( List<org.apache.archiva.repository.validation.ValidationError> errorList ) {
56 return new ValidationException( errorList.stream( ).map( ValidationError::of ).collect( Collectors.toList( ) ) );
59 public static <R extends Repository> ValidationException of( ValidationResponse<R> result ) {
60 if (result.isValid()) {
61 return new ValidationException( );
64 return new ValidationException( result.getResult( ).entrySet( ).stream( ).flatMap(
65 v -> v.getValue( ).stream( ).map( e -> ValidationError.of( v.getKey( ), e ) ) ).collect( Collectors.toList( ) ) );
69 public List<ValidationError> getValidationErrors( )
71 return validationErrors==null? Collections.emptyList() : validationErrors;
74 public void setValidationErrors( List<ValidationError> validationErrors )
76 this.validationErrors = validationErrors;
79 public void addValidationError( ValidationError error) {
80 if (this.validationErrors==null) {
81 this.validationErrors = new ArrayList<>( );
83 this.validationErrors.add( error );