1 package org.apache.maven.archiva.web.action.admin.connectors.proxy;
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.lang.StringUtils;
24 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
25 import org.apache.maven.archiva.policies.DownloadErrorPolicy;
26 import org.apache.maven.archiva.policies.Policy;
27 import org.apache.maven.archiva.policies.PostDownloadPolicy;
28 import org.apache.maven.archiva.policies.PreDownloadPolicy;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
37 * AbstractProxyConnectorFormAction - generic fields and methods for either add or edit actions related with the
42 public abstract class AbstractProxyConnectorFormAction
43 extends AbstractProxyConnectorAction
48 * @plexus.requirement role="org.apache.maven.archiva.policies.PreDownloadPolicy"
50 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
53 * @plexus.requirement role="org.apache.maven.archiva.policies.PostDownloadPolicy"
55 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
58 * @plexus.requirement role="org.apache.maven.archiva.policies.DownloadErrorPolicy"
60 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
63 * The list of network proxy ids that are available.
65 private List<String> proxyIdOptions;
68 * The list of managed repository ids that are available.
70 private List<String> managedRepoIdList;
73 * The list of remove repository ids that are available.
75 private List<String> remoteRepoIdList;
78 * The map of policies that are available to be set.
80 private Map<String, Policy> policyMap;
83 * The property key to add or remove.
85 private String propertyKey;
88 * The property value to add.
90 private String propertyValue;
93 * The blacklist pattern to add.
95 private String blackListPattern;
98 * The whitelist pattern to add.
100 private String whiteListPattern;
103 * The pattern to add or remove (black or white).
105 private String pattern;
108 * The model for this action.
110 protected ProxyConnectorConfiguration connector;
112 public String addBlackListPattern()
114 String pattern = getBlackListPattern();
116 if ( StringUtils.isBlank( pattern ) )
118 addActionError( "Cannot add a blank black list pattern." );
121 if ( !hasActionErrors() )
123 getConnector().getBlackListPatterns().add( pattern );
124 setBlackListPattern( null );
130 public String addProperty()
132 String key = getPropertyKey();
133 String value = getPropertyValue();
135 if ( StringUtils.isBlank( key ) )
137 addActionError( "Unable to add property with blank key." );
140 if ( StringUtils.isBlank( value ) )
142 addActionError( "Unable to add property with blank value." );
145 if ( !hasActionErrors() )
147 getConnector().getProperties().put( key, value );
148 setPropertyKey( null );
149 setPropertyValue( null );
155 public String addWhiteListPattern()
157 String pattern = getWhiteListPattern();
159 if ( StringUtils.isBlank( pattern ) )
161 addActionError( "Cannot add a blank white list pattern." );
164 if ( !hasActionErrors() )
166 getConnector().getWhiteListPatterns().add( pattern );
167 setWhiteListPattern( null );
173 public String getBlackListPattern()
175 return blackListPattern;
178 public ProxyConnectorConfiguration getConnector()
183 public List<String> getManagedRepoIdList()
185 return managedRepoIdList;
188 public String getPattern()
193 public Map<String, Policy> getPolicyMap()
198 public String getPropertyKey()
203 public String getPropertyValue()
205 return propertyValue;
208 public List<String> getProxyIdOptions()
210 return proxyIdOptions;
213 public List<String> getRemoteRepoIdList()
215 return remoteRepoIdList;
218 public String getWhiteListPattern()
220 return whiteListPattern;
223 public void prepare()
225 proxyIdOptions = createNetworkProxyOptions();
226 managedRepoIdList = createManagedRepoOptions();
227 remoteRepoIdList = createRemoteRepoOptions();
228 policyMap = createPolicyMap();
231 public String removeBlackListPattern()
233 String pattern = getPattern();
235 if ( StringUtils.isBlank( pattern ) )
237 addActionError( "Cannot remove a blank black list pattern." );
240 if ( !getConnector().getBlackListPatterns().contains( pattern ) )
242 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
245 if ( !hasActionErrors() )
247 getConnector().getBlackListPatterns().remove( pattern );
250 setBlackListPattern( null );
256 public String removeProperty()
258 String key = getPropertyKey();
260 if ( StringUtils.isBlank( key ) )
262 addActionError( "Unable to remove property with blank key." );
265 if ( !getConnector().getProperties().containsKey( key ) )
267 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
270 if ( !hasActionErrors() )
272 getConnector().getProperties().remove( key );
275 setPropertyKey( null );
276 setPropertyValue( null );
281 public String removeWhiteListPattern()
283 String pattern = getPattern();
285 if ( StringUtils.isBlank( pattern ) )
287 addActionError( "Cannot remove a blank white list pattern." );
290 if ( !getConnector().getWhiteListPatterns().contains( pattern ) )
292 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
295 if ( !hasActionErrors() )
297 getConnector().getWhiteListPatterns().remove( pattern );
300 setWhiteListPattern( null );
306 public void setBlackListPattern( String blackListPattern )
308 this.blackListPattern = blackListPattern;
311 public void setConnector( ProxyConnectorConfiguration connector )
313 this.connector = connector;
316 public void setManagedRepoIdList( List<String> managedRepoIdList )
318 this.managedRepoIdList = managedRepoIdList;
321 public void setPattern( String pattern )
323 this.pattern = pattern;
326 public void setPolicyMap( Map<String, Policy> policyMap )
328 this.policyMap = policyMap;
331 public void setPropertyKey( String propertyKey )
333 this.propertyKey = propertyKey;
336 public void setPropertyValue( String propertyValue )
338 this.propertyValue = propertyValue;
341 public void setProxyIdOptions( List<String> proxyIdOptions )
343 this.proxyIdOptions = proxyIdOptions;
346 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
348 this.remoteRepoIdList = remoteRepoIdList;
351 public void setWhiteListPattern( String whiteListPattern )
353 this.whiteListPattern = whiteListPattern;
356 protected List<String> createManagedRepoOptions()
358 return new ArrayList<String>( getConfig().getManagedRepositoriesAsMap().keySet() );
361 protected List<String> createNetworkProxyOptions()
363 List<String> options = new ArrayList<String>();
365 options.add( DIRECT_CONNECTION );
366 options.addAll( getConfig().getNetworkProxiesAsMap().keySet() );
371 protected Map<String, Policy> createPolicyMap()
373 Map<String, Policy> policyMap = new HashMap<String, Policy>();
375 policyMap.putAll( preDownloadPolicyMap );
376 policyMap.putAll( postDownloadPolicyMap );
377 policyMap.putAll( downloadErrorPolicyMap );
382 protected List<String> createRemoteRepoOptions()
384 return new ArrayList<String>( getConfig().getRemoteRepositoriesAsMap().keySet() );
387 protected void validateConnector()
389 if ( connector.getPolicies() == null )
391 addActionError( "Policies must be set." );
395 // Validate / Fix policy settings arriving from browser.
396 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
398 String policyId = entry.getKey();
399 Policy policy = entry.getValue();
400 List<String> options = policy.getOptions();
402 if ( !connector.getPolicies().containsKey( policyId ) )
404 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
408 Map properties = connector.getProperties();
409 for ( Iterator j = properties.keySet().iterator(); j.hasNext(); )
411 String key = (String) j.next();
413 Object value = properties.get( key );
414 if ( value.getClass().isArray() )
416 String[] arr = (String[]) value;
417 properties.put( key, arr[0] );
421 // Ugly hack to compensate for ugly browsers.
422 Object o = connector.getPolicies().get( policyId );
424 if ( o.getClass().isArray() )
426 String arr[] = (String[]) o;
434 connector.getPolicies().put( policyId, value );
436 if ( StringUtils.isBlank( value ) )
438 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
442 if ( !options.contains( value ) )
444 addActionError( "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: "