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