From 3ebdc4a971d06808cb9e8ca0f02dd8ece7f09f29 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 2 Oct 2012 15:12:47 +0000 Subject: [PATCH] [MRM-1681] addProxy Connectors Rules screen add REST services. git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1392952 13f79535-47bb-0310-9956-ffa450edef68 --- .../services/ProxyConnectorRuleService.java | 76 +++++++++++++++ .../DefaultProxyConnectorRuleService.java | 97 +++++++++++++++++++ .../resources/META-INF/spring-context.xml | 1 + .../services/AbstractArchivaRestTest.java | 58 ++++------- .../ProxyConnectorRuleServiceTest.java | 76 +++++++++++++++ 5 files changed, 269 insertions(+), 39 deletions(-) create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ProxyConnectorRuleService.java create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorRuleService.java create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ProxyConnectorRuleServiceTest.java diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ProxyConnectorRuleService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ProxyConnectorRuleService.java new file mode 100644 index 000000000..d57fed33a --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/ProxyConnectorRuleService.java @@ -0,0 +1,76 @@ +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.model.beans.ProxyConnectorRule; +import org.apache.archiva.redback.authorization.RedbackAuthorization; +import org.apache.archiva.security.common.ArchivaRoleConstants; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.List; + +/** + * No update method for changing pattern as id is pattern, use delete then add. + * + * @author Olivier Lamy + * @since 1.4-M3 + */ +@Path ( "/proxyConnectorRuleService/" ) +public interface ProxyConnectorRuleService +{ + @Path ( "proxyConnectorRules" ) + @GET + @Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + List getProxyConnectorRules() + throws ArchivaRestServiceException; + + @Path ( "proxyConnectorRule" ) + @PUT + @Consumes ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + Boolean addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule ) + throws ArchivaRestServiceException; + + @Path ( "deleteProxyConnectorRule" ) + @POST + @Consumes ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + Boolean deleteProxyConnectorRule( ProxyConnectorRule proxyConnectorRule ) + throws ArchivaRestServiceException; + + /** + * only to update attached proxy connectors to update pattern use delete then add + */ + @Path ( "updateProxyConnectorRule" ) + @POST + @Consumes ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Produces ( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @RedbackAuthorization ( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + Boolean updateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule ) + throws ArchivaRestServiceException; +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorRuleService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorRuleService.java new file mode 100644 index 000000000..fd74d15bc --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultProxyConnectorRuleService.java @@ -0,0 +1,97 @@ +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.model.RepositoryAdminException; +import org.apache.archiva.admin.model.beans.ProxyConnectorRule; +import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin; +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; +import org.apache.archiva.rest.api.services.ProxyConnectorRuleService; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import java.util.List; + +/** + * @author Olivier Lamy + */ +@Service ( "proxyConnectorRuleService#rest" ) +public class DefaultProxyConnectorRuleService + extends AbstractRestService + implements ProxyConnectorRuleService +{ + + @Inject + private ProxyConnectorRuleAdmin proxyConnectorRuleAdmin; + + public List getProxyConnectorRules() + throws ArchivaRestServiceException + { + try + { + return proxyConnectorRuleAdmin.getProxyConnectorRules(); + } + catch ( RepositoryAdminException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), e ); + } + } + + public Boolean addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule ) + throws ArchivaRestServiceException + { + try + { + proxyConnectorRuleAdmin.addProxyConnectorRule( proxyConnectorRule, getAuditInformation() ); + return Boolean.TRUE; + } + catch ( RepositoryAdminException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), e ); + } + } + + public Boolean deleteProxyConnectorRule( ProxyConnectorRule proxyConnectorRule ) + throws ArchivaRestServiceException + { + try + { + proxyConnectorRuleAdmin.deleteProxyConnectorRule( proxyConnectorRule, getAuditInformation() ); + return Boolean.TRUE; + } + catch ( RepositoryAdminException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), e ); + } + } + + public Boolean updateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule ) + throws ArchivaRestServiceException + { + try + { + proxyConnectorRuleAdmin.updateProxyConnectorRule( proxyConnectorRule, getAuditInformation() ); + return Boolean.TRUE; + } + catch ( RepositoryAdminException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), e ); + } + } +} 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 d9468127b..4d0c7fcf3 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 @@ -71,6 +71,7 @@ + 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 index d45a9d522..ee6ff9156 100644 --- 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 @@ -29,6 +29,7 @@ import org.apache.archiva.rest.api.services.ManagedRepositoriesService; import org.apache.archiva.rest.api.services.MergeRepositoriesService; import org.apache.archiva.rest.api.services.NetworkProxyService; import org.apache.archiva.rest.api.services.PingService; +import org.apache.archiva.rest.api.services.ProxyConnectorRuleService; import org.apache.archiva.rest.api.services.ProxyConnectorService; import org.apache.archiva.rest.api.services.RemoteRepositoriesService; import org.apache.archiva.rest.api.services.RepositoriesService; @@ -55,7 +56,7 @@ import java.util.Date; /** * @author Olivier Lamy */ -@RunWith (ArchivaBlockJUnit4ClassRunner.class) +@RunWith ( ArchivaBlockJUnit4ClassRunner.class ) public abstract class AbstractArchivaRestTest extends AbstractRestServicesTest { @@ -123,12 +124,10 @@ public abstract class AbstractArchivaRestTest return getRepositoriesService( null ); } - protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader ) + protected T getService( Class clazz, String authzHeader ) { - MergeRepositoriesService service = - JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", - MergeRepositoriesService.class, - Collections.singletonList( new JacksonJaxbJsonProvider() ) ); + T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz, + Collections.singletonList( new JacksonJaxbJsonProvider() ) ); if ( authzHeader != null ) { @@ -140,54 +139,35 @@ public abstract class AbstractArchivaRestTest return service; } - protected RepositoriesService getRepositoriesService( String authzHeader ) + protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader ) { - RepositoriesService service = - JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", - RepositoriesService.class, - Collections.singletonList( new JacksonJaxbJsonProvider() ) ); - - if ( authzHeader != null ) - { - WebClient.client( service ).header( "Authorization", authzHeader ); - } - WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 ); - WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE ); - WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE ); - return service; + return getService( ProxyConnectorRuleService.class, authzHeader ); + } + protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader ) + { + return getService( MergeRepositoriesService.class, authzHeader ); } - protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader ) + protected RepositoriesService getRepositoriesService( String authzHeader ) { - ManagedRepositoriesService service = - JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", - ManagedRepositoriesService.class, - Collections.singletonList( new JacksonJaxbJsonProvider() ) ); + return getService( RepositoriesService.class, authzHeader ); - if ( authzHeader != null ) - { - WebClient.client( service ).header( "Authorization", authzHeader ); - } - WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 ); - WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE ); - WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE ); - return service; + } + protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader ) + { + return getService( ManagedRepositoriesService.class, authzHeader ); } protected PingService getPingService() { - return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", - PingService.class, - Collections.singletonList( new JacksonJaxbJsonProvider() ) ); + return getService( PingService.class, null ); } protected RemoteRepositoriesService getRemoteRepositoriesService() { - return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", - RemoteRepositoriesService.class, - Collections.singletonList( new JacksonJaxbJsonProvider() ) ); + return getService( RemoteRepositoriesService.class, null ); } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ProxyConnectorRuleServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ProxyConnectorRuleServiceTest.java new file mode 100644 index 000000000..04d26d09f --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ProxyConnectorRuleServiceTest.java @@ -0,0 +1,76 @@ +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.model.beans.ProxyConnector; +import org.apache.archiva.admin.model.beans.ProxyConnectorRule; +import org.apache.archiva.admin.model.beans.ProxyConnectorRuleType; +import org.apache.archiva.rest.api.services.ProxyConnectorRuleService; +import org.junit.Test; + +import java.util.Arrays; + +/** + * @author Olivier Lamy + */ +public class ProxyConnectorRuleServiceTest + extends AbstractArchivaRestTest +{ + + @Test + public void addProxyConnectorRule() + throws Exception + { + ProxyConnector proxyConnector = new ProxyConnector(); + proxyConnector.setSourceRepoId( "snapshots" ); + proxyConnector.setTargetRepoId( "central" ); + + ProxyConnectorRuleService service = getProxyConnectorRuleService( authorizationHeader ); + + ProxyConnectorRule rule = null; + try + { + + int size = service.getProxyConnectorRules().size(); + assertEquals( 0, size ); + + getProxyConnectorService().addProxyConnector( proxyConnector ); + + rule = new ProxyConnectorRule( "org/apache/maven", ProxyConnectorRuleType.BLACK_LIST, + Arrays.asList( proxyConnector ) ); + + service.addProxyConnectorRule( rule ); + assertEquals( size + 1, service.getProxyConnectorRules().size() ); + + rule = service.getProxyConnectorRules().get( 0 ); + + assertEquals( "org/apache/maven", rule.getPattern() ); + assertEquals( 1, rule.getProxyConnectors().size() ); + assertEquals( "snapshots", rule.getProxyConnectors().get( 0 ).getSourceRepoId() ); + assertEquals( "central", rule.getProxyConnectors().get( 0 ).getTargetRepoId() ); + assertEquals( ProxyConnectorRuleType.BLACK_LIST, rule.getProxyConnectorRuleType() ); + } + finally + { + service.deleteProxyConnectorRule( rule ); + getProxyConnectorService().deleteProxyConnector( proxyConnector ); + } + } +} + -- 2.39.5