Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aether-users] Usage of ProxySelector with Request Object?

Hi,

We're trying to use the ProxySelector of Aether for a MavenRepositorySystemSession and a RemoteRepository (see Code attached below).
However, the JavaDoc for the ProxySelector says:
"Note that this selector is not used for remote repositories which are passed as request parameters to the repository system, those repositories are supposed to have their proxy (if any) already set."
Which astonishes me a little bit, since I have no idea how to use the RepositorySystemSession and my RemoteRepository without a Request Object for sending requests (DeployRequest in the Code example below).
I've debugged the Code below and came to the following result:

The getProxy(...) method of my set ProxySelector is called by Aether and the Proxy is set right via setProxy(...) to my RemoteRepository.
But it seems like that after the call, Aether uses a new RemoteRepository with id="central" and proxy = null  for the actual request. My RemoteRepository isn't used after all.

So, how to get this right?
And how to use Aether with a ProxySelector, but without request parameters (like mentioned in the JavaDoc) the right way?

Best regards

Patrick Gottschaemmer
Code Recommenders

Code:

public void deploy(Artifact artifact) throws DeploymentException {

        RepositorySystemSession session = newSession();

        DeployRequest r = new DeployRequest();

        r.addArtifact(artifact);

        r.setRepository(remoteRepository);

        system.deploy(session, r);

        log.info("deployed '{}' to {}", artifact, remoteRepository.getUrl());

    }


private DefaultRepositorySystemSession newSession() {

        MavenRepositorySystemSession session = new MavenRepositorySystemSession();

        session.setProxySelector(new MyProxySelector(proxyService));

        LocalRepository localRepo = new LocalRepository(location);

        session.setLocalRepositoryManager(system.newLocalRepositoryManager(localRepo));

        return session;

    }


public class MyProxySelector implements ProxySelector {

 

    private final IProxyService proxyService;

 

    public MyProxySelector(IProxyService proxyService) {

        this.proxyService = proxyService;

    }

 

    @Override

    public Proxy getProxy(RemoteRepository remote) {

        if (proxyService != null && proxyService.isProxiesEnabled()) {

            URI uri = URI.create(remote.getUrl());

            for (IProxyData data : proxyService.select(uri)) {

                String host = data.getHost();

                if (host != null) {

                    String type = "http";

                    int port = data.getPort();

                    Authentication auth = new Authentication(data.getUserId(), data.getPassword());

                    return new Proxy(type, host, port, auth);

                }

            }

        }

        return null; // Proxies disabled or no matching proxy found.

    }

 

}


Back to the top