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