1 package org.apache.maven.archiva.web.action.admin.networkproxies;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork2.Preparable;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections.functors.NotPredicate;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
30 import org.apache.maven.archiva.configuration.functors.NetworkProxySelectionPredicate;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.apache.maven.archiva.web.action.PlexusActionSupport;
33 import org.codehaus.plexus.redback.rbac.Resource;
34 import org.codehaus.plexus.registry.RegistryException;
35 import org.codehaus.redback.integration.interceptor.SecureAction;
36 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
37 import org.codehaus.redback.integration.interceptor.SecureActionException;
40 * ConfigureNetworkProxyAction
43 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="configureNetworkProxyAction" instantiation-strategy="per-lookup"
45 public class ConfigureNetworkProxyAction
46 extends PlexusActionSupport
47 implements SecureAction, Preparable
52 private ArchivaConfiguration archivaConfiguration;
56 private String proxyid;
58 private NetworkProxyConfiguration proxy;
66 public String confirm()
71 public String delete()
73 Configuration config = archivaConfiguration.getConfiguration();
75 String id = getProxyid();
76 if ( StringUtils.isBlank( id ) )
78 addActionError( "Unable to delete network proxy with blank id." );
82 NetworkProxySelectionPredicate networkProxySelection = new NetworkProxySelectionPredicate( id );
83 NetworkProxyConfiguration proxyConfig = (NetworkProxyConfiguration) CollectionUtils.find( config
84 .getNetworkProxies(), networkProxySelection );
85 if ( proxyConfig == null )
87 addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." );
91 archivaConfiguration.getConfiguration().removeNetworkProxy( proxyConfig );
92 addActionMessage( "Successfully removed network proxy [" + id + "]" );
93 return saveConfiguration();
102 public String getMode()
107 public NetworkProxyConfiguration getProxy()
112 public String getProxyid()
117 public SecureActionBundle getSecureActionBundle()
118 throws SecureActionException
120 SecureActionBundle bundle = new SecureActionBundle();
122 bundle.setRequiresAuthentication( true );
123 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
128 public String input()
133 public void prepare()
136 String id = getProxyid();
138 if ( StringUtils.isNotBlank( id ) )
140 proxy = findNetworkProxy( id );
145 proxy = new NetworkProxyConfiguration();
151 String mode = getMode();
153 String id = getProxy().getId();
155 if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
157 removeNetworkProxy( id );
161 if ( findNetworkProxy( id ) != null )
163 addActionError( "Unable to add new repository with id [" + id + "], that id already exists." );
168 addNetworkProxy( getProxy() );
169 return saveConfiguration();
172 public void setMode( String mode )
177 public void setProxy( NetworkProxyConfiguration proxy )
182 public void setProxyid( String proxyid )
184 this.proxyid = proxyid;
187 private void addNetworkProxy( NetworkProxyConfiguration proxy )
189 archivaConfiguration.getConfiguration().addNetworkProxy( proxy );
192 private NetworkProxyConfiguration findNetworkProxy( String id )
194 Configuration config = archivaConfiguration.getConfiguration();
196 NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
198 return (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), selectedProxy );
201 private void removeNetworkProxy( String id )
203 NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
204 NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
205 CollectionUtils.filter( archivaConfiguration.getConfiguration().getNetworkProxies(), notSelectedProxy );
208 private String saveConfiguration()
212 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
213 addActionMessage( "Successfully saved configuration" );
215 catch ( RegistryException e )
217 addActionError( "Unable to save configuration: " + e.getMessage() );
220 catch ( IndeterminateConfigurationException e )
222 addActionError( e.getMessage() );