]> source.dussan.org Git - archiva.git/blob
03ae1dfb92220906bb1fd1cb482704829cc176f9
[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 org.apache.archiva.admin.AuditInformation;
22 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
23 import org.apache.archiva.admin.repository.RepositoryAdminException;
24 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.remote.RemoteRepositoryAdmin;
26 import org.apache.archiva.audit.AuditEvent;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
31 import org.apache.maven.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
32 import org.springframework.stereotype.Service;
33
34 import javax.inject.Inject;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38
39 /**
40  * @author Olivier Lamy
41  * @since 1.4
42  */
43 @Service( "proxyConnectorAdmin#default" )
44 public class DefaultProxyConnectorAdmin
45     extends AbstractRepositoryAdmin
46     implements ProxyConnectorAdmin
47 {
48
49     @Inject
50     private ManagedRepositoryAdmin managedRepositoryAdmin;
51
52     @Inject
53     private RemoteRepositoryAdmin remoteRepositoryAdmin;
54
55     public List<ProxyConnector> getProxyConnectors()
56         throws RepositoryAdminException
57     {
58         List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
59             getArchivaConfiguration().getConfiguration().getProxyConnectors();
60         List<ProxyConnector> proxyConnectors = new ArrayList<ProxyConnector>( proxyConnectorConfigurations.size() );
61         for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
62         {
63             ProxyConnector proxyConnector = new ProxyConnector();
64             proxyConnectors.add( proxyConnector );
65             proxyConnector.setOrder( configuration.getOrder() );
66             proxyConnector.setBlackListPatterns( new ArrayList<String>( configuration.getBlackListPatterns() ) );
67             proxyConnector.setWhiteListPatterns( new ArrayList<String>( configuration.getWhiteListPatterns() ) );
68             proxyConnector.setDisabled( configuration.isDisabled() );
69             proxyConnector.setPolicies( new HashMap<String, String>( configuration.getPolicies() ) );
70             proxyConnector.setProperties( new HashMap<String, String>( configuration.getProperties() ) );
71             proxyConnector.setProxyId( configuration.getProxyId() );
72             proxyConnector.setSourceRepoId( configuration.getSourceRepoId() );
73             proxyConnector.setTargetRepoId( configuration.getTargetRepoId() );
74         }
75
76         return proxyConnectors;
77     }
78
79     public ProxyConnector getProxyConnector( String sourceRepoId, String targetRepoId )
80         throws RepositoryAdminException
81     {
82         for ( ProxyConnector proxyConnector : getProxyConnectors() )
83         {
84             if ( StringUtils.equals( sourceRepoId, proxyConnector.getSourceRepoId() ) && StringUtils.equals(
85                 targetRepoId, proxyConnector.getTargetRepoId() ) )
86             {
87                 return proxyConnector;
88             }
89         }
90         return null;
91     }
92
93     public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
94         throws RepositoryAdminException
95     {
96         if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
97         {
98             throw new RepositoryAdminException(
99                 "Unable to add proxy connector, as one already exists with source repository id ["
100                     + proxyConnector.getSourceRepoId() + "] and target repository id ["
101                     + proxyConnector.getTargetRepoId() + "]." );
102         }
103
104         validateProxyConnector( proxyConnector );
105
106         proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
107         proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
108
109         Configuration configuration = getArchivaConfiguration().getConfiguration();
110
111         ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
112         configuration.addProxyConnector( proxyConnectorConfiguration );
113         saveConfiguration( configuration );
114         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
115                            AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
116         return Boolean.TRUE;
117
118     }
119
120     public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
121         throws RepositoryAdminException
122     {
123         Configuration configuration = getArchivaConfiguration().getConfiguration();
124         ProxyConnectorConfiguration proxyConnectorConfiguration =
125             findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
126         if ( proxyConnectorConfiguration == null )
127         {
128             throw new RepositoryAdminException(
129                 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
130                     + proxyConnector.getTargetRepoId() );
131         }
132         configuration.removeProxyConnector( proxyConnectorConfiguration );
133         saveConfiguration( configuration );
134         triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
135                            AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
136         return Boolean.TRUE;
137     }
138
139     protected List<String> unescapePatterns( List<String> patterns )
140     {
141         List<String> rawPatterns = new ArrayList<String>();
142         if ( patterns != null )
143         {
144             for ( String pattern : patterns )
145             {
146                 rawPatterns.add( StringUtils.replace( pattern, "\\\\", "\\" ) );
147             }
148         }
149
150         return rawPatterns;
151     }
152
153     protected ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
154                                                               Configuration configuration )
155     {
156         if ( StringUtils.isBlank( sourceId ) )
157         {
158             return null;
159         }
160
161         if ( StringUtils.isBlank( targetId ) )
162         {
163             return null;
164         }
165
166         ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
167         return (ProxyConnectorConfiguration) CollectionUtils.find( configuration.getProxyConnectors(), selectedProxy );
168     }
169
170     protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
171     {
172         ProxyConnectorConfiguration proxyConnectorConfiguration = new ProxyConnectorConfiguration();
173         proxyConnectorConfiguration.setOrder( proxyConnector.getOrder() );
174         proxyConnectorConfiguration.setBlackListPatterns(
175             new ArrayList<String>( proxyConnector.getBlackListPatterns() ) );
176         proxyConnectorConfiguration.setWhiteListPatterns(
177             new ArrayList<String>( proxyConnector.getWhiteListPatterns() ) );
178         proxyConnectorConfiguration.setDisabled( proxyConnector.isDisabled() );
179         proxyConnectorConfiguration.setPolicies( new HashMap( proxyConnector.getPolicies() ) );
180         proxyConnectorConfiguration.setProperties( new HashMap( proxyConnector.getProperties() ) );
181         proxyConnectorConfiguration.setProxyId( proxyConnector.getProxyId() );
182         proxyConnectorConfiguration.setSourceRepoId( proxyConnector.getSourceRepoId() );
183         proxyConnectorConfiguration.setTargetRepoId( proxyConnector.getTargetRepoId() );
184         return proxyConnectorConfiguration;
185     }
186
187     protected void validateProxyConnector( ProxyConnector proxyConnector )
188         throws RepositoryAdminException
189     {
190         // validate source a Managed target a Remote
191         if ( managedRepositoryAdmin.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
192         {
193             throw new RepositoryAdminException(
194                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
195                     + " is not a ManagedRepository" );
196         }
197         if ( remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
198         {
199             throw new RepositoryAdminException(
200                 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
201                     + " is not a RemoteRepository" );
202         }
203
204         // FIXME validate NetworkProxyConfiguration too
205     }
206 }