]> source.dussan.org Git - archiva.git/blob
9c0bdd925bd8591bdc1eee5786802ba21f364eee
[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.inject.Inject;
42 import javax.ws.rs.core.Response;
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
57     @Inject
58     private RemoteRepositoryAdmin remoteRepositoryAdmin;
59
60     @Inject
61     private WagonFactory wagonFactory;
62
63
64     @Inject
65     private NetworkProxyAdmin networkProxyAdmin;
66
67     int checkReadTimeout = 10000;
68     int checkTimeout = 9000;
69
70     @Override
71     public List<RemoteRepository> getRemoteRepositories()
72         throws ArchivaRestServiceException
73     {
74         try
75         {
76             List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
77             return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
78         }
79         catch ( RepositoryAdminException e )
80         {
81             log.error( e.getMessage(), e );
82             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
83         }
84     }
85
86     @Override
87     public RemoteRepository getRemoteRepository( String repositoryId )
88         throws ArchivaRestServiceException
89     {
90
91         List<RemoteRepository> remoteRepositories = getRemoteRepositories();
92         for ( RemoteRepository repository : remoteRepositories )
93         {
94             if ( StringUtils.equals( repositoryId, repository.getId() ) )
95             {
96                 return repository;
97             }
98         }
99         return null;
100     }
101
102     @Override
103     public Boolean deleteRemoteRepository( String repositoryId )
104         throws ArchivaRestServiceException
105     {
106         try
107         {
108             return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
109         }
110         catch ( RepositoryAdminException e )
111         {
112             log.error( e.getMessage(), e );
113             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
114         }
115     }
116
117     @Override
118     public Boolean addRemoteRepository( RemoteRepository remoteRepository )
119         throws ArchivaRestServiceException
120     {
121         try
122         {
123             return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
124         }
125         catch ( RepositoryAdminException e )
126         {
127             log.error( e.getMessage(), e );
128             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
129         }
130     }
131
132     @Override
133     public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
134         throws ArchivaRestServiceException
135     {
136         try
137         {
138             return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
139         }
140         catch ( RepositoryAdminException e )
141         {
142             log.error( e.getMessage(), e );
143             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
144         }
145     }
146
147     @Override
148     public Boolean checkRemoteConnectivity( String repositoryId )
149         throws ArchivaRestServiceException
150     {
151         try
152         {
153             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
154             if ( remoteRepository == null )
155             {
156                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
157                 return Boolean.FALSE;
158             }
159             NetworkProxy networkProxy = null;
160             if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
161             {
162                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
163                 if ( networkProxy == null )
164                 {
165                     log.warn(
166                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
167                         remoteRepository.getRemoteDownloadNetworkProxyId() );
168                 }
169             }
170
171             String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
172
173             final Wagon wagon =
174                 wagonFactory.getWagon( new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ) //
175                                            .networkProxy( networkProxy ) );
176
177             // hardcoded value as it's a check of the remote repo connectivity
178             wagon.setReadTimeout( checkReadTimeout );
179             wagon.setTimeout( checkTimeout );
180
181             if ( wagon instanceof AbstractHttpClientWagon )
182             {
183                 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration() //
184                     .setUsePreemptive( true ) //
185                     .setReadTimeout( checkReadTimeout );
186                 HttpConfiguration httpConfiguration = new HttpConfiguration().setGet( httpMethodConfiguration );
187                 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
188             }
189             
190             ProxyInfo proxyInfo = null;
191             if ( networkProxy != null )
192             {
193                 proxyInfo = new ProxyInfo();
194                 proxyInfo.setType( networkProxy.getProtocol() );
195                 proxyInfo.setHost( networkProxy.getHost() );
196                 proxyInfo.setPort( networkProxy.getPort() );
197                 proxyInfo.setUserName( networkProxy.getUsername() );
198                 proxyInfo.setPassword( networkProxy.getPassword() );
199             }            
200
201             wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ), proxyInfo );
202
203             // we only check connectivity as remote repo can be empty
204             wagon.getFileList( "/" );
205
206             return Boolean.TRUE;
207         }
208         catch ( TransferFailedException e )
209         {
210             log.info( "TransferFailedException :{}", e.getMessage() );
211             return Boolean.FALSE;
212         }
213         catch ( Exception e )
214         {
215             throw new ArchivaRestServiceException( e.getMessage(),
216                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
217         }
218
219     }
220
221     public int getCheckReadTimeout() {
222         return checkReadTimeout;
223     }
224
225     public void setCheckReadTimeout(int checkReadTimeout) {
226         this.checkReadTimeout = checkReadTimeout;
227     }
228
229     public int getCheckTimeout() {
230         return checkTimeout;
231     }
232
233     public void setCheckTimeout(int checkTimeout) {
234         this.checkTimeout = checkTimeout;
235     }
236 }