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.8KB

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