Search in sources :

Example 1 with NetworkProxyConfiguration

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);
}
Also used : Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) MavenManagedRepository(org.apache.archiva.repository.maven2.MavenManagedRepository) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) RepositoryProxyConnectors(org.apache.archiva.proxy.model.RepositoryProxyConnectors) URI(java.net.URI) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConnector(org.eclipse.jetty.server.ServerConnector) RepositoryContentProvider(org.apache.archiva.repository.RepositoryContentProvider) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ManagedRepositoryAdmin(org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin) DefaultManagedRepositoryAdmin(org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) RepositoryRegistry(org.apache.archiva.repository.RepositoryRegistry) RemoteRepositoryConfiguration(org.apache.archiva.configuration.RemoteRepositoryConfiguration) Before(org.junit.Before)

Example 2 with NetworkProxyConfiguration

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);
    }
}
Also used : ManagedRepository(org.apache.archiva.repository.ManagedRepository) ArchivaConfiguration(org.apache.archiva.configuration.ArchivaConfiguration) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) ProxyConnectorRuleConfiguration(org.apache.archiva.configuration.ProxyConnectorRuleConfiguration) Configuration(org.apache.archiva.configuration.Configuration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) ArrayList(java.util.ArrayList) ProxyConnectorConfiguration(org.apache.archiva.configuration.ProxyConnectorConfiguration) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) ProxyConnectorRuleConfiguration(org.apache.archiva.configuration.ProxyConnectorRuleConfiguration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) ProxyConnector(org.apache.archiva.proxy.model.ProxyConnector)

Example 3 with NetworkProxyConfiguration

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);
}
Also used : Configuration(org.apache.archiva.configuration.Configuration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException)

Example 4 with NetworkProxyConfiguration

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);
}
Also used : Configuration(org.apache.archiva.configuration.Configuration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) NetworkProxyConfiguration(org.apache.archiva.configuration.NetworkProxyConfiguration) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) RemoteRepository(org.apache.archiva.repository.RemoteRepository) RepositoryException(org.apache.archiva.repository.RepositoryException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy)

Aggregations

NetworkProxyConfiguration (org.apache.archiva.configuration.NetworkProxyConfiguration)4 Configuration (org.apache.archiva.configuration.Configuration)3 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)2 ArchivaConfiguration (org.apache.archiva.configuration.ArchivaConfiguration)2 RemoteRepository (org.apache.archiva.repository.RemoteRepository)2 URI (java.net.URI)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)1 ManagedRepositoryAdmin (org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin)1 DefaultManagedRepositoryAdmin (org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin)1 ProxyConnectorConfiguration (org.apache.archiva.configuration.ProxyConnectorConfiguration)1 ProxyConnectorRuleConfiguration (org.apache.archiva.configuration.ProxyConnectorRuleConfiguration)1 RemoteRepositoryConfiguration (org.apache.archiva.configuration.RemoteRepositoryConfiguration)1 ProxyConnector (org.apache.archiva.proxy.model.ProxyConnector)1 RepositoryProxyConnectors (org.apache.archiva.proxy.model.RepositoryProxyConnectors)1 ManagedRepository (org.apache.archiva.repository.ManagedRepository)1 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)1