1 package org.apache.archiva.rest.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
41 import javax.inject.Inject;
42 import javax.ws.rs.core.Response;
44 import java.util.Collections;
45 import java.util.List;
48 * @author Olivier Lamy
51 @Service( "remoteRepositoriesService#rest" )
52 public class DefaultRemoteRepositoriesService
53 extends AbstractRestService
54 implements RemoteRepositoriesService
58 private RemoteRepositoryAdmin remoteRepositoryAdmin;
61 private WagonFactory wagonFactory;
65 private NetworkProxyAdmin networkProxyAdmin;
68 public List<RemoteRepository> getRemoteRepositories()
69 throws ArchivaRestServiceException
73 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
74 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
76 catch ( RepositoryAdminException e )
78 log.error( e.getMessage(), e );
79 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
84 public RemoteRepository getRemoteRepository( String repositoryId )
85 throws ArchivaRestServiceException
88 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
89 for ( RemoteRepository repository : remoteRepositories )
91 if ( StringUtils.equals( repositoryId, repository.getId() ) )
100 public Boolean deleteRemoteRepository( String repositoryId )
101 throws ArchivaRestServiceException
105 return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
107 catch ( RepositoryAdminException e )
109 log.error( e.getMessage(), e );
110 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
115 public Boolean addRemoteRepository( RemoteRepository remoteRepository )
116 throws ArchivaRestServiceException
120 return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
122 catch ( RepositoryAdminException e )
124 log.error( e.getMessage(), e );
125 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
130 public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
131 throws ArchivaRestServiceException
135 return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
137 catch ( RepositoryAdminException e )
139 log.error( e.getMessage(), e );
140 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
145 public Boolean checkRemoteConnectivity( String repositoryId )
146 throws ArchivaRestServiceException
150 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
151 if ( remoteRepository == null )
153 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
154 return Boolean.FALSE;
156 NetworkProxy networkProxy = null;
157 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
159 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
160 if ( networkProxy == null )
163 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
164 remoteRepository.getRemoteDownloadNetworkProxyId() );
168 String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
171 wagonFactory.getWagon( new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ) //
172 .networkProxy( networkProxy ) );
174 // hardcoded value as it's a check of the remote repo connectivity
175 wagon.setReadTimeout( 4000 );
176 wagon.setTimeout( 3000 );
178 if ( wagon instanceof AbstractHttpClientWagon )
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 );
187 ProxyInfo proxyInfo = null;
188 if ( networkProxy != null )
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() );
198 wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ), proxyInfo );
200 // we only check connectivity as remote repo can be empty
201 wagon.getFileList( "/" );
205 catch ( TransferFailedException e )
207 log.info( "TransferFailedException :{}", e.getMessage() );
208 return Boolean.FALSE;
210 catch ( Exception e )
212 throw new ArchivaRestServiceException( e.getMessage(),
213 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );