From d52d2dc6d75f0912b58e7a4e22759ad7959d9aa5 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 8 Sep 2011 09:34:43 +0000 Subject: [PATCH] [MRM-1490] REST services : network proxy services tru rest git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1166587 13f79535-47bb-0310-9956-ffa450edef68 --- .../archiva/rest/api/model/NetworkProxy.java | 179 ++++++++++++++++++ .../api/services/NetworkProxyService.java | 78 ++++++++ .../services/DefaultNetworkProxyService.java | 102 ++++++++++ .../resources/META-INF/spring-context.xml | 1 + .../services/AbstractArchivaRestTest.java | 12 ++ .../services/NetworkProxyServiceTest.java | 94 +++++++++ 6 files changed, 466 insertions(+) create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/NetworkProxy.java create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/NetworkProxyService.java create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultNetworkProxyService.java create mode 100644 archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/NetworkProxyServiceTest.java diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/NetworkProxy.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/NetworkProxy.java new file mode 100644 index 000000000..0dd6543c9 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/model/NetworkProxy.java @@ -0,0 +1,179 @@ +package org.apache.archiva.rest.api.model; +/* + * 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 javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; + +/** + * @author Olivier Lamy + * @since 1.4 + */ +@XmlRootElement( name = "networkProxy" ) +public class NetworkProxy + implements Serializable +{ + private String id; + + /** + * The network protocol to use with this proxy: "http", "socks-4" + * . + */ + private String protocol = "http"; + + /** + * The proxy host. + */ + private String host; + + /** + * The proxy port. + */ + private int port = 8080; + + /** + * The proxy user. + */ + private String username; + + /** + * The proxy password. + */ + private String password; + + public NetworkProxy() + { + // no op + } + + public NetworkProxy( String id, String protocol, String host, int port, String username, String password ) + { + this.id = id; + this.protocol = protocol; + this.host = host; + this.port = port; + this.username = username; + this.password = password; + } + + public String getId() + { + return id; + } + + public void setId( String id ) + { + this.id = id; + } + + public String getProtocol() + { + return protocol; + } + + public void setProtocol( String protocol ) + { + this.protocol = protocol; + } + + public String getHost() + { + return host; + } + + public void setHost( String host ) + { + this.host = host; + } + + public int getPort() + { + return port; + } + + public void setPort( int port ) + { + this.port = port; + } + + public String getUsername() + { + return username; + } + + public void setUsername( String username ) + { + this.username = username; + } + + public String getPassword() + { + return password; + } + + public void setPassword( String password ) + { + this.password = password; + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + NetworkProxy that = (NetworkProxy) o; + + if ( id != null ? !id.equals( that.id ) : that.id != null ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = 17; + result = 37 * result + ( id != null ? id.hashCode() : 0 ); + return result; + } + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder(); + sb.append( "NetworkProxy" ); + sb.append( "{id='" ).append( id ).append( '\'' ); + sb.append( ", protocol='" ).append( protocol ).append( '\'' ); + sb.append( ", host='" ).append( host ).append( '\'' ); + sb.append( ", port=" ).append( port ); + sb.append( ", username='" ).append( username ).append( '\'' ); + sb.append( ", password='" ).append( password ).append( '\'' ); + sb.append( '}' ); + return sb.toString(); + } +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/NetworkProxyService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/NetworkProxyService.java new file mode 100644 index 000000000..d1850e212 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/NetworkProxyService.java @@ -0,0 +1,78 @@ +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.NetworkProxy; +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.core.MediaType; +import java.util.List; + +/** + * @author Olivier Lamy + * @since 1.4 + */ +@Path( "/networkProxyService/" ) +public interface NetworkProxyService +{ + @Path( "getNetworkProxies" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + List getNetworkProxies() + throws RepositoryAdminException; + + @Path( "getNetworkProxy/{networkProxyId}" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + NetworkProxy getNetworkProxy( @PathParam( "networkProxyId" ) String networkProxyId ) + throws RepositoryAdminException; + + @Path( "addNetworkProxy" ) + @POST + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + void addNetworkProxy( NetworkProxy networkProxy ) + throws RepositoryAdminException; + + @Path( "updateNetworkProxy" ) + @POST + @Consumes( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + void updateNetworkProxy( NetworkProxy networkProxy ) + throws RepositoryAdminException; + + @Path( "deleteNetworkProxy/{networkProxyId}" ) + @GET + @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) + @RedbackAuthorization( permission = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) + Boolean deleteNetworkProxy( @PathParam( "networkProxyId" ) String networkProxyId ) + throws RepositoryAdminException; +} diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultNetworkProxyService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultNetworkProxyService.java new file mode 100644 index 000000000..cf046febb --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultNetworkProxyService.java @@ -0,0 +1,102 @@ +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 net.sf.beanlib.provider.replicator.BeanReplicator; +import org.apache.archiva.admin.repository.RepositoryAdminException; +import org.apache.archiva.admin.repository.networkproxy.NetworkProxyAdmin; +import org.apache.archiva.rest.api.model.NetworkProxy; +import org.apache.archiva.rest.api.services.NetworkProxyService; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import java.util.ArrayList; +import java.util.List; + +/** + * @author Olivier Lamy + */ +@Service( "networkProxyService#rest" ) +public class DefaultNetworkProxyService + extends AbstractRestService + implements NetworkProxyService +{ + @Inject + private NetworkProxyAdmin networkProxyAdmin; + + public List getNetworkProxies() + throws RepositoryAdminException + { + List networkProxies = new ArrayList(); + for ( org.apache.archiva.admin.repository.networkproxy.NetworkProxy networkProxy : networkProxyAdmin.getNetworkProxies() ) + { + networkProxies.add( new BeanReplicator().replicateBean( networkProxy, NetworkProxy.class ) ); + } + return networkProxies; + } + + public NetworkProxy getNetworkProxy( String networkProxyId ) + throws RepositoryAdminException + { + org.apache.archiva.admin.repository.networkproxy.NetworkProxy networkProxy = + networkProxyAdmin.getNetworkProxy( networkProxyId ); + return networkProxy == null ? null : new BeanReplicator().replicateBean( networkProxy, NetworkProxy.class ); + } + + public void addNetworkProxy( NetworkProxy networkProxy ) + throws RepositoryAdminException + { + if ( networkProxy == null ) + { + return; + } + getNetworkProxyAdmin().addNetworkProxy( new BeanReplicator().replicateBean( networkProxy, + org.apache.archiva.admin.repository.networkproxy.NetworkProxy.class ), + getAuditInformation() ); + } + + public void updateNetworkProxy( NetworkProxy networkProxy ) + throws RepositoryAdminException + { + if ( networkProxy == null ) + { + return; + } + getNetworkProxyAdmin().updateNetworkProxy( new BeanReplicator().replicateBean( networkProxy, + org.apache.archiva.admin.repository.networkproxy.NetworkProxy.class ), + getAuditInformation() ); + } + + public Boolean deleteNetworkProxy( String networkProxyId ) + throws RepositoryAdminException + { + getNetworkProxyAdmin().deleteNetworkProxy( networkProxyId, getAuditInformation() ); + return Boolean.TRUE; + } + + public NetworkProxyAdmin getNetworkProxyAdmin() + { + return networkProxyAdmin; + } + + public void setNetworkProxyAdmin( NetworkProxyAdmin networkProxyAdmin ) + { + this.networkProxyAdmin = networkProxyAdmin; + } +} 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 e5ad02ac9..678c39cdb 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 @@ -54,6 +54,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 91b3269c1..361db4b71 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 @@ -21,6 +21,7 @@ package org.apache.archiva.rest.services; import org.apache.archiva.rest.api.model.ManagedRepository; import org.apache.archiva.rest.api.services.ManagedRepositoriesService; +import org.apache.archiva.rest.api.services.NetworkProxyService; import org.apache.archiva.rest.api.services.PingService; import org.apache.archiva.rest.api.services.ProxyConnectorService; import org.apache.archiva.rest.api.services.RemoteRepositoriesService; @@ -93,6 +94,17 @@ public abstract class AbstractArchivaRestTest return service; } + protected NetworkProxyService getNetworkProxyService() + { + NetworkProxyService service = + JAXRSClientFactory.create( "http://localhost:" + port + "/services/archivaServices/", + NetworkProxyService.class ); + + WebClient.client( service ).header( "Authorization", authorizationHeader ); + WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 ); + return service; + } + protected ManagedRepository getTestManagedRepository() { String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath(); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/NetworkProxyServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/NetworkProxyServiceTest.java new file mode 100644 index 000000000..a3d8af416 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/NetworkProxyServiceTest.java @@ -0,0 +1,94 @@ +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.NetworkProxy; +import org.junit.Test; + +/** + * @author Olivier Lamy + */ +public class NetworkProxyServiceTest + extends AbstractArchivaRestTest +{ + @Test + public void addAndDelete() + throws Exception + { + assertNotNull( getNetworkProxyService().getNetworkProxies() ); + assertTrue( getNetworkProxyService().getNetworkProxies().isEmpty() ); + + getNetworkProxyService().addNetworkProxy( getNetworkProxy( "foo" ) ); + + assertNotNull( getNetworkProxyService().getNetworkProxies() ); + assertFalse( getNetworkProxyService().getNetworkProxies().isEmpty() ); + assertEquals( 1, getNetworkProxyService().getNetworkProxies().size() ); + + getNetworkProxyService().deleteNetworkProxy( "foo" ); + + assertNotNull( getNetworkProxyService().getNetworkProxies() ); + assertTrue( getNetworkProxyService().getNetworkProxies().isEmpty() ); + + } + + @Test + public void addAndUpdateAndDelete() + throws Exception + { + assertNotNull( getNetworkProxyService().getNetworkProxies() ); + assertTrue( getNetworkProxyService().getNetworkProxies().isEmpty() ); + + getNetworkProxyService().addNetworkProxy( getNetworkProxy( "foo" ) ); + + assertNotNull( getNetworkProxyService().getNetworkProxies() ); + assertFalse( getNetworkProxyService().getNetworkProxies().isEmpty() ); + assertEquals( 1, getNetworkProxyService().getNetworkProxies().size() ); + + NetworkProxy networkProxy = getNetworkProxy( "foo" ); + networkProxy.setHost( "http://toto.com" ); + networkProxy.setPassword( "newpasswd" ); + networkProxy.setUsername( "newusername" ); + networkProxy.setPort( 9191 ); + + getNetworkProxyService().updateNetworkProxy( networkProxy ); + + assertEquals( networkProxy.getHost(), getNetworkProxyService().getNetworkProxy( "foo" ).getHost() ); + assertEquals( networkProxy.getPassword(), getNetworkProxyService().getNetworkProxy( "foo" ).getPassword() ); + assertEquals( networkProxy.getUsername(), getNetworkProxyService().getNetworkProxy( "foo" ).getUsername() ); + assertEquals( networkProxy.getPort(), getNetworkProxyService().getNetworkProxy( "foo" ).getPort() ); + + getNetworkProxyService().deleteNetworkProxy( "foo" ); + + assertNotNull( getNetworkProxyService().getNetworkProxies() ); + assertTrue( getNetworkProxyService().getNetworkProxies().isEmpty() ); + + } + + NetworkProxy getNetworkProxy( String id ) + { + + NetworkProxy networkProxy = new NetworkProxy(); + networkProxy.setId( id ); + networkProxy.setHost( "http://foo.com" ); + networkProxy.setPassword( "passwd" ); + networkProxy.setUsername( "username" ); + networkProxy.setPort( 9090 ); + return networkProxy; + } +} -- 2.39.5