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 java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
27 import com.google.common.collect.Lists;
28 import org.apache.archiva.audit.AuditListener;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
31 import org.apache.maven.archiva.policies.DownloadErrorPolicy;
32 import org.apache.maven.archiva.policies.Policy;
33 import org.apache.maven.archiva.policies.PostDownloadPolicy;
34 import org.apache.maven.archiva.policies.PreDownloadPolicy;
36 import com.opensymphony.xwork2.Preparable;
38 import javax.annotation.PostConstruct;
41 * AbstractProxyConnectorFormAction - generic fields and methods for either add or edit actions related with the
46 public abstract class AbstractProxyConnectorFormAction
47 extends AbstractProxyConnectorAction
52 * plexus.requirement role="org.apache.maven.archiva.policies.PreDownloadPolicy"
54 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
57 * plexus.requirement role="org.apache.maven.archiva.policies.PostDownloadPolicy"
59 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
62 * plexus.requirement role="org.apache.maven.archiva.policies.DownloadErrorPolicy"
64 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
67 * The list of network proxy ids that are available.
69 private List<String> proxyIdOptions;
72 * The list of managed repository ids that are available.
74 private List<String> managedRepoIdList;
77 * The list of remove repository ids that are available.
79 private List<String> remoteRepoIdList;
82 * The map of policies that are available to be set.
84 private Map<String, Policy> policyMap;
87 * The property key to add or remove.
89 private String propertyKey;
92 * The property value to add.
94 private String propertyValue;
97 * The blacklist pattern to add.
99 private String blackListPattern;
102 * The whitelist pattern to add.
104 private String whiteListPattern;
107 * The pattern to add or remove (black or white).
109 private String pattern;
112 * The model for this action.
114 protected ProxyConnectorConfiguration connector;
118 public void initialize()
121 this.preDownloadPolicyMap = getBeansOfType( PreDownloadPolicy.class );
122 this.postDownloadPolicyMap = getBeansOfType( PostDownloadPolicy.class );
123 this.downloadErrorPolicyMap = getBeansOfType( DownloadErrorPolicy.class );
126 protected List<String> escapePatterns( List<String> patterns )
128 List<String> escapedPatterns = new ArrayList<String>();
129 if( patterns != null )
131 for( String pattern : patterns )
133 escapedPatterns.add( StringUtils.replace( pattern, "\\", "\\\\" ) );
137 return escapedPatterns;
140 protected List<String> unescapePatterns( List<String> patterns )
142 List<String> rawPatterns = new ArrayList<String>();
143 if( patterns != null )
145 for( String pattern : patterns )
147 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
154 private String escapePattern( String pattern )
156 return StringUtils.replace( pattern, "\\", "\\\\" );
159 public String addBlackListPattern()
161 String pattern = getBlackListPattern();
163 if ( StringUtils.isBlank( pattern ) )
165 addActionError( "Cannot add a blank black list pattern." );
168 if ( !hasActionErrors() )
170 getConnector().getBlackListPatterns().add( escapePattern( pattern ) );
171 setBlackListPattern( null );
177 @SuppressWarnings("unchecked")
178 public String addProperty()
180 String key = getPropertyKey();
181 String value = getPropertyValue();
183 if ( StringUtils.isBlank( key ) )
185 addActionError( "Unable to add property with blank key." );
188 if ( StringUtils.isBlank( value ) )
190 addActionError( "Unable to add property with blank value." );
193 if ( !hasActionErrors() )
195 getConnector().getProperties().put( key, value );
196 setPropertyKey( null );
197 setPropertyValue( null );
203 public String addWhiteListPattern()
205 String pattern = getWhiteListPattern();
207 if ( StringUtils.isBlank( pattern ) )
209 addActionError( "Cannot add a blank white list pattern." );
212 if ( !hasActionErrors() )
214 getConnector().getWhiteListPatterns().add( escapePattern( pattern ) );
215 setWhiteListPattern( null );
221 public String getBlackListPattern()
223 return blackListPattern;
226 public ProxyConnectorConfiguration getConnector()
231 public List<String> getManagedRepoIdList()
233 return managedRepoIdList;
236 public String getPattern()
241 public Map<String, Policy> getPolicyMap()
246 public String getPropertyKey()
251 public String getPropertyValue()
253 return propertyValue;
256 public List<String> getProxyIdOptions()
258 return proxyIdOptions;
261 public List<String> getRemoteRepoIdList()
263 return remoteRepoIdList;
266 public String getWhiteListPattern()
268 return whiteListPattern;
271 public void prepare()
273 proxyIdOptions = createNetworkProxyOptions();
274 managedRepoIdList = createManagedRepoOptions();
275 remoteRepoIdList = createRemoteRepoOptions();
276 policyMap = createPolicyMap();
279 public String removeBlackListPattern()
281 String pattern = getPattern();
283 if ( StringUtils.isBlank( pattern ) )
285 addActionError( "Cannot remove a blank black list pattern." );
288 if ( !getConnector().getBlackListPatterns().contains( pattern ) &&
289 !getConnector().getBlackListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
291 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
294 if ( !hasActionErrors() )
296 getConnector().getBlackListPatterns().remove( escapePattern( pattern ) );
299 setBlackListPattern( null );
305 public String removeProperty()
307 String key = getPropertyKey();
309 if ( StringUtils.isBlank( key ) )
311 addActionError( "Unable to remove property with blank key." );
314 if ( !getConnector().getProperties().containsKey( key ) )
316 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
319 if ( !hasActionErrors() )
321 getConnector().getProperties().remove( key );
324 setPropertyKey( null );
325 setPropertyValue( null );
330 public String removeWhiteListPattern()
332 String pattern = getPattern();
334 if ( StringUtils.isBlank( pattern ) )
336 addActionError( "Cannot remove a blank white list pattern." );
339 if ( !getConnector().getWhiteListPatterns().contains( pattern ) &&
340 !getConnector().getWhiteListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
342 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
345 if ( !hasActionErrors() )
347 getConnector().getWhiteListPatterns().remove( escapePattern( pattern ) );
350 setWhiteListPattern( null );
356 public void setBlackListPattern( String blackListPattern )
358 this.blackListPattern = blackListPattern;
361 public void setConnector( ProxyConnectorConfiguration connector )
363 this.connector = connector;
366 public void setManagedRepoIdList( List<String> managedRepoIdList )
368 this.managedRepoIdList = managedRepoIdList;
371 public void setPattern( String pattern )
373 this.pattern = pattern;
376 public void setPolicyMap( Map<String, Policy> policyMap )
378 this.policyMap = policyMap;
381 public void setPropertyKey( String propertyKey )
383 this.propertyKey = propertyKey;
386 public void setPropertyValue( String propertyValue )
388 this.propertyValue = propertyValue;
391 public void setProxyIdOptions( List<String> proxyIdOptions )
393 this.proxyIdOptions = proxyIdOptions;
396 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
398 this.remoteRepoIdList = remoteRepoIdList;
401 public void setWhiteListPattern( String whiteListPattern )
403 this.whiteListPattern = whiteListPattern;
406 protected List<String> createManagedRepoOptions()
408 return new ArrayList<String>( getConfig().getManagedRepositoriesAsMap().keySet() );
411 protected List<String> createNetworkProxyOptions()
413 List<String> options = new ArrayList<String>();
415 options.add( DIRECT_CONNECTION );
416 options.addAll( getConfig().getNetworkProxiesAsMap().keySet() );
421 protected Map<String, Policy> createPolicyMap()
423 Map<String, Policy> policyMap = new HashMap<String, Policy>();
425 policyMap.putAll( preDownloadPolicyMap );
426 policyMap.putAll( postDownloadPolicyMap );
427 policyMap.putAll( downloadErrorPolicyMap );
432 protected List<String> createRemoteRepoOptions()
434 return new ArrayList<String>( getConfig().getRemoteRepositoriesAsMap().keySet() );
437 @SuppressWarnings("unchecked")
438 protected void validateConnector()
440 if ( connector.getPolicies() == null )
442 addActionError( "Policies must be set." );
446 // Validate / Fix policy settings arriving from browser.
447 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
449 String policyId = entry.getKey();
450 Policy policy = entry.getValue();
451 List<String> options = policy.getOptions();
453 if ( !connector.getPolicies().containsKey( policyId ) )
455 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
459 Map<String, Object> properties = connector.getProperties();
460 for ( String key : properties.keySet() )
462 Object value = properties.get( key );
463 if ( value.getClass().isArray() )
465 String[] arr = (String[]) value;
466 properties.put( key, arr[0] );
470 // Ugly hack to compensate for ugly browsers.
471 Object o = connector.getPolicies().get( policyId );
473 if ( o.getClass().isArray() )
475 String arr[] = (String[]) o;
483 connector.getPolicies().put( policyId, value );
485 if ( StringUtils.isBlank( value ) )
487 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
491 if ( !options.contains( value ) )
493 addActionError( "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: "