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.maven.proxy.WagonFactory;
27 import org.apache.archiva.maven.proxy.WagonFactoryRequest;
28 import org.apache.archiva.proxy.model.NetworkProxy;
29 import org.apache.archiva.rest.api.model.ActionStatus;
30 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
31 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
32 import org.apache.commons.lang3.StringUtils;
33 import org.apache.maven.wagon.TransferFailedException;
34 import org.apache.maven.wagon.Wagon;
35 import org.apache.maven.wagon.proxy.ProxyInfo;
36 import org.apache.maven.wagon.repository.Repository;
37 import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
38 import org.apache.maven.wagon.shared.http.HttpConfiguration;
39 import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
40 import org.springframework.stereotype.Service;
42 import javax.inject.Inject;
44 import java.util.Collections;
45 import java.util.List;
48 * @author Olivier Lamy
51 @Service( "remoteRepositoriesService#rest" )
52 public class DefaultRemoteRepositoriesService
53 extends AbstractRestService
54 implements RemoteRepositoriesService {
57 private RemoteRepositoryAdmin remoteRepositoryAdmin;
60 private WagonFactory wagonFactory;
63 private ProxyRegistry proxyRegistry;
65 int checkReadTimeout = 10000;
66 int checkTimeout = 9000;
70 public List<RemoteRepository> getRemoteRepositories()
71 throws ArchivaRestServiceException {
73 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
74 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
75 } catch (RepositoryAdminException e) {
76 log.error(e.getMessage(), e);
77 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
82 public RemoteRepository getRemoteRepository(String repositoryId)
83 throws ArchivaRestServiceException {
85 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
86 for (RemoteRepository repository : remoteRepositories) {
87 if (StringUtils.equals(repositoryId, repository.getId())) {
95 public ActionStatus deleteRemoteRepository( String repositoryId)
96 throws ArchivaRestServiceException {
98 return new ActionStatus( remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation( ) ) );
99 } catch (RepositoryAdminException e) {
100 log.error(e.getMessage(), e);
101 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
106 public ActionStatus addRemoteRepository(RemoteRepository remoteRepository)
107 throws ArchivaRestServiceException {
109 return new ActionStatus( remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation( ) ) );
110 } catch (RepositoryAdminException e) {
111 log.error(e.getMessage(), e);
112 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
117 public ActionStatus updateRemoteRepository(RemoteRepository remoteRepository)
118 throws ArchivaRestServiceException {
120 return new ActionStatus( remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation( ) ) );
121 } catch (RepositoryAdminException e) {
122 log.error(e.getMessage(), e);
123 throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
128 public ActionStatus checkRemoteConnectivity( String repositoryId)
129 throws ArchivaRestServiceException {
131 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
132 if (remoteRepository == null) {
133 log.warn("Remote repository {} does not exist. Connectivity check returns false.", repositoryId);
134 return ActionStatus.FAIL;
136 NetworkProxy networkProxy = null;
137 if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
138 networkProxy = proxyRegistry.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
139 if (networkProxy == null) {
141 "A network proxy {} was configured for repository {}. But the proxy with the given id does not exist.",
142 remoteRepository.getRemoteDownloadNetworkProxyId(), repositoryId);
146 String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
149 wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
150 .networkProxy(networkProxy));
152 // hardcoded value as it's a check of the remote repo connectivity
153 wagon.setReadTimeout(checkReadTimeout);
154 wagon.setTimeout(checkTimeout);
156 if (wagon instanceof AbstractHttpClientWagon ) {
157 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
158 .setUsePreemptive(true) //
159 .setReadTimeout(checkReadTimeout);
160 HttpConfiguration httpConfiguration = new HttpConfiguration().setGet( httpMethodConfiguration);
161 AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
164 ProxyInfo proxyInfo = null;
165 if (networkProxy != null) {
166 proxyInfo = new ProxyInfo();
167 proxyInfo.setType(networkProxy.getProtocol());
168 proxyInfo.setHost(networkProxy.getHost());
169 proxyInfo.setPort(networkProxy.getPort());
170 proxyInfo.setUserName(networkProxy.getUsername());
171 proxyInfo.setPassword(new String(networkProxy.getPassword()));
173 String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
174 wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
176 // MRM-1933, there are certain servers that do not allow browsing
177 if (!(StringUtils.isEmpty(remoteRepository.getCheckPath()) ||
178 "/".equals(remoteRepository.getCheckPath()))) {
179 return new ActionStatus( wagon.resourceExists( remoteRepository.getCheckPath( ) ) );
181 // we only check connectivity as remote repo can be empty
182 // MRM-1909: Wagon implementation appends a slash already
183 wagon.getFileList("");
186 return ActionStatus.SUCCESS;
187 } catch (TransferFailedException e) {
188 log.info("TransferFailedException :{}", e.getMessage());
189 return ActionStatus.FAIL;
190 } catch (Exception e) {
191 // This service returns either true or false, Exception cannot be handled by the clients
192 log.debug("Exception occured on connectivity test.", e);
193 log.info("Connection exception: {}", e.getMessage());
194 return ActionStatus.FAIL;
199 public int getCheckReadTimeout() {
200 return checkReadTimeout;
203 public void setCheckReadTimeout(int checkReadTimeout) {
204 this.checkReadTimeout = checkReadTimeout;
207 public int getCheckTimeout() {
211 public void setCheckTimeout(int checkTimeout) {
212 this.checkTimeout = checkTimeout;