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.archiva.policies.DownloadErrorPolicy;
28 import org.apache.archiva.policies.Policy;
29 import org.apache.archiva.policies.PostDownloadPolicy;
30 import org.apache.archiva.policies.PreDownloadPolicy;
31 import org.apache.commons.lang.StringEscapeUtils;
32 import org.apache.commons.lang.StringUtils;
34 import javax.annotation.PostConstruct;
35 import javax.inject.Inject;
36 import java.util.ArrayList;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
44 * AbstractProxyConnectorFormAction - generic fields and methods for either add or edit actions related with the
49 public abstract class AbstractProxyConnectorFormAction
50 extends AbstractProxyConnectorAction
55 private Map<String, PreDownloadPolicy> preDownloadPolicyMap;
57 private Map<String, PostDownloadPolicy> postDownloadPolicyMap;
59 private Map<String, DownloadErrorPolicy> downloadErrorPolicyMap;
61 private List<String> proxyIdOptions;
63 private List<String> managedRepoIdList;
65 private List<String> remoteRepoIdList;
68 * The map of policies that are available to be set.
70 private Map<String, Policy> policyMap;
73 * The property key to add or remove.
75 private String propertyKey;
78 * The property value to add.
80 private String propertyValue;
83 * The blacklist pattern to add.
85 private String blackListPattern;
88 * The whitelist pattern to add.
90 private String whiteListPattern;
93 * The pattern to add or remove (black or white).
95 private String pattern;
98 * The model for this action.
100 protected ProxyConnector connector;
103 private NetworkProxyAdmin networkProxyAdmin;
106 public void initialize( )
109 this.preDownloadPolicyMap = getBeansOfType( PreDownloadPolicy.class );
110 this.postDownloadPolicyMap = getBeansOfType( PostDownloadPolicy.class );
111 this.downloadErrorPolicyMap = getBeansOfType( DownloadErrorPolicy.class );
114 public String addBlackListPattern( )
116 String pattern = getBlackListPattern( );
118 //pattern = StringEscapeUtils.unescapeJavaScript( pattern );
120 if ( StringUtils.isBlank( pattern ) )
122 addActionError( "Cannot add a blank black list pattern." );
125 if ( !hasActionErrors( ) )
127 getConnector( ).getBlackListPatterns( ).add( pattern );
128 setBlackListPattern( null );
134 @SuppressWarnings( "unchecked" )
135 public String addProperty( )
137 String key = getPropertyKey( );
138 String value = getPropertyValue( );
140 if ( StringUtils.isBlank( key ) )
142 addActionError( "Unable to add property with blank key." );
145 if ( StringUtils.isBlank( value ) )
147 addActionError( "Unable to add property with blank value." );
150 if ( !hasActionErrors( ) )
152 getConnector( ).getProperties( ).put( key, value );
153 setPropertyKey( null );
154 setPropertyValue( null );
160 public String addWhiteListPattern( )
162 String pattern = getWhiteListPattern( );
163 //pattern = StringEscapeUtils.unescapeJavaScript( pattern );
164 if ( StringUtils.isBlank( pattern ) )
166 addActionError( "Cannot add a blank white list pattern." );
169 if ( !hasActionErrors( ) )
171 getConnector( ).getWhiteListPatterns( ).add( pattern );
172 setWhiteListPattern( null );
178 public String getBlackListPattern( )
180 return blackListPattern;
183 public ProxyConnector getConnector( )
188 public List<String> getManagedRepoIdList( )
190 return managedRepoIdList;
193 public String getPattern( )
198 public Map<String, Policy> getPolicyMap( )
203 public String getPropertyKey( )
208 public String getPropertyValue( )
210 return propertyValue;
213 public List<String> getProxyIdOptions( )
215 return proxyIdOptions;
218 public List<String> getRemoteRepoIdList( )
220 return remoteRepoIdList;
223 public String getWhiteListPattern( )
225 return whiteListPattern;
228 public void prepare( )
229 throws RepositoryAdminException
231 proxyIdOptions = createNetworkProxyOptions( );
232 managedRepoIdList = createManagedRepoOptions( );
233 remoteRepoIdList = createRemoteRepoOptions( );
234 policyMap = createPolicyMap( );
237 public String removeBlackListPattern( )
239 String pattern = getPattern( );
241 if ( StringUtils.isBlank( pattern ) )
243 addActionError( "Cannot remove a blank black list pattern." );
246 if ( !getConnector( ).getBlackListPatterns( ).contains( pattern )
247 && !getConnector( ).getBlackListPatterns( ).contains( pattern ))
249 addActionError( "Non-existant black list pattern [" + pattern + "], no black list pattern removed." );
252 if ( !hasActionErrors( ) )
254 getConnector( ).getBlackListPatterns( ).remove( pattern );
257 setBlackListPattern( null );
263 public String removeProperty( )
265 String key = getPropertyKey( );
267 if ( StringUtils.isBlank( key ) )
269 addActionError( "Unable to remove property with blank key." );
272 if ( !getConnector( ).getProperties( ).containsKey( key ) )
274 addActionError( "Non-existant property key [" + pattern + "], no property was removed." );
277 if ( !hasActionErrors( ) )
279 getConnector( ).getProperties( ).remove( key );
282 setPropertyKey( null );
283 setPropertyValue( null );
288 public String removeWhiteListPattern( )
290 String pattern = getPattern( );
292 if ( StringUtils.isBlank( pattern ) )
294 addActionError( "Cannot remove a blank white list pattern." );
297 if ( !getConnector( ).getWhiteListPatterns( ).contains( pattern )
298 && !getConnector( ).getWhiteListPatterns( ).contains( pattern ))
300 addActionError( "Non-existant white list pattern [" + pattern + "], no white list pattern removed." );
303 if ( !hasActionErrors( ) )
305 getConnector( ).getWhiteListPatterns( ).remove( pattern );
308 setWhiteListPattern( null );
314 public void setBlackListPattern( String blackListPattern )
316 this.blackListPattern = blackListPattern;
319 public void setConnector( ProxyConnector connector )
321 this.connector = connector;
324 public void setManagedRepoIdList( List<String> managedRepoIdList )
326 this.managedRepoIdList = managedRepoIdList;
329 public void setPattern( String pattern )
331 this.pattern = pattern;
334 public void setPolicyMap( Map<String, Policy> policyMap )
336 this.policyMap = policyMap;
339 public void setPropertyKey( String propertyKey )
341 this.propertyKey = propertyKey;
344 public void setPropertyValue( String propertyValue )
346 this.propertyValue = propertyValue;
349 public void setProxyIdOptions( List<String> proxyIdOptions )
351 this.proxyIdOptions = proxyIdOptions;
354 public void setRemoteRepoIdList( List<String> remoteRepoIdList )
356 this.remoteRepoIdList = remoteRepoIdList;
359 public void setWhiteListPattern( String whiteListPattern )
361 this.whiteListPattern = whiteListPattern;
364 protected List<String> createManagedRepoOptions( )
365 throws RepositoryAdminException
367 return new ArrayList<String>( getManagedRepositoryAdmin( ).getManagedRepositoriesAsMap( ).keySet( ) );
370 protected List<String> createNetworkProxyOptions( )
371 throws RepositoryAdminException
373 List<String> options = new ArrayList<String>( );
375 options.add( DIRECT_CONNECTION );
376 options.addAll( getNetworkProxiesKeys( ) );
381 private Collection<String> getNetworkProxiesKeys( )
382 throws RepositoryAdminException
384 List<NetworkProxy> networkProxies = networkProxyAdmin.getNetworkProxies( );
385 if ( networkProxies == null || networkProxies.isEmpty( ) )
387 return Collections.emptyList( );
389 List<String> keys = new ArrayList<String>( networkProxies.size( ) );
390 for ( NetworkProxy networkProxy : networkProxies )
392 keys.add( networkProxy.getId( ) );
398 protected Map<String, Policy> createPolicyMap( )
400 Map<String, Policy> policyMap = new HashMap<String, Policy>( );
402 policyMap.putAll( preDownloadPolicyMap );
403 policyMap.putAll( postDownloadPolicyMap );
404 policyMap.putAll( downloadErrorPolicyMap );
409 protected List<String> createRemoteRepoOptions( )
410 throws RepositoryAdminException
412 return new ArrayList<String>( getRemoteRepositoryAdmin( ).getRemoteRepositoriesAsMap( ).keySet( ) );
415 @SuppressWarnings( "unchecked" )
416 protected void validateConnector( )
418 if ( connector.getPolicies( ) == null )
420 addActionError( "Policies must be set." );
424 // Validate / Fix policy settings arriving from browser.
425 for ( Map.Entry<String, Policy> entry : getPolicyMap( ).entrySet( ) )
427 String policyId = entry.getKey( );
428 Policy policy = entry.getValue( );
429 List<String> options = policy.getOptions( );
431 if ( !connector.getPolicies( ).containsKey( policyId ) )
433 addActionError( "Policy [" + policyId + "] must be set (missing id)." );
437 Map<String, String> properties = connector.getProperties( );
438 for ( Map.Entry<String, String> entry2 : properties.entrySet( ) )
440 Object value = entry2.getValue( );
441 if ( value.getClass( ).isArray( ) )
443 String[] arr = (String[]) value;
444 properties.put( entry2.getKey( ), arr[0] );
448 // Ugly hack to compensate for ugly browsers.
449 Object o = connector.getPolicies( ).get( policyId );
451 if ( o.getClass( ).isArray( ) )
453 String arr[] = (String[]) o;
461 connector.getPolicies( ).put( policyId, value );
463 if ( StringUtils.isBlank( value ) )
465 addActionError( "Policy [" + policyId + "] must be set (missing value)." );
469 if ( !options.contains( value ) )
472 "Value of [" + value + "] is invalid for policy [" + policyId + "], valid values: " + options );
479 public NetworkProxyAdmin getNetworkProxyAdmin( )
481 return networkProxyAdmin;
484 public void setNetworkProxyAdmin( NetworkProxyAdmin networkProxyAdmin )
486 this.networkProxyAdmin = networkProxyAdmin;