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.springframework.stereotype.Service;
42 import javax.inject.Inject;
43 import javax.ws.rs.core.Response;
44 import java.net.MalformedURLException;
46 import java.util.Collections;
47 import java.util.List;
50 * @author Olivier Lamy
53 @Service( "remoteRepositoriesService#rest" )
54 public class DefaultRemoteRepositoriesService
55 extends AbstractRestService
56 implements RemoteRepositoriesService
60 private RemoteRepositoryAdmin remoteRepositoryAdmin;
63 private WagonFactory wagonFactory;
67 private NetworkProxyAdmin networkProxyAdmin;
69 public List<RemoteRepository> getRemoteRepositories()
70 throws ArchivaRestServiceException
74 List<RemoteRepository> remoteRepositories = remoteRepositoryAdmin.getRemoteRepositories();
75 return remoteRepositories == null ? Collections.<RemoteRepository>emptyList() : remoteRepositories;
77 catch ( RepositoryAdminException e )
79 log.error( e.getMessage(), e );
80 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() ) )
99 public Boolean deleteRemoteRepository( String repositoryId )
100 throws ArchivaRestServiceException
104 return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
106 catch ( RepositoryAdminException e )
108 log.error( e.getMessage(), e );
109 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
113 public Boolean addRemoteRepository( RemoteRepository remoteRepository )
114 throws ArchivaRestServiceException
118 return remoteRepositoryAdmin.addRemoteRepository( remoteRepository, getAuditInformation() );
120 catch ( RepositoryAdminException e )
122 log.error( e.getMessage(), e );
123 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
127 public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
128 throws ArchivaRestServiceException
132 return remoteRepositoryAdmin.updateRemoteRepository( remoteRepository, getAuditInformation() );
134 catch ( RepositoryAdminException e )
136 log.error( e.getMessage(), e );
137 throw new ArchivaRestServiceException( e.getMessage(), e.getFieldName(), e );
142 public Boolean checkRemoteConnectivity( String repositoryId )
143 throws ArchivaRestServiceException
147 RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
148 if ( remoteRepository == null )
150 log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
151 return Boolean.FALSE;
153 NetworkProxy networkProxy = null;
154 if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
156 networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
157 if ( networkProxy == null )
160 "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
161 remoteRepository.getRemoteDownloadNetworkProxyId() );
165 String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
167 final Wagon wagon = wagonFactory.getWagon(
168 new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ).networkProxy(
171 wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
172 wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
174 if ( wagon instanceof AbstractHttpClientWagon )
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 );
184 // we only check connectivity as remote repo can be empty
185 wagon.getFileList( "/" );
189 catch ( RepositoryAdminException e )
191 throw new ArchivaRestServiceException( e.getMessage(),
192 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
194 catch ( MalformedURLException e )
196 throw new ArchivaRestServiceException( e.getMessage(),
197 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
199 catch ( WagonFactoryException e )
201 throw new ArchivaRestServiceException( e.getMessage(),
202 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
204 catch ( TransferFailedException e )
206 throw new ArchivaRestServiceException( e.getMessage(),
207 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
209 catch ( ResourceDoesNotExistException e )
211 throw new ArchivaRestServiceException( e.getMessage(),
212 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
214 catch ( AuthorizationException e )
216 throw new ArchivaRestServiceException( e.getMessage(),
217 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );