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