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.model.AuditInformation;
23 import org.apache.archiva.admin.model.RepositoryAdminException;
24 import org.apache.archiva.admin.model.beans.ProxyConnector;
25 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
27 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorOrderComparator;
28 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
29 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
30 import org.apache.archiva.audit.AuditEvent;
31 import org.apache.archiva.configuration.Configuration;
32 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
33 import org.apache.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
34 import org.apache.commons.collections.CollectionUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
47 * @author Olivier Lamy
50 @Service ( "proxyConnectorAdmin#default" )
51 public class DefaultProxyConnectorAdmin
52 extends AbstractRepositoryAdmin
53 implements ProxyConnectorAdmin
57 private ManagedRepositoryAdmin managedRepositoryAdmin;
60 private RemoteRepositoryAdmin remoteRepositoryAdmin;
62 public List<ProxyConnector> getProxyConnectors()
63 throws RepositoryAdminException
65 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
66 getArchivaConfiguration().getConfiguration().getProxyConnectors();
67 List<ProxyConnector> proxyConnectors = new ArrayList<ProxyConnector>( proxyConnectorConfigurations.size() );
68 for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
70 proxyConnectors.add( getProxyConnector( configuration ) );
72 Collections.sort( proxyConnectors, ProxyConnectorOrderComparator.getInstance() );
73 return proxyConnectors;
76 public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
77 throws RepositoryAdminException
79 for ( ProxyConnector proxyConnector : getProxyConnectors() )
81 if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
82 targetRepoId, proxyConnector.getTargetRepoId() ) )
84 return proxyConnector;
90 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
91 throws RepositoryAdminException
93 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
95 throw new RepositoryAdminException(
96 "Unable to add proxy connector, as one already exists with source repository id ["
97 + proxyConnector.getSourceRepoId() + "] and target repository id ["
98 + proxyConnector.getTargetRepoId() + "]." );
101 validateProxyConnector( proxyConnector );
103 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
104 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
106 Configuration configuration = getArchivaConfiguration().getConfiguration();
108 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
109 configuration.addProxyConnector( proxyConnectorConfiguration );
110 saveConfiguration( configuration );
111 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
112 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
117 // FIXME take care of proxyConnectorRules !
118 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
119 throws RepositoryAdminException
121 Configuration configuration = getArchivaConfiguration().getConfiguration();
122 ProxyConnectorConfiguration proxyConnectorConfiguration =
123 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
124 if ( proxyConnectorConfiguration == null )
126 throw new RepositoryAdminException(
127 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
128 + proxyConnector.getTargetRepoId() );
130 configuration.removeProxyConnector( proxyConnectorConfiguration );
131 saveConfiguration( configuration );
132 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
133 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
137 // FIXME care take of proxyConnectorRules !
138 public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
139 throws RepositoryAdminException
141 Configuration configuration = getArchivaConfiguration().getConfiguration();
142 ProxyConnectorConfiguration proxyConnectorConfiguration =
143 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
144 configuration.removeProxyConnector( proxyConnectorConfiguration );
145 configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
146 saveConfiguration( configuration );
147 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
148 AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
152 protected List<String> unescapePatterns( List<String> patterns )
154 if ( patterns != null )
156 List<String> rawPatterns = new ArrayList<String>( patterns.size() );
157 for ( String pattern : patterns )
159 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
164 return Collections.emptyList();
167 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
168 throws RepositoryAdminException
170 Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, java.util.List<ProxyConnector>>();
172 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
173 while ( it.hasNext() )
175 ProxyConnector proxyConfig = it.next();
176 String key = proxyConfig.getSourceRepoId();
178 List<ProxyConnector> connectors = proxyConnectorMap.get( key );
179 if ( connectors == null )
181 connectors = new ArrayList<ProxyConnector>( 1 );
182 proxyConnectorMap.put( key, connectors );
185 connectors.add( proxyConfig );
187 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
190 return proxyConnectorMap;
193 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
194 Configuration configuration )
196 if ( StringUtils.isBlank( sourceId ) )
201 if ( StringUtils.isBlank( targetId ) )
206 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
207 return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
210 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
212 return proxyConnector == null
214 : new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
217 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
219 return proxyConnectorConfiguration == null
221 : new BeanReplicator().replicateBean( proxyConnectorConfiguration, ProxyConnector.class );
224 protected void validateProxyConnector( ProxyConnector proxyConnector )
225 throws RepositoryAdminException
227 // validate source a Managed target a Remote
228 if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
230 throw new RepositoryAdminException(
231 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
232 + " is not a ManagedRepository" );
234 if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
236 throw new RepositoryAdminException(
237 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
238 + " is not a RemoteRepository" );
241 // FIXME validate NetworkProxyConfiguration too when available