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