[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
This commit is contained in:
Olivier Lamy 2011-09-08 09:33:34 +00:00
parent 07ae616d7e
commit 4060f79073
2 changed files with 60 additions and 85 deletions

View File

@ -21,18 +21,13 @@ package org.apache.maven.archiva.web.action.admin.networkproxies;
import com.opensymphony.xwork2.Preparable; import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable; 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.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.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.apache.maven.archiva.web.action.AbstractActionSupport;
import org.codehaus.plexus.redback.rbac.Resource; 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.SecureAction;
import org.codehaus.redback.integration.interceptor.SecureActionBundle; import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException; import org.codehaus.redback.integration.interceptor.SecureActionException;
@ -54,13 +49,13 @@ public class ConfigureNetworkProxyAction
{ {
@Inject @Inject
private ArchivaConfiguration archivaConfiguration; private NetworkProxyAdmin networkProxyAdmin;
private String mode; private String mode;
private String proxyid; private String proxyid;
private NetworkProxyConfiguration proxy; private NetworkProxy proxy;
public String add() public String add()
{ {
@ -74,8 +69,8 @@ public class ConfigureNetworkProxyAction
} }
public String delete() public String delete()
throws RepositoryAdminException
{ {
Configuration config = archivaConfiguration.getConfiguration();
String id = getProxyid(); String id = getProxyid();
if ( StringUtils.isBlank( id ) ) if ( StringUtils.isBlank( id ) )
@ -84,18 +79,16 @@ public class ConfigureNetworkProxyAction
return SUCCESS; return SUCCESS;
} }
NetworkProxySelectionPredicate networkProxySelection = new NetworkProxySelectionPredicate( id ); NetworkProxy networkProxy = getNetworkProxyAdmin().getNetworkProxy( id );
NetworkProxyConfiguration proxyConfig = if ( networkProxy == null )
(NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), networkProxySelection );
if ( proxyConfig == null )
{ {
addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." ); addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." );
return SUCCESS; return SUCCESS;
} }
archivaConfiguration.getConfiguration().removeNetworkProxy( proxyConfig ); getNetworkProxyAdmin().deleteNetworkProxy( id, getAuditInformation() );
addActionMessage( "Successfully removed network proxy [" + id + "]" ); addActionMessage( "Successfully removed network proxy [" + id + "]" );
return saveConfiguration(); return SUCCESS;
} }
public String edit() public String edit()
@ -109,7 +102,7 @@ public class ConfigureNetworkProxyAction
return mode; return mode;
} }
public NetworkProxyConfiguration getProxy() public NetworkProxy getProxy()
{ {
return proxy; return proxy;
} }
@ -147,11 +140,12 @@ public class ConfigureNetworkProxyAction
if ( proxy == null ) if ( proxy == null )
{ {
proxy = new NetworkProxyConfiguration(); proxy = new NetworkProxy();
} }
} }
public String save() public String save()
throws RepositoryAdminException
{ {
String mode = getMode(); String mode = getMode();
@ -159,19 +153,14 @@ public class ConfigureNetworkProxyAction
if ( StringUtils.equalsIgnoreCase( "edit", mode ) ) if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
{ {
removeNetworkProxy( id ); getNetworkProxyAdmin().updateNetworkProxy( proxy, getAuditInformation() );
} }
else else
{ {
if ( findNetworkProxy( id ) != null ) getNetworkProxyAdmin().addNetworkProxy( proxy, getAuditInformation() );
{
addActionError( "Unable to add new repository with id [" + id + "], that id already exists." );
return INPUT;
}
} }
addNetworkProxy( getProxy() ); return SUCCESS;
return saveConfiguration();
} }
public void validate() public void validate()
@ -185,7 +174,7 @@ public class ConfigureNetworkProxyAction
this.mode = mode; this.mode = mode;
} }
public void setProxy( NetworkProxyConfiguration proxy ) public void setProxy( NetworkProxy proxy )
{ {
this.proxy = proxy; this.proxy = proxy;
} }
@ -195,47 +184,19 @@ public class ConfigureNetworkProxyAction
this.proxyid = proxyid; this.proxyid = proxyid;
} }
private void addNetworkProxy( NetworkProxyConfiguration proxy )
private NetworkProxy findNetworkProxy( String id )
throws RepositoryAdminException
{ {
archivaConfiguration.getConfiguration().addNetworkProxy( proxy ); return getNetworkProxyAdmin().getNetworkProxy( id );
}
private NetworkProxyConfiguration findNetworkProxy( String id )
{
Configuration config = archivaConfiguration.getConfiguration();
NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
return (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), selectedProxy );
} }
private void removeNetworkProxy( String id ) private void removeNetworkProxy( String id )
throws RepositoryAdminException
{ {
NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id ); getNetworkProxyAdmin().deleteNetworkProxy( id, getAuditInformation() );
NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
CollectionUtils.filter( archivaConfiguration.getConfiguration().getNetworkProxies(), notSelectedProxy );
} }
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() private void trimAllRequestParameterValues()
{ {
@ -264,4 +225,15 @@ public class ConfigureNetworkProxyAction
proxy.setUsername( proxy.getUsername().trim() ); proxy.setUsername( proxy.getUsername().trim() );
} }
} }
public NetworkProxyAdmin getNetworkProxyAdmin()
{
return networkProxyAdmin;
}
public void setNetworkProxyAdmin( NetworkProxyAdmin networkProxyAdmin )
{
this.networkProxyAdmin = networkProxyAdmin;
}
} }

View File

@ -26,6 +26,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.archiva.admin.repository.networkproxy.NetworkProxy;
import org.apache.maven.archiva.configuration.NetworkProxyConfiguration; import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory; import org.apache.maven.archiva.web.action.admin.repositories.DefaultActionValidatorManagerFactory;
import org.apache.archiva.web.validator.utils.ValidatorUtil; import org.apache.archiva.web.validator.utils.ValidatorUtil;
@ -77,8 +78,8 @@ public class ConfigureNetworkProxyActionTest extends TestCase
public void testStruts2ValidationFrameworkWithNullInputs() throws Exception public void testStruts2ValidationFrameworkWithNullInputs() throws Exception
{ {
// prep // prep
NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(null, null, null, null); NetworkProxy networkProxy = createNetworkProxy(null, null, null, null);
configureNetworkProxyAction.setProxy(networkProxyConfiguration); configureNetworkProxyAction.setProxy(networkProxy);
// test // test
actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
@ -110,8 +111,8 @@ public class ConfigureNetworkProxyActionTest extends TestCase
public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception public void testStruts2ValidationFrameworkWithBlankInputs() throws Exception
{ {
// prep // prep
NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING); NetworkProxy networkProxy = createNetworkProxy(EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING);
configureNetworkProxyAction.setProxy(networkProxyConfiguration); configureNetworkProxyAction.setProxy(networkProxy);
// test // test
actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
@ -143,8 +144,10 @@ public class ConfigureNetworkProxyActionTest extends TestCase
public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception public void testStruts2ValidationFrameworkWithInvalidInputs() throws Exception
{ {
// prep // prep
NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT, PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT, PROXY_USERNAME_INVALID_INPUT); NetworkProxy networkProxy = createNetworkProxy( PROXY_ID_INVALID_INPUT, PROXY_HOST_INVALID_INPUT,
configureNetworkProxyAction.setProxy(networkProxyConfiguration); PROXY_PORT_INVALID_INPUT, PROXY_PROTOCOL_INVALID_INPUT,
PROXY_USERNAME_INVALID_INPUT );
configureNetworkProxyAction.setProxy(networkProxy);
// test // test
actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
@ -184,8 +187,8 @@ public class ConfigureNetworkProxyActionTest extends TestCase
public void testStruts2ValidationFrameworkWithValidInputs() throws Exception public void testStruts2ValidationFrameworkWithValidInputs() throws Exception
{ {
// prep // prep
NetworkProxyConfiguration networkProxyConfiguration = createNetworkProxyConfiguration(PROXY_ID_VALID_INPUT, PROXY_HOST_VALID_INPUT, PROXY_PORT_VALID_INPUT, PROXY_PROTOCOL_VALID_INPUT, PROXY_USERNAME_VALID_INPUT); 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(networkProxyConfiguration); configureNetworkProxyAction.setProxy(networkProxy);
// test // test
actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT); actionValidatorManager.validate(configureNetworkProxyAction, VALIDATION_CONTEXT);
@ -194,26 +197,26 @@ public class ConfigureNetworkProxyActionTest extends TestCase
assertFalse(configureNetworkProxyAction.hasFieldErrors()); 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(); NetworkProxy networkProxy = new NetworkProxy();
networkProxyConfiguration.setId(id); networkProxy.setId( id );
networkProxyConfiguration.setHost(host); networkProxy.setHost( host );
networkProxyConfiguration.setPort(port); networkProxy.setPort( port );
networkProxyConfiguration.setProtocol(protocol); networkProxy.setProtocol( protocol );
networkProxyConfiguration.setUsername(username); networkProxy.setUsername( username );
return networkProxyConfiguration; return networkProxy;
} }
// over-loaded // over-loaded
// for simulating empty/null form purposes; excluding primitive data-typed values // 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(); NetworkProxy networkProxy = new NetworkProxy();
networkProxyConfiguration.setId(id); networkProxy.setId( id );
networkProxyConfiguration.setHost(host); networkProxy.setHost( host );
networkProxyConfiguration.setProtocol(protocol); networkProxy.setProtocol( protocol );
networkProxyConfiguration.setUsername(username); networkProxy.setUsername( username );
return networkProxyConfiguration; return networkProxy;
} }
} }