1 package org.apache.archiva.configuration;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.FileTypeUtils;
23 import org.apache.archiva.configuration.functors.FiletypeSelectionPredicate;
24 import org.apache.archiva.components.registry.Registry;
25 import org.apache.archiva.components.registry.RegistryListener;
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;
31 import javax.annotation.PostConstruct;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import java.nio.file.FileSystems;
35 import java.nio.file.Paths;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
46 public class FileTypes
47 implements RegistryListener
49 public static final String ARTIFACTS = "artifacts";
51 public static final String AUTO_REMOVE = "auto-remove";
53 public static final String INDEXABLE_CONTENT = "indexable-content";
55 public static final String IGNORED = "ignored";
58 @Named(value = "archivaConfiguration#default")
59 private ArchivaConfiguration archivaConfiguration;
67 * Map of default values for the file types.
69 private Map<String, List<String>> defaultTypeMap = new HashMap<>();
71 private List<String> artifactPatterns;
74 * Default exclusions from artifact consumers that are using the file types. Note that this is simplistic in the
75 * case of the support files (based on extension) as it is elsewhere - it may be better to match these to actual
76 * artifacts and exclude later during scanning.
80 public static final List<String> DEFAULT_EXCLUSIONS = FileTypeUtils.DEFAULT_EXCLUSIONS;
82 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
84 this.archivaConfiguration = archivaConfiguration;
88 * Get the list of patterns for a specified filetype.
89 * You will always get a list. In this order.
91 * <li>The Configured List</li>
92 * <li>The Default List</li>
93 * <li>A single item list of <code>"**/*"</code></li>
96 * @param id the id to lookup.
97 * @return the list of patterns.
99 public List<String> getFileTypePatterns( String id )
101 Configuration config = archivaConfiguration.getConfiguration();
102 Predicate selectedFiletype = new FiletypeSelectionPredicate( id );
103 RepositoryScanningConfiguration repositoryScanningConfiguration = config.getRepositoryScanning();
104 if ( repositoryScanningConfiguration != null )
107 IterableUtils.find( config.getRepositoryScanning().getFileTypes(), selectedFiletype );
109 if ( ( filetype != null ) && CollectionUtils.isNotEmpty( filetype.getPatterns() ) )
111 return filetype.getPatterns();
114 List<String> defaultPatterns = defaultTypeMap.get( id );
116 if ( CollectionUtils.isEmpty( defaultPatterns ) )
118 return Collections.singletonList( "**/*" );
121 return defaultPatterns;
124 public synchronized boolean matchesArtifactPattern( String relativePath )
126 // Correct the slash pattern.
127 relativePath = relativePath.replace( '\\', '/' );
129 if ( artifactPatterns == null )
131 artifactPatterns = getFileTypePatterns( ARTIFACTS );
134 for ( String pattern : artifactPatterns )
136 if ( FileSystems.getDefault().getPathMatcher( "glob:" + pattern).matches( Paths.get( relativePath ) ) )
147 public boolean matchesDefaultExclusions( String relativePath )
149 // Correct the slash pattern.
150 relativePath = relativePath.replace( '\\', '/' );
152 for ( String pattern : DEFAULT_EXCLUSIONS )
154 if ( FileSystems.getDefault().getPathMatcher( "glob:" + pattern).matches( Paths.get( relativePath ) ) )
166 public void initialize()
168 initialiseTypeMap( this.archivaConfiguration.getConfiguration() );
170 this.archivaConfiguration.addChangeListener( this );
173 private void initialiseTypeMap( Configuration configuration )
175 defaultTypeMap.clear();
177 // Store the default file type declaration.
178 List<FileType> filetypes = configuration.getRepositoryScanning().getFileTypes();
179 for ( FileType filetype : filetypes )
181 List<String> patterns = defaultTypeMap.get( filetype.getId() );
182 if ( patterns == null )
184 patterns = new ArrayList<>( filetype.getPatterns().size() );
186 patterns.addAll( filetype.getPatterns() );
188 defaultTypeMap.put( filetype.getId(), patterns );
193 public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
195 if ( propertyName.contains( "fileType" ) )
197 artifactPatterns = null;
199 initialiseTypeMap( archivaConfiguration.getConfiguration() );
204 public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )