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.

PropagateErrorsOnUpdateDownloadPolicy.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package org.apache.archiva.policies;
  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. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.commons.lang.StringUtils;
  21. import org.springframework.stereotype.Service;
  22. import java.io.File;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Properties;
  27. /**
  28. * PropagateErrorsPolicy - a policy applied on error to determine how to treat the error.
  29. */
  30. @Service( "downloadErrorPolicy#propagate-errors-on-update" )
  31. public class PropagateErrorsOnUpdateDownloadPolicy
  32. implements DownloadErrorPolicy
  33. {
  34. /**
  35. * Signifies any error should cause a failure whether the artifact is already present or not.
  36. */
  37. public static final String ALWAYS = "always";
  38. /**
  39. * Signifies any error should cause a failure only if the artifact is not already present.
  40. */
  41. public static final String NOT_PRESENT = "artifact not already present";
  42. private List<String> options = new ArrayList<>( 2 );
  43. public PropagateErrorsOnUpdateDownloadPolicy()
  44. {
  45. options.add( ALWAYS );
  46. options.add( NOT_PRESENT );
  47. }
  48. public boolean applyPolicy( String policySetting, Properties request, File localFile, Exception exception,
  49. Map<String, Exception> previousExceptions )
  50. throws PolicyConfigurationException
  51. {
  52. if ( !options.contains( policySetting ) )
  53. {
  54. // Not a valid code.
  55. throw new PolicyConfigurationException(
  56. "Unknown error policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
  57. options.iterator(), "," ) + "]" );
  58. }
  59. if ( ALWAYS.equals( policySetting ) )
  60. {
  61. // throw ther exception regardless
  62. return true;
  63. }
  64. if ( NOT_PRESENT.equals( policySetting ) )
  65. {
  66. // cancel the exception if the file exists
  67. return !localFile.exists();
  68. }
  69. throw new PolicyConfigurationException(
  70. "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
  71. }
  72. public String getDefaultOption()
  73. {
  74. return NOT_PRESENT;
  75. }
  76. public String getId()
  77. {
  78. return "propagate-errors-on-update";
  79. }
  80. public String getName()
  81. {
  82. return "Return error when";
  83. }
  84. public List<String> getOptions()
  85. {
  86. return options;
  87. }
  88. }