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