]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1750] Add remote repository health check
authorOlivier Lamy <olamy@apache.org>
Sun, 22 Dec 2013 12:25:23 +0000 (12:25 +0000)
committerOlivier Lamy <olamy@apache.org>
Sun, 22 Dec 2013 12:25:23 +0000 (12:25 +0000)
Add rest api

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1552981 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-scheduler/archiva-scheduler-indexing/src/main/java/org/apache/archiva/scheduler/indexing/DownloadRemoteIndexTask.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RemoteRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java

index c87617880a5aa973b139dd975b03f6be649da912..b8db00ae8bab48e5b6027e7e56beae8802eabadb 100644 (file)
@@ -135,19 +135,18 @@ public class DownloadRemoteIndexTask
             final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(
                 new WagonFactoryRequest( wagonProtocol, this.remoteRepository.getExtraHeaders() ).networkProxy(
                     this.networkProxy ) );
-            int timeoutInMilliseconds = remoteRepository.getTimeout() * 1000;
             // FIXME olamy having 2 config values
-            wagon.setReadTimeout( timeoutInMilliseconds );
-            wagon.setTimeout( timeoutInMilliseconds );
+            wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
+            wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
 
             if ( wagon instanceof AbstractHttpClientWagon )
             {
                 HttpConfiguration httpConfiguration = new HttpConfiguration();
                 HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
                 httpMethodConfiguration.setUsePreemptive( true );
-                httpMethodConfiguration.setReadTimeout( timeoutInMilliseconds );
+                httpMethodConfiguration.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
                 httpConfiguration.setGet( httpMethodConfiguration );
-                ( (AbstractHttpClientWagon) wagon ).setHttpConfiguration( httpConfiguration );
+                AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
             }
 
             wagon.addTransferListener( new DownloadListener() );
index fc861402c4c191ed2e8b66fb28627d2a57c3e8d0..19cbf5d69ebb20abcec7bbcc6e4df4865fd03131 100644 (file)
@@ -36,47 +36,52 @@ import java.util.List;
  * @author Olivier Lamy
  * @since 1.4-M1
  */
-@Path( "/remoteRepositoriesService/" )
+@Path("/remoteRepositoriesService/")
 public interface RemoteRepositoriesService
 {
-    @Path( "getRemoteRepositories" )
+    @Path("getRemoteRepositories")
     @GET
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
-    @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
     List<RemoteRepository> getRemoteRepositories()
         throws ArchivaRestServiceException;
 
-    @Path( "getRemoteRepository/{repositoryId}" )
+    @Path("getRemoteRepository/{repositoryId}")
     @GET
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
-    @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
-    RemoteRepository getRemoteRepository( @PathParam( "repositoryId" ) String repositoryId )
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
+    RemoteRepository getRemoteRepository( @PathParam("repositoryId") String repositoryId )
         throws ArchivaRestServiceException;
 
-    @Path( "deleteRemoteRepository/{repositoryId}" )
+    @Path("deleteRemoteRepository/{repositoryId}")
     @GET
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
-    @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
-    Boolean deleteRemoteRepository( @PathParam( "repositoryId" ) String repositoryId )
-        throws Exception;
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
+    @RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
+    Boolean deleteRemoteRepository( @PathParam("repositoryId") String repositoryId )
+        throws ArchivaRestServiceException;
 
 
-    @Path( "addRemoteRepository" )
+    @Path("addRemoteRepository")
     @POST
-    @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
-    @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
+    @RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
     Boolean addRemoteRepository( RemoteRepository remoteRepository )
-        throws Exception;
+        throws ArchivaRestServiceException;
 
 
-    @Path( "updateRemoteRepository" )
+    @Path("updateRemoteRepository")
     @POST
-    @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
-    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
-    @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
+    @RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
     Boolean updateRemoteRepository( RemoteRepository remoteRepository )
-        throws Exception;
-
+        throws ArchivaRestServiceException;
 
+    @Path("checkRemoteConnectivity/{repositoryId}")
+    @GET
+    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
+    @RedbackAuthorization(permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
+    Boolean checkRemoteConnectivity( @PathParam( "repositoryId" ) String repositoryId )
+        throws ArchivaRestServiceException;
 }
index 345665151ee88dcd39fce2ab820a9c2c8291a2ca..e58c40a3658daa954b70f95b23193014c7aac73f 100644 (file)
       <artifactId>redback-rest-api</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>org.apache.maven.wagon</groupId>
+      <artifactId>wagon-http</artifactId>
+      <scope>provided</scope>
+    </dependency>
+
     <dependency>
       <groupId>commons-validator</groupId>
       <artifactId>commons-validator</artifactId>
index f19ae3201df64aaa3fb4029040482bf07d4c0dd2..b48bccfb2f05df0c7931b40ddaddfa7f1ed29efa 100644 (file)
@@ -19,14 +19,30 @@ package org.apache.archiva.rest.services;
  */
 
 import org.apache.archiva.admin.model.RepositoryAdminException;
+import org.apache.archiva.admin.model.beans.NetworkProxy;
 import org.apache.archiva.admin.model.beans.RemoteRepository;
+import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
+import org.apache.archiva.proxy.common.WagonFactory;
+import org.apache.archiva.proxy.common.WagonFactoryException;
+import org.apache.archiva.proxy.common.WagonFactoryRequest;
 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
 import org.apache.commons.lang.StringUtils;
+import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.wagon.StreamWagon;
+import org.apache.maven.wagon.TransferFailedException;
+import org.apache.maven.wagon.Wagon;
+import org.apache.maven.wagon.authorization.AuthorizationException;
+import org.apache.maven.wagon.providers.http.AbstractHttpClientWagon;
+import org.apache.maven.wagon.providers.http.HttpConfiguration;
+import org.apache.maven.wagon.providers.http.HttpMethodConfiguration;
 import org.springframework.stereotype.Service;
 
 import javax.inject.Inject;
+import javax.ws.rs.core.Response;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.Collections;
 import java.util.List;
 
@@ -34,7 +50,7 @@ import java.util.List;
  * @author Olivier Lamy
  * @since 1.4-M1
  */
-@Service ("remoteRepositoriesService#rest")
+@Service( "remoteRepositoriesService#rest" )
 public class DefaultRemoteRepositoriesService
     extends AbstractRestService
     implements RemoteRepositoriesService
@@ -43,6 +59,13 @@ public class DefaultRemoteRepositoriesService
     @Inject
     private RemoteRepositoryAdmin remoteRepositoryAdmin;
 
+    @Inject
+    private WagonFactory wagonFactory;
+
+
+    @Inject
+    private NetworkProxyAdmin networkProxyAdmin;
+
     public List<RemoteRepository> getRemoteRepositories()
         throws ArchivaRestServiceException
     {
@@ -74,7 +97,7 @@ public class DefaultRemoteRepositoriesService
     }
 
     public Boolean deleteRemoteRepository( String repositoryId )
-        throws Exception
+        throws ArchivaRestServiceException
     {
         try
         {
@@ -88,7 +111,7 @@ public class DefaultRemoteRepositoriesService
     }
 
     public Boolean addRemoteRepository( RemoteRepository remoteRepository )
-        throws Exception
+        throws ArchivaRestServiceException
     {
         try
         {
@@ -102,7 +125,7 @@ public class DefaultRemoteRepositoriesService
     }
 
     public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
-        throws Exception
+        throws ArchivaRestServiceException
     {
         try
         {
@@ -115,5 +138,84 @@ public class DefaultRemoteRepositoriesService
         }
     }
 
+    @Override
+    public Boolean checkRemoteConnectivity( String repositoryId )
+        throws ArchivaRestServiceException
+    {
+        try
+        {
+            RemoteRepository remoteRepository = remoteRepositoryAdmin.getRemoteRepository( repositoryId );
+            if ( remoteRepository == null )
+            {
+                log.warn( "ignore scheduleDownloadRemote for repo with id {} as not exists", repositoryId );
+                return Boolean.FALSE;
+            }
+            NetworkProxy networkProxy = null;
+            if ( StringUtils.isNotBlank( remoteRepository.getRemoteDownloadNetworkProxyId() ) )
+            {
+                networkProxy = networkProxyAdmin.getNetworkProxy( remoteRepository.getRemoteDownloadNetworkProxyId() );
+                if ( networkProxy == null )
+                {
+                    log.warn(
+                        "your remote repository is configured to download remote index trought a proxy we cannot find id:{}",
+                        remoteRepository.getRemoteDownloadNetworkProxyId() );
+                }
+            }
+
+            String wagonProtocol = new URL( remoteRepository.getUrl() ).getProtocol();
+
+            final Wagon wagon = wagonFactory.getWagon(
+                new WagonFactoryRequest( wagonProtocol, remoteRepository.getExtraHeaders() ).networkProxy(
+                    networkProxy ) );
 
+            wagon.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
+            wagon.setTimeout( remoteRepository.getTimeout() * 1000 );
+
+            if ( wagon instanceof AbstractHttpClientWagon )
+            {
+                HttpConfiguration httpConfiguration = new HttpConfiguration();
+                HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
+                httpMethodConfiguration.setUsePreemptive( true );
+                httpMethodConfiguration.setReadTimeout( remoteRepository.getRemoteDownloadTimeout() * 1000 );
+                httpConfiguration.setGet( httpMethodConfiguration );
+                AbstractHttpClientWagon.class.cast( wagon ).setHttpConfiguration( httpConfiguration );
+            }
+
+            // we only check connectivity as remote repo can be empty
+            wagon.getFileList( "/" );
+
+            return Boolean.TRUE;
+        }
+        catch ( RepositoryAdminException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
+        catch ( MalformedURLException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
+        catch ( WagonFactoryException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
+        catch ( TransferFailedException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
+        catch ( AuthorizationException e )
+        {
+            throw new ArchivaRestServiceException( e.getMessage(),
+                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
+        }
+
+    }
 }