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