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.*;
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;
37 import javax.annotation.PostConstruct;
38 import javax.inject.Inject;
40 import java.util.function.Function;
41 import java.util.stream.Collectors;
44 * Default proxy registry implementation. Uses the archiva configuration for accessing and storing the
48 @SuppressWarnings( "SpringJavaInjectionPointsAutowiringInspection" )
49 @Service("proxyRegistry#default")
50 public class ArchivaProxyRegistry implements ProxyRegistry, RepositoryEventListener {
52 private static final Logger log = LoggerFactory.getLogger(ArchivaProxyRegistry.class);
55 ArchivaConfiguration archivaConfiguration;
58 List<RepositoryProxyHandler> repositoryProxyHandlers;
61 List<Policy> policies;
64 RepositoryRegistry repositoryRegistry;
66 private Map<String, NetworkProxy> networkProxyMap = new HashMap<>();
67 private Map<RepositoryType, List<RepositoryProxyHandler>> handlerMap = new HashMap<>();
68 private ProxyConnectorOrderComparator comparator = ProxyConnectorOrderComparator.getInstance();
70 private Map<String, List<ProxyConnector>> connectorMap = new HashMap<>();
71 private List<ProxyConnector> connectorList = new ArrayList<>();
72 private Map<Policy, PolicyOption> policyMap = new HashMap<>( );
77 if (repositoryProxyHandlers == null) {
78 repositoryProxyHandlers = new ArrayList<>();
82 updateNetworkProxies();
83 repositoryRegistry.register(this, RepositoryRegistryEvent.RegistryEventType.RELOADED);
86 private ArchivaConfiguration getArchivaConfiguration() {
87 return archivaConfiguration;
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();
96 NetworkProxy proxy = new NetworkProxy();
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());
105 this.networkProxyMap.put(key, proxy);
107 for (RepositoryProxyHandler proxyHandler : repositoryProxyHandlers) {
108 proxyHandler.setNetworkProxies(this.networkProxyMap);
112 private void updateHandler( ) {
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<>());
120 handlerMap.get(type).add(handler);
122 handler.setPolicies( policies );
126 private void updateConnectors() {
127 List<ProxyConnectorConfiguration> proxyConnectorConfigurations =
128 getArchivaConfiguration().getConfiguration().getProxyConnectors();
130 connectorList = proxyConnectorConfigurations.stream()
131 .map(this::buildProxyConnector)
132 .filter(Optional::isPresent)
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 );
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 ) ) );
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();
155 proxyConnector.enable();
157 proxyConnector.setPolicies(getPolicyMap( configuration ));
158 proxyConnector.setProperties(configuration.getProperties());
159 proxyConnector.setProxyId(configuration.getProxyId());
160 ManagedRepository srcRepo = repositoryRegistry.getManagedRepository(configuration.getSourceRepoId());
162 return Optional.empty();
164 proxyConnector.setSourceRepository(srcRepo);
165 RemoteRepository targetRepo = repositoryRegistry.getRemoteRepository(configuration.getTargetRepoId());
166 if (targetRepo==null) {
167 return Optional.empty();
169 proxyConnector.setTargetRepository(targetRepo);
170 return Optional.of(proxyConnector);
174 public NetworkProxy getNetworkProxy(String id) {
175 return this.networkProxyMap.get(id);
179 public Map<RepositoryType, List<RepositoryProxyHandler>> getAllHandler() {
180 return this.handlerMap;
184 public List<RepositoryProxyHandler> getHandler(RepositoryType type) {
185 if (this.handlerMap.containsKey(type)) {
186 return this.handlerMap.get(type);
188 return new ArrayList<>();
193 public boolean hasHandler(RepositoryType type) {
194 return this.handlerMap.containsKey(type);
199 public List<ProxyConnector> getProxyConnectors() {
200 return connectorList;
205 public Map<String, List<ProxyConnector>> getProxyConnectorAsMap() {
210 public void reload( )
216 public void raise(Event event) {
217 log.debug("Reload happened, updating proxy list");
218 if (event.getType()== RepositoryRegistryEvent.RegistryEventType.RELOADED) {