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