Author: jonesde
Date: Wed Jul 4 08:11:07 2007 New Revision: 553233 URL: http://svn.apache.org/viewvc?view=rev&rev=553233 Log: Added some stuff for client authentication with HTTP Basic Auth, and doing so more easily (ie does the base64 encode and sets the request header, etc) Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/HttpClient.java ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/URLConnector.java Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/HttpClient.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/HttpClient.java?view=diff&rev=553233&r1=553232&r2=553233 ============================================================================== --- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/HttpClient.java (original) +++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/HttpClient.java Wed Jul 4 08:11:07 2007 @@ -52,6 +52,9 @@ private String url = null; private String rawStream = null; private String clientCertAlias = null; + private String basicAuthUsername = null; + private String basicAuthPassword = null; + private Map parameters = null; private Map headers = null; @@ -221,6 +224,11 @@ return this.trustAny; } + public void setBasicAuthInfo(String basicAuthUsername, String basicAuthPassword) { + this.basicAuthUsername = basicAuthUsername; + this.basicAuthPassword = basicAuthPassword; + } + /** Invoke HTTP request GET. */ public String get() throws HttpClientException { return sendHttpRequest("get"); @@ -430,11 +438,19 @@ } if (method.equalsIgnoreCase("post")) { - if (contentType == null) + if (contentType == null) { con.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); + } con.setDoInput(true); } - + + // if there is basicAuth info set the request property for it + if (basicAuthUsername != null) { + String basicAuthString = "Basic " + Base64.base64Encode(basicAuthUsername + ":" + (basicAuthPassword == null ? "" : basicAuthPassword)); + con.setRequestProperty("Authorization", basicAuthString); + if (Debug.verboseOn() || debug) Debug.log("Header - Authorization: " + basicAuthString, module); + } + if (headers != null && headers.size() > 0) { Set headerSet = headers.keySet(); Iterator i = headerSet.iterator(); @@ -443,7 +459,7 @@ String headerName = (String) i.next(); String headerValue = (String) headers.get(headerName); con.setRequestProperty(headerName, headerValue); - if (Debug.verboseOn() || debug) Debug.log("Header : " + headerName + " -> " + headerValue, module); + if (Debug.verboseOn() || debug) Debug.log("Header - " + headerName + ": " + headerValue, module); } } Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/URLConnector.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/URLConnector.java?view=diff&rev=553233&r1=553232&r2=553233 ============================================================================== --- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/URLConnector.java (original) +++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/URLConnector.java Wed Jul 4 08:11:07 2007 @@ -19,6 +19,7 @@ package org.ofbiz.base.util; import org.ofbiz.base.config.GenericConfigException; +import org.ofbiz.base.util.Base64; import java.io.IOException; import java.net.HttpURLConnection; @@ -116,6 +117,7 @@ URLConnection con = null; try { con = url.openConnection(); + if ("HTTPS".equalsIgnoreCase(url.getProtocol())) { HttpsURLConnection scon = (HttpsURLConnection) con; try { |
Free forum by Nabble | Edit this page |