]> source.dussan.org Git - archiva.git/blob
d11008a687a968b87fc49f5628a5ca5ab679f0a3
[archiva.git] /
1 package org.apache.archiva.repository.mock;
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.ManagedRepositoryConfiguration;
23 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
24 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
25 import org.apache.archiva.event.EventHandler;
26 import org.apache.archiva.repository.base.BasicManagedRepository;
27 import org.apache.archiva.repository.base.BasicRemoteRepository;
28 import org.apache.archiva.repository.EditableManagedRepository;
29 import org.apache.archiva.repository.EditableRemoteRepository;
30 import org.apache.archiva.repository.EditableRepositoryGroup;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.base.PasswordCredentials;
33 import org.apache.archiva.repository.ReleaseScheme;
34 import org.apache.archiva.repository.RemoteRepository;
35 import org.apache.archiva.repository.RepositoryCredentials;
36 import org.apache.archiva.event.Event;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryGroup;
39 import org.apache.archiva.repository.RepositoryProvider;
40 import org.apache.archiva.repository.RepositoryType;
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").resolve(id) );
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 EditableRepositoryGroup createRepositoryGroup( String id, String name )
99     {
100
101         return null;
102     }
103
104     @Override
105     public ManagedRepository createManagedInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
106     {
107         BasicManagedRepository managedRepository = null;
108         try {
109             managedRepository = BasicManagedRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/repositories").resolve(configuration.getId()) );
110         } catch (IOException e) {
111             throw new RepositoryException(e);
112         }
113         updateManagedInstance( managedRepository, configuration );
114         return managedRepository;
115     }
116
117
118     @Override
119     public void updateManagedInstance( EditableManagedRepository managedRepository, ManagedRepositoryConfiguration configuration ) throws RepositoryException
120     {
121         try
122         {
123             managedRepository.setName( managedRepository.getPrimaryLocale(), configuration.getName( ) );
124             managedRepository.setLocation( new URI( configuration.getLocation( )==null ?"" : configuration.getLocation() ) );
125             managedRepository.setBaseUri( new URI( "" ) );
126             managedRepository.setBlocksRedeployment( configuration.isBlockRedeployments( ) );
127             managedRepository.setDescription( managedRepository.getPrimaryLocale(), configuration.getDescription( ) );
128             managedRepository.setLayout( configuration.getLayout( ) );
129             managedRepository.setScanned( configuration.isScanned( ) );
130             managedRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
131             if (configuration.isReleases()) {
132                 managedRepository.addActiveReleaseScheme( ReleaseScheme.RELEASE );
133             }
134             if (configuration.isSnapshots()) {
135                 managedRepository.addActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
136             }
137             ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
138             acf.setRetentionPeriod( Period.ofDays( configuration.getRetentionPeriod( ) ) );
139             acf.setDeleteReleasedSnapshots( configuration.isDeleteReleasedSnapshots( ) );
140             acf.setRetentionCount( configuration.getRetentionCount( ) );
141             IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
142             icf.setIndexPath( new URI( configuration.getIndexDir( ) ) );
143             icf.setSkipPackedIndexCreation( configuration.isSkipPackedIndexCreation( ) );
144             StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
145             srf.setStageRepoNeeded( configuration.isStageRepoNeeded( ) );
146         }
147         catch ( Exception e )
148         {
149             throw new RepositoryException( "Error", e );
150         }
151
152     }
153
154
155     @Override
156     public ManagedRepository createStagingInstance( ManagedRepositoryConfiguration configuration ) throws RepositoryException
157     {
158         String id = configuration.getId( ) + StagingRepositoryFeature.STAGING_REPO_POSTFIX;
159         BasicManagedRepository managedRepository = null;
160         try {
161             managedRepository = BasicManagedRepository.newFilesystemInstance(id, configuration.getName(), Paths.get("target/repositories").resolve(id));
162         } catch (IOException e) {
163             throw new RepositoryException(e);
164         }
165         updateManagedInstance( managedRepository, configuration );
166         return managedRepository;
167     }
168
169     @Override
170     public RemoteRepository createRemoteInstance( RemoteRepositoryConfiguration configuration ) throws RepositoryException
171     {
172         BasicRemoteRepository remoteRepository = null;
173         try {
174             remoteRepository = BasicRemoteRepository.newFilesystemInstance( configuration.getId( ), configuration.getName( ), Paths.get("target/remotes") );
175         } catch (IOException e) {
176             throw new RepositoryException(e);
177         }
178         updateRemoteInstance( remoteRepository, configuration );
179         return remoteRepository;
180     }
181
182     @SuppressWarnings("unchecked")
183     @Override
184     public void updateRemoteInstance( EditableRemoteRepository remoteRepository, RemoteRepositoryConfiguration configuration ) throws RepositoryException
185     {
186         try
187         {
188             remoteRepository.setName( remoteRepository.getPrimaryLocale(), configuration.getName( ) );
189             remoteRepository.setBaseUri( new URI( "" ) );
190             remoteRepository.setDescription( remoteRepository.getPrimaryLocale(), configuration.getDescription( ) );
191             remoteRepository.setLayout( configuration.getLayout( ) );
192             remoteRepository.setSchedulingDefinition( configuration.getRefreshCronExpression( ) );
193             remoteRepository.setCheckPath( configuration.getCheckPath( ) );
194             remoteRepository.setExtraHeaders( configuration.getExtraHeaders( ) );
195             remoteRepository.setExtraParameters( configuration.getExtraParameters( ) );
196             remoteRepository.setTimeout( Duration.ofSeconds( configuration.getTimeout( ) ) );
197             char[] pwd = configuration.getPassword()==null ? "".toCharArray() : configuration.getPassword().toCharArray();
198             remoteRepository.setCredentials( new PasswordCredentials( configuration.getUsername( ), pwd ) );
199             remoteRepository.setLocation( new URI( configuration.getUrl( )==null ? "" : configuration.getUrl() ) );
200             RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
201             rif.setDownloadRemoteIndexOnStartup( configuration.isDownloadRemoteIndexOnStartup( ) );
202             rif.setDownloadRemoteIndex( configuration.isDownloadRemoteIndex( ) );
203             rif.setIndexUri( new URI( configuration.getIndexDir( ) ) );
204             rif.setDownloadTimeout( Duration.ofSeconds( configuration.getRemoteDownloadTimeout( ) ) );
205             rif.setProxyId( configuration.getRemoteDownloadNetworkProxyId( ) );
206         }
207         catch ( Exception e )
208         {
209             throw new RepositoryException( "Error", e );
210         }
211
212     }
213
214     @Override
215     public RepositoryGroup createRepositoryGroup( RepositoryGroupConfiguration configuration ) throws RepositoryException
216     {
217         return null;
218     }
219
220     @Override
221     public void updateRepositoryGroupInstance( EditableRepositoryGroup repositoryGroup, RepositoryGroupConfiguration configuration ) throws RepositoryException
222     {
223
224     }
225
226     @Override
227     public ManagedRepositoryConfiguration getManagedConfiguration( ManagedRepository managedRepository )
228     {
229         ManagedRepositoryConfiguration configuration = new ManagedRepositoryConfiguration( );
230         configuration.setId( managedRepository.getId( ) );
231         configuration.setName(managedRepository.getName());
232         configuration.setLocation( managedRepository.getLocation( ) == null ? "" : managedRepository.getLocation().toString( ) );
233         configuration.setBlockRedeployments( managedRepository.blocksRedeployments( ) );
234         configuration.setDescription( managedRepository.getDescription( ) );
235         configuration.setScanned( managedRepository.isScanned( ) );
236         configuration.setRefreshCronExpression( managedRepository.getSchedulingDefinition( ) );
237         configuration.setReleases( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.RELEASE) );
238         configuration.setSnapshots( managedRepository.getActiveReleaseSchemes().contains(ReleaseScheme.SNAPSHOT) );
239         configuration.setLayout( managedRepository.getLayout() );
240         ArtifactCleanupFeature acf = managedRepository.getFeature( ArtifactCleanupFeature.class ).get( );
241         configuration.setRetentionPeriod( acf.getRetentionPeriod( ).getDays( ) );
242         configuration.setDeleteReleasedSnapshots( acf.isDeleteReleasedSnapshots( ) );
243         configuration.setRetentionCount( acf.getRetentionCount( ) );
244         IndexCreationFeature icf = managedRepository.getFeature( IndexCreationFeature.class ).get( );
245         configuration.setSkipPackedIndexCreation( icf.isSkipPackedIndexCreation( ) );
246         configuration.setIndexDir( icf.getIndexPath( ) == null ? "" : icf.getIndexPath().toString( ) );
247         StagingRepositoryFeature srf = managedRepository.getFeature( StagingRepositoryFeature.class ).get( );
248         configuration.setStageRepoNeeded( srf.isStageRepoNeeded( ) );
249         return configuration;
250     }
251
252     @Override
253     public RepositoryGroupConfiguration getRepositoryGroupConfiguration( RepositoryGroup repositoryGroup ) throws RepositoryException
254     {
255         return null;
256     }
257
258     @Override
259     public void addRepositoryEventHandler( EventHandler<? super RepositoryEvent> eventHandler )
260     {
261         // do nothing
262     }
263
264
265     @Override
266     public RemoteRepositoryConfiguration getRemoteConfiguration( RemoteRepository remoteRepository )
267     {
268         RemoteRepositoryConfiguration configuration = new RemoteRepositoryConfiguration( );
269         configuration.setId( remoteRepository.getId( ) );
270         configuration.setName( remoteRepository.getName( ) );
271         configuration.setDescription( remoteRepository.getDescription( ) );
272         configuration.setLayout( remoteRepository.getLayout( ) );
273         configuration.setRefreshCronExpression( remoteRepository.getSchedulingDefinition( ) );
274         configuration.setCheckPath( remoteRepository.getCheckPath( ) );
275         configuration.setExtraHeaders( remoteRepository.getExtraHeaders( ) );
276         configuration.setExtraParameters( remoteRepository.getExtraParameters( ) );
277         configuration.setTimeout( (int) remoteRepository.getTimeout( ).getSeconds( ) );
278         RepositoryCredentials creds = remoteRepository.getLoginCredentials( );
279         if (creds!=null)
280         {
281             PasswordCredentials pwdCreds = (PasswordCredentials) creds;
282             configuration.setUsername( pwdCreds.getUsername( ) );
283             configuration.setPassword( new String( pwdCreds.getPassword( ) ) );
284         }
285         configuration.setUrl( remoteRepository.getLocation( ) == null ? "" : remoteRepository.getLocation().toString( ) );
286         RemoteIndexFeature rif = remoteRepository.getFeature( RemoteIndexFeature.class ).get( );
287         configuration.setDownloadRemoteIndex( rif.isDownloadRemoteIndex( ) );
288         configuration.setDownloadRemoteIndexOnStartup( rif.isDownloadRemoteIndexOnStartup( ) );
289         configuration.setIndexDir( rif.getIndexUri( )==null ? "" : rif.getIndexUri().toString( ) );
290         configuration.setRemoteDownloadNetworkProxyId( rif.getProxyId( ) );
291         return configuration;
292     }
293
294     @Override
295     public void handle(Event event) {
296
297     }
298 }