1 /*
2 * Copyright 2012 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.ci.maven;
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
28 /**
29 * Hudson CI mojo.
30 */
31 public abstract class AbstractProxyEnabledHudsonCiMojo extends
32 AbstractConfigChoiceHudsonCiMojo
33 {
34 // ********************************* Fields *********************************
35
36 // --- constants ------------------------------------------------------------
37
38 // --- members --------------------------------------------------------------
39
40 /**
41 * The id of the proxy entry in the settings to declare which proxy shall be
42 * used to connect to the ci server.
43 *
44 * @parameter expression="${hudson-maven-plugin.proxyId}"
45 * @since 1.0
46 */
47 protected String proxyId;
48
49 /**
50 * The proxy Windows-NT domain name that must be used to get access through
51 * the proxy.
52 *
53 * @parameter expression="${hudson-maven-plugin.proxyDomain}"
54 * @since 1.0
55 */
56 private String proxyDomain;
57
58 /**
59 * The client name that shall be used to connect to the proxy.
60 *
61 * @parameter expression="${hudson-maven-plugin.proxyClientName}"
62 * @since 1.0
63 */
64 private String proxyClientName;
65
66 // ****************************** Initializer *******************************
67
68 // ****************************** Constructors ******************************
69
70 // ****************************** Inner Classes *****************************
71
72 // ********************************* Methods ********************************
73
74 // --- init -----------------------------------------------------------------
75
76 // --- get&set --------------------------------------------------------------
77
78 // --- business -------------------------------------------------------------
79
80 /**
81 * {@inheritDoc}
82 *
83 * @see de.smartics.ci.maven.AbstractHudsonCiMojo#handleLogin(de.smartics.ci.comm.CiController)
84 */
85 protected void handleLogin(final CiController controller)
86 throws LoginCommandException, MojoExecutionException
87 {
88 applyProxyInformation(controller);
89 super.handleLogin(controller);
90 }
91
92 /**
93 * Apply the proxy configuration to the controller.
94 *
95 * @param controller the controller that connect to the ci server.
96 * @throws MojoExecutionException when the proxy configuration is invalid.
97 */
98 protected final void applyProxyInformation(final CiController controller)
99 throws MojoExecutionException
100 {
101 if (HudsonUtils.checkIfProxyIsConfigured(this.proxyId, this.proxyDomain,
102 this.proxyClientName))
103 {
104 final ProxyConfiguration proxyConfig = getProxyConfiguration();
105 if (proxyConfig != null)
106 {
107 final ProxyInformation proxy =
108 new ProxyInformation(proxyConfig.getHost(), proxyConfig.getPort());
109 final ProxyCredentials proxyCredentials =
110 new ProxyCredentials(proxyConfig.getUserName(),
111 proxyConfig.getPassword(), proxyConfig.getClientName(),
112 proxyConfig.getDomain());
113 proxy.setCredentials(proxyCredentials);
114 controller.setProxyInformation(proxy);
115 }
116 }
117 }
118
119 /**
120 * Returns the maven proxy configuration.
121 *
122 * @return the proxy information.
123 * @throws MojoExecutionException when the proxy is configured in the mojo,
124 * but no proxy with the given proxyId can be found in the
125 * settings.xml.
126 */
127 protected final ProxyConfiguration getProxyConfiguration()
128 throws MojoExecutionException
129 {
130 final Proxy configuredProxy = fetchProxyFromSettings();
131 final ProxyConfiguration proxyConfiguration = new ProxyConfiguration();
132 final String proxyId = configuredProxy.getId();
133 proxyConfiguration.setId(proxyId);
134 proxyConfiguration.setHost(configuredProxy.getHost());
135 proxyConfiguration.setPort(configuredProxy.getPort());
136 proxyConfiguration.setPassword(configuredProxy.getPassword());
137 proxyConfiguration.setUserName(configuredProxy.getUsername());
138 proxyConfiguration.setClientName(this.proxyClientName);
139 proxyConfiguration.setDomain(this.proxyDomain);
140 return proxyConfiguration;
141 }
142
143 private Proxy fetchProxyFromSettings() throws MojoExecutionException
144 {
145 final List<Proxy> proxies = settings.getProxies();
146 Proxy configuredProxy = null;
147 for (final Proxy proxy : proxies)
148 {
149 final String currentProxyId = proxy.getId();
150 if (this.proxyId.equals(currentProxyId))
151 {
152 configuredProxy = proxy;
153 }
154 }
155 if (configuredProxy == null)
156 {
157 throw new MojoExecutionException(
158 "Proxy is configured, but in the settings.xml there is no porxy-entry for proxy with id: "
159 + this.proxyId);
160 }
161 return configuredProxy;
162 }
163
164 // --- object basics --------------------------------------------------------
165
166 }