]> source.dussan.org Git - archiva.git/blob
38afc67587682cdf47fdf2f4c2a89d0a0575560d
[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.apache.maven.wagon.providers.http.HttpWagon;
41 import org.apache.maven.wagon.repository.Repository;
42 import org.springframework.stereotype.Service;
43
44 import javax.inject.Inject;
45 import javax.ws.rs.core.Response;
46 import java.net.MalformedURLException;
47 import java.net.URL;
48 import java.util.Collections;
49 import java.util.List;
50
51 /**
52  * @author Olivier Lamy
53  * @since 1.4-M1
54  */
55 @Service( "remoteRepositoriesService#rest" )
56 public class DefaultRemoteRepositoriesService
57     extends AbstractRestService
58     implements RemoteRepositoriesService
59 {
60
61     @Inject
62     private RemoteRepositoryAdmin remoteRepositoryAdmin;
63
64     @Inject
65     private WagonFactory wagonFactory;
66
67
68     @Inject
69     private NetworkProxyAdmin networkProxyAdmin;
70
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     public RemoteRepository getRemoteRepository( String repositoryId )
87         throws ArchivaRestServiceException
88     {
89
90         List<RemoteRepository> remoteRepositories = getRemoteRepositories();
91         for ( RemoteRepository repository : remoteRepositories )
92         {
93             if ( StringUtils.equals( repositoryId, repository.getId() ) )
94             {
95                 return repository;
96             }
97         }
98         return null;
99     }
100
101     public Boolean deleteRemoteRepository( String repositoryId )
102         throws ArchivaRestServiceException
103     {
104         try
105         {
106             return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
107         }
108         catch ( RepositoryAdminException e )
109         {
110             log.error( e.getMessage(), e );
111             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
112         }
113     }
114
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     public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
130         throws ArchivaRestServiceException
131     {
132         try
133         {
134             return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
135         }
136         catch ( RepositoryAdminException e )
137         {
138             log.error( e.getMessage(), e );
139             throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
140         }
141     }
142
143     @Override
144     public Boolean checkRemoteConnectivity( String repositoryId )
145         throws ArchivaRestServiceException
146     {
147         try
148         {
149             RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
150             if ( remoteRepository == null )
151             {
152                 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
153                 return Boolean.FALSE;
154             }
155             NetworkProxy networkProxy = null;
156             if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
157             {
158                 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
159                 if ( networkProxy == null )
160                 {
161                     log.warn(
162                         "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
163                         remoteRepository.getRemoteDownloadNetworkProxyId() );
164                 }
165             }
166
167             String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
168
169             final Wagon wagon = wagonFactory.getWagon(
170                 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ).networkProxy(
171                     networkProxy ) );
172
173             wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
174             wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
175             HttpWagon foo;
176             if ( wagon instanceof AbstractHttpClientWagon )
177             {
178                 HttpConfiguration httpConfiguration = new HttpConfiguration();
179                 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
180                 httpMethodConfiguration.setUsePreemptive( true );
181                 httpMethodConfiguration.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
182                 httpConfiguration.setGet( httpMethodConfiguration );
183                 AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
184             }
185
186             wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ) );
187
188             // we only check connectivity as remote repo can be empty
189             wagon.getFileList( "/" );
190
191             return Boolean.TRUE;
192         }
193         catch ( Exception e )
194         {
195             throw new ArchivaRestServiceException( e.getMessage(),
196                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
197         }
198
199     }
200 }