]> source.dussan.org Git - archiva.git/blob
7b77cfd3c70377f863e5273e2a717180c80b4b35
[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             proxyConnectors.add( getProxyConnector( configuration ) );
68         }
69
70         return proxyConnectors;
71     }
72
73     public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
74         throws RepositoryAdminException
75     {
76         for ( ProxyConnector proxyConnector : getProxyConnectors() )
77         {
78             if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
79                 targetRepoId, proxyConnector.getTargetRepoId() ) )
80             {
81                 return proxyConnector;
82             }
83         }
84         return null;
85     }
86
87     public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
88         throws RepositoryAdminException
89     {
90         if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
91         {
92             throw new RepositoryAdminException(
93                 "Unable to add proxy connector, as one already exists with source repository id ["
94                     + proxyConnector.getSourceRepoId() + "] and target repository id ["
95                     + proxyConnector.getTargetRepoId() + "]." );
96         }
97
98         validateProxyConnector( proxyConnector );
99
100         proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
101         proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
102
103         Configuration configuration = getArchivaConfiguration().getConfiguration();
104
105         ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
106         configuration.addProxyConnector( proxyConnectorConfiguration );
107         saveConfiguration( configuration );
108         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
109                            AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
110         return Boolean.TRUE;
111
112     }
113
114     public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
115         throws RepositoryAdminException
116     {
117         Configuration configuration = getArchivaConfiguration().getConfiguration();
118         ProxyConnectorConfiguration proxyConnectorConfiguration =
119             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
120         if ( proxyConnectorConfiguration == null )
121         {
122             throw new RepositoryAdminException(
123                 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
124                     + proxyConnector.getTargetRepoId() );
125         }
126         configuration.removeProxyConnector( proxyConnectorConfiguration );
127         saveConfiguration( configuration );
128         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
129                            AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
130         return Boolean.TRUE;
131     }
132
133     public Boolean updateProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
134         throws RepositoryAdminException
135     {
136         Configuration configuration = getArchivaConfiguration().getConfiguration();
137         ProxyConnectorConfiguration proxyConnectorConfiguration =
138             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
139         configuration.removeProxyConnector( proxyConnectorConfiguration );
140         configuration.addProxyConnector( getProxyConnectorConfiguration( proxyConnector ) );
141         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
142                            AuditEvent.MODIFY_PROXY_CONNECTOR, auditInformation );
143         return Boolean.TRUE;
144     }
145
146     protected List<String> unescapePatterns( List<String> patterns )
147     {
148         List<String> rawPatterns = new ArrayList<String>();
149         if ( patterns != null )
150         {
151             for ( String pattern : patterns )
152             {
153                 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
154             }
155         }
156
157         return rawPatterns;
158     }
159
160     public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
161         throws RepositoryAdminException
162     {
163         Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<String, java.util.List<ProxyConnector>>();
164
165         Iterator<ProxyConnector> it = getProxyConnectors().iterator();
166         while ( it.hasNext() )
167         {
168             ProxyConnector proxyConfig = it.next();
169             String key = proxyConfig.getSourceRepoId();
170
171             List<ProxyConnector> connectors = proxyConnectorMap.get( key );
172             if ( connectors == null )
173             {
174                 connectors = new ArrayList<ProxyConnector>();
175                 proxyConnectorMap.put( key, connectors );
176             }
177
178             connectors.add( proxyConfig );
179
180             Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
181         }
182
183         return proxyConnectorMap;
184     }
185
186     private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
187                                                             Configuration configuration )
188     {
189         if ( StringUtils.isBlank( sourceId ) )
190         {
191             return null;
192         }
193
194         if ( StringUtils.isBlank( targetId ) )
195         {
196             return null;
197         }
198
199         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
200         return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
201     }
202
203     protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
204     {
205         /*
206         ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
207         proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
208         proxyConnectorConfiguration.setBlackListPatterns(
209             new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
210         proxyConnectorConfiguration.setWhiteListPatterns(
211             new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
212         proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
213         proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
214         proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
215         proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
216         proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
217         proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
218         return proxyConnectorConfiguration;*/
219
220         return proxyConnector == null
221             ? null
222             : new BeanReplicator().replicateBean( proxyConnector, ProxyConnectorConfiguration.class );
223     }
224
225     protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
226     {
227         return proxyConnectorConfiguration == null
228             ? null
229             : 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 when available
250     }
251 }