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