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.archiva.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.admin.repository.proxyconnector.ProxyConnector;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.policies.DownloadErrorPolicy;
28 import org.apache.maven.archiva.policies.Policy;
29 import org.apache.maven.archiva.policies.PostDownloadPolicy;
30 import org.apache.maven.archiva.policies.PreDownloadPolicy;
32 import javax.annotation.PostConstruct;
33 import javax.inject.Inject;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
40 * AbstractProxyConnectorFormAction - generic fields and methods for either add or edit actions related with the
45 public abstract class AbstractProxyConnectorFormAction
46 extends AbstractProxyConnectorAction
51 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
53 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
55 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
57 private List<String> proxyIdOptions;
59 private List<String> managedRepoIdList;
61 private List<String> remoteRepoIdList;
64 * The map of policies that are available to be set.
66 private Map<String, Policy> policyMap;
69 * The property key to add or remove.
71 private String propertyKey;
74 * The property value to add.
76 private String propertyValue;
79 * The blacklist pattern to add.
81 private String blackListPattern;
84 * The whitelist pattern to add.
86 private String whiteListPattern;
89 * The pattern to add or remove (black or white).
91 private String pattern;
94 * The model for this action.
96 protected ProxyConnector connector;
99 private ArchivaConfiguration archivaConfiguration;
102 public void initialize()
105 this.preDownloadPolicyMap = getBeansOfType( PreDownloadPolicy.class );
106 this.postDownloadPolicyMap = getBeansOfType( PostDownloadPolicy.class );
107 this.downloadErrorPolicyMap = getBeansOfType( DownloadErrorPolicy.class );
110 protected List<String> escapePatterns( List<String> patterns )
112 List<String> escapedPatterns = new ArrayList<String>();
113 if ( patterns != null )
115 for ( String pattern : patterns )
117 escapedPatterns.add( StringUtils.replace( pattern, "\\", "\\\\" ) );
121 return escapedPatterns;
124 protected List<String> unescapePatterns( List<String> patterns )
126 List<String> rawPatterns = new ArrayList<String>();
127 if ( patterns != null )
129 for ( String pattern : patterns )
131 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
138 private String escapePattern( String pattern )
140 return StringUtils.replace( pattern, "\\", "\\\\" );
143 public String addBlackListPattern()
145 String pattern = getBlackListPattern();
147 if ( StringUtils.isBlank( pattern ) )
149 addActionError( "Cannot add a blank black list pattern." );
152 if ( !hasActionErrors() )
154 getConnector().getBlackListPatterns().add( escapePattern( pattern ) );
155 setBlackListPattern( null );
161 @SuppressWarnings( "unchecked" )
162 public String addProperty()
164 String key = getPropertyKey();
165 String value = getPropertyValue();
167 if ( StringUtils.isBlank( key ) )
169 addActionError( "Unable to add property with blank key." );
172 if ( StringUtils.isBlank( value ) )
174 addActionError( "Unable to add property with blank value." );
177 if ( !hasActionErrors() )
179 getConnector().getProperties().put( key, value );
180 setPropertyKey( null );
181 setPropertyValue( null );
187 public String addWhiteListPattern()
189 String pattern = getWhiteListPattern();
191 if ( StringUtils.isBlank( pattern ) )
193 addActionError( "Cannot add a blank white list pattern." );
196 if ( !hasActionErrors() )
198 getConnector().getWhiteListPatterns().add( escapePattern( pattern ) );
199 setWhiteListPattern( null );
205 public String getBlackListPattern()
207 return blackListPattern;
210 public ProxyConnector getConnector()
215 public List<String> getManagedRepoIdList()
217 return managedRepoIdList;
220 public String getPattern()
225 public Map<String, Policy> getPolicyMap()
230 public String getPropertyKey()
235 public String getPropertyValue()
237 return propertyValue;
240 public List<String> getProxyIdOptions()
242 return proxyIdOptions;
245 public List<String> getRemoteRepoIdList()
247 return remoteRepoIdList;
250 public String getWhiteListPattern()
252 return whiteListPattern;
255 public void prepare()
256 throws RepositoryAdminException
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( ProxyConnector 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()
392 throws RepositoryAdminException
394 return new ArrayList<String>( getManagedRepositoryAdmin().getManagedRepositoriesAsMap().keySet() );
397 protected List<String> createNetworkProxyOptions()
399 List<String> options = new ArrayList<String>();
401 options.add( DIRECT_CONNECTION );
402 options.addAll( archivaConfiguration.getConfiguration().getNetworkProxiesAsMap().keySet() );
407 protected Map<String, Policy> createPolicyMap()
409 Map<String, Policy> policyMap = new HashMap<String, Policy>();
411 policyMap.putAll( preDownloadPolicyMap );
412 policyMap.putAll( postDownloadPolicyMap );
413 policyMap.putAll( downloadErrorPolicyMap );
418 protected List<String> createRemoteRepoOptions()
419 throws RepositoryAdminException
421 return new ArrayList<String>( getRemoteRepositoryAdmin().getRemoteRepositoriesAsMap().keySet() );
424 @SuppressWarnings( "unchecked" )
425 protected void validateConnector()
427 if ( connector.getPolicies() == null )
429 addActionError( "Policies must be set." );
433 // Validate / Fix policy settings arriving from browser.
434 for ( Map.Entry<String, Policy> entry : getPolicyMap().entrySet() )
436 String policyId = entry.getKey();
437 Policy policy = entry.getValue();
438 List<String> options = policy.getOptions();
440 if ( !connector.getPolicies().containsKey( policyId ) )
442 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
446 Map<String, String> properties = connector.getProperties();
447 for ( Map.Entry<String, String> entry2 : properties.entrySet() )
449 Object value = entry2.getValue();
450 if ( value.getClass().isArray() )
452 String[] arr = (String[]) value;
453 properties.put( entry2.getKey(), arr[0] );
457 // Ugly hack to compensate for ugly browsers.
458 Object o = connector.getPolicies().get( policyId );
460 if ( o.getClass().isArray() )
462 String arr[] = (String[]) o;
470 connector.getPolicies().put( policyId, value );
472 if ( StringUtils.isBlank( value ) )
474 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
478 if ( !options.contains( value ) )
481 "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: " + options );
488 public ArchivaConfiguration getArchivaConfiguration()
490 return archivaConfiguration;
493 public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration )
495 this.archivaConfiguration = archivaConfiguration;