Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions source/java/org/rsna/util/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
package org.rsna.util;

import java.io.*;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.security.SecureRandom;
import java.util.Properties;
Expand Down Expand Up @@ -56,6 +58,7 @@ public static HttpURLConnection getConnection(String urlString) throws Exception
*/
public static HttpURLConnection getConnection(URL url) throws Exception {

initializeProxyAuthenticator();
String protocol = url.getProtocol().toLowerCase();
if (!protocol.startsWith("https") && !protocol.startsWith("http")) {
throw new Exception("Unsupported protocol ("+protocol+")");
Expand Down Expand Up @@ -115,4 +118,33 @@ public static CookieManager initializeCookieManager() {
CookieHandler.setDefault(cookieManager);
return cookieManager;
}

/**
* Initialize a default Authenticator for proxy server basic authentication.
*/
private static void initializeProxyAuthenticator() {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == RequestorType.PROXY) {
String prot = getRequestingProtocol().toLowerCase();
String host = System.getProperty(prot + ".proxyHost");
String port = System.getProperty(prot + ".proxyPort");
String user = System.getProperty(prot + ".proxyUser");
String password = System.getProperty(prot + ".proxyPassword");
if (user != null && password != null) {
if (getRequestingHost().equalsIgnoreCase(host)) {
if (Integer.parseInt(port) == getRequestingPort()) {
// Host and port seems to match configured proxy details
return new PasswordAuthentication(user, password.toCharArray());
}
}
}
}
return null;
}
});
// Need to remove basic auth from disabled schemes in Java 8
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
}
}