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 com.opensymphony.xwork2.Validateable;
24 import org.apache.commons.collections.CollectionUtils;
25 import org.apache.commons.collections.functors.NotPredicate;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.maven.archiva.configuration.NetworkProxyConfiguration;
31 import org.apache.maven.archiva.configuration.functors.NetworkProxySelectionPredicate;
32 import org.apache.archiva.security.ArchivaRoleConstants;
33 import org.apache.maven.archiva.web.action.AbstractActionSupport;
34 import org.codehaus.plexus.redback.rbac.Resource;
35 import org.codehaus.plexus.registry.RegistryException;
36 import org.codehaus.redback.integration.interceptor.SecureAction;
37 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
38 import org.codehaus.redback.integration.interceptor.SecureActionException;
39 import org.springframework.context.annotation.Scope;
40 import org.springframework.stereotype.Controller;
42 import javax.inject.Inject;
45 * ConfigureNetworkProxyAction
48 * plexus.component role="com.opensymphony.xwork2.Action" role-hint="configureNetworkProxyAction" instantiation-strategy="per-lookup"
50 @Controller( "configureNetworkProxyAction" )
52 public class ConfigureNetworkProxyAction
53 extends AbstractActionSupport
54 implements SecureAction, Preparable, Validateable
60 private ArchivaConfiguration archivaConfiguration;
64 private String proxyid;
66 private NetworkProxyConfiguration proxy;
74 public String confirm()
79 public String delete()
81 Configuration config = archivaConfiguration.getConfiguration();
83 String id = getProxyid();
84 if ( StringUtils.isBlank( id ) )
86 addActionError( "Unable to delete network proxy with blank id." );
90 NetworkProxySelectionPredicate networkProxySelection = new NetworkProxySelectionPredicate( id );
91 NetworkProxyConfiguration proxyConfig =
92 (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), networkProxySelection );
93 if ( proxyConfig == null )
95 addActionError( "Unable to remove network proxy, proxy with id [" + id + "] not found." );
99 archivaConfiguration.getConfiguration().removeNetworkProxy( proxyConfig );
100 addActionMessage( "Successfully removed network proxy [" + id + "]" );
101 return saveConfiguration();
110 public String getMode()
115 public NetworkProxyConfiguration getProxy()
120 public String getProxyid()
125 public SecureActionBundle getSecureActionBundle()
126 throws SecureActionException
128 SecureActionBundle bundle = new SecureActionBundle();
130 bundle.setRequiresAuthentication( true );
131 bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION, Resource.GLOBAL );
136 public String input()
141 public void prepare()
144 String id = getProxyid();
146 if ( StringUtils.isNotBlank( id ) )
148 proxy = findNetworkProxy( id );
153 proxy = new NetworkProxyConfiguration();
159 String mode = getMode();
161 String id = getProxy().getId();
163 if ( StringUtils.equalsIgnoreCase( "edit", mode ) )
165 removeNetworkProxy( id );
169 if ( findNetworkProxy( id ) != null )
171 addActionError( "Unable to add new repository with id [" + id + "], that id already exists." );
176 addNetworkProxy( getProxy() );
177 return saveConfiguration();
180 public void validate()
182 // trim all unecessary trailing/leading white-spaces; always put this statement before the closing braces(after all validation).
183 trimAllRequestParameterValues();
186 public void setMode( String mode )
191 public void setProxy( NetworkProxyConfiguration proxy )
196 public void setProxyid( String proxyid )
198 this.proxyid = proxyid;
201 private void addNetworkProxy( NetworkProxyConfiguration proxy )
203 archivaConfiguration.getConfiguration().addNetworkProxy( proxy );
206 private NetworkProxyConfiguration findNetworkProxy( String id )
208 Configuration config = archivaConfiguration.getConfiguration();
210 NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
212 return (NetworkProxyConfiguration) CollectionUtils.find( config.getNetworkProxies(), selectedProxy );
215 private void removeNetworkProxy( String id )
217 NetworkProxySelectionPredicate selectedProxy = new NetworkProxySelectionPredicate( id );
218 NotPredicate notSelectedProxy = new NotPredicate( selectedProxy );
219 CollectionUtils.filter( archivaConfiguration.getConfiguration().getNetworkProxies(), notSelectedProxy );
222 private String saveConfiguration()
226 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
227 addActionMessage( "Successfully saved configuration" );
229 catch ( RegistryException e )
231 addActionError( "Unable to save configuration: " + e.getMessage() );
234 catch ( IndeterminateConfigurationException e )
236 addActionError( e.getMessage() );
243 private void trimAllRequestParameterValues()
245 if ( StringUtils.isNotEmpty( proxy.getId() ) )
247 proxy.setId( proxy.getId().trim() );
250 if ( StringUtils.isNotEmpty( proxy.getHost() ) )
252 proxy.setHost( proxy.getHost().trim() );
255 if ( StringUtils.isNotEmpty( proxy.getPassword() ) )
257 proxy.setPassword( proxy.getPassword().trim() );
260 if ( StringUtils.isNotEmpty( proxy.getProtocol() ) )
262 proxy.setProtocol( proxy.getProtocol().trim() );
265 if ( StringUtils.isNotEmpty( proxy.getUsername() ) )
267 proxy.setUsername( proxy.getUsername().trim() );