]> source.dussan.org Git - archiva.git/blob
b48bccfb2f05df0c7931b40ddaddfa7f1ed29efa
[archiva.git] /
1 package org.apache.archiva.rest.services;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.NetworkProxy;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
25 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
26 import org.apache.archiva.proxy.common.WagonFactory;
27 import org.apache.archiva.proxy.common.WagonFactoryException;
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.ResourceDoesNotExistException;
33 import org.apache.maven.wagon.StreamWagon;
34 import org.apache.maven.wagon.TransferFailedException;
35 import org.apache.maven.wagon.Wagon;
36 import org.apache.maven.wagon.authorization.AuthorizationException;
37 import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon;
38 import org.apache.maven.wagon.providers.http.HttpConfiguration;
39 import org.apache.maven.wagon.providers.http.HttpMethodConfiguration;
40 import org.springframework.stereotype.Service;
41
42 import javax.inject.Inject;
43 import javax.ws.rs.core.Response;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import java.util.Collections;
47 import java.util.List;
48
49 /**
50  * @author Olivier Lamy
51  * @since 1.4-M1
52  */
53 @Service( "remoteRepositoriesService#rest" )
54 public class DefaultRemoteRepositoriesService
55     extends AbstractRestService
56     implements RemoteRepositoriesService
57 {
58
59     @Inject
60     private RemoteRepositoryAdmin remoteRepositoryAdmin;
61
62     @Inject
63     private WagonFactory wagonFactory;
64
65
66     @Inject
67     private NetworkProxyAdmin networkProxyAdmin;
68
69     public List<RemoteRepository> getRemoteRepositories()
70         throws ArchivaRestServiceException
71     {
72         try
73         {
74             List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
75             return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
76         }
77         catch ( RepositoryAdminException e )
78         {
79             log.error( e.getMessage(), e );
80             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
81         }
82     }
83
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     public Boolean deleteRemoteRepository( String repositoryId )
100         throws ArchivaRestServiceException
101     {
102         try
103         {
104             return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
105         }
106         catch ( RepositoryAdminException e )
107         {
108             log.error( e.getMessage(), e );
109             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
110         }
111     }
112
113     public Boolean addRemoteRepository( RemoteRepository remoteRepository )
114         throws ArchivaRestServiceException
115     {
116         try
117         {
118             return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
119         }
120         catch ( RepositoryAdminException e )
121         {
122             log.error( e.getMessage(), e );
123             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
124         }
125     }
126
127     public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
128         throws ArchivaRestServiceException
129     {
130         try
131         {
132             return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
133         }
134         catch ( RepositoryAdminException e )
135         {
136             log.error( e.getMessage(), e );
137             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
138         }
139     }
140
141     @Override
142     public Boolean checkRemoteConnectivity( String repositoryId )
143         throws ArchivaRestServiceException
144     {
145         try
146         {
147             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
148             if ( remoteRepository == null )
149             {
150                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
151                 return Boolean.FALSE;
152             }
153             NetworkProxy networkProxy = null;
154             if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
155             {
156                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
157                 if ( networkProxy == null )
158                 {
159                     log.warn(
160                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
161                         remoteRepository.getRemoteDownloadNetworkProxyId() );
162                 }
163             }
164
165             String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
166
167             final Wagon wagon = wagonFactory.getWagon(
168                 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ).networkProxy(
169                     networkProxy ) );
170
171             wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
172             wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
173
174             if ( wagon instanceof AbstractHttpClientWagon )
175             {
176                 HttpConfiguration httpConfiguration = new HttpConfiguration();
177                 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
178                 httpMethodConfiguration.setUsePreemptive( true );
179                 httpMethodConfiguration.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
180                 httpConfiguration.setGet( httpMethodConfiguration );
181                 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
182             }
183
184             // we only check connectivity as remote repo can be empty
185             wagon.getFileList( "/" );
186
187             return Boolean.TRUE;
188         }
189         catch ( RepositoryAdminException e )
190         {
191             throw new ArchivaRestServiceException( e.getMessage(),
192                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
193         }
194         catch ( MalformedURLException e )
195         {
196             throw new ArchivaRestServiceException( e.getMessage(),
197                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
198         }
199         catch ( WagonFactoryException e )
200         {
201             throw new ArchivaRestServiceException( e.getMessage(),
202                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
203         }
204         catch ( TransferFailedException e )
205         {
206             throw new ArchivaRestServiceException( e.getMessage(),
207                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
208         }
209         catch ( ResourceDoesNotExistException e )
210         {
211             throw new ArchivaRestServiceException( e.getMessage(),
212                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
213         }
214         catch ( AuthorizationException e )
215         {
216             throw new ArchivaRestServiceException( e.getMessage(),
217                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
218         }
219
220     }
221 }