use of java.net.URLConnection in project hudson-2.x by hudson.
the class ProxyConfiguration method open.
/**
* This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly.
*/
public static URLConnection open(URL url) throws IOException {
// this code might run on slaves
Hudson hudson = Hudson.getInstance();
ProxyConfiguration proxyConfig = hudson != null ? hudson.proxy : null;
if (proxyConfig == null) {
return url.openConnection();
}
if (proxyConfig.noProxyFor != null) {
StringTokenizer tokenizer = new StringTokenizer(proxyConfig.noProxyFor, ",");
while (tokenizer.hasMoreTokens()) {
String noProxyHost = tokenizer.nextToken().trim();
if (noProxyHost.contains("*")) {
if (url.getHost().trim().contains(noProxyHost.replaceAll("\\*", ""))) {
return url.openConnection(Proxy.NO_PROXY);
}
} else if (url.getHost().trim().equals(noProxyHost)) {
return url.openConnection(Proxy.NO_PROXY);
}
}
}
URLConnection urlConnection = url.openConnection(proxyConfig.createProxy());
if (proxyConfig.isAuthNeeded()) {
String credentials = proxyConfig.getUserName() + ":" + proxyConfig.getPassword();
String encoded = new String(Base64.encodeBase64(credentials.getBytes()));
urlConnection.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
}
boolean connected = false;
int count = 0;
while (!connected) {
try {
urlConnection.connect();
connected = true;
} catch (SocketTimeoutException exc) {
LOGGER.fine("Connection timed out. trying again " + count);
if (++count > TIME_OUT_RETRY_COUNT) {
throw new IOException("Could not connect to " + url.toExternalForm() + ". Connection timed out after " + TIME_OUT_RETRY_COUNT + " tries.");
}
connected = false;
} catch (UnknownHostException exc) {
throw new IOException2("Could not connect to " + url.toExternalForm() + ". Check your internet connection.", exc);
} catch (ConnectException exc) {
throw new IOException2("Could not connect to " + url.toExternalForm() + ". Check your internet connection.", exc);
}
}
return urlConnection;
}
use of java.net.URLConnection in project hudson-2.x by hudson.
the class CLI method getCliTcpPort.
/**
* If the server advertises CLI port, returns it.
*/
private int getCliTcpPort(String url) throws IOException {
URLConnection head = new URL(url).openConnection();
try {
head.connect();
} catch (IOException e) {
throw (IOException) new IOException("Failed to connect to " + url).initCause(e);
}
String p = head.getHeaderField("X-Hudson-CLI-Port");
if (p == null)
return -1;
return Integer.parseInt(p);
}
use of java.net.URLConnection in project jmonkeyengine by jMonkeyEngine.
the class NativeLibraryLoader method computeNativesHash.
private static int computeNativesHash() {
URLConnection conn = null;
try {
String classpath = System.getProperty("java.class.path");
URL url = Thread.currentThread().getContextClassLoader().getResource("com/jme3/system/NativeLibraryLoader.class");
StringBuilder sb = new StringBuilder(url.toString());
if (sb.indexOf("jar:") == 0) {
sb.delete(0, 4);
sb.delete(sb.indexOf("!"), sb.length());
sb.delete(sb.lastIndexOf("/") + 1, sb.length());
}
try {
url = new URL(sb.toString());
} catch (MalformedURLException ex) {
throw new UnsupportedOperationException(ex);
}
conn = url.openConnection();
int hash = classpath.hashCode() ^ (int) conn.getLastModified();
return hash;
} catch (IOException ex) {
throw new UnsupportedOperationException(ex);
} finally {
if (conn != null) {
try {
conn.getInputStream().close();
conn.getOutputStream().close();
} catch (IOException ex) {
}
}
}
}
use of java.net.URLConnection in project Talon-for-Twitter by klinker24.
the class IOUtils method saveGiffy.
public static final Uri saveGiffy(String videoUrl) throws Exception {
File myDir = new File(Environment.getExternalStorageDirectory() + "/Talon");
myDir.mkdirs();
final File file = new File(Environment.getExternalStorageDirectory(), "Talon/giffy.gif");
if (!file.createNewFile()) {
// file already exists
}
URL url = new URL(videoUrl);
URLConnection connection = url.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(30000);
InputStream is = connection.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
FileOutputStream outStream = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 5];
int len;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
outStream.flush();
outStream.close();
inStream.close();
return Uri.fromFile(file);
}
use of java.net.URLConnection in project hadoop by apache.
the class TestGlobalFilter method access.
/** access a url, ignoring some IOException such as the page does not exist */
static void access(String urlstring) throws IOException {
LOG.warn("access " + urlstring);
URL url = new URL(urlstring);
URLConnection connection = url.openConnection();
connection.connect();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
try {
for (; in.readLine() != null; ) ;
} finally {
in.close();
}
} catch (IOException ioe) {
LOG.warn("urlstring=" + urlstring, ioe);
}
}
Aggregations