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 proxyConnectors.add( getProxyConnector( configuration ) );
70 return proxyConnectors;
73 public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
74 throws RepositoryAdminException
76 for ( ProxyConnector proxyConnector : getProxyConnectors() )
78 if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
79 targetRepoId, proxyConnector.getTargetRepoId() ) )
81 return proxyConnector;
87 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
88 throws RepositoryAdminException
90 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
92 throw new RepositoryAdminException(
93 "Unable to add proxy connector, as one already exists with source repository id ["
94 + proxyConnector.getSourceRepoId() + "] and target repository id ["
95 + proxyConnector.getTargetRepoId() + "]." );
98 validateProxyConnector( proxyConnector );
100 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
101 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
103 Configuration configuration = getArchivaConfiguration().getConfiguration();
105 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
106 configuration.addProxyConnector( proxyConnectorConfiguration );
107 saveConfiguration( configuration );
108 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
109 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
114 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
115 throws RepositoryAdminException
117 Configuration configuration = getArchivaConfiguration().getConfiguration();
118 ProxyConnectorConfiguration proxyConnectorConfiguration =
119 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
120 if ( proxyConnectorConfiguration == null )
122 throw new RepositoryAdminException(
123 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
124 + proxyConnector.getTargetRepoId() );
126 configuration.removeProxyConnector( proxyConnectorConfiguration );
127 saveConfiguration( configuration );
128 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
129 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
133 public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
134 throws RepositoryAdminException
136 Configuration configuration = getArchivaConfiguration().getConfiguration();
137 ProxyConnectorConfiguration proxyConnectorConfiguration =
138 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
139 configuration.removeProxyConnector( proxyConnectorConfiguration );
140 configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
141 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
142 AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
146 protected List<String> unescapePatterns( List<String> patterns )
148 List<String> rawPatterns = new ArrayList<String>();
149 if ( patterns != null )
151 for ( String pattern : patterns )
153 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
160 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
161 throws RepositoryAdminException
163 Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, java.util.List<ProxyConnector>>();
165 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
166 while ( it.hasNext() )
168 ProxyConnector proxyConfig = it.next();
169 String key = proxyConfig.getSourceRepoId();
171 List<ProxyConnector> connectors = proxyConnectorMap.get( key );
172 if ( connectors == null )
174 connectors = new ArrayList<ProxyConnector>();
175 proxyConnectorMap.put( key, connectors );
178 connectors.add( proxyConfig );
180 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
183 return proxyConnectorMap;
186 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
187 Configuration configuration )
189 if ( StringUtils.isBlank( sourceId ) )
194 if ( StringUtils.isBlank( targetId ) )
199 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
200 return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
203 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
206 ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
207 proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
208 proxyConnectorConfiguration.setBlackListPatterns(
209 new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
210 proxyConnectorConfiguration.setWhiteListPatterns(
211 new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
212 proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
213 proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
214 proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
215 proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
216 proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
217 proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
218 return proxyConnectorConfiguration;*/
220 return proxyConnector == null
222 : new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
225 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
227 return proxyConnectorConfiguration == null
229 : 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 when available