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 protected List<String> escapePatterns( List<String> patterns )
114 List<String> escapedPatterns = new ArrayList<String>();
115 if( patterns != null )
117 for( String pattern : patterns )
119 escapedPatterns.add( StringUtils.replace( pattern, "\\", "\\\\" ) );
123 return escapedPatterns;
126 protected List<String> unescapePatterns( List<String> patterns )
128 List<String> rawPatterns = new ArrayList<String>();
129 if( patterns != null )
131 for( String pattern : patterns )
133 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
140 private String escapePattern( String pattern )
142 return StringUtils.replace( pattern, "\\", "\\\\" );
145 public String addBlackListPattern()
147 String pattern = getBlackListPattern();
149 if ( StringUtils.isBlank( pattern ) )
151 addActionError( "Cannot add a blank black list pattern." );
154 if ( !hasActionErrors() )
156 getConnector().getBlackListPatterns().add( escapePattern( pattern ) );
157 setBlackListPattern( null );
163 public String addProperty()
165 String key = getPropertyKey();
166 String value = getPropertyValue();
168 if ( StringUtils.isBlank( key ) )
170 addActionError( "Unable to add property with blank key." );
173 if ( StringUtils.isBlank( value ) )
175 addActionError( "Unable to add property with blank value." );
178 if ( !hasActionErrors() )
180 getConnector().getProperties().put( key, value );
181 setPropertyKey( null );
182 setPropertyValue( null );
188 public String addWhiteListPattern()
190 String pattern = getWhiteListPattern();
192 if ( StringUtils.isBlank( pattern ) )
194 addActionError( "Cannot add a blank white list pattern." );
197 if ( !hasActionErrors() )
199 getConnector().getWhiteListPatterns().add( escapePattern( pattern ) );
200 setWhiteListPattern( null );
206 public String getBlackListPattern()
208 return blackListPattern;
211 public ProxyConnectorConfiguration getConnector()
216 public List<String> getManagedRepoIdList()
218 return managedRepoIdList;
221 public String getPattern()
226 public Map<String, Policy> getPolicyMap()
231 public String getPropertyKey()
236 public String getPropertyValue()
238 return propertyValue;
241 public List<String> getProxyIdOptions()
243 return proxyIdOptions;
246 public List<String> getRemoteRepoIdList()
248 return remoteRepoIdList;
251 public String getWhiteListPattern()
253 return whiteListPattern;
256 public void prepare()
258 proxyIdOptions = createNetworkProxyOptions();
259 managedRepoIdList = createManagedRepoOptions();
260 remoteRepoIdList = createRemoteRepoOptions();
261 policyMap = createPolicyMap();
264 public String removeBlackListPattern()
266 String pattern = getPattern();
268 if ( StringUtils.isBlank( pattern ) )
270 addActionError( "Cannot remove a blank black list pattern." );
273 if ( !getConnector().getBlackListPatterns().contains( pattern ) &&
274 !getConnector().getBlackListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
276 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
279 if ( !hasActionErrors() )
281 getConnector().getBlackListPatterns().remove( escapePattern( pattern ) );
284 setBlackListPattern( null );
290 public String removeProperty()
292 String key = getPropertyKey();
294 if ( StringUtils.isBlank( key ) )
296 addActionError( "Unable to remove property with blank key." );
299 if ( !getConnector().getProperties().containsKey( key ) )
301 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
304 if ( !hasActionErrors() )
306 getConnector().getProperties().remove( key );
309 setPropertyKey( null );
310 setPropertyValue( null );
315 public String removeWhiteListPattern()
317 String pattern = getPattern();
319 if ( StringUtils.isBlank( pattern ) )
321 addActionError( "Cannot remove a blank white list pattern." );
324 if ( !getConnector().getWhiteListPatterns().contains( pattern ) &&
325 !getConnector().getWhiteListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
327 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
330 if ( !hasActionErrors() )
332 getConnector().getWhiteListPatterns().remove( escapePattern( pattern ) );
335 setWhiteListPattern( null );
341 public void setBlackListPattern( String blackListPattern )
343 this.blackListPattern = blackListPattern;
346 public void setConnector( ProxyConnectorConfiguration connector )
348 this.connector = connector;
351 public void setManagedRepoIdList( List<String> managedRepoIdList )
353 this.managedRepoIdList = managedRepoIdList;
356 public void setPattern( String pattern )
358 this.pattern = pattern;
361 public void setPolicyMap( Map<String, Policy> policyMap )
363 this.policyMap = policyMap;
366 public void setPropertyKey( String propertyKey )
368 this.propertyKey = propertyKey;
371 public void setPropertyValue( String propertyValue )
373 this.propertyValue = propertyValue;
376 public void setProxyIdOptions( List<String> proxyIdOptions )
378 this.proxyIdOptions = proxyIdOptions;
381 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
383 this.remoteRepoIdList = remoteRepoIdList;
386 public void setWhiteListPattern( String whiteListPattern )
388 this.whiteListPattern = whiteListPattern;
391 protected List<String> createManagedRepoOptions()
393 return new ArrayList<String>( getConfig().getManagedRepositoriesAsMap().keySet() );
396 protected List<String> createNetworkProxyOptions()
398 List<String> options = new ArrayList<String>();
400 options.add( DIRECT_CONNECTION );
401 options.addAll( getConfig().getNetworkProxiesAsMap().keySet() );
406 protected Map<String, Policy> createPolicyMap()
408 Map<String, Policy> policyMap = new HashMap<String, Policy>();
410 policyMap.putAll( preDownloadPolicyMap );
411 policyMap.putAll( postDownloadPolicyMap );
412 policyMap.putAll( downloadErrorPolicyMap );
417 protected List<String> createRemoteRepoOptions()
419 return new ArrayList<String>( getConfig().getRemoteRepositoriesAsMap().keySet() );
422 protected void validateConnector()
424 if ( connector.getPolicies() == null )
426 addActionError( "Policies must be set." );
430 // Validate / Fix policy settings arriving from browser.
431 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
433 String policyId = entry.getKey();
434 Policy policy = entry.getValue();
435 List<String> options = policy.getOptions();
437 if ( !connector.getPolicies().containsKey( policyId ) )
439 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
443 Map properties = connector.getProperties();
444 for ( Iterator j = properties.keySet().iterator(); j.hasNext(); )
446 String key = (String) j.next();
448 Object value = properties.get( key );
449 if ( value.getClass().isArray() )
451 String[] arr = (String[]) value;
452 properties.put( key, arr[0] );
456 // Ugly hack to compensate for ugly browsers.
457 Object o = connector.getPolicies().get( policyId );
459 if ( o.getClass().isArray() )
461 String arr[] = (String[]) o;
469 connector.getPolicies().put( policyId, value );
471 if ( StringUtils.isBlank( value ) )
473 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
477 if ( !options.contains( value ) )
479 addActionError( "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: "