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;
67 int checkReadTimeout = 10000;
68 int checkTimeout = 9000;
71 public List<RemoteRepository> getRemoteRepositories()
72 throws ArchivaRestServiceException
76 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
77 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
79 catch ( RepositoryAdminException e )
81 log.error( e.getMessage(), e );
82 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
87 public RemoteRepository getRemoteRepository( String repositoryId )
88 throws ArchivaRestServiceException
91 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
92 for ( RemoteRepository repository : remoteRepositories )
94 if ( StringUtils.equals( repositoryId, repository.getId() ) )
103 public Boolean deleteRemoteRepository( String repositoryId )
104 throws ArchivaRestServiceException
108 return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
110 catch ( RepositoryAdminException e )
112 log.error( e.getMessage(), e );
113 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
118 public Boolean addRemoteRepository( RemoteRepository remoteRepository )
119 throws ArchivaRestServiceException
123 return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
125 catch ( RepositoryAdminException e )
127 log.error( e.getMessage(), e );
128 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
133 public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
134 throws ArchivaRestServiceException
138 return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
140 catch ( RepositoryAdminException e )
142 log.error( e.getMessage(), e );
143 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
148 public Boolean checkRemoteConnectivity( String repositoryId )
149 throws ArchivaRestServiceException
153 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
154 if ( remoteRepository == null )
156 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
157 return Boolean.FALSE;
159 NetworkProxy networkProxy = null;
160 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
162 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
163 if ( networkProxy == null )
166 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
167 remoteRepository.getRemoteDownloadNetworkProxyId() );
171 String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
174 wagonFactory.getWagon( new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ) //
175 .networkProxy( networkProxy ) );
177 // hardcoded value as it's a check of the remote repo connectivity
178 wagon.setReadTimeout( checkReadTimeout );
179 wagon.setTimeout( checkTimeout );
181 if ( wagon instanceof AbstractHttpClientWagon )
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 );
190 ProxyInfo proxyInfo = null;
191 if ( networkProxy != null )
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() );
201 wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ), proxyInfo );
203 // we only check connectivity as remote repo can be empty
204 // MRM-1909: Wagon implementation appends a slash already
205 wagon.getFileList( "" );
209 catch ( TransferFailedException e )
211 log.info( "TransferFailedException :{}", e.getMessage() );
212 return Boolean.FALSE;
214 catch ( Exception e )
216 throw new ArchivaRestServiceException( e.getMessage(),
217 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
222 public int getCheckReadTimeout() {
223 return checkReadTimeout;
226 public void setCheckReadTimeout(int checkReadTimeout) {
227 this.checkReadTimeout = checkReadTimeout;
230 public int getCheckTimeout() {
234 public void setCheckTimeout(int checkTimeout) {
235 this.checkTimeout = checkTimeout;