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.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;
40  
41  import javax.annotation.PostConstruct;
42  import javax.inject.Inject;
43  import javax.ws.rs.core.Response;
44  import java.net.URL;
45  import java.util.Collections;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  /**
51   * @author Olivier Lamy
52   * @since 1.4-M1
53   */
54  @Service( "remoteRepositoriesService#rest" )
55  public class DefaultRemoteRepositoriesService
56      extends AbstractRestService
57      implements RemoteRepositoriesService {
58  
59      @Inject
60      private RemoteRepositoryAdmin remoteRepositoryAdmin;
61  
62      @Inject
63      private WagonFactory wagonFactory;
64  
65  
66      @Inject
67      private NetworkProxyAdmin networkProxyAdmin;
68  
69      int checkReadTimeout = 10000;
70      int checkTimeout = 9000;
71  
72  
73      @Override
74      public List<RemoteRepository> getRemoteRepositories()
75              throws ArchivaRestServiceException {
76          try {
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);
82          }
83      }
84  
85      @Override
86      public RemoteRepository getRemoteRepository(String repositoryId)
87              throws ArchivaRestServiceException {
88  
89          List<RemoteRepository> remoteRepositories = getRemoteRepositories();
90          for (RemoteRepository repository : remoteRepositories) {
91              if (StringUtils.equals(repositoryId, repository.getId())) {
92                  return repository;
93              }
94          }
95          return null;
96      }
97  
98      @Override
99      public Boolean deleteRemoteRepository(String repositoryId)
100             throws ArchivaRestServiceException {
101         try {
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);
106         }
107     }
108 
109     @Override
110     public Boolean addRemoteRepository(RemoteRepository remoteRepository)
111             throws ArchivaRestServiceException {
112         try {
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);
117         }
118     }
119 
120     @Override
121     public Boolean updateRemoteRepository(RemoteRepository remoteRepository)
122             throws ArchivaRestServiceException {
123         try {
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);
128         }
129     }
130 
131     @Override
132     public Boolean checkRemoteConnectivity(String repositoryId)
133             throws ArchivaRestServiceException {
134         try {
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;
139             }
140             NetworkProxy networkProxy = null;
141             if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
142                 networkProxy = networkProxyAdmin.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
143                 if (networkProxy == null) {
144                     log.warn(
145                             "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
146                             remoteRepository.getRemoteDownloadNetworkProxyId());
147                 }
148             }
149 
150             String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
151 
152             final Wagon wagon =
153                     wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
154                             .networkProxy(networkProxy));
155 
156             // hardcoded value as it's a check of the remote repo connectivity
157             wagon.setReadTimeout(checkReadTimeout);
158             wagon.setTimeout(checkTimeout);
159 
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);
166             }
167 
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());
176             }
177             String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
178             wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
179 
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());
184             } else {
185                 // we only check connectivity as remote repo can be empty
186                 // MRM-1909: Wagon implementation appends a slash already
187                 wagon.getFileList("");
188             }
189 
190             return Boolean.TRUE;
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;
199         }
200 
201     }
202 
203     public int getCheckReadTimeout() {
204         return checkReadTimeout;
205     }
206 
207     public void setCheckReadTimeout(int checkReadTimeout) {
208         this.checkReadTimeout = checkReadTimeout;
209     }
210 
211     public int getCheckTimeout() {
212         return checkTimeout;
213     }
214 
215     public void setCheckTimeout(int checkTimeout) {
216         this.checkTimeout = checkTimeout;
217     }
218 
219 }