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.

FileTypes.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package org.apache.archiva.configuration.provider;
  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.common.FileTypeUtils;
  20. import org.apache.archiva.components.registry.Registry;
  21. import org.apache.archiva.components.registry.RegistryListener;
  22. import org.apache.archiva.configuration.model.Configuration;
  23. import org.apache.archiva.configuration.model.FileType;
  24. import org.apache.archiva.configuration.model.RepositoryScanningConfiguration;
  25. import org.apache.archiva.configuration.model.functors.FiletypeSelectionPredicate;
  26. import org.apache.commons.collections4.CollectionUtils;
  27. import org.apache.commons.collections4.IterableUtils;
  28. import org.apache.commons.collections4.Predicate;
  29. import org.springframework.stereotype.Service;
  30. import javax.annotation.PostConstruct;
  31. import javax.inject.Inject;
  32. import javax.inject.Named;
  33. import java.nio.file.FileSystems;
  34. import java.nio.file.Paths;
  35. import java.util.ArrayList;
  36. import java.util.Collections;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. /**
  41. * FileTypes
  42. */
  43. @Service("fileTypes")
  44. public class FileTypes
  45. implements RegistryListener
  46. {
  47. public static final String ARTIFACTS = "artifacts";
  48. public static final String AUTO_REMOVE = "auto-remove";
  49. public static final String INDEXABLE_CONTENT = "indexable-content";
  50. public static final String IGNORED = "ignored";
  51. @Inject
  52. @Named(value = "archivaConfiguration#default")
  53. private ArchivaConfiguration archivaConfiguration;
  54. public FileTypes() {
  55. }
  56. /**
  57. * Map of default values for the file types.
  58. */
  59. private Map<String, List<String>> defaultTypeMap = new HashMap<>();
  60. private List<String> artifactPatterns;
  61. /**
  62. * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
  63. * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
  64. * artifacts and exclude later during scanning.
  65. *
  66. * @deprecated
  67. */
  68. public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
  69. public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
  70. {
  71. this.archivaConfiguration = archivaConfiguration;
  72. }
  73. /**
  74. * Get the list of patterns for a specified filetype.
  75. * You will always get a list. In this order.
  76. * <ul>
  77. * <li>The Configured List</li>
  78. * <li>The Default List</li>
  79. * <li>A single item list of <code>&quot;**&#47;*&quot;</code></li>
  80. * </ul>
  81. *
  82. * @param id the id to lookup.
  83. * @return the list of patterns.
  84. */
  85. public List<String> getFileTypePatterns( String id )
  86. {
  87. Configuration config = archivaConfiguration.getConfiguration();
  88. Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
  89. RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
  90. if ( repositoryScanningConfiguration != null )
  91. {
  92. FileType filetype =
  93. IterableUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
  94. if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
  95. {
  96. return filetype.getPatterns();
  97. }
  98. }
  99. List<String> defaultPatterns = defaultTypeMap.get( id );
  100. if ( CollectionUtils.isEmpty( defaultPatterns ) )
  101. {
  102. return Collections.singletonList( "**/*" );
  103. }
  104. return defaultPatterns;
  105. }
  106. public synchronized boolean matchesArtifactPattern( String relativePath )
  107. {
  108. // Correct the slash pattern.
  109. relativePath = relativePath.replace( '\\', '/' );
  110. if ( artifactPatterns == null )
  111. {
  112. artifactPatterns = getFileTypePatterns( ARTIFACTS );
  113. }
  114. for ( String pattern : artifactPatterns )
  115. {
  116. if ( FileSystems.getDefault().getPathMatcher( "glob:" + pattern).matches( Paths.get( relativePath ) ) )
  117. {
  118. // Found match
  119. return true;
  120. }
  121. }
  122. // No match.
  123. return false;
  124. }
  125. public boolean matchesDefaultExclusions( String relativePath )
  126. {
  127. // Correct the slash pattern.
  128. relativePath = relativePath.replace( '\\', '/' );
  129. for ( String pattern : DEFAULT_EXCLUSIONS )
  130. {
  131. if ( FileSystems.getDefault().getPathMatcher( "glob:" + pattern).matches( Paths.get( relativePath ) ) )
  132. {
  133. // Found match
  134. return true;
  135. }
  136. }
  137. // No match.
  138. return false;
  139. }
  140. @PostConstruct
  141. public void initialize()
  142. {
  143. initialiseTypeMap( this.archivaConfiguration.getConfiguration() );
  144. this.archivaConfiguration.addChangeListener( this );
  145. }
  146. private void initialiseTypeMap( Configuration configuration )
  147. {
  148. defaultTypeMap.clear();
  149. // Store the default file type declaration.
  150. List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
  151. for ( FileType filetype : filetypes )
  152. {
  153. List<String> patterns = defaultTypeMap.get( filetype.getId() );
  154. if ( patterns == null )
  155. {
  156. patterns = new ArrayList<>( filetype.getPatterns().size() );
  157. }
  158. patterns.addAll( filetype.getPatterns() );
  159. defaultTypeMap.put( filetype.getId(), patterns );
  160. }
  161. }
  162. @Override
  163. public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  164. {
  165. if ( propertyName.contains( "fileType" ) )
  166. {
  167. artifactPatterns = null;
  168. initialiseTypeMap( archivaConfiguration.getConfiguration() );
  169. }
  170. }
  171. @Override
  172. public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
  173. {
  174. /* nothing to do */
  175. }
  176. }