]> source.dussan.org Git - archiva.git/blob
e7d5b85b3e92f57ac266cd0cb3615ec4bbb4c8aa
[archiva.git] /
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.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;
41
42 import javax.inject.Inject;
43 import java.net.URL;
44 import java.util.Collections;
45 import java.util.List;
46
47 /**
48  * @author Olivier Lamy
49  * @since 1.4-M1
50  */
51 @Service( "remoteRepositoriesService#rest" )
52 public class DefaultRemoteRepositoriesService
53     extends AbstractRestService
54     implements RemoteRepositoriesService {
55
56     @Inject
57     private RemoteRepositoryAdmin remoteRepositoryAdmin;
58
59     @Inject
60     private WagonFactory wagonFactory;
61
62     @Inject
63     private ProxyRegistry proxyRegistry;
64
65     int checkReadTimeout = 10000;
66     int checkTimeout = 9000;
67
68
69     @Override
70     public List<RemoteRepository> getRemoteRepositories()
71             throws ArchivaRestServiceException {
72         try {
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);
78         }
79     }
80
81     @Override
82     public RemoteRepository getRemoteRepository(String repositoryId)
83             throws ArchivaRestServiceException {
84
85         List<RemoteRepository> remoteRepositories = getRemoteRepositories();
86         for (RemoteRepository repository : remoteRepositories) {
87             if (StringUtils.equals(repositoryId, repository.getId())) {
88                 return repository;
89             }
90         }
91         return null;
92     }
93
94     @Override
95     public ActionStatus deleteRemoteRepository( String repositoryId)
96             throws ArchivaRestServiceException {
97         try {
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);
102         }
103     }
104
105     @Override
106     public ActionStatus addRemoteRepository(RemoteRepository remoteRepository)
107             throws ArchivaRestServiceException {
108         try {
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);
113         }
114     }
115
116     @Override
117     public ActionStatus updateRemoteRepository(RemoteRepository remoteRepository)
118             throws ArchivaRestServiceException {
119         try {
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);
124         }
125     }
126
127     @Override
128     public ActionStatus checkRemoteConnectivity( String repositoryId)
129             throws ArchivaRestServiceException {
130         try {
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;
135             }
136             NetworkProxy networkProxy = null;
137             if (StringUtils.isNotBlank(remoteRepository.getRemoteDownloadNetworkProxyId())) {
138                 networkProxy = proxyRegistry.getNetworkProxy(remoteRepository.getRemoteDownloadNetworkProxyId());
139                 if (networkProxy == null) {
140                     log.warn(
141                             "A network proxy {} was configured for repository {}. But the proxy with the given id does not exist.",
142                             remoteRepository.getRemoteDownloadNetworkProxyId(), repositoryId);
143                 }
144             }
145
146             String wagonProtocol = new URL(remoteRepository.getUrl()).getProtocol();
147
148             final Wagon wagon =
149                     wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, remoteRepository.getExtraHeaders()) //
150                             .networkProxy(networkProxy));
151
152             // hardcoded value as it's a check of the remote repo connectivity
153             wagon.setReadTimeout(checkReadTimeout);
154             wagon.setTimeout(checkTimeout);
155
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);
162             }
163
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()));
172             }
173             String url = StringUtils.stripEnd(remoteRepository.getUrl(), "/");
174             wagon.connect(new Repository(remoteRepository.getId(), url), proxyInfo);
175
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( ) ) );
180             } else {
181                 // we only check connectivity as remote repo can be empty
182                 // MRM-1909: Wagon implementation appends a slash already
183                 wagon.getFileList("");
184             }
185
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;
195         }
196
197     }
198
199     public int getCheckReadTimeout() {
200         return checkReadTimeout;
201     }
202
203     public void setCheckReadTimeout(int checkReadTimeout) {
204         this.checkReadTimeout = checkReadTimeout;
205     }
206
207     public int getCheckTimeout() {
208         return checkTimeout;
209     }
210
211     public void setCheckTimeout(int checkTimeout) {
212         this.checkTimeout = checkTimeout;
213     }
214
215 }