This project has retired. For details please refer to its Attic page.
DefaultRemoteRepositoriesService xref
View Javadoc
1   package org.apache.archiva.rest.services;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
40  
41  import javax.inject.Inject;
42  import java.net.URL;
43  import java.util.Collections;
44  import java.util.List;
45  
46  /**
47   * @author Olivier Lamy
48   * @since 1.4-M1
49   */
50  @Service( "remoteRepositoriesService#rest" )
51  public class DefaultRemoteRepositoriesService
52      extends AbstractRestService
53      implements RemoteRepositoriesService {
54  
55      @Inject
56      private RemoteRepositoryAdmin remoteRepositoryAdmin;
57  
58      @Inject
59      private WagonFactory wagonFactory;
60  
61      @Inject
62      private ProxyRegistry proxyRegistry;
63  
64      int checkReadTimeout = 10000;
65      int checkTimeout = 9000;
66  
67  
68      @Override
69      public List<RemoteRepository> getRemoteRepositories()
70              throws ArchivaRestServiceException {
71          try {
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);
77          }
78      }
79  
80      @Override
81      public RemoteRepository getRemoteRepository(String repositoryId)
82              throws ArchivaRestServiceException {
83  
84          List<RemoteRepository> remoteRepositories = getRemoteRepositories();
85          for (RemoteRepository repository : remoteRepositories) {
86              if (StringUtils.equals(repositoryId, repository.getId())) {
87                  return repository;
88              }
89          }
90          return null;
91      }
92  
93      @Override
94      public Boolean deleteRemoteRepository(String repositoryId)
95              throws ArchivaRestServiceException {
96          try {
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);
101         }
102     }
103 
104     @Override
105     public Boolean addRemoteRepository(RemoteRepository remoteRepository)
106             throws ArchivaRestServiceException {
107         try {
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);
112         }
113     }
114 
115     @Override
116     public Boolean updateRemoteRepository(RemoteRepository remoteRepository)
117             throws ArchivaRestServiceException {
118         try {
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);
123         }
124     }
125 
126     @Override
127     public Boolean checkRemoteConnectivity(String repositoryId)
128             throws ArchivaRestServiceException {
129         try {
130             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository(repositoryId);
131             if (remoteRepository == null) {
132                 log.warn("ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId);
133                 return Boolean.FALSE;
134             }
135             NetworkProxy networkProxy = null;
136             if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
137                 networkProxy = proxyRegistry.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
138                 if (networkProxy == null) {
139                     log.warn(
140                             "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
141                             remoteRepository.getRemoteDownloadNetworkProxyId());
142                 }
143             }
144 
145             String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
146 
147             final Wagon wagon =
148                     wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
149                             .networkProxy(networkProxy));
150 
151             // hardcoded value as it's a check of the remote repo connectivity
152             wagon.setReadTimeout(checkReadTimeout);
153             wagon.setTimeout(checkTimeout);
154 
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);
161             }
162 
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()));
171             }
172             String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
173             wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
174 
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());
179             } else {
180                 // we only check connectivity as remote repo can be empty
181                 // MRM-1909: Wagon implementation appends a slash already
182                 wagon.getFileList("");
183             }
184 
185             return Boolean.TRUE;
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;
194         }
195 
196     }
197 
198     public int getCheckReadTimeout() {
199         return checkReadTimeout;
200     }
201 
202     public void setCheckReadTimeout(int checkReadTimeout) {
203         this.checkReadTimeout = checkReadTimeout;
204     }
205 
206     public int getCheckTimeout() {
207         return checkTimeout;
208     }
209 
210     public void setCheckTimeout(int checkTimeout) {
211         this.checkTimeout = checkTimeout;
212     }
213 
214 }