]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1490] expose service tru rest
authorOlivier Lamy <olamy@apache.org>
Fri, 19 Aug 2011 07:53:42 +0000 (07:53 +0000)
committerOlivier Lamy <olamy@apache.org>
Fri, 19 Aug 2011 07:53:42 +0000 (07:53 +0000)
impl Repo management service

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

13 files changed:
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ManagedRepository.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/RemoteRepository.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.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/DefaultPingService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java [new file with mode: 0644]
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/PingServiceTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/resources/log4j.xml
archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/AbstractManagedRepositoriesAction.java

index 231a168620b17aa188216550a3fb59cb7008761c..cf2bb75be0e8da531b2d95d1ba865e0966f32432 100644 (file)
   <name>Archiva Web :: REST support :: Api</name>
 
   <dependencies>
+    <dependency>
+      <groupId>org.apache.archiva</groupId>
+      <artifactId>archiva-security</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>org.codehaus.redback</groupId>
       <artifactId>redback-authorization-api</artifactId>
index 07b19ac62e9a1c8dac44c91a8bd138344d240a9b..8a849224794c356b7a0a91fc960bfadb02b7f7ca 100644 (file)
@@ -28,11 +28,11 @@ import java.io.Serializable;
 @XmlRootElement( name = "managedRepository" )
 public class ManagedRepository
     implements Serializable
-{    
+{
     private String id;
-   
+
     private String name;
-   
+
     private String url;
 
     private String layout;
@@ -40,12 +40,12 @@ public class ManagedRepository
     private boolean snapshots = false;
 
     private boolean releases = false;
-   
+
     public ManagedRepository()
     {
         // no op
     }
-    
+
     public ManagedRepository( String id, String name, String url, String layout, boolean snapshots, boolean releases )
     {
         this.id = id;
@@ -55,56 +55,33 @@ public class ManagedRepository
         this.snapshots = snapshots;
         this.releases = releases;
     }
-    
-    public boolean equals(Object other)
-    {
-        if ( this == other)
-        {
-            return true;
-        }
-        
-        if ( !(other instanceof ManagedRepository) )
-        {
-            return false;
-        }
-        
-        ManagedRepository that = (ManagedRepository) other;
-        boolean result = true;
-        result = result && ( getId() == null ? that.getId() == null : getId().equals( that.getId() ) );
-        return result;
-    } 
-    
+
+
     public String getId()
     {
         return this.id;
-    } 
+    }
 
     public String getLayout()
     {
         return this.layout;
-    } 
+    }
 
     public String getName()
     {
         return this.name;
     }
-    
+
     public String getUrl()
     {
         return this.url;
-    } 
-    
-    public int hashCode()
-    {
-        int result = 17;
-        result = 37 * result + ( id != null ? id.hashCode() : 0 );
-        return result;
-    } 
+    }
+
 
     public boolean isReleases()
     {
         return this.releases;
-    } 
+    }
 
     /**
      * Get null
@@ -112,35 +89,68 @@ public class ManagedRepository
     public boolean isSnapshots()
     {
         return this.snapshots;
-    } 
-    
-    public void setId(String id)
+    }
+
+    public void setId( String id )
     {
         this.id = id;
-    } 
-    
-    public void setLayout(String layout)
+    }
+
+    public void setLayout( String layout )
     {
         this.layout = layout;
-    } 
+    }
 
-    public void setName(String name)
+    public void setName( String name )
     {
         this.name = name;
-    } 
-    
-    public void setReleases(boolean releases)
+    }
+
+    public void setReleases( boolean releases )
     {
         this.releases = releases;
-    } 
+    }
 
-    public void setSnapshots(boolean snapshots)
+    public void setSnapshots( boolean snapshots )
     {
         this.snapshots = snapshots;
-    } 
+    }
 
-    public void setUrl(String url)
+    public void setUrl( String url )
     {
         this.url = url;
     }
+
+
+    public int hashCode()
+    {
+        int result = 17;
+        result = 37 * result + ( id != null ? id.hashCode() : 0 );
+        return result;
+    }
+
+    public boolean equals( Object other )
+    {
+        if ( this == other )
+        {
+            return true;
+        }
+
+        if ( !( other instanceof ManagedRepository ) )
+        {
+            return false;
+        }
+
+        ManagedRepository that = (ManagedRepository) other;
+        boolean result = true;
+        result = result && ( getId() == null ? that.getId() == null : getId().equals( that.getId() ) );
+        return result;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "ManagedRepository{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", url='" + url + '\''
+            + ", layout='" + layout + '\'' + ", snapshots=" + snapshots + ", releases=" + releases + '}';
+    }
 }
\ No newline at end of file
index fba20a579273082ef85f2dfb9bf5e57fadb127b1..a31b8aac21f46f2fb5efea7dfcf7912d519dbdd0 100644 (file)
@@ -26,20 +26,20 @@ import java.io.Serializable;
 @XmlRootElement( name = "remoteRepository" )
 public class RemoteRepository
     implements Serializable
-{   
+{
     private String id;
 
     private String name;
 
     private String url;
-    
+
     private String layout;
 
     public RemoteRepository()
     {
         // no op
     }
-    
+
     public RemoteRepository( String id, String name, String url, String layout )
     {
         this.id = id;
@@ -47,69 +47,79 @@ public class RemoteRepository
         this.url = url;
         this.layout = layout;
     }
-    
-    public boolean equals(Object other)
-    {
-        if ( this == other)
-        {
-            return true;
-        }
-        
-        if ( !(other instanceof RemoteRepository) )
-        {
-            return false;
-        }
-        
-        RemoteRepository that = (RemoteRepository) other;
-        boolean result = true;
-        result = result && ( getId() == null ? that.getId() == null : getId().equals( that.getId() ) );
-        return result;
-    } 
-    
+
+
     public String getId()
     {
         return this.id;
-    } 
-    
+    }
+
     public String getLayout()
     {
         return this.layout;
-    } 
-    
+    }
+
     public String getName()
     {
         return this.name;
-    } 
-    
+    }
+
     public String getUrl()
     {
         return this.url;
     }
-    
-    public int hashCode()
-    {
-        int result = 17;
-        result = 37 * result + ( id != null ? id.hashCode() : 0 );
-        return result;
-    }
-    
-    public void setId(String id)
+
+
+    public void setId( String id )
     {
         this.id = id;
     }
-    
-    public void setLayout(String layout)
+
+    public void setLayout( String layout )
     {
         this.layout = layout;
     }
-    
-    public void setName(String name)
+
+    public void setName( String name )
     {
         this.name = name;
-    } 
-    
-    public void setUrl(String url)
+    }
+
+    public void setUrl( String url )
     {
         this.url = url;
-    }     
+    }
+
+
+    public int hashCode()
+    {
+        int result = 17;
+        result = 37 * result + ( id != null ? id.hashCode() : 0 );
+        return result;
+    }
+
+    public boolean equals( Object other )
+    {
+        if ( this == other )
+        {
+            return true;
+        }
+
+        if ( !( other instanceof RemoteRepository ) )
+        {
+            return false;
+        }
+
+        RemoteRepository that = (RemoteRepository) other;
+        boolean result = true;
+        result = result && ( getId() == null ? that.getId() == null : getId().equals( that.getId() ) );
+        return result;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "RemoteRepository{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", url='" + url + '\''
+            + ", layout='" + layout + '\'' + '}';
+    }
 }
\ No newline at end of file
index 2c2a19955f8bfccbe0c135982c27504a2511a9fb..0e5413d9dd18d756fd30cf20a6e70dbb1e75adf1 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.archiva.rest.api.services;
 
 import org.apache.archiva.rest.api.model.ManagedRepository;
 import org.apache.archiva.rest.api.model.RemoteRepository;
+import org.apache.maven.archiva.security.ArchivaRoleConstants;
 import org.codehaus.plexus.redback.authorization.RedbackAuthorization;
 
 import javax.ws.rs.GET;
@@ -40,13 +41,13 @@ public interface RepositoriesService
     @Path( "getManagedRepositories" )
     @GET
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
-    @RedbackAuthorization( noRestriction = true )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
     List<ManagedRepository> getManagedRepositories();
 
     @Path( "getRemoteRepositories" )
     @GET
     @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
-    @RedbackAuthorization( noRestriction = true )
+    @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION)
     List<RemoteRepository> getRemoteRepositories();
 
     @Path( "scanRepository" )
index 994221d59165679fcd40ed6eb1264dfe7dbbded9..ecae76c2291a4f2552f7640c5a3697cbd3ee45c7 100644 (file)
   </properties>
 
   <dependencies>
+    <dependency>
+      <groupId>org.apache.archiva</groupId>
+      <artifactId>archiva-security</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.codehaus.redback</groupId>
       <artifactId>redback-authorization-api</artifactId>
index 3f7a5c80fa3dbad352e5b423aea6eca46c9702fd..d1041930f2217108f4c609bb6932dd8c72bad844 100644 (file)
@@ -20,6 +20,8 @@ package org.apache.archiva.rest.services;
  */
 
 import org.apache.archiva.rest.api.services.PingService;
+import org.codehaus.plexus.redback.users.User;
+import org.codehaus.redback.rest.services.RedbackAuthenticationThreadLocal;
 import org.springframework.stereotype.Service;
 
 /**
@@ -37,6 +39,7 @@ public class DefaultPingService
 
     public String pingWithAuthz()
     {
+        User user = RedbackAuthenticationThreadLocal.get();
         return ping();
     }
 }
index a08c4a5a1ffafae44deae9a608bd189322d7b4dc..4b7259d9c86217c36d1973ccfffa455280a12f90 100644 (file)
@@ -3,26 +3,62 @@ package org.apache.archiva.rest.services;
 import org.apache.archiva.rest.api.model.ManagedRepository;
 import org.apache.archiva.rest.api.model.RemoteRepository;
 import org.apache.archiva.rest.api.services.RepositoriesService;
+import org.apache.maven.archiva.configuration.ArchivaConfiguration;
+import org.apache.maven.archiva.configuration.Configuration;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
+import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
 import org.springframework.stereotype.Service;
 
+import javax.inject.Inject;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
  * @author Olivier Lamy
  * @since 1.4
  */
-@Service("repositoriesService#rest")
+@Service( "repositoriesService#rest" )
 public class DefaultRepositoriesService
     implements RepositoriesService
 {
+
+    @Inject
+    protected ArchivaConfiguration archivaConfiguration;
+
     public List<ManagedRepository> getManagedRepositories()
     {
-        return null;  //To change body of implemented methods use File | Settings | File Templates.
+        List<ManagedRepositoryConfiguration> managedRepoConfigs =
+            archivaConfiguration.getConfiguration().getManagedRepositories();
+
+        List<ManagedRepository> managedRepos = new ArrayList<ManagedRepository>( managedRepoConfigs.size() );
+
+        for ( ManagedRepositoryConfiguration repoConfig : managedRepoConfigs )
+        {
+            // TODO fix resolution of repo url!
+            ManagedRepository repo =
+                new ManagedRepository( repoConfig.getId(), repoConfig.getName(), "URL", repoConfig.getLayout(),
+                                       repoConfig.isSnapshots(), repoConfig.isReleases() );
+            managedRepos.add( repo );
+        }
+
+        return managedRepos;
     }
 
     public List<RemoteRepository> getRemoteRepositories()
     {
-        return null;  //To change body of implemented methods use File | Settings | File Templates.
+        Configuration config = archivaConfiguration.getConfiguration();
+        List<RemoteRepositoryConfiguration> remoteRepoConfigs = config.getRemoteRepositories();
+
+        List<RemoteRepository> remoteRepos = new ArrayList<RemoteRepository>( remoteRepoConfigs.size() );
+
+        for ( RemoteRepositoryConfiguration repoConfig : remoteRepoConfigs )
+        {
+            RemoteRepository repo = new RemoteRepository( repoConfig.getId(), repoConfig.getName(), repoConfig.getUrl(),
+                                                          repoConfig.getLayout() );
+            remoteRepos.add( repo );
+        }
+
+        return remoteRepos;
     }
 
     public Boolean scanRepository( String repositoryId, boolean fullScan )
index 9d5ba24b4e0baa49359d710c43bc8934a52d90c1..b251fbc7d2bde167d268e006114b94e6c357031d 100644 (file)
 
   <jaxrs:server id="archivaServices" address="/archivaServices">
 
-    <jaxrs:serviceBeans>
-      <ref bean="pingService#rest"/>
-    </jaxrs:serviceBeans>
     <jaxrs:providers>
       <ref bean="authenticationInterceptor#rest"/>
       <ref bean="permissionInterceptor#rest"/>
       <ref bean="repositoriesService#rest"/>
     </jaxrs:providers>
 
+    <jaxrs:serviceBeans>
+      <ref bean="pingService#rest"/>
+      <ref bean="repositoriesService#rest"/>
+    </jaxrs:serviceBeans>
+
+    <jaxrs:outInterceptors>
+      <ref bean="threadLocalUserCleaner#rest"/>
+    </jaxrs:outInterceptors>
+
    </jaxrs:server>
 
 </beans>
\ No newline at end of file
diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
new file mode 100644 (file)
index 0000000..12b36e9
--- /dev/null
@@ -0,0 +1,33 @@
+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.codehaus.redback.rest.services.AbstractRestServicesTest;
+
+/**
+ * @author Olivier Lamy
+ * @since TODO
+ */
+public abstract class AbstractArchivaRestTest
+    extends AbstractRestServicesTest
+{
+    public String guestAuthzHeader =
+        "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
+}
index a44d8a2bed4ef48a255e537ce7c5b957dbb9e268..da9ea8233033fdf9e9920f16f8bcfe828b8bf0b5 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.codehaus.redback.rest.services.AbstractRestServicesTest;
+import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -31,7 +32,7 @@ import org.junit.Test;
  * @since 1.4
  */
 public class PingServiceTest
-    extends AbstractRestServicesTest
+    extends AbstractArchivaRestTest
 {
 
     PingService getPingService()
@@ -76,8 +77,21 @@ public class PingServiceTest
     {
 
         PingService service = getPingService();
+        WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
         WebClient.client( service ).header( "Authorization", authorizationHeader );
         String res = service.pingWithAuthz();
         assertEquals( "Yeah Baby It rocks!", res );
     }
+
+    @Ignore("FIXME guest failed ???")
+    public void pingWithAuthzGuest()
+        throws Exception
+    {
+
+        PingService service = getPingService();
+        WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
+        WebClient.client( service ).header( "Authorization", guestAuthzHeader );
+        String res = service.pingWithAuthz();
+        assertEquals( "Yeah Baby It rocks!", res );
+    }
 }
index 07527e22177f73df316ea4c0dc34713d376bbceb..75b047a2143540c1759d6da019059ead2b4b3e24 100644 (file)
@@ -19,27 +19,55 @@ package org.apache.archiva.rest.services;
  * under the License.
  */
 
+import org.apache.archiva.rest.api.model.RemoteRepository;
 import org.apache.archiva.rest.api.services.RepositoriesService;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
-import org.codehaus.redback.rest.services.AbstractRestServicesTest;
+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 RepositoriesServiceTest
-    extends AbstractRestServicesTest
+    extends AbstractArchivaRestTest
 {
-    RepositoriesService getPingService()
+    RepositoriesService getRepositoriesService()
     {
         return JAXRSClientFactory.create( "http://localhost:" + port + "/services/archivaServices/",
                                           RepositoriesService.class );
 
     }
 
+    @Test( expected = ServerWebApplicationException.class )
+    public void listRemoteRepositoriesKarmaFailed()
+        throws Exception
+    {
+        RepositoriesService service = getRepositoriesService();
+        try
+        {
+            assertFalse( service.getRemoteRepositories().isEmpty() );
+        }
+        catch ( ServerWebApplicationException e )
+        {
+            assertEquals( 403, e.getStatus() );
+            throw e;
+        }
+    }
+
     @Test
-    public void listRemoteRepositories() throws Exception
+    public void listRemoteRepositoriesKarma()
+        throws Exception
     {
+        RepositoriesService service = getRepositoriesService();
+
+        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 );
 
     }
 }
index 8c55a1c3d2d31052d16c53a535bdb0ab2a3c6900..102f7cdae5a03e7b9d8d9437ce89579b2c1a41a6 100644 (file)
   <logger name="org.codehaus.redback.rest.services" >
     <level value="debug"/>
   </logger>
+  <logger name="JPOX">
+    <level value="ERROR"/>
+  </logger>
+
+  <logger name="org.springframework">
+    <level value="ERROR"/>
+  </logger>
 
   <root>
     <priority value ="info" />