]> source.dussan.org Git - archiva.git/blob
c83a57766a07a42530a45b86a1f5ab54c0a70a06
[archiva.git] /
1 package org.apache.archiva.admin.repository.proxyconnector;
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 net.sf.beanlib.provider.replicator.BeanReplicator;
22 import org.apache.archiva.admin.AuditInformation;
23 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
24 import org.apache.archiva.admin.repository.RepositoryAdminException;
25 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
26 import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
27 import org.apache.archiva.audit.AuditEvent;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
33 import org.springframework.stereotype.Service;
34
35 import javax.inject.Inject;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Iterator;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44  * @author Olivier Lamy
45  * @since 1.4
46  */
47 @Service( "proxyConnectorAdmin#default" )
48 public class DefaultProxyConnectorAdmin
49     extends AbstractRepositoryAdmin
50     implements ProxyConnectorAdmin
51 {
52
53     @Inject
54     private ManagedRepositoryAdmin managedRepositoryAdmin;
55
56     @Inject
57     private RemoteRepositoryAdmin remoteRepositoryAdmin;
58
59     public List<ProxyConnector> getProxyConnectors()
60         throws RepositoryAdminException
61     {
62         List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
63             getArchivaConfiguration().getConfiguration().getProxyConnectors();
64         List<ProxyConnector> proxyConnectors = new ArrayList<ProxyConnector>( proxyConnectorConfigurations.size() );
65         for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
66         {
67             ProxyConnector proxyConnector = new ProxyConnector();
68             proxyConnectors.add( proxyConnector );
69             proxyConnector.setOrder( configuration.getOrder() );
70             proxyConnector.setBlackListPatterns( new ArrayList<String>( configuration.getBlackListPatterns() ) );
71             proxyConnector.setWhiteListPatterns( new ArrayList<String>( configuration.getWhiteListPatterns() ) );
72             proxyConnector.setDisabled( configuration.isDisabled() );
73             proxyConnector.setPolicies( new HashMap<String, String>( configuration.getPolicies() ) );
74             proxyConnector.setProperties( new HashMap<String, String>( configuration.getProperties() ) );
75             proxyConnector.setProxyId( configuration.getProxyId() );
76             proxyConnector.setSourceRepoId( configuration.getSourceRepoId() );
77             proxyConnector.setTargetRepoId( configuration.getTargetRepoId() );
78         }
79
80         return proxyConnectors;
81     }
82
83     public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
84         throws RepositoryAdminException
85     {
86         for ( ProxyConnector proxyConnector : getProxyConnectors() )
87         {
88             if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
89                 targetRepoId, proxyConnector.getTargetRepoId() ) )
90             {
91                 return proxyConnector;
92             }
93         }
94         return null;
95     }
96
97     public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
98         throws RepositoryAdminException
99     {
100         if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
101         {
102             throw new RepositoryAdminException(
103                 "Unable to add proxy connector, as one already exists with source repository id ["
104                     + proxyConnector.getSourceRepoId() + "] and target repository id ["
105                     + proxyConnector.getTargetRepoId() + "]." );
106         }
107
108         validateProxyConnector( proxyConnector );
109
110         proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
111         proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
112
113         Configuration configuration = getArchivaConfiguration().getConfiguration();
114
115         ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
116         configuration.addProxyConnector( proxyConnectorConfiguration );
117         saveConfiguration( configuration );
118         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
119                            AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
120         return Boolean.TRUE;
121
122     }
123
124     public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
125         throws RepositoryAdminException
126     {
127         Configuration configuration = getArchivaConfiguration().getConfiguration();
128         ProxyConnectorConfiguration proxyConnectorConfiguration =
129             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
130         if ( proxyConnectorConfiguration == null )
131         {
132             throw new RepositoryAdminException(
133                 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
134                     + proxyConnector.getTargetRepoId() );
135         }
136         configuration.removeProxyConnector( proxyConnectorConfiguration );
137         saveConfiguration( configuration );
138         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
139                            AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
140         return Boolean.TRUE;
141     }
142
143     public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
144         throws RepositoryAdminException
145     {
146         Configuration configuration = getArchivaConfiguration().getConfiguration();
147         ProxyConnectorConfiguration proxyConnectorConfiguration =
148             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
149         configuration.removeProxyConnector( proxyConnectorConfiguration );
150         configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
151         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
152                            AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
153         return Boolean.TRUE;
154     }
155
156     protected List<String> unescapePatterns( List<String> patterns )
157     {
158         List<String> rawPatterns = new ArrayList<String>();
159         if ( patterns != null )
160         {
161             for ( String pattern : patterns )
162             {
163                 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
164             }
165         }
166
167         return rawPatterns;
168     }
169
170     public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
171         throws RepositoryAdminException
172     {
173         Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, java.util.List<ProxyConnector>>();
174
175         Iterator<ProxyConnector> it = getProxyConnectors().iterator();
176         while ( it.hasNext() )
177         {
178             ProxyConnector proxyConfig = it.next();
179             String key = proxyConfig.getSourceRepoId();
180
181             List<ProxyConnector> connectors = proxyConnectorMap.get( key );
182             if ( connectors == null )
183             {
184                 connectors = new ArrayList<ProxyConnector>();
185                 proxyConnectorMap.put( key, connectors );
186             }
187
188             connectors.add( proxyConfig );
189
190             Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
191         }
192
193         return proxyConnectorMap;
194     }
195
196     public ProxyConnector findProxyConnector( String sourceId, String targetId )
197         throws RepositoryAdminException
198     {
199         return getProxyConnector(
200             findProxyConnector( sourceId, targetId, getArchivaConfiguration().getConfiguration() ) );
201     }
202
203     private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
204                                                             Configuration configuration )
205     {
206         if ( StringUtils.isBlank( sourceId ) )
207         {
208             return null;
209         }
210
211         if ( StringUtils.isBlank( targetId ) )
212         {
213             return null;
214         }
215
216         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
217         return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
218     }
219
220     protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
221     {
222         /*
223         ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
224         proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
225         proxyConnectorConfiguration.setBlackListPatterns(
226             new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
227         proxyConnectorConfiguration.setWhiteListPatterns(
228             new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
229         proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
230         proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
231         proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
232         proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
233         proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
234         proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
235         return proxyConnectorConfiguration;*/
236
237         return proxyConnector == null
238             ? null
239             : new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
240     }
241
242     protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
243     {
244         return proxyConnectorConfiguration == null
245             ? null
246             : new BeanReplicator().replicateBean( proxyConnectorConfiguration, ProxyConnector.class );
247     }
248
249     protected void validateProxyConnector( ProxyConnector proxyConnector )
250         throws RepositoryAdminException
251     {
252         // validate source a Managed target a Remote
253         if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
254         {
255             throw new RepositoryAdminException(
256                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
257                     + " is not a ManagedRepository" );
258         }
259         if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
260         {
261             throw new RepositoryAdminException(
262                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
263                     + " is not a RemoteRepository" );
264         }
265
266         // FIXME validate NetworkProxyConfiguration too when available
267     }
268 }