1 package org.apache.archiva.proxy;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.configuration.ArchivaConfiguration;
23 import org.apache.archiva.configuration.NetworkProxyConfiguration;
24 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
25 import org.apache.archiva.policies.Policy;
26 import org.apache.archiva.policies.PolicyOption;
27 import org.apache.archiva.policies.PolicyUtil;
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.ManagedRepository;
32 import org.apache.archiva.repository.RemoteRepository;
33 import org.apache.archiva.repository.RepositoryRegistry;
34 import org.apache.archiva.repository.RepositoryType;
35 import org.apache.archiva.repository.events.EventHandler;
36 import org.apache.archiva.repository.events.RepositoryRegistryEvent;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Service;
41 import javax.annotation.PostConstruct;
42 import javax.inject.Inject;
44 import java.util.function.Function;
45 import java.util.stream.Collectors;
48 * Default proxy registry implementation. Uses the archiva configuration for accessing and storing the
52 @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" )
53 @Service("proxyRegistry#default")
54 public class ArchivaProxyRegistry implements ProxyRegistry, EventHandler<RepositoryRegistryEvent> {
56 private static final Logger log = LoggerFactory.getLogger(ArchivaProxyRegistry.class);
59 ArchivaConfiguration archivaConfiguration;
62 List<RepositoryProxyHandler> repositoryProxyHandlers;
65 List<Policy> policies;
68 RepositoryRegistry repositoryRegistry;
70 private Map<String, NetworkProxy> networkProxyMap = new HashMap<>();
71 private Map<RepositoryType, List<RepositoryProxyHandler>> handlerMap = new HashMap<>();
72 private ProxyConnectorOrderComparator comparator = ProxyConnectorOrderComparator.getInstance();
74 private Map<String, List<ProxyConnector>> connectorMap = new HashMap<>();
75 private List<ProxyConnector> connectorList = new ArrayList<>();
76 private Map<Policy, PolicyOption> policyMap = new HashMap<>( );
81 if (repositoryProxyHandlers == null) {
82 repositoryProxyHandlers = new ArrayList<>();
86 updateNetworkProxies();
87 repositoryRegistry.registerEventHandler(RepositoryRegistryEvent.RELOADED, this);
90 private ArchivaConfiguration getArchivaConfiguration() {
91 return archivaConfiguration;
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();
100 NetworkProxy proxy = new NetworkProxy();
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());
109 this.networkProxyMap.put(key, proxy);
111 for (RepositoryProxyHandler proxyHandler : repositoryProxyHandlers) {
112 proxyHandler.setNetworkProxies(this.networkProxyMap);
116 private void updateHandler( ) {
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<>());
124 handlerMap.get(type).add(handler);
126 handler.setPolicies( policies );
130 private void updateConnectors() {
131 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
132 getArchivaConfiguration().getConfiguration().getProxyConnectors();
134 connectorList = proxyConnectorConfigurations.stream()
135 .map(this::buildProxyConnector)
136 .filter(Optional::isPresent)
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 );
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 ) ) );
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();
159 proxyConnector.enable();
161 proxyConnector.setPolicies(getPolicyMap( configuration ));
162 proxyConnector.setProperties(configuration.getProperties());
163 proxyConnector.setProxyId(configuration.getProxyId());
164 ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId());
166 return Optional.empty();
168 proxyConnector.setSourceRepository(srcRepo);
169 RemoteRepository targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId());
170 if (targetRepo==null) {
171 return Optional.empty();
173 proxyConnector.setTargetRepository(targetRepo);
174 return Optional.of(proxyConnector);
178 public NetworkProxy getNetworkProxy(String id) {
179 return this.networkProxyMap.get(id);
183 public Map<RepositoryType, List<RepositoryProxyHandler>> getAllHandler() {
184 return this.handlerMap;
188 public List<RepositoryProxyHandler> getHandler(RepositoryType type) {
189 if (this.handlerMap.containsKey(type)) {
190 return this.handlerMap.get(type);
192 return new ArrayList<>();
197 public boolean hasHandler(RepositoryType type) {
198 return this.handlerMap.containsKey(type);
203 public List<ProxyConnector> getProxyConnectors() {
204 return connectorList;
209 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap() {
214 public void reload( )
220 public void handle(RepositoryRegistryEvent event) {
221 log.debug("Reload happened, updating proxy list");
222 if (event.getType()== RepositoryRegistryEvent.RELOADED) {