1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.aether;
20
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.maven.RepositoryUtils;
26 import org.apache.maven.api.di.Named;
27 import org.apache.maven.api.di.Singleton;
28 import org.apache.maven.artifact.repository.ArtifactRepository;
29 import org.apache.maven.artifact.repository.Authentication;
30 import org.apache.maven.bridge.MavenRepositorySystem;
31 import org.apache.maven.execution.MavenExecutionRequest;
32 import org.apache.maven.settings.Mirror;
33 import org.eclipse.aether.repository.AuthenticationContext;
34 import org.eclipse.aether.repository.AuthenticationSelector;
35 import org.eclipse.aether.repository.MirrorSelector;
36 import org.eclipse.aether.repository.ProxySelector;
37 import org.eclipse.aether.repository.RemoteRepository;
38
39
40
41
42
43
44 @Named
45 @Singleton
46 public class LegacyRepositorySystemSessionExtender implements RepositorySystemSessionExtender {
47 @Override
48 public void extend(
49 MavenExecutionRequest mavenExecutionRequest,
50 Map<String, Object> configProperties,
51 MirrorSelector mirrorSelector,
52 ProxySelector proxySelector,
53 AuthenticationSelector authenticationSelector) {
54 injectMirror(mavenExecutionRequest.getRemoteRepositories(), mavenExecutionRequest.getMirrors());
55 injectProxy(proxySelector, mavenExecutionRequest.getRemoteRepositories());
56 injectAuthentication(authenticationSelector, mavenExecutionRequest.getRemoteRepositories());
57
58 injectMirror(mavenExecutionRequest.getPluginArtifactRepositories(), mavenExecutionRequest.getMirrors());
59 injectProxy(proxySelector, mavenExecutionRequest.getPluginArtifactRepositories());
60 injectAuthentication(authenticationSelector, mavenExecutionRequest.getPluginArtifactRepositories());
61 }
62
63 private void injectMirror(List<ArtifactRepository> repositories, List<Mirror> mirrors) {
64 if (repositories != null && mirrors != null) {
65 for (ArtifactRepository repository : repositories) {
66 Mirror mirror = MavenRepositorySystem.getMirror(repository, mirrors);
67 injectMirror(repository, mirror);
68 }
69 }
70 }
71
72 private void injectMirror(ArtifactRepository repository, Mirror mirror) {
73 if (mirror != null) {
74 ArtifactRepository original = MavenRepositorySystem.createArtifactRepository(
75 repository.getId(),
76 repository.getUrl(),
77 repository.getLayout(),
78 repository.getSnapshots(),
79 repository.getReleases());
80
81 repository.setMirroredRepositories(Collections.singletonList(original));
82
83 repository.setId(mirror.getId());
84 repository.setUrl(mirror.getUrl());
85
86 if (mirror.getLayout() != null && !mirror.getLayout().isEmpty()) {
87 repository.setLayout(original.getLayout());
88 }
89
90 repository.setBlocked(mirror.isBlocked());
91 }
92 }
93
94 private void injectProxy(ProxySelector selector, List<ArtifactRepository> repositories) {
95 if (repositories != null && selector != null) {
96 for (ArtifactRepository repository : repositories) {
97 repository.setProxy(getProxy(selector, repository));
98 }
99 }
100 }
101
102 private org.apache.maven.repository.Proxy getProxy(ProxySelector selector, ArtifactRepository repository) {
103 if (selector != null) {
104 RemoteRepository repo = RepositoryUtils.toRepo(repository);
105 org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo);
106 if (proxy != null) {
107 org.apache.maven.repository.Proxy p = new org.apache.maven.repository.Proxy();
108 p.setHost(proxy.getHost());
109 p.setProtocol(proxy.getType());
110 p.setPort(proxy.getPort());
111 if (proxy.getAuthentication() != null) {
112 repo = new RemoteRepository.Builder(repo).setProxy(proxy).build();
113 AuthenticationContext authCtx = AuthenticationContext.forProxy(null, repo);
114 p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
115 p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
116 p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
117 p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
118 authCtx.close();
119 }
120 return p;
121 }
122 }
123 return null;
124 }
125
126 private void injectAuthentication(AuthenticationSelector selector, List<ArtifactRepository> repositories) {
127 if (repositories != null && selector != null) {
128 for (ArtifactRepository repository : repositories) {
129 repository.setAuthentication(getAuthentication(selector, repository));
130 }
131 }
132 }
133
134 private Authentication getAuthentication(AuthenticationSelector selector, ArtifactRepository repository) {
135 if (selector != null) {
136 RemoteRepository repo = RepositoryUtils.toRepo(repository);
137 org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo);
138 if (auth != null) {
139 repo = new RemoteRepository.Builder(repo)
140 .setAuthentication(auth)
141 .build();
142 AuthenticationContext authCtx = AuthenticationContext.forRepository(null, repo);
143 Authentication result = new Authentication(
144 authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
145 result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
146 result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
147 authCtx.close();
148 return result;
149 }
150 }
151 return null;
152 }
153 }