1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.maven.mojo;
17
18 import java.util.List;
19
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.settings.Proxy;
22
23 import de.smartics.ci.comm.CiController;
24 import de.smartics.ci.comm.ProxyCredentials;
25 import de.smartics.ci.comm.ProxyInformation;
26 import de.smartics.ci.comm.command.LoginCommandException;
27 import de.smartics.ci.maven.HudsonUtils;
28 import de.smartics.ci.maven.ProxyConfiguration;
29
30
31
32
33 public abstract class AbstractProxyEnabledHudsonCiMojo extends
34 AbstractConfigChoiceHudsonCiMojo
35 {
36
37
38
39
40
41
42
43
44
45
46
47
48
49 protected String proxyId;
50
51
52
53
54
55
56
57
58 private String proxyDomain;
59
60
61
62
63
64
65
66 private String proxyClientName;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 protected void handleLogin(final CiController controller)
88 throws LoginCommandException, MojoExecutionException
89 {
90 applyProxyInformation(controller);
91 super.handleLogin(controller);
92 }
93
94
95
96
97
98
99
100 protected final void applyProxyInformation(final CiController controller)
101 throws MojoExecutionException
102 {
103 if (HudsonUtils.checkIfProxyIsConfigured(this.proxyId, this.proxyDomain,
104 this.proxyClientName))
105 {
106 final ProxyConfiguration proxyConfig = getProxyConfiguration();
107 if (proxyConfig != null)
108 {
109 final ProxyInformation proxy =
110 new ProxyInformation(proxyConfig.getHost(), proxyConfig.getPort());
111 final ProxyCredentials proxyCredentials =
112 new ProxyCredentials(proxyConfig.getUserName(),
113 proxyConfig.getPassword(), proxyConfig.getClientName(),
114 proxyConfig.getDomain());
115 proxy.setCredentials(proxyCredentials);
116 controller.setProxyInformation(proxy);
117 }
118 }
119 }
120
121
122
123
124
125
126
127
128
129 protected final ProxyConfiguration getProxyConfiguration()
130 throws MojoExecutionException
131 {
132 final Proxy configuredProxy = fetchProxyFromSettings();
133 final ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
134 final String proxyId = configuredProxy.getId();
135 proxyConfiguration.setId(proxyId);
136 proxyConfiguration.setHost(configuredProxy.getHost());
137 proxyConfiguration.setPort(configuredProxy.getPort());
138 proxyConfiguration.setPassword(configuredProxy.getPassword());
139 proxyConfiguration.setUserName(configuredProxy.getUsername());
140 proxyConfiguration.setClientName(this.proxyClientName);
141 proxyConfiguration.setDomain(this.proxyDomain);
142 return proxyConfiguration;
143 }
144
145 private Proxy fetchProxyFromSettings() throws MojoExecutionException
146 {
147 final List<Proxy> proxies = settings.getProxies();
148 Proxy configuredProxy = null;
149 for (final Proxy proxy : proxies)
150 {
151 final String currentProxyId = proxy.getId();
152 if (this.proxyId.equals(currentProxyId))
153 {
154 configuredProxy = proxy;
155 }
156 }
157 if (configuredProxy == null)
158 {
159 throw new MojoExecutionException(
160 "Proxy is configured, but in the settings.xml there is no porxy-entry for proxy with id: "
161 + this.proxyId);
162 }
163 return configuredProxy;
164 }
165
166
167
168 }