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 {
60 private RemoteRepositoryAdmin remoteRepositoryAdmin;
63 private WagonFactory wagonFactory;
67 private NetworkProxyAdmin networkProxyAdmin;
69 int checkReadTimeout = 10000;
70 int checkTimeout = 9000;
74 public List<RemoteRepository> getRemoteRepositories()
75 throws ArchivaRestServiceException {
77 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
78 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
79 } catch (RepositoryAdminException e) {
80 log.error(e.getMessage(), e);
81 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
86 public RemoteRepository getRemoteRepository(String repositoryId)
87 throws ArchivaRestServiceException {
89 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
90 for (RemoteRepository repository : remoteRepositories) {
91 if (StringUtils.equals(repositoryId, repository.getId())) {
99 public Boolean deleteRemoteRepository(String repositoryId)
100 throws ArchivaRestServiceException {
102 return remoteRepositoryAdmin.deleteRemoteRepository(repositoryId, getAuditInformation());
103 } catch (RepositoryAdminException e) {
104 log.error(e.getMessage(), e);
105 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
110 public Boolean addRemoteRepository(RemoteRepository remoteRepository)
111 throws ArchivaRestServiceException {
113 return remoteRepositoryAdmin.addRemoteRepository(remoteRepository, getAuditInformation());
114 } catch (RepositoryAdminException e) {
115 log.error(e.getMessage(), e);
116 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
121 public Boolean updateRemoteRepository(RemoteRepository remoteRepository)
122 throws ArchivaRestServiceException {
124 return remoteRepositoryAdmin.updateRemoteRepository(remoteRepository, getAuditInformation());
125 } catch (RepositoryAdminException e) {
126 log.error(e.getMessage(), e);
127 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
132 public Boolean checkRemoteConnectivity(String repositoryId)
133 throws ArchivaRestServiceException {
135 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
136 if (remoteRepository == null) {
137 log.warn("ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId);
138 return Boolean.FALSE;
140 NetworkProxy networkProxy = null;
141 if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
142 networkProxy = networkProxyAdmin.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
143 if (networkProxy == null) {
145 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
146 remoteRepository.getRemoteDownloadNetworkProxyId());
150 String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
153 wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
154 .networkProxy(networkProxy));
156 // hardcoded value as it's a check of the remote repo connectivity
157 wagon.setReadTimeout(checkReadTimeout);
158 wagon.setTimeout(checkTimeout);
160 if (wagon instanceof AbstractHttpClientWagon) {
161 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
162 .setUsePreemptive(true) //
163 .setReadTimeout(checkReadTimeout);
164 HttpConfiguration httpConfiguration = new HttpConfiguration().setGet(httpMethodConfiguration);
165 AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
168 ProxyInfo proxyInfo = null;
169 if (networkProxy != null) {
170 proxyInfo = new ProxyInfo();
171 proxyInfo.setType(networkProxy.getProtocol());
172 proxyInfo.setHost(networkProxy.getHost());
173 proxyInfo.setPort(networkProxy.getPort());
174 proxyInfo.setUserName(networkProxy.getUsername());
175 proxyInfo.setPassword(networkProxy.getPassword());
177 String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
178 wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
180 // MRM-1933, there are certain servers that do not allow browsing
181 if (!(StringUtils.isEmpty(remoteRepository.getCheckPath()) ||
182 "/".equals(remoteRepository.getCheckPath()))) {
183 return wagon.resourceExists(remoteRepository.getCheckPath());
185 // we only check connectivity as remote repo can be empty
186 // MRM-1909: Wagon implementation appends a slash already
187 wagon.getFileList("");
191 } catch (TransferFailedException e) {
192 log.info("TransferFailedException :{}", e.getMessage());
193 return Boolean.FALSE;
194 } catch (Exception e) {
195 // This service returns either true or false, Exception cannot be handled by the clients
196 log.debug("Exception occured on connectivity test.", e);
197 log.info("Connection exception: {}", e.getMessage());
198 return Boolean.FALSE;
203 public int getCheckReadTimeout() {
204 return checkReadTimeout;
207 public void setCheckReadTimeout(int checkReadTimeout) {
208 this.checkReadTimeout = checkReadTimeout;
211 public int getCheckTimeout() {
215 public void setCheckTimeout(int checkTimeout) {
216 this.checkTimeout = checkTimeout;