Index: Http.java =================================================================== --- Http.java (revision 430) +++ Http.java (working copy) @@ -27,6 +27,8 @@ import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.net.Authenticator; +import java.net.PasswordAuthentication; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; @@ -34,6 +36,7 @@ import java.util.Iterator; import java.util.Map; +import java.util.Properties; import javax.portlet.ActionRequest; import javax.portlet.RenderRequest; @@ -48,6 +51,8 @@ import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.SimpleHttpConnectionManager; import org.apache.commons.httpclient.URI; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; @@ -56,6 +61,7 @@ * View Source * * @author Brian Wing Shun Chan + * @version $Revision: 1.39 $ * */ public class Http { @@ -80,9 +86,29 @@ public static final int PROXY_PORT = GetterUtil.getInteger( SystemProperties.get(Http.class.getName() + ".proxy.port")); + public static final String PROXY_USER = GetterUtil.getString( + SystemProperties.get(Http.class.getName() + ".proxy.user")); + + public static final String PROXY_PASS = GetterUtil.getString( + SystemProperties.get(Http.class.getName() + ".proxy.pass")); + public static final int TIMEOUT = GetterUtil.getInteger( SystemProperties.get(Http.class.getName() + ".timeout"), 5000); + static { + if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) { + Properties systemProperties = System.getProperties(); + if (systemProperties.getProperty("http.proxyHost") == null) { + systemProperties.setProperty("http.proxyHost", PROXY_HOST); + systemProperties.setProperty("http.proxyPort", String.valueOf(PROXY_PORT)); + + if (Validator.isNotNull(PROXY_USER)) { + Authenticator.setDefault(new SimpleAuthenticator(PROXY_USER, PROXY_PASS)); + } + } + } + } + public static String decodeURL(String url) { if (url == null) { return null; @@ -280,15 +306,6 @@ location = HTTP_WITH_SLASH + location; } - HostConfiguration hostConfig = new HostConfiguration(); - - hostConfig.setHost(new URI(location)); - - if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) { - hostConfig.setProxy(PROXY_HOST, PROXY_PORT); - } - - client.setHostConfiguration(hostConfig); client.setConnectionTimeout(TIMEOUT); client.setTimeout(TIMEOUT); @@ -301,6 +318,22 @@ client.setState(state); } + HostConfiguration hostConfig = new HostConfiguration(); + + hostConfig.setHost(new URI(location)); + + if (Validator.isNotNull(PROXY_HOST) && PROXY_PORT > 0) { + hostConfig.setProxy(PROXY_HOST, PROXY_PORT); + + if (Validator.isNotNull(PROXY_USER)) { + client.getState().setProxyCredentials( + new AuthScope(PROXY_HOST, PROXY_PORT, null), + new UsernamePasswordCredentials(PROXY_USER, PROXY_PASS)); + } + } + + client.setHostConfiguration(hostConfig); + if (post) { method = new PostMethod(location); } @@ -336,6 +369,12 @@ } return byteArray; + } catch (IOException exc) { + exc.printStackTrace(); + throw exc; + } catch (RuntimeException exc) { + exc.printStackTrace(); + throw exc; } finally { try { @@ -407,4 +446,17 @@ return xml; } + public static class SimpleAuthenticator extends Authenticator { + private String username, password; + + public SimpleAuthenticator(String username,String password) { + this.username = username; + this.password = password; + } + + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username,password.toCharArray()); + } + } + } \ No newline at end of file