Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FileTypes.java 6.5KB

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