]> source.dussan.org Git - archiva.git/blob
a77d0ebc4bb646a33a5b3953eaf1c3c2e3151733
[archiva.git] /
1 package $package;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.archiva.configuration.model.ManagedRepositoryConfiguration;
23 import org.apache.archiva.configuration.model.RemoteRepositoryConfiguration;
24 import org.apache.archiva.configuration.model.RepositoryGroupConfiguration;
25 import org.apache.archiva.event.EventHandler;
26 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
27 import org.apache.archiva.repository.base.remote.BasicRemoteRepository;
28 import org.apache.archiva.repository.EditableManagedRepository;
29 import org.apache.archiva.repository.EditableRemoteRepository;
30 import org.apache.archiva.repository.RepositoryGroup;
31 import org.apache.archiva.repository.EditableRepositoryGroup;
32 import org.apache.archiva.repository.ManagedRepository;
33 import org.apache.archiva.repository.base.PasswordCredentials;
34 import org.apache.archiva.repository.ReleaseScheme;
35 import org.apache.archiva.repository.RemoteRepository;
36 import org.apache.archiva.repository.RepositoryCredentials;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryProvider;
39 import org.apache.archiva.repository.RepositoryType;
40 import org.apache.archiva.event.Event;
41 import org.apache.archiva.repository.event.RepositoryEvent;
42 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
43 import org.apache.archiva.repository.features.IndexCreationFeature;
44 import org.apache.archiva.repository.features.RemoteIndexFeature;
45 import org.apache.archiva.repository.features.StagingRepositoryFeature;
46 import org.springframework.stereotype.Service;
47
48 import java.io.IOException;
49 import java.net.URI;
50 import java.nio.file.Paths;
51 import java.time.Duration;
52 import java.time.Period;
53 import java.util.HashSet;
54 import java.util.Set;
55
56 /**
57  * Just a simple mock class for the repository provider
58  */
59 @Service("mockRepositoryProvider")
60 public class RepositoryProviderMock implements RepositoryProvider
61 {
62
63     private static final Set<RepositoryType> TYPES = new HashSet<>( );
64
65     static
66     {
67         TYPES.add( RepositoryType.MAVEN );
68         TYPES.add( RepositoryType.NPM );
69     }
70
71     @Override
72     public Set<RepositoryType> provides( )
73     {
74         return TYPES;
75     }
76
77     @Override
78     public EditableManagedRepository createManagedInstance( String id, String name )
79     {
80         try {
81             return BasicManagedRepository.newFilesystemInstance(id, name, Paths.get("target/repositories"));
82         } catch (IOException e) {
83             throw new RuntimeException(e);
84         }
85     }
86
87     @Override
88     public EditableRemoteRepository createRemoteInstance( String id, String name )
89     {
90         try {
91             return BasicRemoteRepository.newFilesystemInstance(id, name, Paths.get("target/remotes"));
92         } catch (IOException e) {
93             throw new RuntimeException(e);
94         }
95     }
96
97     @Override
98     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
99     {
100         BasicManagedRepository managedRepository;
101         try {
102             managedRepository = BasicManagedRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/repositories"));
103         } catch (IOException e) {
104             throw new RepositoryException(e);
105         }
106         updateManagedInstance( managedRepository, configuration );
107         return managedRepository;
108     }
109
110
111     @Override
112     public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
113     {
114         try
115         {
116             managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
117             managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
118             managedRepository.setBaseUri( new URI( "" ) );
119             managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
120             managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
121             managedRepository.setLayout( configuration.getLayout( ) );
122             managedRepository.setScanned( configuration.isScanned( ) );
123             managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
124             if (configuration.isReleases()) {
125                 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
126             }
127             if (configuration.isSnapshots()) {
128                 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
129             }
130             ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
131             acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
132             acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
133             acf.setRetentionCount( configuration.getRetentionCount( ) );
134             IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
135             icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
136             icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
137             StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
138             srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
139         }
140         catch ( Exception e )
141         {
142             throw new RepositoryException( "Error", e );
143         }
144
145     }
146
147
148     @Override
149     public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
150     {
151         String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
152         try {
153             BasicManagedRepository managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories"));
154             updateManagedInstance(managedRepository, configuration);
155             return managedRepository;
156         } catch (IOException e) {
157             throw new RepositoryException(e);
158         }
159     }
160
161     @Override
162     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
163     {
164         try {
165             BasicRemoteRepository remoteRepository = BasicRemoteRepository.newFilesystemInstance(configuration.getId(), configuration.getName(), Paths.get("target/remotes"));
166             updateRemoteInstance(remoteRepository, configuration);
167             return remoteRepository;
168         } catch (IOException e) {
169             throw new RepositoryException(e);
170         }
171     }
172
173     @Override
174     public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
175     {
176         try
177         {
178             remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
179             remoteRepository.setBaseUri( new URI( "" ) );
180             remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
181             remoteRepository.setLayout( configuration.getLayout( ) );
182             remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
183             remoteRepository.setCheckPath( configuration.getCheckPath( ) );
184             remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
185             remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
186             remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
187             char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
188             remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
189             remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
190             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
191             rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
192             rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
193             rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
194             rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
195             rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
196         }
197         catch ( Exception e )
198         {
199             throw new RepositoryException( "Error", e );
200         }
201
202     }
203
204     @Override
205     public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository ) throws RepositoryException
206     {
207         ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
208         configuration.setId( managedRepository.getId( ) );
209         configuration.setName(managedRepository.getName());
210         configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
211         configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
212         configuration.setDescription( managedRepository.getDescription( ) );
213         configuration.setLayout( managedRepository.getLayout( ) );
214         configuration.setScanned( managedRepository.isScanned( ) );
215         configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
216         configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
217         configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
218         ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
219         configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
220         configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
221         configuration.setRetentionCount( acf.getRetentionCount( ) );
222         IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
223         configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
224         configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
225         StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
226         configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
227         return configuration;
228     }
229
230
231     @Override
232     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository ) throws RepositoryException
233     {
234         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
235         configuration.setId( remoteRepository.getId( ) );
236         configuration.setName( remoteRepository.getName( ) );
237         configuration.setDescription( remoteRepository.getDescription( ) );
238         configuration.setLayout( remoteRepository.getLayout( ) );
239         configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
240         configuration.setCheckPath( remoteRepository.getCheckPath( ) );
241         configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
242         configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
243         configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
244         RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
245         if (creds!=null)
246         {
247             PasswordCredentials pwdCreds = (PasswordCredentials) creds;
248             configuration.setUsername( pwdCreds.getUsername( ) );
249             configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
250         }
251         configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
252         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
253         configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
254         configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
255         configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
256         configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
257         return configuration;
258     }
259
260     @Override
261     public void handle(Event event) {
262
263     }
264
265     @Override
266     public RepositoryGroupConfiguration getRepositoryGroupConfiguration(RepositoryGroup repositoryGroup) throws RepositoryException {
267         return null;
268     }
269
270     @Override
271     public RepositoryGroup createRepositoryGroup(RepositoryGroupConfiguration configuration) throws RepositoryException {
272         return null;
273     }
274     @Override
275     public EditableRepositoryGroup createRepositoryGroup(String id, String name) {
276         return null;
277     }
278
279
280     @Override
281     public void updateRepositoryGroupInstance(EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration) throws RepositoryException {
282
283     }
284
285     public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
286     {
287         // do nothing
288     }
289 }