]> source.dussan.org Git - archiva.git/blob
d05c65a2f2b0fd7adc40194d99fb5639c3dd0ce2
[archiva.git] /
1 package org.apache.archiva.admin.repository.proxyconnectorrule;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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.admin.model.beans.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.lang.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Service;
35
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
39
40 /**
41  * @author Olivier Lamy
42  * @since 1.4-M3
43  */
44 @Service ("proxyConnectorRuleAdmin#default")
45 public class DefaultProxyConnectorRuleAdmin
46     extends AbstractRepositoryAdmin
47     implements ProxyConnectorRuleAdmin
48 {
49     private Logger log = LoggerFactory.getLogger( getClass() );
50
51     public List<ProxyConnectorRule> getProxyConnectorRules()
52         throws RepositoryAdminException
53     {
54         List<ProxyConnectorRuleConfiguration> proxyConnectorRuleConfigurations =
55             getArchivaConfiguration().getConfiguration().getProxyConnectorRuleConfigurations();
56         if ( proxyConnectorRuleConfigurations.isEmpty() )
57         {
58             return Collections.emptyList();
59         }
60         List<ProxyConnectorRule> proxyConnectorRules =
61             new ArrayList<ProxyConnectorRule>( proxyConnectorRuleConfigurations.size() );
62         for ( ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : proxyConnectorRuleConfigurations )
63         {
64
65             ProxyConnectorRule proxyConnectorRule = new ProxyConnectorRule();
66             proxyConnectorRule.setPattern( proxyConnectorRuleConfiguration.getPattern() );
67             proxyConnectorRule.setProxyConnectorRuleType(
68                 getProxyConnectorRuleType( proxyConnectorRuleConfiguration.getRuleType() ) );
69             for ( ProxyConnectorConfiguration proxyConnectorConfiguration : proxyConnectorRuleConfiguration.getProxyConnectors() )
70             {
71                 ProxyConnector proxyConnector = new ProxyConnector();
72                 proxyConnector.setSourceRepoId( proxyConnectorConfiguration.getSourceRepoId() );
73                 proxyConnector.setTargetRepoId( proxyConnectorConfiguration.getTargetRepoId() );
74                 proxyConnectorRule.getProxyConnectors().add( proxyConnector );
75             }
76             proxyConnectorRules.add( proxyConnectorRule );
77         }
78
79         return proxyConnectorRules;
80     }
81
82
83     private ProxyConnectorRuleType getProxyConnectorRuleType( String type )
84     {
85         if ( StringUtils.equals( ProxyConnectorRuleType.WHITE_LIST.getRuleType(), type ) )
86         {
87             return ProxyConnectorRuleType.WHITE_LIST;
88         }
89         if ( StringUtils.equals( ProxyConnectorRuleType.BLACK_LIST.getRuleType(), type ) )
90         {
91             return ProxyConnectorRuleType.BLACK_LIST;
92         }
93         return null;
94     }
95
96     public void addProxyConnectorRule( ProxyConnectorRule proxyConnectorRule, AuditInformation auditInformation )
97         throws RepositoryAdminException
98     {
99         ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration = new ProxyConnectorRuleConfiguration();
100         proxyConnectorRuleConfiguration.setPattern( proxyConnectorRule.getPattern() );
101         proxyConnectorRuleConfiguration.setRuleType( proxyConnectorRule.getProxyConnectorRuleType().getRuleType() );
102         for ( ProxyConnector proxyConnector : proxyConnectorRule.getProxyConnectors() )
103         {
104             ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
105             proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
106             proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
107             proxyConnectorRuleConfiguration.getProxyConnectors().add( proxyConnectorConfiguration );
108         }
109         Configuration configuration = getArchivaConfiguration().getConfiguration();
110         configuration.getProxyConnectorRuleConfigurations().add( proxyConnectorRuleConfiguration );
111         saveConfiguration( configuration );
112     }
113
114     public void deleteProxyConnectorRule( ProxyConnectorRule proxyConnectorRule, AuditInformation auditInformation )
115         throws RepositoryAdminException
116     {
117         Configuration configuration = getArchivaConfiguration().getConfiguration();
118         // key is the pattern !!
119         // recreate a list without the pattern
120
121         boolean toSave = false;
122
123         List<ProxyConnectorRuleConfiguration> proxyConnectorRuleConfigurations =
124             new ArrayList<ProxyConnectorRuleConfiguration>();
125
126         for ( ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : configuration.getProxyConnectorRuleConfigurations() )
127         {
128             if ( StringUtils.equals( proxyConnectorRuleConfiguration.getPattern(), proxyConnectorRule.getPattern() )
129                 && StringUtils.equals( proxyConnectorRuleConfiguration.getRuleType(),
130                                        proxyConnectorRule.getProxyConnectorRuleType().toString() ) )
131             {
132                 toSave = true;
133             }
134             else
135             {
136                 proxyConnectorRuleConfigurations.add( proxyConnectorRuleConfiguration );
137             }
138         }
139
140         if ( toSave )
141         {
142             configuration.setProxyConnectorRuleConfigurations( proxyConnectorRuleConfigurations );
143             saveConfiguration( configuration );
144         }
145
146     }
147
148     public void updateProxyConnectorRule( ProxyConnectorRule proxyConnectorRule, AuditInformation auditInformation )
149         throws RepositoryAdminException
150     {
151         Configuration configuration = getArchivaConfiguration().getConfiguration();
152
153         for ( ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : configuration.getProxyConnectorRuleConfigurations() )
154         {
155             if ( StringUtils.equals( proxyConnectorRuleConfiguration.getPattern(), proxyConnectorRule.getPattern() )
156                 && StringUtils.equals( proxyConnectorRuleConfiguration.getRuleType(),
157                                        proxyConnectorRule.getProxyConnectorRuleType().toString() ) )
158             {
159                 List<ProxyConnectorConfiguration> proxyConnectors =
160                     new ArrayList<ProxyConnectorConfiguration>( proxyConnectorRule.getProxyConnectors().size() );
161                 for ( ProxyConnector proxyConnector : proxyConnectorRule.getProxyConnectors() )
162                 {
163                     ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
164                     proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
165                     proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
166                     proxyConnectors.add( proxyConnectorConfiguration );
167                 }
168                 proxyConnectorRuleConfiguration.setProxyConnectors( proxyConnectors );
169                 saveConfiguration( configuration );
170             }
171         }
172
173     }
174 }