Search in sources :

Example 26 with URLConnection

use of java.net.URLConnection in project hadoop by apache.

the class URLConnectionFactory method openConnection.

/**
   * Opens a url with read and connect timeouts
   *
   * @param url
   *          URL to open
   * @param isSpnego
   *          whether the url should be authenticated via SPNEGO
   * @return URLConnection
   * @throws IOException
   * @throws AuthenticationException
   */
public URLConnection openConnection(URL url, boolean isSpnego) throws IOException, AuthenticationException {
    if (isSpnego) {
        LOG.debug("open AuthenticatedURL connection {}", url);
        UserGroupInformation.getCurrentUser().checkTGTAndReloginFromKeytab();
        final AuthenticatedURL.Token authToken = new AuthenticatedURL.Token();
        return new AuthenticatedURL(new KerberosUgiAuthenticator(), connConfigurator).openConnection(url, authToken);
    } else {
        LOG.debug("open URL connection");
        URLConnection connection = url.openConnection();
        if (connection instanceof HttpURLConnection) {
            connConfigurator.configure((HttpURLConnection) connection);
        }
        return connection;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL)

Example 27 with URLConnection

use of java.net.URLConnection in project hadoop by apache.

the class TestPathFilter 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)

Example 28 with URLConnection

use of java.net.URLConnection in project hadoop by apache.

the class TestServletFilter 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)

Example 29 with URLConnection

use of java.net.URLConnection in project hadoop by apache.

the class DFSck method listCorruptFileBlocks.

/*
   * To get the list, we need to call iteratively until the server says
   * there is no more left.
   */
private Integer listCorruptFileBlocks(String dir, String baseUrl) throws IOException {
    int errCode = -1;
    int numCorrupt = 0;
    int cookie = 0;
    final String noCorruptLine = "has no CORRUPT files";
    final String noMoreCorruptLine = "has no more CORRUPT files";
    final String cookiePrefix = "Cookie:";
    boolean allDone = false;
    while (!allDone) {
        final StringBuffer url = new StringBuffer(baseUrl);
        if (cookie > 0) {
            url.append("&startblockafter=").append(String.valueOf(cookie));
        }
        URL path = new URL(url.toString());
        URLConnection connection;
        try {
            connection = connectionFactory.openConnection(path, isSpnegoEnabled);
        } catch (AuthenticationException e) {
            throw new IOException(e);
        }
        InputStream stream = connection.getInputStream();
        BufferedReader input = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                if (line.startsWith(cookiePrefix)) {
                    try {
                        cookie = Integer.parseInt(line.split("\t")[1]);
                    } catch (Exception e) {
                        allDone = true;
                        break;
                    }
                    continue;
                }
                if ((line.endsWith(noCorruptLine)) || (line.endsWith(noMoreCorruptLine)) || (line.endsWith(NamenodeFsck.NONEXISTENT_STATUS))) {
                    allDone = true;
                    break;
                }
                if ((line.isEmpty()) || (line.startsWith("FSCK started by")) || (line.startsWith("The filesystem under path")))
                    continue;
                numCorrupt++;
                if (numCorrupt == 1) {
                    out.println("The list of corrupt files under path '" + dir + "' are:");
                }
                out.println(line);
            }
        } finally {
            input.close();
        }
    }
    out.println("The filesystem under path '" + dir + "' has " + numCorrupt + " CORRUPT files");
    if (numCorrupt == 0)
        errCode = 0;
    return errCode;
}
Also used : InputStreamReader(java.io.InputStreamReader) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) IOException(java.io.IOException)

Example 30 with URLConnection

use of java.net.URLConnection in project atlas by alibaba.

the class TPatchTool method downloadFile.

/**
     * http下载
     */
private void downloadFile(String httpUrl, File saveFile) throws IOException {
    // 下载网络文件
    int bytesum = 0;
    int byteread = 0;
    URL url = new URL(httpUrl);
    URLConnection conn = url.openConnection();
    InputStream inStream = conn.getInputStream();
    FileOutputStream fs = new FileOutputStream(saveFile);
    byte[] buffer = new byte[1204];
    while ((byteread = inStream.read(buffer)) != -1) {
        bytesum += byteread;
        fs.write(buffer, 0, byteread);
    }
    fs.flush();
    inStream.close();
    fs.close();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) 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