From: Olivier Lamy Date: Thu, 8 Sep 2011 09:33:34 +0000 (+0000) Subject: [MRM-1510] api to configure NetworkProxy : use it webapp X-Git-Tag: archiva-1.4-M1~355 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4060f790733baaa56337d7f8e23b1e16279a28a4;p=archiva.git [MRM-1510] api to configure NetworkProxy : use it webapp git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1166585 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyAction.java index fdcffe701..9779a3c7d 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyAction.java @@ -21,18 +21,13 @@ package org.apache.maven.archiva.web.action.admin.networkproxies; import com.opensymphony.xwork2.Preparable; import com.opensymphony.xwork2.Validateable; +import org.apache.archiva.admin.repository.RepositoryAdminException; +import org.apache.archiva.admin.repository.networkproxy.NetworkProxy; +import org.apache.archiva.admin.repository.networkproxy.NetworkProxyAdmin; import org.apache.archiva.security.common.ArchivaRoleConstants; -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.functors.NotPredicate; import org.apache.commons.lang.StringUtils; -import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.configuration.Configuration; -import org.apache.maven.archiva.configuration.IndeterminateConfigurationException; -import org.apache.maven.archiva.configuration.NetworkProxyConfiguration; -import org.apache.maven.archiva.configuration.functors.NetworkProxySelectionPredicate; import org.apache.maven.archiva.web.action.AbstractActionSupport; import org.codehaus.plexus.redback.rbac.Resource; -import org.codehaus.plexus.registry.RegistryException; import org.codehaus.redback.integration.interceptor.SecureAction; import org.codehaus.redback.integration.interceptor.SecureActionBundle; import org.codehaus.redback.integration.interceptor.SecureActionException; @@ -54,13 +49,13 @@ public class ConfigureNetworkProxyAction { @Inject - private ArchivaConfiguration archivaConfiguration; + private NetworkProxyAdmin networkProxyAdmin; private String mode; private String proxyid; - private NetworkProxyConfiguration proxy; + private NetworkProxy proxy; public String add() { @@ -74,8 +69,8 @@ public class ConfigureNetworkProxyAction } public String delete() + throws RepositoryAdminException { - Configuration config = archivaConfiguration.getConfiguration(); String id = getProxyid(); if ( StringUtils.isBlank( id ) ) @@ -84,18 +79,16 @@ public class ConfigureNetworkProxyAction return SUCCESS; } - NetworkProxySelectionPredicate networkProxySelection = new NetworkProxySelectionPredicate( id ); - NetworkProxyConfiguration proxyConfig = - (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), networkProxySelection ); - if ( proxyConfig == null ) + NetworkProxy networkProxy = getNetworkProxyAdmin().getNetworkProxy( id ); + if ( networkProxy == null ) { addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." ); return SUCCESS; } - archivaConfiguration.getConfiguration().removeNetworkProxy( proxyConfig ); + getNetworkProxyAdmin().deleteNetworkProxy( id, getAuditInformation() ); addActionMessage( "Successfully removed network proxy [" + id + "]" ); - return saveConfiguration(); + return SUCCESS; } public String edit() @@ -109,7 +102,7 @@ public class ConfigureNetworkProxyAction return mode; } - public NetworkProxyConfiguration getProxy() + public NetworkProxy getProxy() { return proxy; } @@ -147,11 +140,12 @@ public class ConfigureNetworkProxyAction if ( proxy == null ) { - proxy = new NetworkProxyConfiguration(); + proxy = new NetworkProxy(); } } public String save() + throws RepositoryAdminException { String mode = getMode(); @@ -159,19 +153,14 @@ public class ConfigureNetworkProxyAction if ( StringUtils.equalsIgnoreCase( "edit", mode ) ) { - removeNetworkProxy( id ); + getNetworkProxyAdmin().updateNetworkProxy( proxy, getAuditInformation() ); } else { - if ( findNetworkProxy( id ) != null ) - { - addActionError( "Unable to add new repository with id [" + id + "], that id already exists." ); - return INPUT; - } + getNetworkProxyAdmin().addNetworkProxy( proxy, getAuditInformation() ); } - addNetworkProxy( getProxy() ); - return saveConfiguration(); + return SUCCESS; } public void validate() @@ -185,7 +174,7 @@ public class ConfigureNetworkProxyAction this.mode = mode; } - public void setProxy( NetworkProxyConfiguration proxy ) + public void setProxy( NetworkProxy proxy ) { this.proxy = proxy; } @@ -195,47 +184,19 @@ public class ConfigureNetworkProxyAction this.proxyid = proxyid; } - private void addNetworkProxy( NetworkProxyConfiguration proxy ) - { - archivaConfiguration.getConfiguration().addNetworkProxy( proxy ); - } - private NetworkProxyConfiguration findNetworkProxy( String id ) + private NetworkProxy findNetworkProxy( String id ) + throws RepositoryAdminException { - Configuration config = archivaConfiguration.getConfiguration(); - - NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id ); - - return (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), selectedProxy ); + return getNetworkProxyAdmin().getNetworkProxy( id ); } private void removeNetworkProxy( String id ) + throws RepositoryAdminException { - NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id ); - NotPredicate notSelectedProxy = new NotPredicate( selectedProxy ); - CollectionUtils.filter( archivaConfiguration.getConfiguration().getNetworkProxies(), notSelectedProxy ); + getNetworkProxyAdmin().deleteNetworkProxy( id, getAuditInformation() ); } - private String saveConfiguration() - { - try - { - archivaConfiguration.save( archivaConfiguration.getConfiguration() ); - addActionMessage( "Successfully saved configuration" ); - } - catch ( RegistryException e ) - { - addActionError( "Unable to save configuration: " + e.getMessage() ); - return INPUT; - } - catch ( IndeterminateConfigurationException e ) - { - addActionError( e.getMessage() ); - return INPUT; - } - - return SUCCESS; - } private void trimAllRequestParameterValues() { @@ -264,4 +225,15 @@ public class ConfigureNetworkProxyAction proxy.setUsername( proxy.getUsername().trim() ); } } + + public NetworkProxyAdmin getNetworkProxyAdmin() + { + return networkProxyAdmin; + } + + public void setNetworkProxyAdmin( NetworkProxyAdmin networkProxyAdmin ) + { + this.networkProxyAdmin = networkProxyAdmin; + } } + diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyActionTest.java index da478ccbd..8b1efe173 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/networkproxies/ConfigureNetworkProxyActionTest.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import junit.framework.TestCase; +import org.apache.archiva.admin.repository.networkproxy.NetworkProxy; import org.apache.maven.archiva.configuration.NetworkProxyConfiguration; import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory; import org.apache.archiva.web.validator.utils.ValidatorUtil; @@ -77,8 +78,8 @@ public class ConfigureNetworkProxyActionTest extends TestCase public void testStruts2ValidationFrameworkWithNullInputs() throws Exception { // prep - NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(null, null, null, null); - configureNetworkProxyAction.setProxy(networkProxyConfiguration); + NetworkProxy networkProxy = createNetworkProxy(null, null, null, null); + configureNetworkProxyAction.setProxy(networkProxy); // test actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); @@ -110,8 +111,8 @@ public class ConfigureNetworkProxyActionTest extends TestCase public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception { // prep - NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING); - configureNetworkProxyAction.setProxy(networkProxyConfiguration); + NetworkProxy networkProxy = createNetworkProxy(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING); + configureNetworkProxyAction.setProxy(networkProxy); // test actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); @@ -143,8 +144,10 @@ public class ConfigureNetworkProxyActionTest extends TestCase public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception { // prep - NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT, PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT, PROXY_USERNAME_INVALID_INPUT); - configureNetworkProxyAction.setProxy(networkProxyConfiguration); + NetworkProxy networkProxy = createNetworkProxy( PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT, + PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT, + PROXY_USERNAME_INVALID_INPUT ); + configureNetworkProxyAction.setProxy(networkProxy); // test actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); @@ -184,8 +187,8 @@ public class ConfigureNetworkProxyActionTest extends TestCase public void testStruts2ValidationFrameworkWithValidInputs() throws Exception { // prep - NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT); - configureNetworkProxyAction.setProxy(networkProxyConfiguration); + NetworkProxy networkProxy = createNetworkProxy(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT); + configureNetworkProxyAction.setProxy(networkProxy); // test actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); @@ -194,26 +197,26 @@ public class ConfigureNetworkProxyActionTest extends TestCase assertFalse(configureNetworkProxyAction.hasFieldErrors()); } - private NetworkProxyConfiguration createNetworkProxyConfiguration(String id, String host, int port, String protocol, String username) + private NetworkProxy createNetworkProxy(String id, String host, int port, String protocol, String username) { - NetworkProxyConfiguration networkProxyConfiguration = new NetworkProxyConfiguration(); - networkProxyConfiguration.setId(id); - networkProxyConfiguration.setHost(host); - networkProxyConfiguration.setPort(port); - networkProxyConfiguration.setProtocol(protocol); - networkProxyConfiguration.setUsername(username); - return networkProxyConfiguration; + NetworkProxy networkProxy = new NetworkProxy(); + networkProxy.setId( id ); + networkProxy.setHost( host ); + networkProxy.setPort( port ); + networkProxy.setProtocol( protocol ); + networkProxy.setUsername( username ); + return networkProxy; } // over-loaded // for simulating empty/null form purposes; excluding primitive data-typed values - private NetworkProxyConfiguration createNetworkProxyConfiguration(String id, String host, String protocol, String username) + private NetworkProxy createNetworkProxy(String id, String host, String protocol, String username) { - NetworkProxyConfiguration networkProxyConfiguration = new NetworkProxyConfiguration(); - networkProxyConfiguration.setId(id); - networkProxyConfiguration.setHost(host); - networkProxyConfiguration.setProtocol(protocol); - networkProxyConfiguration.setUsername(username); - return networkProxyConfiguration; + NetworkProxy networkProxy = new NetworkProxy(); + networkProxy.setId( id ); + networkProxy.setHost( host ); + networkProxy.setProtocol( protocol ); + networkProxy.setUsername( username ); + return networkProxy; } }