1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
44 import javax.inject.Inject;
45 import javax.ws.rs.core.Response;
46 import java.net.MalformedURLException;
48 import java.util.Collections;
49 import java.util.List;
52 * @author Olivier Lamy
55 @Service( "remoteRepositoriesService#rest" )
56 public class DefaultRemoteRepositoriesService
57 extends AbstractRestService
58 implements RemoteRepositoriesService
62 private RemoteRepositoryAdmin remoteRepositoryAdmin;
65 private WagonFactory wagonFactory;
69 private NetworkProxyAdmin networkProxyAdmin;
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 );
86 public RemoteRepository getRemoteRepository( String repositoryId )
87 throws ArchivaRestServiceException
90 List<RemoteRepository> remoteRepositories = getRemoteRepositories();
91 for ( RemoteRepository repository : remoteRepositories )
93 if ( StringUtils.equals( repositoryId, repository.getId() ) )
101 public Boolean deleteRemoteRepository( String repositoryId )
102 throws ArchivaRestServiceException
106 return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
108 catch ( RepositoryAdminException e )
110 log.error( e.getMessage(), e );
111 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 );
129 public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
130 throws ArchivaRestServiceException
134 return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
136 catch ( RepositoryAdminException e )
138 log.error( e.getMessage(), e );
139 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
144 public Boolean checkRemoteConnectivity( String repositoryId )
145 throws ArchivaRestServiceException
149 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
150 if ( remoteRepository == null )
152 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
153 return Boolean.FALSE;
155 NetworkProxy networkProxy = null;
156 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
158 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
159 if ( networkProxy == null )
162 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
163 remoteRepository.getRemoteDownloadNetworkProxyId() );
167 String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
169 final Wagon wagon = wagonFactory.getWagon(
170 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ).networkProxy(
173 wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
174 wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
176 if ( wagon instanceof AbstractHttpClientWagon )
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 );
186 wagon.connect( new Repository( remoteRepository.getId(), remoteRepository.getUrl() ) );
188 // we only check connectivity as remote repo can be empty
189 wagon.getFileList( "/" );
193 catch ( Exception e )
195 throw new ArchivaRestServiceException( e.getMessage(),
196 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );