This project has retired. For details please refer to its
Attic page.
DefaultProxyConnectorAdmin xref
1 package org.apache.archiva.admin.repository.proxyconnector;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ProxyConnector;
24 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
25 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorOrderComparator;
26 import org.apache.archiva.admin.repository.AbstractRepositoryAdmin;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.archiva.configuration.functors.ProxyConnectorSelectionPredicate;
30 import org.apache.archiva.metadata.model.facets.AuditEvent;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.commons.collections4.IterableUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.springframework.stereotype.Service;
35
36 import javax.inject.Inject;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Map;
43
44
45
46
47
48 @Service ( "proxyConnectorAdmin#default" )
49 public class DefaultProxyConnectorAdmin
50 extends AbstractRepositoryAdmin
51 implements ProxyConnectorAdmin
52 {
53
54 @Inject
55 RepositoryRegistry repositoryRegistry;
56
57 @Override
58 public List<ProxyConnector> getProxyConnectors()
59 throws RepositoryAdminException
60 {
61 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
62 getArchivaConfiguration().getConfiguration().getProxyConnectors();
63 List<ProxyConnector> proxyConnectors = new ArrayList<>( proxyConnectorConfigurations.size() );
64 for ( ProxyConnectorConfiguration configuration : proxyConnectorConfigurations )
65 {
66 proxyConnectors.add( getProxyConnector( configuration ) );
67 }
68 Collections.sort( proxyConnectors, ProxyConnectorOrderComparator.getInstance() );
69 return proxyConnectors;
70 }
71
72 @Override
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 @Override
88 public Boolean addProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
89 throws RepositoryAdminException
90 {
91 if ( getProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId() ) != null )
92 {
93 throw new RepositoryAdminException(
94 "Unable to add proxy connector, as one already exists with source repository id ["
95 + proxyConnector.getSourceRepoId() + "] and target repository id ["
96 + proxyConnector.getTargetRepoId() + "]." );
97 }
98
99 validateProxyConnector( proxyConnector );
100
101 proxyConnector.setBlackListPatterns( unescapePatterns( proxyConnector.getBlackListPatterns() ) );
102 proxyConnector.setWhiteListPatterns( unescapePatterns( proxyConnector.getWhiteListPatterns() ) );
103
104 Configuration configuration = getArchivaConfiguration().getConfiguration();
105
106 ProxyConnectorConfiguration proxyConnectorConfiguration = getProxyConnectorConfiguration( proxyConnector );
107 configuration.addProxyConnector( proxyConnectorConfiguration );
108 saveConfiguration( configuration );
109 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
110 AuditEvent.ADD_PROXY_CONNECTOR, auditInformation );
111 return Boolean.TRUE;
112
113 }
114
115
116 @Override
117 public Boolean deleteProxyConnector( ProxyConnector proxyConnector, AuditInformation auditInformation )
118 throws RepositoryAdminException
119 {
120 Configuration configuration = getArchivaConfiguration().getConfiguration();
121 ProxyConnectorConfiguration proxyConnectorConfiguration =
122 findProxyConnector( proxyConnector.getSourceRepoId(), proxyConnector.getTargetRepoId(), configuration );
123 if ( proxyConnectorConfiguration == null )
124 {
125 throw new RepositoryAdminException(
126 "unable to find ProxyConnector with source " + proxyConnector.getSourceRepoId() + " and target "
127 + proxyConnector.getTargetRepoId() );
128 }
129 configuration.removeProxyConnector( proxyConnectorConfiguration );
130 saveConfiguration( configuration );
131 triggerAuditEvent( proxyConnector.getSourceRepoId() + "-" + proxyConnector.getTargetRepoId(), null,
132 AuditEvent.DELETE_PROXY_CONNECTOR, auditInformation );
133 return Boolean.TRUE;
134 }
135
136
137 @Override
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<>( 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 @Override
168 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap()
169 throws RepositoryAdminException
170 {
171 Map<String, List<ProxyConnector>> proxyConnectorMap = new HashMap<>();
172
173 Iterator<ProxyConnector> it = getProxyConnectors().iterator();
174 while ( it.hasNext() )
175 {
176 ProxyConnector proxyConfig = it.next();
177 String key = proxyConfig.getSourceRepoId();
178
179 List<ProxyConnector> connectors = proxyConnectorMap.get( key );
180 if ( connectors == null )
181 {
182 connectors = new ArrayList<>( 1 );
183 proxyConnectorMap.put( key, connectors );
184 }
185
186 connectors.add( proxyConfig );
187
188 Collections.sort( connectors, ProxyConnectorOrderComparator.getInstance() );
189 }
190
191 return proxyConnectorMap;
192 }
193
194 private ProxyConnectorConfiguration findProxyConnector( String sourceId, String targetId,
195 Configuration configuration )
196 {
197 if ( StringUtils.isBlank( sourceId ) )
198 {
199 return null;
200 }
201
202 if ( StringUtils.isBlank( targetId ) )
203 {
204 return null;
205 }
206
207 ProxyConnectorSelectionPredicaterSelectionPredicate.html#ProxyConnectorSelectionPredicate">ProxyConnectorSelectionPredicate selectedProxy = new ProxyConnectorSelectionPredicate( sourceId, targetId );
208 return IterableUtils.find( configuration.getProxyConnectors(), selectedProxy );
209 }
210
211 protected ProxyConnectorConfiguration getProxyConnectorConfiguration( ProxyConnector proxyConnector )
212 {
213 return proxyConnector == null
214 ? null
215 : getModelMapper().map( proxyConnector, ProxyConnectorConfiguration.class );
216 }
217
218 protected ProxyConnector getProxyConnector( ProxyConnectorConfiguration proxyConnectorConfiguration )
219 {
220 return proxyConnectorConfiguration == null
221 ? null
222 : getModelMapper().map( proxyConnectorConfiguration, ProxyConnector.class );
223 }
224
225 protected void validateProxyConnector( ProxyConnector proxyConnector )
226 throws RepositoryAdminException
227 {
228
229 if ( repositoryRegistry.getManagedRepository( proxyConnector.getSourceRepoId() ) == null )
230 {
231 throw new RepositoryAdminException(
232 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getSourceRepoId()
233 + " is not a ManagedRepository" );
234 }
235 if ( repositoryRegistry.getRemoteRepository( proxyConnector.getTargetRepoId() ) == null )
236 {
237 throw new RepositoryAdminException(
238 "non valid ProxyConnector sourceRepo with id " + proxyConnector.getTargetRepoId()
239 + " is not a RemoteRepository" );
240 }
241
242
243 }
244 }