]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1490] Expose Archiva services trough REST : remote repository management availab...
authorOlivier Lamy <olamy@apache.org>
Mon, 5 Sep 2011 13:24:58 +0000 (13:24 +0000)
committerOlivier Lamy <olamy@apache.org>
Mon, 5 Sep 2011 13:24:58 +0000 (13:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1165277 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RemoteRepositoriesService.java [new file with mode: 0644]
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java [new file with mode: 0644]
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java [new file with mode: 0644]

diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RemoteRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RemoteRepositoriesService.java
new file mode 100644 (file)
index 0000000..20a4b8b
--- /dev/null
@@ -0,0 +1,85 @@
+package org.apache.archiva.rest.api.services;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.repository.RepositoryAdminException;
+import org.apache.archiva.rest.api.model.ManagedRepository;
+import org.apache.archiva.rest.api.model.RemoteRepository;
+import org.apache.archiva.security.common.ArchivaRoleConstants;
+import org.codehaus.plexus.redback.authorization.RedbackAuthorization;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+@Path( "/remoteRepositoriesService/" )
+public interface RemoteRepositoriesService
+{
+    @Path( "getRemoteRepositories" )
+    @GET
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    List<RemoteRepository> getRemoteRepositories()
+        throws RepositoryAdminException;
+
+    @Path( "getRemoteRepository/{repositoryId}" )
+    @GET
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    RemoteRepository getRemoteRepository( @PathParam( "repositoryId" ) String repositoryId )
+        throws RepositoryAdminException;
+
+    @Path( "deleteRemoteRepository/{repositoryId}" )
+    @GET
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    Boolean deleteRemoteRepository( @PathParam( "repositoryId" ) String repositoryId )
+        throws Exception;
+
+
+    @Path( "addRemoteRepository" )
+    @POST
+    @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    Boolean addRemoteRepository( RemoteRepository remoteRepository )
+        throws Exception;
+
+
+    @Path( "updateRemoteRepository" )
+    @POST
+    @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
+    @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
+    Boolean updateRemoteRepository( RemoteRepository remoteRepository )
+        throws Exception;
+
+
+}
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRemoteRepositoriesService.java
new file mode 100644 (file)
index 0000000..b308835
--- /dev/null
@@ -0,0 +1,106 @@
+package org.apache.archiva.rest.services;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.admin.repository.RepositoryAdminException;
+import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
+import org.apache.archiva.rest.api.model.RemoteRepository;
+import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+import javax.ws.rs.PathParam;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ * @since 1.4
+ */
+@Service( "remoteRepositoriesService#rest" )
+public class DefaultRemoteRepositoriesService
+    extends AbstractRestService
+    implements RemoteRepositoriesService
+{
+
+    @Inject
+    private RemoteRepositoryAdmin remoteRepositoryAdmin;
+
+    public List<RemoteRepository> getRemoteRepositories()
+        throws RepositoryAdminException
+    {
+        List<RemoteRepository> remoteRepositories = new ArrayList<RemoteRepository>();
+        for ( org.apache.archiva.admin.repository.remote.RemoteRepository remoteRepository : remoteRepositoryAdmin.getRemoteRepositories() )
+        {
+            RemoteRepository repo =
+                new RemoteRepository( remoteRepository.getId(), remoteRepository.getName(), remoteRepository.getUrl(),
+                                      remoteRepository.getLayout(), remoteRepository.getUserName(),
+                                      remoteRepository.getPassword(), remoteRepository.getTimeout() );
+            remoteRepositories.add( repo );
+        }
+        return remoteRepositories;
+    }
+
+    public RemoteRepository getRemoteRepository( @PathParam( "repositoryId" ) String repositoryId )
+        throws RepositoryAdminException
+    {
+        List<RemoteRepository> remoteRepositories = getRemoteRepositories();
+        for ( RemoteRepository repository : remoteRepositories )
+        {
+            if ( StringUtils.equals( repositoryId, repository.getId() ) )
+            {
+                return repository;
+            }
+        }
+        return null;
+    }
+
+    public Boolean deleteRemoteRepository( @PathParam( "repositoryId" ) String repositoryId )
+        throws Exception
+    {
+        return remoteRepositoryAdmin.deleteRemoteRepository( repositoryId, getAuditInformation() );
+    }
+
+    public Boolean addRemoteRepository( RemoteRepository remoteRepository )
+        throws Exception
+    {
+        return remoteRepositoryAdmin.addRemoteRepository( getModelRemoteRepository( remoteRepository ),
+                                                          getAuditInformation() );
+    }
+
+    public Boolean updateRemoteRepository( RemoteRepository remoteRepository )
+        throws Exception
+    {
+        return remoteRepositoryAdmin.updateRemoteRepository( getModelRemoteRepository( remoteRepository ),
+                                                             getAuditInformation() );
+    }
+
+    private org.apache.archiva.admin.repository.remote.RemoteRepository getModelRemoteRepository(
+        RemoteRepository remoteRepository )
+    {
+        return new org.apache.archiva.admin.repository.remote.RemoteRepository( remoteRepository.getId(),
+                                                                                remoteRepository.getName(),
+                                                                                remoteRepository.getUrl(),
+                                                                                remoteRepository.getLayout(),
+                                                                                remoteRepository.getUserName(),
+                                                                                remoteRepository.getPassword(),
+                                                                                remoteRepository.getTimeOut() );
+    }
+}
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RemoteRepositoriesServiceTest.java
new file mode 100644 (file)
index 0000000..f6e643d
--- /dev/null
@@ -0,0 +1,69 @@
+package org.apache.archiva.rest.services;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.rest.api.model.RemoteRepository;
+import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
+import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * @author Olivier Lamy
+ */
+public class RemoteRepositoriesServiceTest
+    extends AbstractArchivaRestTest
+{
+
+
+    @Test( expected = ServerWebApplicationException.class )
+    public void listRemoteRepositoriesKarmaFailed()
+        throws Exception
+    {
+        RemoteRepositoriesService service = getRemoteRepositoriesService();
+        try
+        {
+            assertFalse( service.getRemoteRepositories().isEmpty() );
+        }
+        catch ( ServerWebApplicationException e )
+        {
+            assertEquals( 403, e.getStatus() );
+            throw e;
+        }
+    }
+
+    @Test
+    public void listRemoteRepositoriesKarma()
+        throws Exception
+    {
+        RemoteRepositoriesService service = getRemoteRepositoriesService();
+
+        WebClient.client( service ).header( "Authorization", authorizationHeader );
+        WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
+        List<RemoteRepository> repos = service.getRemoteRepositories();
+        assertFalse( repos.isEmpty() );
+        log.info( "repos {}", repos );
+
+    }
+
+
+}