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