001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.transport.http;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.context.MessageContext;
024: import org.apache.axis2.description.Parameter;
025: import org.apache.commons.httpclient.*;
026: import org.apache.commons.httpclient.auth.AuthScope;
027:
028: import javax.xml.namespace.QName;
029: import java.net.URL;
030:
031: /**
032: * The purpose of this class is to configure the proxy auth regardles of the protocol.
033: * Proxy will be set only for HTTP connection
034: */
035:
036: public class ProxyConfiguration {
037:
038: protected String proxyHost;
039: protected String nonProxyHosts;
040: protected int proxyPort = -1; //If port is not set, default is set to -1
041: protected String proxyUser;
042: protected String proxyPassword;
043:
044: protected static final String HTTP_PROXY_HOST = "http.proxyHost";
045: protected static final String HTTP_PROXY_PORT = "http.proxyPort";
046: protected static final String HTTP_NON_PROXY_HOSTS = "http.nonProxyHosts";
047:
048: protected static final String ATTR_PROXY = "Proxy";
049: protected static final String PROXY_HOST_ELEMENT = "ProxyHost";
050: protected static final String PROXY_PORT_ELEMENT = "ProxyPort";
051: protected static final String PROXY_USER_ELEMENT = "ProxyUser";
052: protected static final String PROXY_PASSWORD_ELEMENT = "ProxyPassword";
053:
054: public void configure(MessageContext messageContext,
055: HttpClient httpClient, HostConfiguration config)
056: throws AxisFault {
057:
058: // <parameter name="Proxy">
059: // <Configuration>
060: // <ProxyHost>example.org</ProxyHost>
061: // <ProxyPort>5678</ProxyPort>
062: // <ProxyUser>EXAMPLE\saminda</ProxyUser>
063: // <ProxyPassword>ppp</ProxyPassword>
064: // </Configuration>
065: // </parameter>
066: Credentials proxyCred = null;
067:
068: //Getting configuration values from Axis2.xml
069: Parameter param = messageContext.getConfigurationContext()
070: .getAxisConfiguration().getParameter(ATTR_PROXY);
071:
072: if (param != null) {
073: OMElement configurationEle = param.getParameterElement()
074: .getFirstElement();
075: if (configurationEle == null) {
076: throw new AxisFault(ProxyConfiguration.class.getName()
077: + " Configuration element is missing");
078: }
079:
080: OMElement proxyHostEle = configurationEle
081: .getFirstChildWithName(new QName(PROXY_HOST_ELEMENT));
082: OMElement proxyPortEle = configurationEle
083: .getFirstChildWithName(new QName(PROXY_PORT_ELEMENT));
084: OMElement proxyUserEle = configurationEle
085: .getFirstChildWithName(new QName(PROXY_USER_ELEMENT));
086: OMElement proxyPasswordEle = configurationEle
087: .getFirstChildWithName(new QName(
088: PROXY_PASSWORD_ELEMENT));
089:
090: if (proxyHostEle == null) {
091: throw new AxisFault(ProxyConfiguration.class.getName()
092: + " ProxyHost element is missing");
093: }
094: String text = proxyHostEle.getText();
095: if (text == null) {
096: throw new AxisFault(ProxyConfiguration.class.getName()
097: + " ProxyHost's value is missing");
098: }
099:
100: this .setProxyHost(text);
101:
102: if (proxyPortEle != null) {
103: this .setProxyPort(Integer.parseInt(proxyPortEle
104: .getText()));
105: }
106:
107: if (proxyUserEle != null) {
108: this .setProxyUser(proxyUserEle.getText());
109: }
110:
111: if (proxyPasswordEle != null) {
112: this .setProxyPassword(proxyPasswordEle.getText());
113: }
114:
115: if (this .getProxyUser() == null
116: && this .getProxyUser() == null) {
117: proxyCred = new UsernamePasswordCredentials("", "");
118: } else {
119: proxyCred = new UsernamePasswordCredentials(this
120: .getProxyUser(), this .getProxyPassword());
121: }
122:
123: // if the username is in the form "DOMAIN\\user"
124:// then use NTCredentials instead.
125: if (this .getProxyUser() != null) {
126: int domainIndex = this .getProxyUser().indexOf("\\");
127: if (domainIndex > 0) {
128: String domain = this .getProxyUser().substring(0,
129: domainIndex);
130: if (this .getProxyUser().length() > domainIndex + 1) {
131: String user = this .getProxyUser().substring(
132: domainIndex + 1);
133: proxyCred = new NTCredentials(user, this
134: .getProxyPassword(), this
135: .getProxyHost(), domain);
136: }
137: }
138: }
139: }
140:
141: // Overide the property setting in runtime.
142: HttpTransportProperties.ProxyProperties proxyProperties = (HttpTransportProperties.ProxyProperties) messageContext
143: .getProperty(HTTPConstants.PROXY);
144:
145: if (proxyProperties != null) {
146: String host = proxyProperties.getProxyHostName();
147: if (host == null || host.length() == 0) {
148: throw new AxisFault(
149: ProxyConfiguration.class.getName()
150: + " Proxy host is not available. Host is a MUST parameter");
151:
152: } else {
153: this .setProxyHost(host);
154: }
155:
156: this .setProxyPort(proxyProperties.getProxyPort());
157:
158: //Setting credentials
159:
160: String userName = proxyProperties.getUserName();
161: String password = proxyProperties.getPassWord();
162: String domain = proxyProperties.getDomain();
163:
164: if (userName == null && password == null) {
165: proxyCred = new UsernamePasswordCredentials("", "");
166: } else {
167: proxyCred = new UsernamePasswordCredentials(userName,
168: password);
169: }
170:
171: if (userName != null && password != null && domain != null) {
172: proxyCred = new NTCredentials(userName, password, host,
173: domain);
174: }
175:
176: }
177:
178: //Using Java Networking Properties
179:
180: String host = System.getProperty(HTTP_PROXY_HOST);
181: if (host != null) {
182: this .setProxyHost(host);
183: proxyCred = new UsernamePasswordCredentials("", "");
184: }
185:
186: String port = System.getProperty(HTTP_PROXY_PORT);
187:
188: if (port != null) {
189: this .setProxyPort(Integer.parseInt(port));
190: }
191:
192: if (proxyCred == null) {
193: throw new AxisFault(ProxyConfiguration.class.getName()
194: + " Minimum proxy credentials are not set");
195: }
196: httpClient.getState().setProxyCredentials(AuthScope.ANY,
197: proxyCred);
198: config.setProxy(this .getProxyHost(), this .getProxyPort());
199: }
200:
201: /**
202: * Check first if the proxy is configured or active.
203: * If yes this will return true. This is not a deep check
204: *
205: * @param messageContext
206: * @return boolean
207: */
208:
209: public static boolean isProxyEnabled(MessageContext messageContext,
210: URL targetURL) throws AxisFault {
211:
212: boolean state = false;
213:
214: Parameter param = messageContext.getConfigurationContext()
215: .getAxisConfiguration().getParameter(ATTR_PROXY);
216:
217: //If configuration is over ridden
218: Object obj = messageContext.getProperty(HTTPConstants.PROXY);
219:
220: //From Java Networking Properties
221: String sp = System.getProperty(HTTP_PROXY_HOST);
222:
223: if (param != null || obj != null || sp != null) {
224: state = true;
225: }
226:
227: boolean isNonProxyHost = validateNonProxyHosts(targetURL
228: .getHost());
229:
230: return state && !isNonProxyHost;
231:
232: }
233:
234: /**
235: * Validates for names that shouldn't be listered as proxies.
236: * The http.nonProxyHosts can be set to specify the hosts which should be
237: * connected to directly (not through the proxy server).
238: * The value of the http.nonProxyHosts property can be a list of hosts,
239: * each separated by a |; it can also take a regular expression for matches;
240: * for example: *.sfbay.sun.com would match any fully qualified hostname in the sfbay domain.
241: *
242: * For more information refer to : http://java.sun.com/features/2002/11/hilevel_network.html
243: *
244: * false : validation fail : User can use the proxy
245: * true : validation pass ; User can't use the proxy
246: *
247: * @return boolean
248: */
249: private static boolean validateNonProxyHosts(String targetHost) {
250:
251: //From system property http.nonProxyHosts
252: String nonProxyHosts = System.getProperty(HTTP_NON_PROXY_HOSTS);
253:
254: if (nonProxyHosts == null) {
255: return false;
256: }
257:
258: String[] nonProxyHostsArray = nonProxyHosts.split("\\|");
259:
260: if (nonProxyHostsArray.length == 1) {
261: return targetHost.matches(nonProxyHosts);
262: } else {
263: boolean pass = false;
264: for (int i = 0; i < nonProxyHostsArray.length; i++) {
265: String a = nonProxyHostsArray[i];
266: if (targetHost.matches(a)) {
267: pass = true;
268: break;
269: }
270: }
271: return pass;
272: }
273: }
274:
275: /**
276: * Retrun proxy host
277: *
278: * @return String
279: */
280: public String getProxyHost() {
281: return proxyHost;
282: }
283:
284: /**
285: * set proxy host
286: *
287: * @param proxyHost
288: */
289:
290: public void setProxyHost(String proxyHost) {
291: this .proxyHost = proxyHost;
292: }
293:
294: /**
295: * retrun proxy port
296: *
297: * @return String
298: */
299: public int getProxyPort() {
300: return proxyPort;
301: }
302:
303: /**
304: * set proxy port
305: *
306: * @param proxyPort
307: */
308: public void setProxyPort(int proxyPort) {
309: this .proxyPort = proxyPort;
310: }
311:
312: /**
313: * return proxy user. Proxy user can be user/domain or user
314: *
315: * @return String
316: */
317: public String getProxyUser() {
318: return proxyUser;
319: }
320:
321: /**
322: * get proxy user
323: *
324: * @param proxyUser
325: */
326: public void setProxyUser(String proxyUser) {
327: this .proxyUser = proxyUser;
328: }
329:
330: /**
331: * set password
332: *
333: * @return String
334: */
335: public String getProxyPassword() {
336: return proxyPassword;
337: }
338:
339: /**
340: * get password
341: *
342: * @param proxyPassword
343: */
344: public void setProxyPassword(String proxyPassword) {
345: this.proxyPassword = proxyPassword;
346: }
347:
348: }
|