From 53d4ecac05c0ae0acb5b8986a9fc46f34d7d23ef Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Fri, 19 Aug 2011 07:53:42 +0000 Subject: [PATCH] [MRM-1490] expose service tru rest impl Repo management service git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1159531 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva-rest/archiva-rest-api/pom.xml | 5 + .../rest/api/model/ManagedRepository.java | 112 ++++++++++-------- .../rest/api/model/RemoteRepository.java | 100 +++++++++------- .../api/services/RepositoriesService.java | 5 +- .../archiva-rest-services/pom.xml | 4 + .../DefaultPingService.java | 3 + .../DefaultRepositoriesService.java | 42 ++++++- .../resources/META-INF/spring-context.xml | 12 +- .../services/AbstractArchivaRestTest.java | 33 ++++++ .../rest/services/PingServiceTest.java | 16 ++- .../services/RepositoriesServiceTest.java | 36 +++++- .../src/test/resources/log4j.xml | 7 ++ .../AbstractManagedRepositoriesAction.java | 2 +- 13 files changed, 267 insertions(+), 110 deletions(-) create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml index 231a16862..cf2bb75be 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/pom.xml @@ -28,6 +28,11 @@ Archiva Web :: REST support :: Api + + org.apache.archiva + archiva-security + + org.codehaus.redback redback-authorization-api diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ManagedRepository.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ManagedRepository.java index 07b19ac62..8a8492247 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ManagedRepository.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/ManagedRepository.java @@ -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 diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/RemoteRepository.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/RemoteRepository.java index fba20a579..a31b8aac2 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/RemoteRepository.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/RemoteRepository.java @@ -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 diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java index 2c2a19955..0e5413d9d 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/RepositoriesService.java @@ -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 getManagedRepositories(); @Path( "getRemoteRepositories" ) @GET @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) - @RedbackAuthorization( noRestriction = true ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION) List getRemoteRepositories(); @Path( "scanRepository" ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index 994221d59..ecae76c22 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -33,6 +33,10 @@ + + org.apache.archiva + archiva-security + org.codehaus.redback redback-authorization-api diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultPingService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultPingService.java index 3f7a5c80f..d1041930f 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultPingService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultPingService.java @@ -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(); } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultRepositoriesService.java index a08c4a5a1..4b7259d9c 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org.apache.archiva.rest.services/DefaultRepositoriesService.java @@ -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 getManagedRepositories() { - return null; //To change body of implemented methods use File | Settings | File Templates. + List managedRepoConfigs = + archivaConfiguration.getConfiguration().getManagedRepositories(); + + List managedRepos = new ArrayList( 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 getRemoteRepositories() { - return null; //To change body of implemented methods use File | Settings | File Templates. + Configuration config = archivaConfiguration.getConfiguration(); + List remoteRepoConfigs = config.getRemoteRepositories(); + + List remoteRepos = new ArrayList( 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 ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml index 9d5ba24b4..b251fbc7d 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/resources/META-INF/spring-context.xml @@ -41,15 +41,21 @@ - - - + + + + + + + + + \ 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 index 000000000..12b36e9bf --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java @@ -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() ); +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/PingServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/PingServiceTest.java index a44d8a2be..da9ea8233 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/PingServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/PingServiceTest.java @@ -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 ); + } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java index 07527e221..75b047a21 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java @@ -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 repos = service.getRemoteRepositories(); + assertFalse( repos.isEmpty() ); + log.info( "repos {}", repos ); } } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/resources/log4j.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/resources/log4j.xml index 8c55a1c3d..102f7cdae 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/resources/log4j.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/resources/log4j.xml @@ -38,6 +38,13 @@ + + + + + + + diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/AbstractManagedRepositoriesAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/AbstractManagedRepositoriesAction.java index 63bbb3a76..0f588d1d6 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/AbstractManagedRepositoriesAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/AbstractManagedRepositoriesAction.java @@ -43,7 +43,7 @@ import javax.inject.Named; * * @version $Id$ */ -public abstract class AbstractManagedRepositoriesAction +public abstract class AbstractManagedReposigettoriesAction extends AbstractRepositoriesAdminAction { /** -- 2.39.5