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.AuditInformation;
22 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
23 import org.apache.archiva.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
26 import org.apache.archiva.audit.AuditEvent;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
31 import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
32 import org.springframework.stereotype.Service;
34 import javax.inject.Inject;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
40 * @author Olivier Lamy
43 @Service( "proxyConnectorAdmin#default" )
44 public class DefaultProxyConnectorAdmin
45 extends AbstractRepositoryAdmin
46 implements ProxyConnectorAdmin
50 private ManagedRepositoryAdmin managedRepositoryAdmin;
53 private RemoteRepositoryAdmin remoteRepositoryAdmin;
55 public List<ProxyConnector> getProxyConnectors()
56 throws RepositoryAdminException
58 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
59 getArchivaConfiguration().getConfiguration().getProxyConnectors();
60 List<ProxyConnector> proxyConnectors = new ArrayList<ProxyConnector>( proxyConnectorConfigurations.size() );
61 for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
63 ProxyConnector proxyConnector = new ProxyConnector();
64 proxyConnectors.add( proxyConnector );
65 proxyConnector.setOrder( configuration.getOrder() );
66 proxyConnector.setBlackListPatterns( new ArrayList<String>( configuration.getBlackListPatterns() ) );
67 proxyConnector.setWhiteListPatterns( new ArrayList<String>( configuration.getWhiteListPatterns() ) );
68 proxyConnector.setDisabled( configuration.isDisabled() );
69 proxyConnector.setPolicies( new HashMap<String, String>( configuration.getPolicies() ) );
70 proxyConnector.setProperties( new HashMap<String, String>( configuration.getProperties() ) );
71 proxyConnector.setProxyId( configuration.getProxyId() );
72 proxyConnector.setSourceRepoId( configuration.getSourceRepoId() );
73 proxyConnector.setTargetRepoId( configuration.getTargetRepoId() );
76 return proxyConnectors;
79 public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
80 throws RepositoryAdminException
82 for ( ProxyConnector proxyConnector : getProxyConnectors() )
84 if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
85 targetRepoId, proxyConnector.getTargetRepoId() ) )
87 return proxyConnector;
93 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
94 throws RepositoryAdminException
96 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
98 throw new RepositoryAdminException(
99 "Unable to add proxy connector, as one already exists with source repository id ["
100 + proxyConnector.getSourceRepoId() + "] and target repository id ["
101 + proxyConnector.getTargetRepoId() + "]." );
104 validateProxyConnector( proxyConnector );
106 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
107 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
109 Configuration configuration = getArchivaConfiguration().getConfiguration();
111 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
112 configuration.addProxyConnector( proxyConnectorConfiguration );
113 saveConfiguration( configuration );
114 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
115 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
120 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
121 throws RepositoryAdminException
123 Configuration configuration = getArchivaConfiguration().getConfiguration();
124 ProxyConnectorConfiguration proxyConnectorConfiguration =
125 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
126 if ( proxyConnectorConfiguration == null )
128 throw new RepositoryAdminException(
129 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
130 + proxyConnector.getTargetRepoId() );
132 configuration.removeProxyConnector( proxyConnectorConfiguration );
133 saveConfiguration( configuration );
134 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
135 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
139 protected List<String> unescapePatterns( List<String> patterns )
141 List<String> rawPatterns = new ArrayList<String>();
142 if ( patterns != null )
144 for ( String pattern : patterns )
146 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
153 protected ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
154 Configuration configuration )
156 if ( StringUtils.isBlank( sourceId ) )
161 if ( StringUtils.isBlank( targetId ) )
166 ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
167 return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
170 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
172 ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
173 proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
174 proxyConnectorConfiguration.setBlackListPatterns(
175 new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
176 proxyConnectorConfiguration.setWhiteListPatterns(
177 new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
178 proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
179 proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
180 proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
181 proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
182 proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
183 proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
184 return proxyConnectorConfiguration;
187 protected void validateProxyConnector( ProxyConnector proxyConnector )
188 throws RepositoryAdminException
190 // validate source a Managed target a Remote
191 if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
193 throw new RepositoryAdminException(
194 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
195 + " is not a ManagedRepository" );
197 if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
199 throw new RepositoryAdminException(
200 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
201 + " is not a RemoteRepository" );
204 // FIXME validate NetworkProxyConfiguration too