]> source.dussan.org Git - archiva.git/blob
a86cb3701c6e04fb8b17f8d75329e5065ff1ff54
[archiva.git] /
1 package org.apache.archiva.proxy.base;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
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.configuration.ArchivaConfiguration;
22 import org.apache.archiva.configuration.NetworkProxyConfiguration;
23 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
24 import org.apache.archiva.policies.Policy;
25 import org.apache.archiva.policies.PolicyOption;
26 import org.apache.archiva.policies.PolicyUtil;
27 import org.apache.archiva.proxy.ProxyRegistry;
28 import org.apache.archiva.proxy.model.NetworkProxy;
29 import org.apache.archiva.proxy.model.ProxyConnector;
30 import org.apache.archiva.proxy.model.RepositoryProxyHandler;
31 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
32 import org.apache.archiva.repository.ManagedRepository;
33 import org.apache.archiva.repository.RemoteRepository;
34 import org.apache.archiva.repository.RepositoryType;
35 import org.apache.archiva.event.EventHandler;
36 import org.apache.archiva.repository.event.RepositoryRegistryEvent;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Service;
40
41 import javax.annotation.PostConstruct;
42 import javax.inject.Inject;
43 import java.util.*;
44 import java.util.function.Function;
45 import java.util.stream.Collectors;
46
47 /**
48  * Default proxy registry implementation. Uses the archiva configuration for accessing and storing the
49  * proxy information.
50  *
51  */
52 @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" )
53 @Service("proxyRegistry#default")
54 public class ArchivaProxyRegistry implements ProxyRegistry, EventHandler<RepositoryRegistryEvent> {
55
56     private static final Logger log = LoggerFactory.getLogger(ArchivaProxyRegistry.class);
57
58     @Inject
59     ArchivaConfiguration archivaConfiguration;
60
61     @Inject
62     List<RepositoryProxyHandler> repositoryProxyHandlers;
63
64     @Inject
65     List<Policy> policies;
66
67     @Inject
68     ArchivaRepositoryRegistry repositoryRegistry;
69
70     private Map<String, NetworkProxy> networkProxyMap = new HashMap<>();
71     private Map<RepositoryType, List<RepositoryProxyHandler>> handlerMap = new HashMap<>();
72     private ProxyConnectorOrderComparator comparator = ProxyConnectorOrderComparator.getInstance();
73
74     private Map<String, List<ProxyConnector>> connectorMap = new HashMap<>();
75     private List<ProxyConnector> connectorList = new ArrayList<>();
76     private Map<Policy, PolicyOption> policyMap = new HashMap<>( );
77
78
79     @PostConstruct
80     private void init() {
81         if (repositoryProxyHandlers == null) {
82             repositoryProxyHandlers = new ArrayList<>();
83         }
84         updateHandler();
85         updateConnectors();
86         updateNetworkProxies();
87         repositoryRegistry.registerEventHandler(RepositoryRegistryEvent.RELOADED, this);
88     }
89
90     private ArchivaConfiguration getArchivaConfiguration() {
91         return archivaConfiguration;
92     }
93
94     private void updateNetworkProxies() {
95         this.networkProxyMap.clear();
96         List<NetworkProxyConfiguration> networkProxies = getArchivaConfiguration().getConfiguration().getNetworkProxies();
97         for (NetworkProxyConfiguration networkProxyConfig : networkProxies) {
98             String key = networkProxyConfig.getId();
99
100             NetworkProxy proxy = new NetworkProxy();
101
102             proxy.setProtocol(networkProxyConfig.getProtocol());
103             proxy.setHost(networkProxyConfig.getHost());
104             proxy.setPort(networkProxyConfig.getPort());
105             proxy.setUsername(networkProxyConfig.getUsername());
106             proxy.setPassword(networkProxyConfig.getPassword()==null? new char[0] : networkProxyConfig.getPassword().toCharArray());
107             proxy.setUseNtlm(networkProxyConfig.isUseNtlm());
108
109             this.networkProxyMap.put(key, proxy);
110         }
111         for (RepositoryProxyHandler proxyHandler : repositoryProxyHandlers) {
112             proxyHandler.setNetworkProxies(this.networkProxyMap);
113         }
114     }
115
116     private void updateHandler( ) {
117
118         for (RepositoryProxyHandler handler : repositoryProxyHandlers) {
119             List<RepositoryType> types = handler.supports();
120             for (RepositoryType type : types) {
121                 if (!handlerMap.containsKey(type)) {
122                     handlerMap.put(type, new ArrayList<>());
123                 }
124                 handlerMap.get(type).add(handler);
125             }
126             handler.setPolicies( policies );
127         }
128     }
129
130     private void updateConnectors() {
131         List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
132             getArchivaConfiguration().getConfiguration().getProxyConnectors();
133
134         connectorList = proxyConnectorConfigurations.stream()
135                 .map(this::buildProxyConnector)
136                 .filter(Optional::isPresent)
137                 .map(Optional::get)
138                 .sorted(comparator).collect(Collectors.toList());
139         connectorMap = connectorList.stream().collect(Collectors.groupingBy(a -> a.getSourceRepository().getId()));
140         for (RepositoryProxyHandler handler : repositoryProxyHandlers) {
141             handler.setProxyConnectors( connectorList );
142         }
143     }
144
145
146     private Map<Policy, PolicyOption> getPolicyMap(ProxyConnectorConfiguration configuration) {
147         Map<String, String> policyConfig = configuration.getPolicies( );
148         return policies.stream().collect( Collectors.toMap( Function.identity(), p -> PolicyUtil.findOption( policyConfig.get(p.getId()), p ) ) );
149     }
150
151     private Optional<ProxyConnector> buildProxyConnector(ProxyConnectorConfiguration configuration) {
152         ProxyConnector proxyConnector = new ProxyConnector();
153         proxyConnector.setOrder(configuration.getOrder());
154         proxyConnector.setBlacklist(configuration.getBlackListPatterns());
155         proxyConnector.setWhitelist(configuration.getWhiteListPatterns());
156         if (configuration.isDisabled()) {
157             proxyConnector.disable();
158         } else {
159             proxyConnector.enable();
160         }
161         proxyConnector.setPolicies(getPolicyMap( configuration ));
162         proxyConnector.setProperties(configuration.getProperties());
163         proxyConnector.setProxyId(configuration.getProxyId());
164         ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId());
165         if (srcRepo==null) {
166             return Optional.empty();
167         }
168         proxyConnector.setSourceRepository(srcRepo);
169         RemoteRepository targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId());
170         if (targetRepo==null) {
171             return Optional.empty();
172         }
173         proxyConnector.setTargetRepository(targetRepo);
174         return Optional.of(proxyConnector);
175     }
176
177     @Override
178     public NetworkProxy getNetworkProxy(String id) {
179         return this.networkProxyMap.get(id);
180     }
181
182     @Override
183     public Map<RepositoryType, List<RepositoryProxyHandler>> getAllHandler() {
184         return this.handlerMap;
185     }
186
187     @Override
188     public List<RepositoryProxyHandler> getHandler(RepositoryType type) {
189         if (this.handlerMap.containsKey(type)) {
190             return this.handlerMap.get(type);
191         } else {
192             return new ArrayList<>();
193         }
194     }
195
196     @Override
197     public boolean hasHandler(RepositoryType type) {
198         return this.handlerMap.containsKey(type);
199     }
200
201
202     @Override
203     public List<ProxyConnector> getProxyConnectors() {
204         return connectorList;
205
206     }
207
208     @Override
209     public Map<String, List<ProxyConnector>> getProxyConnectorAsMap() {
210         return connectorMap;
211     }
212
213     @Override
214     public void reload( )
215     {
216         init();
217     }
218
219     @Override
220     public void handle(RepositoryRegistryEvent event) {
221         log.debug("Reload happened, updating proxy list");
222         if (event.getType()== RepositoryRegistryEvent.RELOADED) {
223             init();
224         }
225     }
226 }