1 package org.apache.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.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
25 import org.apache.archiva.admin.model.beans.ProxyConnector;
26 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.archiva.policies.DownloadErrorPolicy;
29 import org.apache.archiva.policies.Policy;
30 import org.apache.archiva.policies.PostDownloadPolicy;
31 import org.apache.archiva.policies.PreDownloadPolicy;
33 import javax.annotation.PostConstruct;
34 import javax.inject.Inject;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.List;
43 * AbstractProxyConnectorFormAction - generic fields and methods for either add or edit actions related with the
48 public abstract class AbstractProxyConnectorFormAction
49 extends AbstractProxyConnectorAction
54 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
56 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
58 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
60 private List<String> proxyIdOptions;
62 private List<String> managedRepoIdList;
64 private List<String> remoteRepoIdList;
67 * The map of policies that are available to be set.
69 private Map<String, Policy> policyMap;
72 * The property key to add or remove.
74 private String propertyKey;
77 * The property value to add.
79 private String propertyValue;
82 * The blacklist pattern to add.
84 private String blackListPattern;
87 * The whitelist pattern to add.
89 private String whiteListPattern;
92 * The pattern to add or remove (black or white).
94 private String pattern;
97 * The model for this action.
99 protected ProxyConnector connector;
102 private NetworkProxyAdmin networkProxyAdmin;
105 public void initialize()
108 this.preDownloadPolicyMap = getBeansOfType( PreDownloadPolicy.class );
109 this.postDownloadPolicyMap = getBeansOfType( PostDownloadPolicy.class );
110 this.downloadErrorPolicyMap = getBeansOfType( DownloadErrorPolicy.class );
113 protected List<String> escapePatterns( List<String> patterns )
115 List<String> escapedPatterns = new ArrayList<String>();
116 if ( patterns != null )
118 for ( String pattern : patterns )
120 escapedPatterns.add( StringUtils.replace( pattern, "\\", "\\\\" ) );
124 return escapedPatterns;
127 protected List<String> unescapePatterns( List<String> patterns )
129 List<String> rawPatterns = new ArrayList<String>();
130 if ( patterns != null )
132 for ( String pattern : patterns )
134 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
141 private String escapePattern( String pattern )
143 return StringUtils.replace( pattern, "\\", "\\\\" );
146 public String addBlackListPattern()
148 String pattern = getBlackListPattern();
150 if ( StringUtils.isBlank( pattern ) )
152 addActionError( "Cannot add a blank black list pattern." );
155 if ( !hasActionErrors() )
157 getConnector().getBlackListPatterns().add( escapePattern( pattern ) );
158 setBlackListPattern( null );
164 @SuppressWarnings( "unchecked" )
165 public String addProperty()
167 String key = getPropertyKey();
168 String value = getPropertyValue();
170 if ( StringUtils.isBlank( key ) )
172 addActionError( "Unable to add property with blank key." );
175 if ( StringUtils.isBlank( value ) )
177 addActionError( "Unable to add property with blank value." );
180 if ( !hasActionErrors() )
182 getConnector().getProperties().put( key, value );
183 setPropertyKey( null );
184 setPropertyValue( null );
190 public String addWhiteListPattern()
192 String pattern = getWhiteListPattern();
194 if ( StringUtils.isBlank( pattern ) )
196 addActionError( "Cannot add a blank white list pattern." );
199 if ( !hasActionErrors() )
201 getConnector().getWhiteListPatterns().add( escapePattern( pattern ) );
202 setWhiteListPattern( null );
208 public String getBlackListPattern()
210 return blackListPattern;
213 public ProxyConnector getConnector()
218 public List<String> getManagedRepoIdList()
220 return managedRepoIdList;
223 public String getPattern()
228 public Map<String, Policy> getPolicyMap()
233 public String getPropertyKey()
238 public String getPropertyValue()
240 return propertyValue;
243 public List<String> getProxyIdOptions()
245 return proxyIdOptions;
248 public List<String> getRemoteRepoIdList()
250 return remoteRepoIdList;
253 public String getWhiteListPattern()
255 return whiteListPattern;
258 public void prepare()
259 throws RepositoryAdminException
261 proxyIdOptions = createNetworkProxyOptions();
262 managedRepoIdList = createManagedRepoOptions();
263 remoteRepoIdList = createRemoteRepoOptions();
264 policyMap = createPolicyMap();
267 public String removeBlackListPattern()
269 String pattern = getPattern();
271 if ( StringUtils.isBlank( pattern ) )
273 addActionError( "Cannot remove a blank black list pattern." );
276 if ( !getConnector().getBlackListPatterns().contains( pattern )
277 && !getConnector().getBlackListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
279 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
282 if ( !hasActionErrors() )
284 getConnector().getBlackListPatterns().remove( escapePattern( pattern ) );
287 setBlackListPattern( null );
293 public String removeProperty()
295 String key = getPropertyKey();
297 if ( StringUtils.isBlank( key ) )
299 addActionError( "Unable to remove property with blank key." );
302 if ( !getConnector().getProperties().containsKey( key ) )
304 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
307 if ( !hasActionErrors() )
309 getConnector().getProperties().remove( key );
312 setPropertyKey( null );
313 setPropertyValue( null );
318 public String removeWhiteListPattern()
320 String pattern = getPattern();
322 if ( StringUtils.isBlank( pattern ) )
324 addActionError( "Cannot remove a blank white list pattern." );
327 if ( !getConnector().getWhiteListPatterns().contains( pattern )
328 && !getConnector().getWhiteListPatterns().contains( StringUtils.replace( pattern, "\\", "\\\\" ) ) )
330 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
333 if ( !hasActionErrors() )
335 getConnector().getWhiteListPatterns().remove( escapePattern( pattern ) );
338 setWhiteListPattern( null );
344 public void setBlackListPattern( String blackListPattern )
346 this.blackListPattern = blackListPattern;
349 public void setConnector( ProxyConnector connector )
351 this.connector = connector;
354 public void setManagedRepoIdList( List<String> managedRepoIdList )
356 this.managedRepoIdList = managedRepoIdList;
359 public void setPattern( String pattern )
361 this.pattern = pattern;
364 public void setPolicyMap( Map<String, Policy> policyMap )
366 this.policyMap = policyMap;
369 public void setPropertyKey( String propertyKey )
371 this.propertyKey = propertyKey;
374 public void setPropertyValue( String propertyValue )
376 this.propertyValue = propertyValue;
379 public void setProxyIdOptions( List<String> proxyIdOptions )
381 this.proxyIdOptions = proxyIdOptions;
384 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
386 this.remoteRepoIdList = remoteRepoIdList;
389 public void setWhiteListPattern( String whiteListPattern )
391 this.whiteListPattern = whiteListPattern;
394 protected List<String> createManagedRepoOptions()
395 throws RepositoryAdminException
397 return new ArrayList<String>( getManagedRepositoryAdmin().getManagedRepositoriesAsMap().keySet() );
400 protected List<String> createNetworkProxyOptions()
401 throws RepositoryAdminException
403 List<String> options = new ArrayList<String>();
405 options.add( DIRECT_CONNECTION );
406 options.addAll( getNetworkProxiesKeys() );
411 private Collection<String> getNetworkProxiesKeys()
412 throws RepositoryAdminException
414 List<NetworkProxy> networkProxies = networkProxyAdmin.getNetworkProxies();
415 if ( networkProxies == null || networkProxies.isEmpty() )
417 return Collections.emptyList();
419 List<String> keys = new ArrayList<String>( networkProxies.size() );
420 for ( NetworkProxy networkProxy : networkProxies )
422 keys.add( networkProxy.getId() );
428 protected Map<String, Policy> createPolicyMap()
430 Map<String, Policy> policyMap = new HashMap<String, Policy>();
432 policyMap.putAll( preDownloadPolicyMap );
433 policyMap.putAll( postDownloadPolicyMap );
434 policyMap.putAll( downloadErrorPolicyMap );
439 protected List<String> createRemoteRepoOptions()
440 throws RepositoryAdminException
442 return new ArrayList<String>( getRemoteRepositoryAdmin().getRemoteRepositoriesAsMap().keySet() );
445 @SuppressWarnings( "unchecked" )
446 protected void validateConnector()
448 if ( connector.getPolicies() == null )
450 addActionError( "Policies must be set." );
454 // Validate / Fix policy settings arriving from browser.
455 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
457 String policyId = entry.getKey();
458 Policy policy = entry.getValue();
459 List<String> options = policy.getOptions();
461 if ( !connector.getPolicies().containsKey( policyId ) )
463 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
467 Map<String, String> properties = connector.getProperties();
468 for ( Map.Entry<String, String> entry2 : properties.entrySet() )
470 Object value = entry2.getValue();
471 if ( value.getClass().isArray() )
473 String[] arr = (String[]) value;
474 properties.put( entry2.getKey(), arr[0] );
478 // Ugly hack to compensate for ugly browsers.
479 Object o = connector.getPolicies().get( policyId );
481 if ( o.getClass().isArray() )
483 String arr[] = (String[]) o;
491 connector.getPolicies().put( policyId, value );
493 if ( StringUtils.isBlank( value ) )
495 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
499 if ( !options.contains( value ) )
502 "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: " + options );
509 public NetworkProxyAdmin getNetworkProxyAdmin()
511 return networkProxyAdmin;
514 public void setNetworkProxyAdmin( NetworkProxyAdmin networkProxyAdmin )
516 this.networkProxyAdmin = networkProxyAdmin;