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.RemoteRepository;
24 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
25 import org.apache.archiva.proxy.ProxyRegistry;
26 import org.apache.archiva.proxy.maven.WagonFactory;
27 import org.apache.archiva.proxy.maven.WagonFactoryRequest;
28 import org.apache.archiva.proxy.model.NetworkProxy;
29 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
30 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.maven.wagon.TransferFailedException;
33 import org.apache.maven.wagon.Wagon;
34 import org.apache.maven.wagon.proxy.ProxyInfo;
35 import org.apache.maven.wagon.repository.Repository;
36 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
37 import org.apache.maven.wagon.shared.http.HttpConfiguration;
38 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
39 import org.springframework.stereotype.Service;
41 import javax.inject.Inject;
43 import java.util.Collections;
44 import java.util.List;
47 * @author Olivier Lamy
50 @Service( "remoteRepositoriesService#rest" )
51 public class DefaultRemoteRepositoriesService
52 extends AbstractRestService
53 implements RemoteRepositoriesService {
56 private RemoteRepositoryAdmin remoteRepositoryAdmin;
59 private WagonFactory wagonFactory;
62 private ProxyRegistry proxyRegistry;
64 int checkReadTimeout = 10000;
65 int checkTimeout = 9000;
69 public List<RemoteRepository> getRemoteRepositories()
70 throws ArchivaRestServiceException {
72 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
73 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
74 } catch (RepositoryAdminException e) {
75 log.error(e.getMessage(), e);
76 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
81 public RemoteRepository getRemoteRepository(String repositoryId)
82 throws ArchivaRestServiceException {
84 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
85 for (RemoteRepository repository : remoteRepositories) {
86 if (StringUtils.equals(repositoryId, repository.getId())) {
94 public Boolean deleteRemoteRepository(String repositoryId)
95 throws ArchivaRestServiceException {
97 return remoteRepositoryAdmin.deleteRemoteRepository(repositoryId, getAuditInformation());
98 } catch (RepositoryAdminException e) {
99 log.error(e.getMessage(), e);
100 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
105 public Boolean addRemoteRepository(RemoteRepository remoteRepository)
106 throws ArchivaRestServiceException {
108 return remoteRepositoryAdmin.addRemoteRepository(remoteRepository, getAuditInformation());
109 } catch (RepositoryAdminException e) {
110 log.error(e.getMessage(), e);
111 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
116 public Boolean updateRemoteRepository(RemoteRepository remoteRepository)
117 throws ArchivaRestServiceException {
119 return remoteRepositoryAdmin.updateRemoteRepository(remoteRepository, getAuditInformation());
120 } catch (RepositoryAdminException e) {
121 log.error(e.getMessage(), e);
122 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
127 public Boolean checkRemoteConnectivity(String repositoryId)
128 throws ArchivaRestServiceException {
130 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
131 if (remoteRepository == null) {
132 log.warn("Remote repository {} does not exist. Connectivity check returns false.", repositoryId);
133 return Boolean.FALSE;
135 NetworkProxy networkProxy = null;
136 if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
137 networkProxy = proxyRegistry.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
138 if (networkProxy == null) {
140 "A network proxy {} was configured for repository {}. But the proxy with the given id does not exist.",
141 remoteRepository.getRemoteDownloadNetworkProxyId(), repositoryId);
145 String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
148 wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
149 .networkProxy(networkProxy));
151 // hardcoded value as it's a check of the remote repo connectivity
152 wagon.setReadTimeout(checkReadTimeout);
153 wagon.setTimeout(checkTimeout);
155 if (wagon instanceof AbstractHttpClientWagon ) {
156 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
157 .setUsePreemptive(true) //
158 .setReadTimeout(checkReadTimeout);
159 HttpConfiguration httpConfiguration = new HttpConfiguration().setGet( httpMethodConfiguration);
160 AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
163 ProxyInfo proxyInfo = null;
164 if (networkProxy != null) {
165 proxyInfo = new ProxyInfo();
166 proxyInfo.setType(networkProxy.getProtocol());
167 proxyInfo.setHost(networkProxy.getHost());
168 proxyInfo.setPort(networkProxy.getPort());
169 proxyInfo.setUserName(networkProxy.getUsername());
170 proxyInfo.setPassword(new String(networkProxy.getPassword()));
172 String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
173 wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
175 // MRM-1933, there are certain servers that do not allow browsing
176 if (!(StringUtils.isEmpty(remoteRepository.getCheckPath()) ||
177 "/".equals(remoteRepository.getCheckPath()))) {
178 return wagon.resourceExists(remoteRepository.getCheckPath());
180 // we only check connectivity as remote repo can be empty
181 // MRM-1909: Wagon implementation appends a slash already
182 wagon.getFileList("");
186 } catch (TransferFailedException e) {
187 log.info("TransferFailedException :{}", e.getMessage());
188 return Boolean.FALSE;
189 } catch (Exception e) {
190 // This service returns either true or false, Exception cannot be handled by the clients
191 log.debug("Exception occured on connectivity test.", e);
192 log.info("Connection exception: {}", e.getMessage());
193 return Boolean.FALSE;
198 public int getCheckReadTimeout() {
199 return checkReadTimeout;
202 public void setCheckReadTimeout(int checkReadTimeout) {
203 this.checkReadTimeout = checkReadTimeout;
206 public int getCheckTimeout() {
210 public void setCheckTimeout(int checkTimeout) {
211 this.checkTimeout = checkTimeout;