]> source.dussan.org Git - archiva.git/blob
28482f7c3912790f33877902ecbb8b2c6fd9b51b
[archiva.git] /
1 package org.apache.archiva.admin.repository.remote;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
25 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
26 import org.apache.archiva.audit.AuditEvent;
27 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
28 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
29 import org.apache.archiva.common.plexusbridge.PlexusSisuBridgeException;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.commons.lang.StringUtils;
34 import org.apache.maven.index.NexusIndexer;
35 import org.apache.maven.index.context.IndexCreator;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
38 import org.springframework.stereotype.Service;
39
40 import javax.annotation.PostConstruct;
41 import javax.annotation.PreDestroy;
42 import javax.inject.Inject;
43 import java.io.File;
44 import java.io.IOException;
45 import java.net.MalformedURLException;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 /**
52  * @author Olivier Lamy
53  * @since 1.4-M1
54  */
55 @Service("remoteRepositoryAdmin#default")
56 public class DefaultRemoteRepositoryAdmin
57     extends AbstractRepositoryAdmin
58     implements RemoteRepositoryAdmin
59 {
60
61     @Inject
62     private PlexusSisuBridge plexusSisuBridge;
63
64     @Inject
65     private MavenIndexerUtils mavenIndexerUtils;
66
67     // fields
68     private List<? extends IndexCreator> indexCreators;
69
70     private NexusIndexer indexer;
71
72     @PostConstruct
73     private void initialize()
74         throws RepositoryAdminException
75     {
76         try
77         {
78             indexCreators = mavenIndexerUtils.getAllIndexCreators();
79             indexer = plexusSisuBridge.lookup( NexusIndexer.class );
80         }
81         catch ( PlexusSisuBridgeException e )
82         {
83             throw new RepositoryAdminException( e.getMessage(), e );
84         }
85         for ( RemoteRepository remoteRepository : getRemoteRepositories() )
86         {
87             createIndexContext( remoteRepository );
88         }
89     }
90
91     @PreDestroy
92     private void shutdown()
93         throws RepositoryAdminException
94     {
95         try
96         {
97             List<RemoteRepository> remoteRepositories = getRemoteRepositories();
98             // close index on shutdown
99             for ( RemoteRepository remoteRepository : remoteRepositories )
100             {
101                 IndexingContext context = indexer.getIndexingContexts().get( remoteRepository.getId() );
102                 if ( context != null )
103                 {
104                     indexer.removeIndexingContext( context, false );
105                 }
106             }
107         }
108         catch ( IOException e )
109         {
110             throw new RepositoryAdminException( e.getMessage(), e );
111         }
112     }
113
114
115     @Override
116     public List<RemoteRepository> getRemoteRepositories()
117         throws RepositoryAdminException
118     {
119         List<RemoteRepository> remoteRepositories =
120             new ArrayList<>( getArchivaConfiguration().getConfiguration().getRemoteRepositories().size() );
121         for ( RemoteRepositoryConfiguration repositoryConfiguration : getArchivaConfiguration().getConfiguration().getRemoteRepositories() )
122         {
123             RemoteRepository remoteRepository =
124                 new RemoteRepository( repositoryConfiguration.getId(), repositoryConfiguration.getName(),
125                                       repositoryConfiguration.getUrl(), repositoryConfiguration.getLayout(),
126                                       repositoryConfiguration.getUsername(), repositoryConfiguration.getPassword(),
127                                       repositoryConfiguration.getTimeout() );
128             remoteRepository.setDownloadRemoteIndex( repositoryConfiguration.isDownloadRemoteIndex() );
129             remoteRepository.setRemoteIndexUrl( repositoryConfiguration.getRemoteIndexUrl() );
130             remoteRepository.setCronExpression( repositoryConfiguration.getRefreshCronExpression() );
131             remoteRepository.setIndexDirectory( repositoryConfiguration.getIndexDir() );
132             remoteRepository.setRemoteDownloadNetworkProxyId(
133                 repositoryConfiguration.getRemoteDownloadNetworkProxyId() );
134             remoteRepository.setRemoteDownloadTimeout( repositoryConfiguration.getRemoteDownloadTimeout() );
135             remoteRepository.setDownloadRemoteIndexOnStartup(
136                 repositoryConfiguration.isDownloadRemoteIndexOnStartup() );
137             remoteRepository.setDescription( repositoryConfiguration.getDescription() );
138             remoteRepository.setExtraHeaders( repositoryConfiguration.getExtraHeaders() );
139             remoteRepository.setExtraParameters( repositoryConfiguration.getExtraParameters() );
140             remoteRepositories.add( remoteRepository );
141         }
142         return remoteRepositories;
143     }
144
145     @Override
146     public RemoteRepository getRemoteRepository( String repositoryId )
147         throws RepositoryAdminException
148     {
149         for ( RemoteRepository remoteRepository : getRemoteRepositories() )
150         {
151             if ( StringUtils.equals( repositoryId, remoteRepository.getId() ) )
152             {
153                 return remoteRepository;
154             }
155         }
156         return null;
157     }
158
159     @Override
160     public Boolean addRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
161         throws RepositoryAdminException
162     {
163         triggerAuditEvent( remoteRepository.getId(), null, AuditEvent.ADD_REMOTE_REPO, auditInformation );
164         getRepositoryCommonValidator().basicValidation( remoteRepository, false );
165
166         //TODO we can validate it's a good uri/url
167         if ( StringUtils.isEmpty( remoteRepository.getUrl() ) )
168         {
169             throw new RepositoryAdminException( "url cannot be null" );
170         }
171
172         //MRM-752 - url needs trimming
173         remoteRepository.setUrl( StringUtils.trim( remoteRepository.getUrl() ) );
174
175         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
176             getRemoteRepositoryConfiguration( remoteRepository );
177
178         Configuration configuration = getArchivaConfiguration().getConfiguration();
179         configuration.addRemoteRepository( remoteRepositoryConfiguration );
180         saveConfiguration( configuration );
181
182         return Boolean.TRUE;
183     }
184
185     @Override
186     public Boolean deleteRemoteRepository( String repositoryId, AuditInformation auditInformation )
187         throws RepositoryAdminException
188     {
189
190         triggerAuditEvent( repositoryId, null, AuditEvent.DELETE_REMOTE_REPO, auditInformation );
191
192         Configuration configuration = getArchivaConfiguration().getConfiguration();
193
194         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
195             configuration.getRemoteRepositoriesAsMap().get( repositoryId );
196         if ( remoteRepositoryConfiguration == null )
197         {
198             throw new RepositoryAdminException(
199                 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
200         }
201
202         configuration.removeRemoteRepository( remoteRepositoryConfiguration );
203
204         // TODO use ProxyConnectorAdmin interface ?
205         // [MRM-520] Proxy Connectors are not deleted with the deletion of a Repository.
206         List<ProxyConnectorConfiguration> proxyConnectors = new ArrayList<>( configuration.getProxyConnectors() );
207         for ( ProxyConnectorConfiguration proxyConnector : proxyConnectors )
208         {
209             if ( StringUtils.equals( proxyConnector.getTargetRepoId(), repositoryId ) )
210             {
211                 configuration.removeProxyConnector( proxyConnector );
212             }
213         }
214
215         saveConfiguration( configuration );
216
217         return Boolean.TRUE;
218     }
219
220     @Override
221     public Boolean updateRemoteRepository( RemoteRepository remoteRepository, AuditInformation auditInformation )
222         throws RepositoryAdminException
223     {
224
225         String repositoryId = remoteRepository.getId();
226
227         triggerAuditEvent( repositoryId, null, AuditEvent.MODIFY_REMOTE_REPO, auditInformation );
228
229         // update means : remove and add
230
231         Configuration configuration = getArchivaConfiguration().getConfiguration();
232
233         RemoteRepositoryConfiguration remoteRepositoryConfiguration =
234             configuration.getRemoteRepositoriesAsMap().get( repositoryId );
235         if ( remoteRepositoryConfiguration == null )
236         {
237             throw new RepositoryAdminException(
238                 "remoteRepository with id " + repositoryId + " not exist cannot remove it" );
239         }
240
241         configuration.removeRemoteRepository( remoteRepositoryConfiguration );
242
243         remoteRepositoryConfiguration = getRemoteRepositoryConfiguration( remoteRepository );
244         configuration.addRemoteRepository( remoteRepositoryConfiguration );
245         saveConfiguration( configuration );
246
247         return Boolean.TRUE;
248     }
249
250     @Override
251     public Map<String, RemoteRepository> getRemoteRepositoriesAsMap()
252         throws RepositoryAdminException
253     {
254         java.util.Map<String, RemoteRepository> map = new HashMap<>();
255
256         for ( RemoteRepository repo : getRemoteRepositories() )
257         {
258             map.put( repo.getId(), repo );
259         }
260
261         return map;
262     }
263
264     @Override
265     public IndexingContext createIndexContext( RemoteRepository remoteRepository )
266         throws RepositoryAdminException
267     {
268         try
269         {
270             String appServerBase = getRegistry().getString( "appserver.base" );
271
272             String contextKey = "remote-" + remoteRepository.getId();
273             IndexingContext indexingContext = indexer.getIndexingContexts().get( contextKey );
274             if ( indexingContext != null )
275             {
276                 return indexingContext;
277             }
278             // create remote repository path
279             File repoDir = new File( appServerBase, "data/remotes/" + remoteRepository.getId() );
280             if ( !repoDir.exists() )
281             {
282                 repoDir.mkdirs();
283             }
284
285             File indexDirectory = null;
286
287             // is there configured indexDirectory ?
288             String indexDirectoryPath = remoteRepository.getIndexDirectory();
289
290             if ( StringUtils.isNotBlank( indexDirectoryPath ) )
291             {
292                 if ( new File( indexDirectoryPath ).isAbsolute() )
293                 {
294                     indexDirectory = new File( indexDirectoryPath );
295                 }
296                 else
297                 {
298                     indexDirectory = new File( repoDir, indexDirectoryPath );
299                 }
300             }
301             // if not configured use a default value
302             if ( indexDirectory == null )
303             {
304                 indexDirectory = new File( repoDir, ".index" );
305             }
306             if ( !indexDirectory.exists() )
307             {
308                 indexDirectory.mkdirs();
309             }
310             return indexer.addIndexingContext( contextKey, remoteRepository.getId(), repoDir, indexDirectory,
311                                                remoteRepository.getUrl(), calculateIndexRemoteUrl( remoteRepository ),
312                                                mavenIndexerUtils.getAllIndexCreators() );
313         }
314         catch ( MalformedURLException e )
315         {
316             throw new RepositoryAdminException( e.getMessage(), e );
317         }
318         catch ( IOException e )
319         {
320             throw new RepositoryAdminException( e.getMessage(), e );
321         }
322         catch ( UnsupportedExistingLuceneIndexException e )
323         {
324             throw new RepositoryAdminException( e.getMessage(), e );
325         }
326
327     }
328
329     protected String calculateIndexRemoteUrl( RemoteRepository remoteRepository )
330     {
331         if ( StringUtils.startsWith( remoteRepository.getRemoteIndexUrl(), "http" ) )
332         {
333             String baseUrl = remoteRepository.getRemoteIndexUrl();
334             return baseUrl.endsWith( "/" ) ? StringUtils.substringBeforeLast( baseUrl, "/" ) : baseUrl;
335         }
336         String baseUrl = StringUtils.endsWith( remoteRepository.getUrl(), "/" ) ? StringUtils.substringBeforeLast(
337             remoteRepository.getUrl(), "/" ) : remoteRepository.getUrl();
338
339         baseUrl = StringUtils.isEmpty( remoteRepository.getRemoteIndexUrl() )
340             ? baseUrl + "/.index"
341             : baseUrl + "/" + remoteRepository.getRemoteIndexUrl();
342         return baseUrl;
343
344     }
345
346     private RemoteRepositoryConfiguration getRemoteRepositoryConfiguration( RemoteRepository remoteRepository )
347     {
348         RemoteRepositoryConfiguration remoteRepositoryConfiguration = new RemoteRepositoryConfiguration();
349         remoteRepositoryConfiguration.setId( remoteRepository.getId() );
350         remoteRepositoryConfiguration.setPassword( remoteRepository.getPassword() );
351         remoteRepositoryConfiguration.setTimeout( remoteRepository.getTimeout() );
352         remoteRepositoryConfiguration.setUrl( remoteRepository.getUrl() );
353         remoteRepositoryConfiguration.setUsername( remoteRepository.getUserName() );
354         remoteRepositoryConfiguration.setLayout( remoteRepository.getLayout() );
355         remoteRepositoryConfiguration.setName( remoteRepository.getName() );
356         remoteRepositoryConfiguration.setDownloadRemoteIndex( remoteRepository.isDownloadRemoteIndex() );
357         remoteRepositoryConfiguration.setRemoteIndexUrl( remoteRepository.getRemoteIndexUrl() );
358         remoteRepositoryConfiguration.setRefreshCronExpression( remoteRepository.getCronExpression() );
359         remoteRepositoryConfiguration.setIndexDir( remoteRepository.getIndexDirectory() );
360         remoteRepositoryConfiguration.setRemoteDownloadNetworkProxyId(
361             remoteRepository.getRemoteDownloadNetworkProxyId() );
362         remoteRepositoryConfiguration.setRemoteDownloadTimeout( remoteRepository.getRemoteDownloadTimeout() );
363         remoteRepositoryConfiguration.setDownloadRemoteIndexOnStartup(
364             remoteRepository.isDownloadRemoteIndexOnStartup() );
365         remoteRepositoryConfiguration.setDescription( remoteRepository.getDescription() );
366         remoteRepositoryConfiguration.setExtraHeaders( remoteRepository.getExtraHeaders() );
367         remoteRepositoryConfiguration.setExtraParameters( remoteRepository.getExtraParameters() );
368         return remoteRepositoryConfiguration;
369     }
370
371 }