Search in sources :

Example 21 with URLConnection

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;
}
Also used : StringTokenizer(java.util.StringTokenizer) SocketTimeoutException(java.net.SocketTimeoutException) UnknownHostException(java.net.UnknownHostException) Hudson(hudson.model.Hudson) IOException(java.io.IOException) URLConnection(java.net.URLConnection) IOException2(hudson.util.IOException2) ConnectException(java.net.ConnectException)

Example 22 with 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);
}
Also used : IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 23 with URLConnection

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) {
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 24 with URLConnection

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);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 25 with URLConnection

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

URLConnection (java.net.URLConnection)1686 URL (java.net.URL)1180 IOException (java.io.IOException)740 InputStream (java.io.InputStream)569 HttpURLConnection (java.net.HttpURLConnection)465 InputStreamReader (java.io.InputStreamReader)404 BufferedReader (java.io.BufferedReader)358 Test (org.junit.Test)206 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)202 File (java.io.File)196 MalformedURLException (java.net.MalformedURLException)190 BufferedInputStream (java.io.BufferedInputStream)119 FileOutputStream (java.io.FileOutputStream)112 OutputStream (java.io.OutputStream)112 FileInputStream (java.io.FileInputStream)111 JarURLConnection (java.net.JarURLConnection)106 ArrayList (java.util.ArrayList)92 MockResponse (okhttp3.mockwebserver.MockResponse)76 ByteArrayOutputStream (java.io.ByteArrayOutputStream)74 FileNotFoundException (java.io.FileNotFoundException)59