1 package org.apache.archiva.rest.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.NetworkProxy;
24 import org.apache.archiva.admin.model.beans.RemoteRepository;
25 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
26 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.proxy.common.WagonFactory;
28 import org.apache.archiva.proxy.common.WagonFactoryRequest;
29 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
30 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.wagon.TransferFailedException;
33 import org.apache.maven.wagon.Wagon;
34 import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon;
35 import org.apache.maven.wagon.providers.http.HttpConfiguration;
36 import org.apache.maven.wagon.providers.http.HttpMethodConfiguration;
37 import org.apache.maven.wagon.proxy.ProxyInfo;
38 import org.apache.maven.wagon.repository.Repository;
39 import org.springframework.stereotype.Service;
41 import javax.annotation.PostConstruct;
42 import javax.inject.Inject;
43 import javax.ws.rs.core.Response;
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.List;
51 * @author Olivier Lamy
54 @Service( "remoteRepositoriesService#rest" )
55 public class DefaultRemoteRepositoriesService
56 extends AbstractRestService
57 implements RemoteRepositoriesService
61 private RemoteRepositoryAdmin remoteRepositoryAdmin;
64 private WagonFactory wagonFactory;
68 private NetworkProxyAdmin networkProxyAdmin;
70 int checkReadTimeout = 10000;
71 int checkTimeout = 9000;
73 // TODO: make this configurable
74 private Map<String,String> remoteConnectivityCheckPaths = new HashMap<>();
78 // default initialization for known servers
79 remoteConnectivityCheckPaths.put("http://download.oracle.com/maven","com/sleepycat/je/license.txt");
80 remoteConnectivityCheckPaths.put("https://download.oracle.com/maven","com/sleepycat/je/license.txt");
84 public List<RemoteRepository> getRemoteRepositories()
85 throws ArchivaRestServiceException
89 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
90 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
92 catch ( RepositoryAdminException e )
94 log.error( e.getMessage(), e );
95 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
100 public RemoteRepository getRemoteRepository( String repositoryId )
101 throws ArchivaRestServiceException
104 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
105 for ( RemoteRepository repository : remoteRepositories )
107 if ( StringUtils.equals( repositoryId, repository.getId() ) )
116 public Boolean deleteRemoteRepository( String repositoryId )
117 throws ArchivaRestServiceException
121 return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
123 catch ( RepositoryAdminException e )
125 log.error( e.getMessage(), e );
126 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
131 public Boolean addRemoteRepository( RemoteRepository remoteRepository )
132 throws ArchivaRestServiceException
136 return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
138 catch ( RepositoryAdminException e )
140 log.error( e.getMessage(), e );
141 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
146 public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
147 throws ArchivaRestServiceException
151 return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
153 catch ( RepositoryAdminException e )
155 log.error( e.getMessage(), e );
156 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
161 public Boolean checkRemoteConnectivity( String repositoryId )
162 throws ArchivaRestServiceException
166 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
167 if ( remoteRepository == null )
169 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
170 return Boolean.FALSE;
172 NetworkProxy networkProxy = null;
173 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
175 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
176 if ( networkProxy == null )
179 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
180 remoteRepository.getRemoteDownloadNetworkProxyId() );
184 String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
187 wagonFactory.getWagon( new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ) //
188 .networkProxy( networkProxy ) );
190 // hardcoded value as it's a check of the remote repo connectivity
191 wagon.setReadTimeout( checkReadTimeout );
192 wagon.setTimeout( checkTimeout );
194 if ( wagon instanceof AbstractHttpClientWagon )
196 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
197 .setUsePreemptive( true ) //
198 .setReadTimeout( checkReadTimeout );
199 HttpConfiguration httpConfiguration = new HttpConfiguration().setGet( httpMethodConfiguration );
200 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
203 ProxyInfo proxyInfo = null;
204 if ( networkProxy != null )
206 proxyInfo = new ProxyInfo();
207 proxyInfo.setType( networkProxy.getProtocol() );
208 proxyInfo.setHost( networkProxy.getHost() );
209 proxyInfo.setPort( networkProxy.getPort() );
210 proxyInfo.setUserName( networkProxy.getUsername() );
211 proxyInfo.setPassword( networkProxy.getPassword() );
213 String url = StringUtils.stripEnd(remoteRepository.getUrl(),"/");
214 wagon.connect( new Repository( remoteRepository.getId(), url ), proxyInfo );
216 // MRM-1933, there are certain servers that do not allow browsing
217 if (remoteConnectivityCheckPaths.containsKey(url)) {
218 return wagon.resourceExists(remoteConnectivityCheckPaths.get(url));
220 // we only check connectivity as remote repo can be empty
221 // MRM-1909: Wagon implementation appends a slash already
222 wagon.getFileList("");
227 catch ( TransferFailedException e )
229 log.info( "TransferFailedException :{}", e.getMessage() );
230 return Boolean.FALSE;
232 catch ( Exception e )
234 // This service returns either true or false, Exception cannot be handled by the clients
235 log.debug("Exception occured on connectivity test.", e);
236 log.info("Connection exception: {}", e.getMessage());
237 return Boolean.FALSE;
242 public int getCheckReadTimeout() {
243 return checkReadTimeout;
246 public void setCheckReadTimeout(int checkReadTimeout) {
247 this.checkReadTimeout = checkReadTimeout;
250 public int getCheckTimeout() {
254 public void setCheckTimeout(int checkTimeout) {
255 this.checkTimeout = checkTimeout;
258 public Map<String, String> getRemoteConnectivityCheckPaths() {
259 return remoteConnectivityCheckPaths;
262 public void setRemoteConnectivityCheckPaths(Map<String, String> remoteConnectivityCheckPaths) {
263 this.remoteConnectivityCheckPaths = remoteConnectivityCheckPaths;