You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValidationResponse.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package org.apache.archiva.repository.validation;
  2. /*
  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
  10. *
  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
  17. * under the License.
  18. */
  19. import org.apache.archiva.repository.Repository;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * A validation response gives information about the validation status for certain attributes.
  27. *
  28. *
  29. * @author Martin Stockhammer <martin_s@apache.org>
  30. */
  31. public class ValidationResponse<R extends Repository> implements CheckedResult<R, Map<String, List<ValidationError>>>
  32. {
  33. final boolean valid;
  34. final R repository;
  35. final Map<String, List<ValidationError>> validationErrors = new HashMap<>( );
  36. public ValidationResponse( R repo, Map<String, List<ValidationError>> errors)
  37. {
  38. if( errors==null || errors.size()==0 ) {
  39. this.valid = true;
  40. } else {
  41. this.valid = false;
  42. validationErrors.putAll( errors );
  43. }
  44. this.repository = repo;
  45. }
  46. public static <S extends Repository> ValidationResponse<S> getValid( S repository )
  47. {
  48. return new ValidationResponse<>( repository, null );
  49. }
  50. @Override
  51. public R getRepository( )
  52. {
  53. return repository;
  54. }
  55. /**
  56. * Returns true, if the validation was successful and there are not validation errors.
  57. * @return <code>true</code>, if the validation was successful, otherwise <code>false</code>
  58. */
  59. @Override
  60. public boolean isValid( )
  61. {
  62. return valid;
  63. }
  64. @Override
  65. public Map<String, List<ValidationError>> getResult( )
  66. {
  67. return validationErrors;
  68. }
  69. /**
  70. * Add the given validation error to the list for the given attribute.
  71. *
  72. * @param attribute the name of the attribute
  73. * @param error the error that is added to the list
  74. */
  75. public void addValidationError(String attribute, ValidationError error) {
  76. if (!validationErrors.containsKey( attribute )) {
  77. validationErrors.put( attribute, new ArrayList<>( ) );
  78. }
  79. validationErrors.get( attribute ).add( error );
  80. }
  81. /**
  82. * Returns a list of validation errors that are stored for the given attribute. If there are no
  83. * errors stored for this attribute, a empty list is returned.
  84. *
  85. * @param attribute the name of the attribute
  86. * @return the list of validation errors
  87. */
  88. public List<ValidationError> getValidationErrors(String attribute) {
  89. if (validationErrors.containsKey( attribute )) {
  90. return validationErrors.get( attribute );
  91. } else {
  92. return Collections.emptyList( );
  93. }
  94. }
  95. }