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.xwork.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.codehaus.plexus.redback.rbac.Resource;
33 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.codehaus.plexus.xwork.action.PlexusActionSupport;
40 * ConfigureNetworkProxyAction
42 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
44 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="configureNetworkProxyAction"
46 public class ConfigureNetworkProxyAction
47 extends PlexusActionSupport
48 implements SecureAction, Preparable
53 private ArchivaConfiguration archivaConfiguration;
57 private String proxyid;
59 private NetworkProxyConfiguration proxy;
67 public String confirm()
72 public String delete()
74 Configuration config = archivaConfiguration.getConfiguration();
76 String id = getProxyid();
77 if ( StringUtils.isBlank( id ) )
79 addActionError( "Unable to delete network proxy with blank id." );
83 NetworkProxySelectionPredicate networkProxySelection = new NetworkProxySelectionPredicate( id );
84 NetworkProxyConfiguration proxyConfig = (NetworkProxyConfiguration) CollectionUtils.find( config
85 .getNetworkProxies(), networkProxySelection );
86 if ( proxyConfig == null )
88 addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." );
92 archivaConfiguration.getConfiguration().removeNetworkProxy( proxyConfig );
93 addActionMessage( "Successfully removed network proxy [" + id + "]" );
94 return saveConfiguration();
103 public String getMode()
108 public NetworkProxyConfiguration getProxy()
113 public String getProxyid()
118 public SecureActionBundle getSecureActionBundle()
119 throws SecureActionException
121 SecureActionBundle bundle = new SecureActionBundle();
123 bundle.setRequiresAuthentication( true );
124 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
129 public String input()
134 public void prepare()
137 String id = getProxyid();
139 if ( StringUtils.isNotBlank( id ) )
141 proxy = findNetworkProxy( id );
146 proxy = new NetworkProxyConfiguration();
152 String mode = getMode();
154 String id = getProxy().getId();
156 if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
158 removeNetworkProxy( id );
162 if ( findNetworkProxy( id ) != null )
164 addActionError( "Unable to add new repository with id [" + id + "], that id already exists." );
169 addNetworkProxy( getProxy() );
170 return saveConfiguration();
173 public void setMode( String mode )
178 public void setProxy( NetworkProxyConfiguration proxy )
183 public void setProxyid( String proxyid )
185 this.proxyid = proxyid;
188 private void addNetworkProxy( NetworkProxyConfiguration proxy )
190 archivaConfiguration.getConfiguration().addNetworkProxy( proxy );
193 private NetworkProxyConfiguration findNetworkProxy( String id )
195 Configuration config = archivaConfiguration.getConfiguration();
197 NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
199 return (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), selectedProxy );
202 private void removeNetworkProxy( String id )
204 NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
205 NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
206 CollectionUtils.filter( archivaConfiguration.getConfiguration().getNetworkProxies(), notSelectedProxy );
209 private String saveConfiguration()
213 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
214 addActionMessage( "Successfully saved configuration" );
216 catch ( RegistryException e )
218 addActionError( "Unable to save configuration: " + e.getMessage() );
221 catch ( IndeterminateConfigurationException e )
223 addActionError( e.getMessage() );