You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultRemoteRepositoriesService.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package org.apache.archiva.rest.services;
  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. import org.apache.archiva.admin.model.RepositoryAdminException;
  21. import org.apache.archiva.admin.model.beans.RemoteRepository;
  22. import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
  23. import org.apache.archiva.proxy.ProxyRegistry;
  24. import org.apache.archiva.proxy.maven.WagonFactory;
  25. import org.apache.archiva.proxy.maven.WagonFactoryRequest;
  26. import org.apache.archiva.proxy.model.NetworkProxy;
  27. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  28. import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.apache.maven.wagon.TransferFailedException;
  31. import org.apache.maven.wagon.Wagon;
  32. import org.apache.maven.wagon.proxy.ProxyInfo;
  33. import org.apache.maven.wagon.repository.Repository;
  34. import org.apache.maven.wagon.shared.http.AbstractHttpClientWagon;
  35. import org.apache.maven.wagon.shared.http.HttpConfiguration;
  36. import org.apache.maven.wagon.shared.http.HttpMethodConfiguration;
  37. import org.springframework.stereotype.Service;
  38. import javax.inject.Inject;
  39. import java.net.URL;
  40. import java.util.Collections;
  41. import java.util.List;
  42. /**
  43. * @author Olivier Lamy
  44. * @since 1.4-M1
  45. */
  46. @Service( "remoteRepositoriesService#rest" )
  47. public class DefaultRemoteRepositoriesService
  48. extends AbstractRestService
  49. implements RemoteRepositoriesService {
  50. @Inject
  51. private RemoteRepositoryAdmin remoteRepositoryAdmin;
  52. @Inject
  53. private WagonFactory wagonFactory;
  54. @Inject
  55. private ProxyRegistry proxyRegistry;
  56. int checkReadTimeout = 10000;
  57. int checkTimeout = 9000;
  58. @Override
  59. public List<RemoteRepository> getRemoteRepositories()
  60. throws ArchivaRestServiceException {
  61. try {
  62. List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
  63. return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
  64. } catch (RepositoryAdminException e) {
  65. log.error(e.getMessage(), e);
  66. throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
  67. }
  68. }
  69. @Override
  70. public RemoteRepository getRemoteRepository(String repositoryId)
  71. throws ArchivaRestServiceException {
  72. List<RemoteRepository> remoteRepositories = getRemoteRepositories();
  73. for (RemoteRepository repository : remoteRepositories) {
  74. if (StringUtils.equals(repositoryId, repository.getId())) {
  75. return repository;
  76. }
  77. }
  78. return null;
  79. }
  80. @Override
  81. public Boolean deleteRemoteRepository(String repositoryId)
  82. throws ArchivaRestServiceException {
  83. try {
  84. return remoteRepositoryAdmin.deleteRemoteRepository(repositoryId, getAuditInformation());
  85. } catch (RepositoryAdminException e) {
  86. log.error(e.getMessage(), e);
  87. throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
  88. }
  89. }
  90. @Override
  91. public Boolean addRemoteRepository(RemoteRepository remoteRepository)
  92. throws ArchivaRestServiceException {
  93. try {
  94. return remoteRepositoryAdmin.addRemoteRepository(remoteRepository, getAuditInformation());
  95. } catch (RepositoryAdminException e) {
  96. log.error(e.getMessage(), e);
  97. throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
  98. }
  99. }
  100. @Override
  101. public Boolean updateRemoteRepository(RemoteRepository remoteRepository)
  102. throws ArchivaRestServiceException {
  103. try {
  104. return remoteRepositoryAdmin.updateRemoteRepository(remoteRepository, getAuditInformation());
  105. } catch (RepositoryAdminException e) {
  106. log.error(e.getMessage(), e);
  107. throw new ArchivaRestServiceException(e.getMessage(), e.getFieldName(), e);
  108. }
  109. }
  110. @Override
  111. public Boolean checkRemoteConnectivity(String repositoryId)
  112. throws ArchivaRestServiceException {
  113. try {
  114. RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
  115. if (remoteRepository == null) {
  116. log.warn("ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId);
  117. return Boolean.FALSE;
  118. }
  119. NetworkProxy networkProxy = null;
  120. if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
  121. networkProxy = proxyRegistry.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
  122. if (networkProxy == null) {
  123. log.warn(
  124. "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
  125. remoteRepository.getRemoteDownloadNetworkProxyId());
  126. }
  127. }
  128. String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
  129. final Wagon wagon =
  130. wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
  131. .networkProxy(networkProxy));
  132. // hardcoded value as it's a check of the remote repo connectivity
  133. wagon.setReadTimeout(checkReadTimeout);
  134. wagon.setTimeout(checkTimeout);
  135. if (wagon instanceof AbstractHttpClientWagon ) {
  136. HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
  137. .setUsePreemptive(true) //
  138. .setReadTimeout(checkReadTimeout);
  139. HttpConfiguration httpConfiguration = new HttpConfiguration().setGet( httpMethodConfiguration);
  140. AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
  141. }
  142. ProxyInfo proxyInfo = null;
  143. if (networkProxy != null) {
  144. proxyInfo = new ProxyInfo();
  145. proxyInfo.setType(networkProxy.getProtocol());
  146. proxyInfo.setHost(networkProxy.getHost());
  147. proxyInfo.setPort(networkProxy.getPort());
  148. proxyInfo.setUserName(networkProxy.getUsername());
  149. proxyInfo.setPassword(networkProxy.getPassword());
  150. }
  151. String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
  152. wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
  153. // MRM-1933, there are certain servers that do not allow browsing
  154. if (!(StringUtils.isEmpty(remoteRepository.getCheckPath()) ||
  155. "/".equals(remoteRepository.getCheckPath()))) {
  156. return wagon.resourceExists(remoteRepository.getCheckPath());
  157. } else {
  158. // we only check connectivity as remote repo can be empty
  159. // MRM-1909: Wagon implementation appends a slash already
  160. wagon.getFileList("");
  161. }
  162. return Boolean.TRUE;
  163. } catch (TransferFailedException e) {
  164. log.info("TransferFailedException :{}", e.getMessage());
  165. return Boolean.FALSE;
  166. } catch (Exception e) {
  167. // This service returns either true or false, Exception cannot be handled by the clients
  168. log.debug("Exception occured on connectivity test.", e);
  169. log.info("Connection exception: {}", e.getMessage());
  170. return Boolean.FALSE;
  171. }
  172. }
  173. public int getCheckReadTimeout() {
  174. return checkReadTimeout;
  175. }
  176. public void setCheckReadTimeout(int checkReadTimeout) {
  177. this.checkReadTimeout = checkReadTimeout;
  178. }
  179. public int getCheckTimeout() {
  180. return checkTimeout;
  181. }
  182. public void setCheckTimeout(int checkTimeout) {
  183. this.checkTimeout = checkTimeout;
  184. }
  185. }