use of org.apache.archiva.configuration.NetworkProxyConfiguration in project archiva by apache.
the class HttpProxyTransferTest method setUp.
@Before
public void setUp() throws Exception {
proxyHandler = applicationContext.getBean("repositoryProxyConnectors#test", RepositoryProxyConnectors.class);
config = applicationContext.getBean("archivaConfiguration#mock", ArchivaConfiguration.class);
// clear from previous tests - TODO the spring context should be initialised per test instead, or the config
// made a complete mock
config.getConfiguration().getProxyConnectors().clear();
// Setup source repository (using default layout)
String repoPath = "target/test-repository/managed/" + getClass().getSimpleName();
Path destRepoDir = Paths.get(repoPath);
// Cleanout destination dirs.
if (Files.exists(destRepoDir)) {
FileUtils.deleteDirectory(destRepoDir.toFile());
}
// Make the destination dir.
Files.createDirectories(destRepoDir);
MavenManagedRepository repo = new MavenManagedRepository(MANAGED_ID, "Default Managed Repository", Paths.get(repoPath).getParent());
repo.setLocation(new URI(repoPath));
repo.setLayout("default");
RepositoryContentProvider provider = applicationContext.getBean("repositoryContentProvider#maven", RepositoryContentProvider.class);
ManagedRepositoryContent repoContent = provider.createManagedContent(repo);
managedDefaultRepository = repoContent;
((DefaultManagedRepositoryAdmin) applicationContext.getBean(ManagedRepositoryAdmin.class)).setArchivaConfiguration(config);
RepositoryRegistry managedRepositoryAdmin = applicationContext.getBean(RepositoryRegistry.class);
if (managedRepositoryAdmin.getManagedRepository(repo.getId()) == null) {
managedRepositoryAdmin.putRepository(repo);
}
// config.getConfiguration().addManagedRepository( repo );
Handler handler = new AbstractHandler() {
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print("get-default-layout-1.0.jar\n\n");
assertNotNull(request.getHeader("Proxy-Connection"));
((Request) request).setHandled(true);
}
public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print("get-default-layout-1.0.jar\n\n");
assertNotNull(request.getHeader("Proxy-Connection"));
((Request) request).setHandled(true);
}
};
server = new Server();
ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory());
server.addConnector(serverConnector);
server.setHandler(handler);
server.start();
int port = serverConnector.getLocalPort();
NetworkProxyConfiguration proxyConfig = new NetworkProxyConfiguration();
proxyConfig.setHost("localhost");
proxyConfig.setPort(port);
proxyConfig.setProtocol("http");
proxyConfig.setId(PROXY_ID);
config.getConfiguration().addNetworkProxy(proxyConfig);
// Setup target (proxied to) repository.
RemoteRepositoryConfiguration repoConfig = new RemoteRepositoryConfiguration();
repoConfig.setId(PROXIED_ID);
repoConfig.setName("Proxied Repository 1");
repoConfig.setLayout("default");
repoConfig.setUrl("http://www.example.com/");
config.getConfiguration().addRemoteRepository(repoConfig);
}
use of org.apache.archiva.configuration.NetworkProxyConfiguration in project archiva by apache.
the class DefaultRepositoryProxyConnectors method initConnectorsAndNetworkProxies.
@SuppressWarnings("unchecked")
private void initConnectorsAndNetworkProxies() {
ProxyConnectorOrderComparator proxyOrderSorter = new ProxyConnectorOrderComparator();
this.proxyConnectorMap.clear();
Configuration configuration = archivaConfiguration.getConfiguration();
List<ProxyConnectorRuleConfiguration> allProxyConnectorRuleConfigurations = configuration.getProxyConnectorRuleConfigurations();
List<ProxyConnectorConfiguration> proxyConfigs = configuration.getProxyConnectors();
for (ProxyConnectorConfiguration proxyConfig : proxyConfigs) {
String key = proxyConfig.getSourceRepoId();
// Create connector object.
ProxyConnector connector = new ProxyConnector();
ManagedRepository repo = repositoryRegistry.getManagedRepository(proxyConfig.getSourceRepoId());
if (repo == null) {
log.error("Cannot find source repository after config change " + proxyConfig.getSourceRepoId());
continue;
}
connector.setSourceRepository(repo.getContent());
RemoteRepository rRepo = repositoryRegistry.getRemoteRepository(proxyConfig.getTargetRepoId());
if (rRepo == null) {
log.error("Cannot find target repository after config change " + proxyConfig.getSourceRepoId());
continue;
}
connector.setTargetRepository(rRepo.getContent());
connector.setProxyId(proxyConfig.getProxyId());
connector.setPolicies(proxyConfig.getPolicies());
connector.setOrder(proxyConfig.getOrder());
connector.setDisabled(proxyConfig.isDisabled());
// Copy any blacklist patterns.
List<String> blacklist = new ArrayList<>(0);
if (CollectionUtils.isNotEmpty(proxyConfig.getBlackListPatterns())) {
blacklist.addAll(proxyConfig.getBlackListPatterns());
}
connector.setBlacklist(blacklist);
// Copy any whitelist patterns.
List<String> whitelist = new ArrayList<>(0);
if (CollectionUtils.isNotEmpty(proxyConfig.getWhiteListPatterns())) {
whitelist.addAll(proxyConfig.getWhiteListPatterns());
}
connector.setWhitelist(whitelist);
List<ProxyConnectorRuleConfiguration> proxyConnectorRuleConfigurations = findProxyConnectorRules(connector.getSourceRepository().getId(), connector.getTargetRepository().getId(), allProxyConnectorRuleConfigurations);
if (!proxyConnectorRuleConfigurations.isEmpty()) {
for (ProxyConnectorRuleConfiguration proxyConnectorRuleConfiguration : proxyConnectorRuleConfigurations) {
if (StringUtils.equals(proxyConnectorRuleConfiguration.getRuleType(), ProxyConnectorRuleType.BLACK_LIST.getRuleType())) {
connector.getBlacklist().add(proxyConnectorRuleConfiguration.getPattern());
}
if (StringUtils.equals(proxyConnectorRuleConfiguration.getRuleType(), ProxyConnectorRuleType.WHITE_LIST.getRuleType())) {
connector.getWhitelist().add(proxyConnectorRuleConfiguration.getPattern());
}
}
}
// Get other connectors
List<ProxyConnector> connectors = this.proxyConnectorMap.get(key);
if (connectors == null) {
// Create if we are the first.
connectors = new ArrayList<>(1);
}
// Add the connector.
connectors.add(connector);
// Ensure the list is sorted.
Collections.sort(connectors, proxyOrderSorter);
// Set the key to the list of connectors.
this.proxyConnectorMap.put(key, connectors);
}
this.networkProxyMap.clear();
List<NetworkProxyConfiguration> networkProxies = archivaConfiguration.getConfiguration().getNetworkProxies();
for (NetworkProxyConfiguration networkProxyConfig : networkProxies) {
String key = networkProxyConfig.getId();
ProxyInfo proxy = new ProxyInfo();
proxy.setType(networkProxyConfig.getProtocol());
proxy.setHost(networkProxyConfig.getHost());
proxy.setPort(networkProxyConfig.getPort());
proxy.setUserName(networkProxyConfig.getUsername());
proxy.setPassword(networkProxyConfig.getPassword());
this.networkProxyMap.put(key, proxy);
}
}
use of org.apache.archiva.configuration.NetworkProxyConfiguration in project archiva by apache.
the class DefaultNetworkProxyAdmin method updateNetworkProxy.
@Override
public void updateNetworkProxy(NetworkProxy networkProxy, AuditInformation auditInformation) throws RepositoryAdminException {
if (networkProxy == null) {
return;
}
if (getNetworkProxy(networkProxy.getId()) == null) {
throw new RepositoryAdminException("cannot update NetworkProxy with id " + networkProxy.getId() + " as not exist");
}
Configuration configuration = getArchivaConfiguration().getConfiguration();
NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration(networkProxy);
configuration.removeNetworkProxy(networkProxyConfiguration);
configuration.addNetworkProxy(networkProxyConfiguration);
triggerAuditEvent(networkProxy.getId(), null, AuditEvent.MODIFY_NETWORK_PROXY, auditInformation);
saveConfiguration(configuration);
}
use of org.apache.archiva.configuration.NetworkProxyConfiguration in project archiva by apache.
the class DefaultNetworkProxyAdmin method deleteNetworkProxy.
@Override
public void deleteNetworkProxy(String networkProxyId, AuditInformation auditInformation) throws RepositoryAdminException {
NetworkProxy networkProxy = getNetworkProxy(networkProxyId);
if (networkProxy == null) {
throw new RepositoryAdminException("cannot delete NetworkProxy with id " + networkProxyId + " as not exist");
}
Configuration configuration = getArchivaConfiguration().getConfiguration();
NetworkProxyConfiguration networkProxyConfiguration = getNetworkProxyConfiguration(networkProxy);
configuration.removeNetworkProxy(networkProxyConfiguration);
for (RemoteRepository repo : repositoryRegistry.getRemoteRepositories()) {
if (repo.supportsFeature(RemoteIndexFeature.class)) {
RemoteIndexFeature rif = repo.getFeature(RemoteIndexFeature.class).get();
if (networkProxyId.equals(rif.getProxyId())) {
rif.setProxyId(null);
try {
repositoryRegistry.putRepository(repo, configuration);
} catch (RepositoryException e) {
log.error("Could not update repository {}", repo.getId(), e);
}
}
}
}
triggerAuditEvent(networkProxy.getId(), null, AuditEvent.DELETE_NETWORK_PROXY, auditInformation);
saveConfiguration(configuration);
}
Aggregations