1 package org.apache.archiva.admin.repository.proxyconnector;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.AuditInformation;
23 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
24 import org.apache.archiva.admin.repository.RepositoryAdminException;
25 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.audit.AuditEvent;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
33 import org.springframework.stereotype.Service;
35 import javax.inject.Inject;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
44 * @author Olivier Lamy
47 @Service( "proxyConnectorAdmin#default" )
48 public class DefaultProxyConnectorAdmin
49 extends AbstractRepositoryAdmin
50 implements ProxyConnectorAdmin
54 private ManagedRepositoryAdmin managedRepositoryAdmin;
57 private RemoteRepositoryAdmin remoteRepositoryAdmin;
59 public List<ProxyConnector> getProxyConnectors()
60 throws RepositoryAdminException
62 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
63 getArchivaConfiguration().getConfiguration().getProxyConnectors();
64 List<ProxyConnector> proxyConnectors = new ArrayList<ProxyConnector>( proxyConnectorConfigurations.size() );
65 for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
67 ProxyConnector proxyConnector = new ProxyConnector();
68 proxyConnectors.add( proxyConnector );
69 proxyConnector.setOrder( configuration.getOrder() );
70 proxyConnector.setBlackListPatterns( new ArrayList<String>( configuration.getBlackListPatterns() ) );
71 proxyConnector.setWhiteListPatterns( new ArrayList<String>( configuration.getWhiteListPatterns() ) );
72 proxyConnector.setDisabled( configuration.isDisabled() );
73 proxyConnector.setPolicies( new HashMap<String, String>( configuration.getPolicies() ) );
74 proxyConnector.setProperties( new HashMap<String, String>( configuration.getProperties() ) );
75 proxyConnector.setProxyId( configuration.getProxyId() );
76 proxyConnector.setSourceRepoId( configuration.getSourceRepoId() );
77 proxyConnector.setTargetRepoId( configuration.getTargetRepoId() );
80 return proxyConnectors;
83 public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
84 throws RepositoryAdminException
86 for ( ProxyConnector proxyConnector : getProxyConnectors() )
88 if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
89 targetRepoId, proxyConnector.getTargetRepoId() ) )
91 return proxyConnector;
97 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
98 throws RepositoryAdminException
100 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
102 throw new RepositoryAdminException(
103 "Unable to add proxy connector, as one already exists with source repository id ["
104 + proxyConnector.getSourceRepoId() + "] and target repository id ["
105 + proxyConnector.getTargetRepoId() + "]." );
108 validateProxyConnector( proxyConnector );
110 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
111 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
113 Configuration configuration = getArchivaConfiguration().getConfiguration();
115 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
116 configuration.addProxyConnector( proxyConnectorConfiguration );
117 saveConfiguration( configuration );
118 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
119 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
124 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
125 throws RepositoryAdminException
127 Configuration configuration = getArchivaConfiguration().getConfiguration();
128 ProxyConnectorConfiguration proxyConnectorConfiguration =
129 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
130 if ( proxyConnectorConfiguration == null )
132 throw new RepositoryAdminException(
133 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
134 + proxyConnector.getTargetRepoId() );
136 configuration.removeProxyConnector( proxyConnectorConfiguration );
137 saveConfiguration( configuration );
138 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
139 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
143 protected List<String> unescapePatterns( List<String> patterns )
145 List<String> rawPatterns = new ArrayList<String>();
146 if ( patterns != null )
148 for ( String pattern : patterns )
150 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
157 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
158 throws RepositoryAdminException
160 java.util.Map<String, List<ProxyConnector>> proxyConnectorMap =
161 new HashMap<String, java.util.List<ProxyConnector>>();
163 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
164 while ( it.hasNext() )
166 ProxyConnector proxyConfig = it.next();
167 String key = proxyConfig.getSourceRepoId();
169 java.util.List<ProxyConnector> connectors = proxyConnectorMap.get( key );
170 if ( connectors == null )
172 connectors = new ArrayList<ProxyConnector>();
173 proxyConnectorMap.put( key, connectors );
176 connectors.add( proxyConfig );
178 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
181 return proxyConnectorMap;
184 public ProxyConnector findProxyConnector( String sourceId, String targetId )
185 throws RepositoryAdminException
187 return getProxyConnector(
188 findProxyConnector( sourceId, targetId, getArchivaConfiguration().getConfiguration() ) );
191 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
192 Configuration configuration )
194 if ( StringUtils.isBlank( sourceId ) )
199 if ( StringUtils.isBlank( targetId ) )
204 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
205 return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
208 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
211 ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
212 proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
213 proxyConnectorConfiguration.setBlackListPatterns(
214 new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
215 proxyConnectorConfiguration.setWhiteListPatterns(
216 new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
217 proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
218 proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
219 proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
220 proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
221 proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
222 proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
223 return proxyConnectorConfiguration;*/
224 return new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
227 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
229 return new BeanReplicator().replicateBean( proxyConnectorConfiguration, ProxyConnector.class );
232 protected void validateProxyConnector( ProxyConnector proxyConnector )
233 throws RepositoryAdminException
235 // validate source a Managed target a Remote
236 if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
238 throw new RepositoryAdminException(
239 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
240 + " is not a ManagedRepository" );
242 if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
244 throw new RepositoryAdminException(
245 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
246 + " is not a RemoteRepository" );
249 // FIXME validate NetworkProxyConfiguration too