1 package org.apache.archiva.admin.repository.proxyconnectorrule;
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.beans.ProxyConnectorRule;
25 import org.apache.archiva.proxy.model.ProxyConnectorRuleType;
26 import org.apache.archiva.admin.model.proxyconnectorrule.ProxyConnectorRuleAdmin;
27 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
30 import org.apache.archiva.configuration.ProxyConnectorRuleConfiguration;
31 import org.apache.commons.lang3.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Service;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
41 * @author Olivier Lamy
44 @Service("proxyConnectorRuleAdmin#default")
45 public class DefaultProxyConnectorRuleAdmin
46 extends AbstractRepositoryAdmin
47 implements ProxyConnectorRuleAdmin
49 private Logger log = LoggerFactory.getLogger( getClass() );
52 public List<ProxyConnectorRule> getProxyConnectorRules()
53 throws RepositoryAdminException
55 List<ProxyConnectorRuleConfiguration> proxyConnectorRuleConfigurations =
56 getArchivaConfiguration().getConfiguration().getProxyConnectorRuleConfigurations();
57 if ( proxyConnectorRuleConfigurations.isEmpty() )
59 return Collections.emptyList();
61 List<ProxyConnectorRule> proxyConnectorRules = new ArrayList<>( proxyConnectorRuleConfigurations.size() );
62 for ( ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : proxyConnectorRuleConfigurations )
65 ProxyConnectorRule proxyConnectorRule = new ProxyConnectorRule();
66 proxyConnectorRule.setPattern( proxyConnectorRuleConfiguration.getPattern() );
67 proxyConnectorRule.setProxyConnectorRuleType(
68 getProxyConnectorRuleType( proxyConnectorRuleConfiguration.getRuleType() ) );
69 for ( ProxyConnectorConfiguration proxyConnectorConfiguration : proxyConnectorRuleConfiguration.getProxyConnectors() )
71 ProxyConnector proxyConnector = new ProxyConnector();
72 proxyConnector.setSourceRepoId( proxyConnectorConfiguration.getSourceRepoId() );
73 proxyConnector.setTargetRepoId( proxyConnectorConfiguration.getTargetRepoId() );
74 proxyConnectorRule.getProxyConnectors().add( proxyConnector );
76 proxyConnectorRules.add( proxyConnectorRule );
79 return proxyConnectorRules;
83 private ProxyConnectorRuleType getProxyConnectorRuleType( String type )
85 if ( StringUtils.equals( ProxyConnectorRuleType.WHITE_LIST.getRuleType(), type ) )
87 return ProxyConnectorRuleType.WHITE_LIST;
89 if ( StringUtils.equals( ProxyConnectorRuleType.BLACK_LIST.getRuleType(), type ) )
91 return ProxyConnectorRuleType.BLACK_LIST;
97 public void addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule, AuditInformation auditInformation )
98 throws RepositoryAdminException
100 ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration = new ProxyConnectorRuleConfiguration();
101 proxyConnectorRuleConfiguration.setPattern( proxyConnectorRule.getPattern() );
102 proxyConnectorRuleConfiguration.setRuleType( proxyConnectorRule.getProxyConnectorRuleType().getRuleType() );
103 for ( ProxyConnector proxyConnector : proxyConnectorRule.getProxyConnectors() )
105 ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
106 proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
107 proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
108 proxyConnectorRuleConfiguration.getProxyConnectors().add( proxyConnectorConfiguration );
110 Configuration configuration = getArchivaConfiguration().getConfiguration();
111 configuration.getProxyConnectorRuleConfigurations().add( proxyConnectorRuleConfiguration );
112 saveConfiguration( configuration );
116 public void deleteProxyConnectorRule( ProxyConnectorRule proxyConnectorRule, AuditInformation auditInformation )
117 throws RepositoryAdminException
119 Configuration configuration = getArchivaConfiguration().getConfiguration();
120 // key is the pattern !!
121 // recreate a list without the pattern
123 boolean toSave = false;
125 List<ProxyConnectorRuleConfiguration> proxyConnectorRuleConfigurations = new ArrayList<>();
127 for ( ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : configuration.getProxyConnectorRuleConfigurations() )
129 if ( StringUtils.equals( proxyConnectorRuleConfiguration.getPattern(), proxyConnectorRule.getPattern() )
130 && StringUtils.equals( proxyConnectorRuleConfiguration.getRuleType(),
131 proxyConnectorRule.getProxyConnectorRuleType().getRuleType() ) )
137 proxyConnectorRuleConfigurations.add( proxyConnectorRuleConfiguration );
143 configuration.setProxyConnectorRuleConfigurations( proxyConnectorRuleConfigurations );
144 saveConfiguration( configuration );
150 public void updateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule, AuditInformation auditInformation )
151 throws RepositoryAdminException
153 Configuration configuration = getArchivaConfiguration().getConfiguration();
155 for ( ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : configuration.getProxyConnectorRuleConfigurations() )
157 if ( StringUtils.equals( proxyConnectorRuleConfiguration.getPattern(), proxyConnectorRule.getPattern() )
158 && StringUtils.equals( proxyConnectorRuleConfiguration.getRuleType(),
159 proxyConnectorRule.getProxyConnectorRuleType().getRuleType() ) )
161 List<ProxyConnectorConfiguration> proxyConnectors =
162 new ArrayList<>( proxyConnectorRule.getProxyConnectors().size() );
163 for ( ProxyConnector proxyConnector : proxyConnectorRule.getProxyConnectors() )
165 ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
166 proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
167 proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
168 proxyConnectors.add( proxyConnectorConfiguration );
170 proxyConnectorRuleConfiguration.setProxyConnectors( proxyConnectors );
171 saveConfiguration( configuration );