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.proxyconnector.ProxyConnectorAdmin;
25 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorOrderComparator;
26 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
30 import org.apache.archiva.metadata.model.facets.AuditEvent;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.commons.collections4.IterableUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.springframework.stereotype.Service;
36 import javax.inject.Inject;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
45 * @author Olivier Lamy
48 @Service ( "proxyConnectorAdmin#default" )
49 public class DefaultProxyConnectorAdmin
50 extends AbstractRepositoryAdmin
51 implements ProxyConnectorAdmin
55 RepositoryRegistry repositoryRegistry;
58 public List<ProxyConnector> getProxyConnectors()
59 throws RepositoryAdminException
61 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
62 getArchivaConfiguration().getConfiguration().getProxyConnectors();
63 List<ProxyConnector> proxyConnectors = new ArrayList<>( proxyConnectorConfigurations.size() );
64 for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
66 proxyConnectors.add( getProxyConnector( configuration ) );
68 Collections.sort( proxyConnectors, ProxyConnectorOrderComparator.getInstance() );
69 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;
88 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
89 throws RepositoryAdminException
91 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
93 throw new RepositoryAdminException(
94 "Unable to add proxy connector, as one already exists with source repository id ["
95 + proxyConnector.getSourceRepoId() + "] and target repository id ["
96 + proxyConnector.getTargetRepoId() + "]." );
99 validateProxyConnector( proxyConnector );
101 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
102 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
104 Configuration configuration = getArchivaConfiguration().getConfiguration();
106 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
107 configuration.addProxyConnector( proxyConnectorConfiguration );
108 saveConfiguration( configuration );
109 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
110 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
115 // FIXME take care of proxyConnectorRules !
117 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
118 throws RepositoryAdminException
120 Configuration configuration = getArchivaConfiguration().getConfiguration();
121 ProxyConnectorConfiguration proxyConnectorConfiguration =
122 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
123 if ( proxyConnectorConfiguration == null )
125 throw new RepositoryAdminException(
126 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
127 + proxyConnector.getTargetRepoId() );
129 configuration.removeProxyConnector( proxyConnectorConfiguration );
130 saveConfiguration( configuration );
131 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
132 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
136 // 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<>( patterns.size() );
157 for ( String pattern : patterns )
159 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
164 return Collections.emptyList();
168 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
169 throws RepositoryAdminException
171 Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<>();
173 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
174 while ( it.hasNext() )
176 ProxyConnector proxyConfig = it.next();
177 String key = proxyConfig.getSourceRepoId();
179 List<ProxyConnector> connectors = proxyConnectorMap.get( key );
180 if ( connectors == null )
182 connectors = new ArrayList<>( 1 );
183 proxyConnectorMap.put( key, connectors );
186 connectors.add( proxyConfig );
188 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
191 return proxyConnectorMap;
194 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
195 Configuration configuration )
197 if ( StringUtils.isBlank( sourceId ) )
202 if ( StringUtils.isBlank( targetId ) )
207 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
208 return IterableUtils.find( configuration.getProxyConnectors(), selectedProxy );
211 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
213 return proxyConnector == null
215 : getModelMapper().map( proxyConnector, ProxyConnectorConfiguration.class );
218 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
220 return proxyConnectorConfiguration == null
222 : getModelMapper().map( proxyConnectorConfiguration, ProxyConnector.class );
225 protected void validateProxyConnector( ProxyConnector proxyConnector )
226 throws RepositoryAdminException
228 // validate source a Managed target a Remote
229 if ( repositoryRegistry.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
231 throw new RepositoryAdminException(
232 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
233 + " is not a ManagedRepository" );
235 if ( repositoryRegistry.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
237 throw new RepositoryAdminException(
238 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
239 + " is not a RemoteRepository" );
242 // FIXME validate NetworkProxyConfiguration too when available