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 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
54 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
56 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
58 private List<String> proxyIdOptions;
60 private List<String> managedRepoIdList;
62 private List<String> remoteRepoIdList;
65 * The map of policies that are available to be set.
67 private Map<String, Policy> policyMap;
70 * The property key to add or remove.
72 private String propertyKey;
75 * The property value to add.
77 private String propertyValue;
80 * The blacklist pattern to add.
82 private String blackListPattern;
85 * The whitelist pattern to add.
87 private String whiteListPattern;
90 * The pattern to add or remove (black or white).
92 private String pattern;
95 * The model for this action.
97 protected ProxyConnectorConfiguration connector;
101 public void initialize()
104 this.preDownloadPolicyMap = getBeansOfType( PreDownloadPolicy.class );
105 this.postDownloadPolicyMap = getBeansOfType( PostDownloadPolicy.class );
106 this.downloadErrorPolicyMap = getBeansOfType( DownloadErrorPolicy.class );
109 protected List<String> escapePatterns( List<String> patterns )
111 List<String> escapedPatterns = new ArrayList<String>();
112 if( patterns != null )
114 for( String pattern : patterns )
116 escapedPatterns.add( StringUtils.replace( pattern, "\\", "\\\\" ) );
120 return escapedPatterns;
123 protected List<String> unescapePatterns( List<String> patterns )
125 List<String> rawPatterns = new ArrayList<String>();
126 if( patterns != null )
128 for( String pattern : patterns )
130 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
137 private String escapePattern( String pattern )
139 return StringUtils.replace( pattern, "\\", "\\\\" );
142 public String addBlackListPattern()
144 String pattern = getBlackListPattern();
146 if ( StringUtils.isBlank( pattern ) )
148 addActionError( "Cannot add a blank black list pattern." );
151 if ( !hasActionErrors() )
153 getConnector().getBlackListPatterns().add( escapePattern( pattern ) );
154 setBlackListPattern( null );
160 @SuppressWarnings("unchecked")
161 public String addProperty()
163 String key = getPropertyKey();
164 String value = getPropertyValue();
166 if ( StringUtils.isBlank( key ) )
168 addActionError( "Unable to add property with blank key." );
171 if ( StringUtils.isBlank( value ) )
173 addActionError( "Unable to add property with blank value." );
176 if ( !hasActionErrors() )
178 getConnector().getProperties().put( key, value );
179 setPropertyKey( null );
180 setPropertyValue( null );
186 public String addWhiteListPattern()
188 String pattern = getWhiteListPattern();
190 if ( StringUtils.isBlank( pattern ) )
192 addActionError( "Cannot add a blank white list pattern." );
195 if ( !hasActionErrors() )
197 getConnector().getWhiteListPatterns().add( escapePattern( pattern ) );
198 setWhiteListPattern( null );
204 public String getBlackListPattern()
206 return blackListPattern;
209 public ProxyConnectorConfiguration getConnector()
214 public List<String> getManagedRepoIdList()
216 return managedRepoIdList;
219 public String getPattern()
224 public Map<String, Policy> getPolicyMap()
229 public String getPropertyKey()
234 public String getPropertyValue()
236 return propertyValue;
239 public List<String> getProxyIdOptions()
241 return proxyIdOptions;
244 public List<String> getRemoteRepoIdList()
246 return remoteRepoIdList;
249 public String getWhiteListPattern()
251 return whiteListPattern;
254 public void prepare()
256 proxyIdOptions = createNetworkProxyOptions();
257 managedRepoIdList = createManagedRepoOptions();
258 remoteRepoIdList = createRemoteRepoOptions();
259 policyMap = createPolicyMap();
262 public String removeBlackListPattern()
264 String pattern = getPattern();
266 if ( StringUtils.isBlank( pattern ) )
268 addActionError( "Cannot remove a blank black list pattern." );
271 if ( !getConnector().getBlackListPatterns().contains( pattern ) &&
272 !getConnector().getBlackListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
274 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
277 if ( !hasActionErrors() )
279 getConnector().getBlackListPatterns().remove( escapePattern( pattern ) );
282 setBlackListPattern( null );
288 public String removeProperty()
290 String key = getPropertyKey();
292 if ( StringUtils.isBlank( key ) )
294 addActionError( "Unable to remove property with blank key." );
297 if ( !getConnector().getProperties().containsKey( key ) )
299 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
302 if ( !hasActionErrors() )
304 getConnector().getProperties().remove( key );
307 setPropertyKey( null );
308 setPropertyValue( null );
313 public String removeWhiteListPattern()
315 String pattern = getPattern();
317 if ( StringUtils.isBlank( pattern ) )
319 addActionError( "Cannot remove a blank white list pattern." );
322 if ( !getConnector().getWhiteListPatterns().contains( pattern ) &&
323 !getConnector().getWhiteListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
325 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
328 if ( !hasActionErrors() )
330 getConnector().getWhiteListPatterns().remove( escapePattern( pattern ) );
333 setWhiteListPattern( null );
339 public void setBlackListPattern( String blackListPattern )
341 this.blackListPattern = blackListPattern;
344 public void setConnector( ProxyConnectorConfiguration connector )
346 this.connector = connector;
349 public void setManagedRepoIdList( List<String> managedRepoIdList )
351 this.managedRepoIdList = managedRepoIdList;
354 public void setPattern( String pattern )
356 this.pattern = pattern;
359 public void setPolicyMap( Map<String, Policy> policyMap )
361 this.policyMap = policyMap;
364 public void setPropertyKey( String propertyKey )
366 this.propertyKey = propertyKey;
369 public void setPropertyValue( String propertyValue )
371 this.propertyValue = propertyValue;
374 public void setProxyIdOptions( List<String> proxyIdOptions )
376 this.proxyIdOptions = proxyIdOptions;
379 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
381 this.remoteRepoIdList = remoteRepoIdList;
384 public void setWhiteListPattern( String whiteListPattern )
386 this.whiteListPattern = whiteListPattern;
389 protected List<String> createManagedRepoOptions()
391 return new ArrayList<String>( getConfig().getManagedRepositoriesAsMap().keySet() );
394 protected List<String> createNetworkProxyOptions()
396 List<String> options = new ArrayList<String>();
398 options.add( DIRECT_CONNECTION );
399 options.addAll( getConfig().getNetworkProxiesAsMap().keySet() );
404 protected Map<String, Policy> createPolicyMap()
406 Map<String, Policy> policyMap = new HashMap<String, Policy>();
408 policyMap.putAll( preDownloadPolicyMap );
409 policyMap.putAll( postDownloadPolicyMap );
410 policyMap.putAll( downloadErrorPolicyMap );
415 protected List<String> createRemoteRepoOptions()
417 return new ArrayList<String>( getConfig().getRemoteRepositoriesAsMap().keySet() );
420 @SuppressWarnings("unchecked")
421 protected void validateConnector()
423 if ( connector.getPolicies() == null )
425 addActionError( "Policies must be set." );
429 // Validate / Fix policy settings arriving from browser.
430 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
432 String policyId = entry.getKey();
433 Policy policy = entry.getValue();
434 List<String> options = policy.getOptions();
436 if ( !connector.getPolicies().containsKey( policyId ) )
438 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
442 Map<String, Object> properties = connector.getProperties();
443 for ( Map.Entry<String, Object> entry2 : properties.entrySet())
445 Object value = entry2.getValue();
446 if ( value.getClass().isArray() )
448 String[] arr = (String[]) value;
449 properties.put( entry2.getKey(), arr[0] );
453 // Ugly hack to compensate for ugly browsers.
454 Object o = connector.getPolicies().get( policyId );
456 if ( o.getClass().isArray() )
458 String arr[] = (String[]) o;
466 connector.getPolicies().put( policyId, value );
468 if ( StringUtils.isBlank( value ) )
470 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
474 if ( !options.contains( value ) )
476 addActionError( "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: "