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;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
26 import org.apache.maven.archiva.policies.DownloadPolicy;
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.List;
36 * AbstractProxyConnectorFormAction - generic fields and methods for either add or edit actions related with the
39 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
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 * The list of network proxy ids that are available.
60 private List<String> proxyIdOptions;
63 * The list of managed repository ids that are available.
65 private List<String> managedRepoIdList;
68 * The list of remove repository ids that are available.
70 private List<String> remoteRepoIdList;
73 * The map of policies that are available to be set.
75 private Map<String, DownloadPolicy> policyMap;
78 * The property key to add or remove.
80 private String propertyKey;
83 * The property value to add.
85 private String propertyValue;
88 * The blacklist pattern to add.
90 private String blackListPattern;
93 * The whitelist pattern to add.
95 private String whiteListPattern;
98 * The pattern to add or remove (black or white).
100 private String pattern;
103 * The model for this action.
105 protected ProxyConnectorConfiguration connector;
107 public String addBlackListPattern()
109 String pattern = getBlackListPattern();
111 if ( StringUtils.isBlank( pattern ) )
113 addActionError( "Cannot add a blank black list pattern." );
116 if ( !hasActionErrors() )
118 getConnector().getBlackListPatterns().add( pattern );
119 setBlackListPattern( null );
125 public String addProperty()
127 String key = getPropertyKey();
128 String value = getPropertyValue();
130 if ( StringUtils.isBlank( key ) )
132 addActionError( "Unable to add property with blank key." );
135 if ( StringUtils.isBlank( value ) )
137 addActionError( "Unable to add property with blank value." );
140 if ( !hasActionErrors() )
142 getConnector().getProperties().put( key, value );
143 setPropertyKey( null );
144 setPropertyValue( null );
150 public String addWhiteListPattern()
152 String pattern = getWhiteListPattern();
154 if ( StringUtils.isBlank( pattern ) )
156 addActionError( "Cannot add a blank white list pattern." );
159 if ( !hasActionErrors() )
161 getConnector().getWhiteListPatterns().add( pattern );
162 setWhiteListPattern( null );
168 public String getBlackListPattern()
170 return blackListPattern;
173 public ProxyConnectorConfiguration getConnector()
178 public List<String> getManagedRepoIdList()
180 return managedRepoIdList;
183 public String getPattern()
188 public Map<String, DownloadPolicy> getPolicyMap()
193 public String getPropertyKey()
198 public String getPropertyValue()
200 return propertyValue;
203 public List<String> getProxyIdOptions()
205 return proxyIdOptions;
208 public List<String> getRemoteRepoIdList()
210 return remoteRepoIdList;
213 public String getWhiteListPattern()
215 return whiteListPattern;
218 public void prepare()
220 proxyIdOptions = createNetworkProxyOptions();
221 managedRepoIdList = createManagedRepoOptions();
222 remoteRepoIdList = createRemoteRepoOptions();
223 policyMap = createPolicyMap();
226 public String removeBlackListPattern()
228 String pattern = getPattern();
230 if ( StringUtils.isBlank( pattern ) )
232 addActionError( "Cannot remove a blank black list pattern." );
235 if ( !getConnector().getBlackListPatterns().contains( pattern ) )
237 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
240 if ( !hasActionErrors() )
242 getConnector().getBlackListPatterns().remove( pattern );
245 setBlackListPattern( null );
251 public String removeProperty()
253 String key = getPropertyKey();
255 if ( StringUtils.isBlank( key ) )
257 addActionError( "Unable to remove property with blank key." );
260 if ( !getConnector().getProperties().containsKey( key ) )
262 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
265 if ( !hasActionErrors() )
267 getConnector().getProperties().remove( key );
270 setPropertyKey( null );
271 setPropertyValue( null );
276 public String removeWhiteListPattern()
278 String pattern = getPattern();
280 if ( StringUtils.isBlank( pattern ) )
282 addActionError( "Cannot remove a blank white list pattern." );
285 if ( !getConnector().getWhiteListPatterns().contains( pattern ) )
287 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
290 if ( !hasActionErrors() )
292 getConnector().getWhiteListPatterns().remove( pattern );
295 setWhiteListPattern( null );
301 public void setBlackListPattern( String blackListPattern )
303 this.blackListPattern = blackListPattern;
306 public void setConnector( ProxyConnectorConfiguration connector )
308 this.connector = connector;
311 public void setManagedRepoIdList( List<String> managedRepoIdList )
313 this.managedRepoIdList = managedRepoIdList;
316 public void setPattern( String pattern )
318 this.pattern = pattern;
321 public void setPolicyMap( Map<String, DownloadPolicy> policyMap )
323 this.policyMap = policyMap;
326 public void setPropertyKey( String propertyKey )
328 this.propertyKey = propertyKey;
331 public void setPropertyValue( String propertyValue )
333 this.propertyValue = propertyValue;
336 public void setProxyIdOptions( List<String> proxyIdOptions )
338 this.proxyIdOptions = proxyIdOptions;
341 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
343 this.remoteRepoIdList = remoteRepoIdList;
346 public void setWhiteListPattern( String whiteListPattern )
348 this.whiteListPattern = whiteListPattern;
351 protected List<String> createManagedRepoOptions()
353 return new ArrayList<String>( getConfig().getManagedRepositoriesAsMap().keySet() );
356 protected List<String> createNetworkProxyOptions()
358 List<String> options = new ArrayList<String>();
360 options.add( DIRECT_CONNECTION );
361 options.addAll( getConfig().getNetworkProxiesAsMap().keySet() );
366 protected Map<String, DownloadPolicy> createPolicyMap()
368 Map<String, DownloadPolicy> policyMap = new HashMap<String, DownloadPolicy>();
370 policyMap.putAll( preDownloadPolicyMap );
371 policyMap.putAll( postDownloadPolicyMap );
376 protected List<String> createRemoteRepoOptions()
378 return new ArrayList<String>( getConfig().getRemoteRepositoriesAsMap().keySet() );
381 protected void validateConnector()
383 if ( connector.getPolicies() == null )
385 addActionError( "Policies must be set." );
389 // Validate / Fix policy settings arriving from browser.
390 for ( Map.Entry<String, DownloadPolicy> entry : getPolicyMap().entrySet() )
392 String policyId = (String) entry.getKey();
393 DownloadPolicy policy = (DownloadPolicy) entry.getValue();
394 List<String> options = policy.getOptions();
396 if ( !connector.getPolicies().containsKey( policyId ) )
398 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
402 // Ugly hack to compensate for ugly browsers.
403 Object o = connector.getPolicies().get( policyId );
405 if ( o.getClass().isArray() )
407 String arr[] = (String[]) o;
415 connector.getPolicies().put( policyId, value );
417 if ( StringUtils.isBlank( value ) )
419 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
423 if ( !options.contains( value ) )
425 addActionError( "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: "