]> source.dussan.org Git - archiva.git/blob
a6b341aa7ef62d8fb4b6f235a348cf8e71d08964
[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.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
60     @Inject
61     private RemoteRepositoryAdmin remoteRepositoryAdmin;
62
63     @Inject
64     private WagonFactory wagonFactory;
65
66
67     @Inject
68     private NetworkProxyAdmin networkProxyAdmin;
69
70     int checkReadTimeout = 10000;
71     int checkTimeout = 9000;
72
73     // TODO: make this configurable
74     private Map<String,String> remoteConnectivityCheckPaths = new HashMap<>();
75
76     @PostConstruct
77     private void init() {
78         // default initialization for known servers
79         remoteConnectivityCheckPaths.put("http://download.oracle.com/maven","com/sleepycat/je/license.txt");
80         remoteConnectivityCheckPaths.put("https://download.oracle.com/maven","com/sleepycat/je/license.txt");
81     }
82
83     @Override
84     public List<RemoteRepository> getRemoteRepositories()
85         throws ArchivaRestServiceException
86     {
87         try
88         {
89             List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
90             return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
91         }
92         catch ( RepositoryAdminException e )
93         {
94             log.error( e.getMessage(), e );
95             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
96         }
97     }
98
99     @Override
100     public RemoteRepository getRemoteRepository( String repositoryId )
101         throws ArchivaRestServiceException
102     {
103
104         List<RemoteRepository> remoteRepositories = getRemoteRepositories();
105         for ( RemoteRepository repository : remoteRepositories )
106         {
107             if ( StringUtils.equals( repositoryId, repository.getId() ) )
108             {
109                 return repository;
110             }
111         }
112         return null;
113     }
114
115     @Override
116     public Boolean deleteRemoteRepository( String repositoryId )
117         throws ArchivaRestServiceException
118     {
119         try
120         {
121             return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
122         }
123         catch ( RepositoryAdminException e )
124         {
125             log.error( e.getMessage(), e );
126             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
127         }
128     }
129
130     @Override
131     public Boolean addRemoteRepository( RemoteRepository remoteRepository )
132         throws ArchivaRestServiceException
133     {
134         try
135         {
136             return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
137         }
138         catch ( RepositoryAdminException e )
139         {
140             log.error( e.getMessage(), e );
141             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
142         }
143     }
144
145     @Override
146     public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
147         throws ArchivaRestServiceException
148     {
149         try
150         {
151             return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
152         }
153         catch ( RepositoryAdminException e )
154         {
155             log.error( e.getMessage(), e );
156             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
157         }
158     }
159
160     @Override
161     public Boolean checkRemoteConnectivity( String repositoryId )
162         throws ArchivaRestServiceException
163     {
164         try
165         {
166             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
167             if ( remoteRepository == null )
168             {
169                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
170                 return Boolean.FALSE;
171             }
172             NetworkProxy networkProxy = null;
173             if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
174             {
175                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
176                 if ( networkProxy == null )
177                 {
178                     log.warn(
179                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
180                         remoteRepository.getRemoteDownloadNetworkProxyId() );
181                 }
182             }
183
184             String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
185
186             final Wagon wagon =
187                 wagonFactory.getWagon( new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ) //
188                                            .networkProxy( networkProxy ) );
189
190             // hardcoded value as it's a check of the remote repo connectivity
191             wagon.setReadTimeout( checkReadTimeout );
192             wagon.setTimeout( checkTimeout );
193
194             if ( wagon instanceof AbstractHttpClientWagon )
195             {
196                 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
197                     .setUsePreemptive( true ) //
198                     .setReadTimeout( checkReadTimeout );
199                 HttpConfiguration httpConfiguration = new HttpConfiguration().setGet( httpMethodConfiguration );
200                 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
201             }
202             
203             ProxyInfo proxyInfo = null;
204             if ( networkProxy != null )
205             {
206                 proxyInfo = new ProxyInfo();
207                 proxyInfo.setType( networkProxy.getProtocol() );
208                 proxyInfo.setHost( networkProxy.getHost() );
209                 proxyInfo.setPort( networkProxy.getPort() );
210                 proxyInfo.setUserName( networkProxy.getUsername() );
211                 proxyInfo.setPassword( networkProxy.getPassword() );
212             }            
213             String url = StringUtils.stripEnd(remoteRepository.getUrl(),"/");
214             wagon.connect( new Repository( remoteRepository.getId(), url ), proxyInfo );
215
216             // MRM-1933, there are certain servers that do not allow browsing
217             if (remoteConnectivityCheckPaths.containsKey(url)) {
218                 return wagon.resourceExists(remoteConnectivityCheckPaths.get(url));
219             } else {
220                 // we only check connectivity as remote repo can be empty
221                 // MRM-1909: Wagon implementation appends a slash already
222                 wagon.getFileList("");
223             }
224
225             return Boolean.TRUE;
226         }
227         catch ( TransferFailedException e )
228         {
229             log.info( "TransferFailedException :{}", e.getMessage() );
230             return Boolean.FALSE;
231         }
232         catch ( Exception e )
233         {
234             // This service returns either true or false, Exception cannot be handled by the clients
235             log.debug("Exception occured on connectivity test.", e);
236             log.info("Connection exception: {}", e.getMessage());
237             return Boolean.FALSE;
238         }
239
240     }
241
242     public int getCheckReadTimeout() {
243         return checkReadTimeout;
244     }
245
246     public void setCheckReadTimeout(int checkReadTimeout) {
247         this.checkReadTimeout = checkReadTimeout;
248     }
249
250     public int getCheckTimeout() {
251         return checkTimeout;
252     }
253
254     public void setCheckTimeout(int checkTimeout) {
255         this.checkTimeout = checkTimeout;
256     }
257
258     public Map<String, String> getRemoteConnectivityCheckPaths() {
259         return remoteConnectivityCheckPaths;
260     }
261
262     public void setRemoteConnectivityCheckPaths(Map<String, String> remoteConnectivityCheckPaths) {
263         this.remoteConnectivityCheckPaths = remoteConnectivityCheckPaths;
264     }
265 }