]> source.dussan.org Git - archiva.git/blob
d49e807441ef184685df6d6d27d3a6e9ed575974
[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     protected List<String> unescapePatterns( List<String> patterns )
144     {
145         List<String> rawPatterns = new ArrayList<String>();
146         if ( patterns != null )
147         {
148             for ( String pattern : patterns )
149             {
150                 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
151             }
152         }
153
154         return rawPatterns;
155     }
156
157     public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
158         throws RepositoryAdminException
159     {
160         java.util.Map<String, List<ProxyConnector>> proxyConnectorMap =
161             new HashMap<String, java.util.List<ProxyConnector>>();
162
163         Iterator<ProxyConnector> it = getProxyConnectors().iterator();
164         while ( it.hasNext() )
165         {
166             ProxyConnector proxyConfig = it.next();
167             String key = proxyConfig.getSourceRepoId();
168
169             java.util.List<ProxyConnector> connectors = proxyConnectorMap.get( key );
170             if ( connectors == null )
171             {
172                 connectors = new ArrayList<ProxyConnector>();
173                 proxyConnectorMap.put( key, connectors );
174             }
175
176             connectors.add( proxyConfig );
177
178             Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
179         }
180
181         return proxyConnectorMap;
182     }
183
184     public ProxyConnector findProxyConnector( String sourceId, String targetId )
185         throws RepositoryAdminException
186     {
187         return getProxyConnector(
188             findProxyConnector( sourceId, targetId, getArchivaConfiguration().getConfiguration() ) );
189     }
190
191     private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
192                                                             Configuration configuration )
193     {
194         if ( StringUtils.isBlank( sourceId ) )
195         {
196             return null;
197         }
198
199         if ( StringUtils.isBlank( targetId ) )
200         {
201             return null;
202         }
203
204         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
205         return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
206     }
207
208     protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
209     {
210         /*
211         ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
212         proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
213         proxyConnectorConfiguration.setBlackListPatterns(
214             new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
215         proxyConnectorConfiguration.setWhiteListPatterns(
216             new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
217         proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
218         proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
219         proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
220         proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
221         proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
222         proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
223         return proxyConnectorConfiguration;*/
224         return new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
225     }
226
227     protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
228     {
229         return new BeanReplicator().replicateBean( proxyConnectorConfiguration, ProxyConnector.class );
230     }
231
232     protected void validateProxyConnector( ProxyConnector proxyConnector )
233         throws RepositoryAdminException
234     {
235         // validate source a Managed target a Remote
236         if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
237         {
238             throw new RepositoryAdminException(
239                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
240                     + " is not a ManagedRepository" );
241         }
242         if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
243         {
244             throw new RepositoryAdminException(
245                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
246                     + " is not a RemoteRepository" );
247         }
248
249         // FIXME validate NetworkProxyConfiguration too
250     }
251 }