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