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