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.xwork.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
40 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
43 public abstract class AbstractProxyConnectorFormAction
44 extends AbstractProxyConnectorAction
49 * @plexus.requirement role="org.apache.maven.archiva.policies.PreDownloadPolicy"
51 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
54 * @plexus.requirement role="org.apache.maven.archiva.policies.PostDownloadPolicy"
56 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
59 * @plexus.requirement role="org.apache.maven.archiva.policies.DownloadErrorPolicy"
61 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
64 * The list of network proxy ids that are available.
66 private List<String> proxyIdOptions;
69 * The list of managed repository ids that are available.
71 private List<String> managedRepoIdList;
74 * The list of remove repository ids that are available.
76 private List<String> remoteRepoIdList;
79 * The map of policies that are available to be set.
81 private Map<String, Policy> policyMap;
84 * The property key to add or remove.
86 private String propertyKey;
89 * The property value to add.
91 private String propertyValue;
94 * The blacklist pattern to add.
96 private String blackListPattern;
99 * The whitelist pattern to add.
101 private String whiteListPattern;
104 * The pattern to add or remove (black or white).
106 private String pattern;
109 * The model for this action.
111 protected ProxyConnectorConfiguration connector;
113 public String addBlackListPattern()
115 String pattern = getBlackListPattern();
117 if ( StringUtils.isBlank( pattern ) )
119 addActionError( "Cannot add a blank black list pattern." );
122 if ( !hasActionErrors() )
124 getConnector().getBlackListPatterns().add( pattern );
125 setBlackListPattern( null );
131 public String addProperty()
133 String key = getPropertyKey();
134 String value = getPropertyValue();
136 if ( StringUtils.isBlank( key ) )
138 addActionError( "Unable to add property with blank key." );
141 if ( StringUtils.isBlank( value ) )
143 addActionError( "Unable to add property with blank value." );
146 if ( !hasActionErrors() )
148 getConnector().getProperties().put( key, value );
149 setPropertyKey( null );
150 setPropertyValue( null );
156 public String addWhiteListPattern()
158 String pattern = getWhiteListPattern();
160 if ( StringUtils.isBlank( pattern ) )
162 addActionError( "Cannot add a blank white list pattern." );
165 if ( !hasActionErrors() )
167 getConnector().getWhiteListPatterns().add( pattern );
168 setWhiteListPattern( null );
174 public String getBlackListPattern()
176 return blackListPattern;
179 public ProxyConnectorConfiguration getConnector()
184 public List<String> getManagedRepoIdList()
186 return managedRepoIdList;
189 public String getPattern()
194 public Map<String, Policy> getPolicyMap()
199 public String getPropertyKey()
204 public String getPropertyValue()
206 return propertyValue;
209 public List<String> getProxyIdOptions()
211 return proxyIdOptions;
214 public List<String> getRemoteRepoIdList()
216 return remoteRepoIdList;
219 public String getWhiteListPattern()
221 return whiteListPattern;
224 public void prepare()
226 proxyIdOptions = createNetworkProxyOptions();
227 managedRepoIdList = createManagedRepoOptions();
228 remoteRepoIdList = createRemoteRepoOptions();
229 policyMap = createPolicyMap();
232 public String removeBlackListPattern()
234 String pattern = getPattern();
236 if ( StringUtils.isBlank( pattern ) )
238 addActionError( "Cannot remove a blank black list pattern." );
241 if ( !getConnector().getBlackListPatterns().contains( pattern ) )
243 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
246 if ( !hasActionErrors() )
248 getConnector().getBlackListPatterns().remove( pattern );
251 setBlackListPattern( null );
257 public String removeProperty()
259 String key = getPropertyKey();
261 if ( StringUtils.isBlank( key ) )
263 addActionError( "Unable to remove property with blank key." );
266 if ( !getConnector().getProperties().containsKey( key ) )
268 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
271 if ( !hasActionErrors() )
273 getConnector().getProperties().remove( key );
276 setPropertyKey( null );
277 setPropertyValue( null );
282 public String removeWhiteListPattern()
284 String pattern = getPattern();
286 if ( StringUtils.isBlank( pattern ) )
288 addActionError( "Cannot remove a blank white list pattern." );
291 if ( !getConnector().getWhiteListPatterns().contains( pattern ) )
293 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
296 if ( !hasActionErrors() )
298 getConnector().getWhiteListPatterns().remove( pattern );
301 setWhiteListPattern( null );
307 public void setBlackListPattern( String blackListPattern )
309 this.blackListPattern = blackListPattern;
312 public void setConnector( ProxyConnectorConfiguration connector )
314 this.connector = connector;
317 public void setManagedRepoIdList( List<String> managedRepoIdList )
319 this.managedRepoIdList = managedRepoIdList;
322 public void setPattern( String pattern )
324 this.pattern = pattern;
327 public void setPolicyMap( Map<String, Policy> policyMap )
329 this.policyMap = policyMap;
332 public void setPropertyKey( String propertyKey )
334 this.propertyKey = propertyKey;
337 public void setPropertyValue( String propertyValue )
339 this.propertyValue = propertyValue;
342 public void setProxyIdOptions( List<String> proxyIdOptions )
344 this.proxyIdOptions = proxyIdOptions;
347 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
349 this.remoteRepoIdList = remoteRepoIdList;
352 public void setWhiteListPattern( String whiteListPattern )
354 this.whiteListPattern = whiteListPattern;
357 protected List<String> createManagedRepoOptions()
359 return new ArrayList<String>( getConfig().getManagedRepositoriesAsMap().keySet() );
362 protected List<String> createNetworkProxyOptions()
364 List<String> options = new ArrayList<String>();
366 options.add( DIRECT_CONNECTION );
367 options.addAll( getConfig().getNetworkProxiesAsMap().keySet() );
372 protected Map<String, Policy> createPolicyMap()
374 Map<String, Policy> policyMap = new HashMap<String, Policy>();
376 policyMap.putAll( preDownloadPolicyMap );
377 policyMap.putAll( postDownloadPolicyMap );
378 policyMap.putAll( downloadErrorPolicyMap );
383 protected List<String> createRemoteRepoOptions()
385 return new ArrayList<String>( getConfig().getRemoteRepositoriesAsMap().keySet() );
388 protected void validateConnector()
390 if ( connector.getPolicies() == null )
392 addActionError( "Policies must be set." );
396 // Validate / Fix policy settings arriving from browser.
397 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
399 String policyId = entry.getKey();
400 Policy policy = entry.getValue();
401 List<String> options = policy.getOptions();
403 if ( !connector.getPolicies().containsKey( policyId ) )
405 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
409 Map properties = connector.getProperties();
410 for ( Iterator j = properties.keySet().iterator(); j.hasNext(); )
412 String key = (String) j.next();
414 Object value = properties.get( key );
415 if ( value.getClass().isArray() )
417 String[] arr = (String[]) value;
418 properties.put( key, arr[0] );
422 // Ugly hack to compensate for ugly browsers.
423 Object o = connector.getPolicies().get( policyId );
425 if ( o.getClass().isArray() )
427 String arr[] = (String[]) o;
435 connector.getPolicies().put( policyId, value );
437 if ( StringUtils.isBlank( value ) )
439 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
443 if ( !options.contains( value ) )
445 addActionError( "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: "