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 public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
144 throws RepositoryAdminException
146 Configuration configuration = getArchivaConfiguration().getConfiguration();
147 ProxyConnectorConfiguration proxyConnectorConfiguration =
148 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
149 configuration.removeProxyConnector( proxyConnectorConfiguration );
150 configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
151 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
152 AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
156 protected List<String> unescapePatterns( List<String> patterns )
158 List<String> rawPatterns = new ArrayList<String>();
159 if ( patterns != null )
161 for ( String pattern : patterns )
163 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
170 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
171 throws RepositoryAdminException
173 Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, java.util.List<ProxyConnector>>();
175 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
176 while ( it.hasNext() )
178 ProxyConnector proxyConfig = it.next();
179 String key = proxyConfig.getSourceRepoId();
181 List<ProxyConnector> connectors = proxyConnectorMap.get( key );
182 if ( connectors == null )
184 connectors = new ArrayList<ProxyConnector>();
185 proxyConnectorMap.put( key, connectors );
188 connectors.add( proxyConfig );
190 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
193 return proxyConnectorMap;
196 public ProxyConnector findProxyConnector( String sourceId, String targetId )
197 throws RepositoryAdminException
199 return getProxyConnector(
200 findProxyConnector( sourceId, targetId, getArchivaConfiguration().getConfiguration() ) );
203 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
204 Configuration configuration )
206 if ( StringUtils.isBlank( sourceId ) )
211 if ( StringUtils.isBlank( targetId ) )
216 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
217 return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
220 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
223 ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
224 proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
225 proxyConnectorConfiguration.setBlackListPatterns(
226 new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
227 proxyConnectorConfiguration.setWhiteListPatterns(
228 new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
229 proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
230 proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
231 proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
232 proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
233 proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
234 proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
235 return proxyConnectorConfiguration;*/
237 return proxyConnector == null
239 : new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
242 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
244 return proxyConnectorConfiguration == null
246 : new BeanReplicator().replicateBean( proxyConnectorConfiguration, ProxyConnector.class );
249 protected void validateProxyConnector( ProxyConnector proxyConnector )
250 throws RepositoryAdminException
252 // validate source a Managed target a Remote
253 if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
255 throw new RepositoryAdminException(
256 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
257 + " is not a ManagedRepository" );
259 if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
261 throw new RepositoryAdminException(
262 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
263 + " is not a RemoteRepository" );
266 // FIXME validate NetworkProxyConfiguration too when available