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 org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ProxyConnector;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
26 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorOrderComparator;
27 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
28 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
29 import org.apache.archiva.audit.AuditEvent;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.lang.StringUtils;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.List;
46 * @author Olivier Lamy
49 @Service ( "proxyConnectorAdmin#default" )
50 public class DefaultProxyConnectorAdmin
51 extends AbstractRepositoryAdmin
52 implements ProxyConnectorAdmin
56 private ManagedRepositoryAdmin managedRepositoryAdmin;
59 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<>( proxyConnectorConfigurations.size() );
68 for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
70 proxyConnectors.add( getProxyConnector( configuration ) );
72 Collections.sort( proxyConnectors, ProxyConnectorOrderComparator.getInstance() );
73 return proxyConnectors;
77 public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
78 throws RepositoryAdminException
80 for ( ProxyConnector proxyConnector : getProxyConnectors() )
82 if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
83 targetRepoId, proxyConnector.getTargetRepoId() ) )
85 return proxyConnector;
92 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
93 throws RepositoryAdminException
95 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
97 throw new RepositoryAdminException(
98 "Unable to add proxy connector, as one already exists with source repository id ["
99 + proxyConnector.getSourceRepoId() + "] and target repository id ["
100 + proxyConnector.getTargetRepoId() + "]." );
103 validateProxyConnector( proxyConnector );
105 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
106 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
108 Configuration configuration = getArchivaConfiguration().getConfiguration();
110 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
111 configuration.addProxyConnector( proxyConnectorConfiguration );
112 saveConfiguration( configuration );
113 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
114 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
119 // FIXME take care of proxyConnectorRules !
121 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
122 throws RepositoryAdminException
124 Configuration configuration = getArchivaConfiguration().getConfiguration();
125 ProxyConnectorConfiguration proxyConnectorConfiguration =
126 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
127 if ( proxyConnectorConfiguration == null )
129 throw new RepositoryAdminException(
130 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
131 + proxyConnector.getTargetRepoId() );
133 configuration.removeProxyConnector( proxyConnectorConfiguration );
134 saveConfiguration( configuration );
135 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
136 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
140 // FIXME care take of proxyConnectorRules !
142 public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
143 throws RepositoryAdminException
145 Configuration configuration = getArchivaConfiguration().getConfiguration();
146 ProxyConnectorConfiguration proxyConnectorConfiguration =
147 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
148 configuration.removeProxyConnector( proxyConnectorConfiguration );
149 configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
150 saveConfiguration( configuration );
151 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
152 AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
156 protected List<String> unescapePatterns( List<String> patterns )
158 if ( patterns != null )
160 List<String> rawPatterns = new ArrayList<>( patterns.size() );
161 for ( String pattern : patterns )
163 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
168 return Collections.emptyList();
172 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
173 throws RepositoryAdminException
175 Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<>();
177 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
178 while ( it.hasNext() )
180 ProxyConnector proxyConfig = it.next();
181 String key = proxyConfig.getSourceRepoId();
183 List<ProxyConnector> connectors = proxyConnectorMap.get( key );
184 if ( connectors == null )
186 connectors = new ArrayList<>( 1 );
187 proxyConnectorMap.put( key, connectors );
190 connectors.add( proxyConfig );
192 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
195 return proxyConnectorMap;
198 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
199 Configuration configuration )
201 if ( StringUtils.isBlank( sourceId ) )
206 if ( StringUtils.isBlank( targetId ) )
211 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
212 return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
215 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
217 return proxyConnector == null
219 : getModelMapper().map( proxyConnector, ProxyConnectorConfiguration.class );
222 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
224 return proxyConnectorConfiguration == null
226 : getModelMapper().map( proxyConnectorConfiguration, ProxyConnector.class );
229 protected void validateProxyConnector( ProxyConnector proxyConnector )
230 throws RepositoryAdminException
232 // validate source a Managed target a Remote
233 if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
235 throw new RepositoryAdminException(
236 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
237 + " is not a ManagedRepository" );
239 if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
241 throw new RepositoryAdminException(
242 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
243 + " is not a RemoteRepository" );
246 // FIXME validate NetworkProxyConfiguration too when available