Search in sources :

Example 6 with URLConnection

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

the class LogRetriever method logAttempt.

// Retrieve attempt log into logDir
private void logAttempt(String job, AttemptInfo attemptInfo, String logDir) throws IOException {
    Path attemptDir = new Path(logDir, attemptInfo.id);
    fs.mkdirs(attemptDir);
    for (String type : new String[] { "stderr", "stdout", "syslog" }) {
        // Retrieve log from task tracker
        String fileInURL = "tasklog?attemptid=" + attemptInfo.id + "&plaintext=true&filter=" + type;
        URL url = new URL(attemptInfo.baseUrl.getProtocol(), attemptInfo.baseUrl.getHost(), attemptInfo.baseUrl.getPort(), "/" + fileInURL);
        URLConnection urlConnection = url.openConnection();
        BufferedReader reader = null;
        PrintWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            writer = new PrintWriter(new OutputStreamWriter(fs.create(new Path(attemptDir, type))));
            // Copy log file
            String line;
            while ((line = reader.readLine()) != null) {
                writer.println(line);
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) URL(java.net.URL) URLConnection(java.net.URLConnection) PrintWriter(java.io.PrintWriter)

Example 7 with URLConnection

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

the class LogRetriever method getMatches.

// Utility to get patterns from a url, every array element is match for one
// pattern
private List<String>[] getMatches(URL url, Pattern[] pattern) throws IOException {
    List<String>[] results = new ArrayList[pattern.length];
    for (int i = 0; i < pattern.length; i++) {
        results[i] = new ArrayList<String>();
    }
    URLConnection urlConnection = url.openConnection();
    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    String line;
    while ((line = reader.readLine()) != null) {
        for (int i = 0; i < pattern.length; i++) {
            Matcher matcher = pattern[i].matcher(line);
            if (matcher.find()) {
                results[i].add(matcher.group(1));
            }
        }
    }
    reader.close();
    return results;
}
Also used : InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List) URLConnection(java.net.URLConnection)

Example 8 with URLConnection

use of java.net.URLConnection in project hbase 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)

Example 9 with URLConnection

use of java.net.URLConnection in project hbase 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 10 with URLConnection

use of java.net.URLConnection in project hudson-2.x by hudson.

the class Which method jarFile.

/**
     * Locates the jar file that contains the given class.
     *
     * <p>
     * Note that jar files are not always loaded from {@link File},
     * so for diagnostics purposes {@link #jarURL(Class)} is preferrable.
     *
     * @throws IllegalArgumentException
     *      if failed to determine.
     */
public static File jarFile(Class clazz) throws IOException {
    URL res = jarURL(clazz);
    String resURL = res.toExternalForm();
    String originalURL = resURL;
    if (resURL.startsWith("jar:file:") || resURL.startsWith("wsjar:file:"))
        return fromJarUrlToFile(resURL);
    if (resURL.startsWith("code-source:/")) {
        // OC4J apparently uses this. See http://www.nabble.com/Hudson-on-OC4J-tt16702113.html
        // cut off jar: and the file name portion
        resURL = resURL.substring("code-source:/".length(), resURL.lastIndexOf('!'));
        return new File(decode(new URL("file:/" + resURL).getPath()));
    }
    if (resURL.startsWith("zip:")) {
        // weblogic uses this. See http://www.nabble.com/patch-to-get-Hudson-working-on-weblogic-td23997258.html
        // also see http://www.nabble.com/Re%3A-Hudson-on-Weblogic-10.3-td25038378.html#a25043415
        // cut off zip: and the file name portion
        resURL = resURL.substring("zip:".length(), resURL.lastIndexOf('!'));
        return new File(decode(new URL("file:" + resURL).getPath()));
    }
    if (resURL.startsWith("file:")) {
        // unpackaged classes
        // how many slashes do wo need to cut?
        int n = clazz.getName().split("\\.").length;
        for (; n > 0; n--) {
            int idx = Math.max(resURL.lastIndexOf('/'), resURL.lastIndexOf('\\'));
            if (idx < 0)
                throw new IllegalArgumentException(originalURL + " - " + resURL);
            resURL = resURL.substring(0, idx);
        }
        return new File(decode(new URL(resURL).getPath()));
    }
    if (resURL.startsWith("vfszip:")) {
        // JBoss5
        InputStream is = res.openStream();
        try {
            Object delegate = is;
            try {
                while (delegate.getClass().getEnclosingClass() != ZipFile.class) {
                    Field f = delegate.getClass().getDeclaredField("delegate");
                    f.setAccessible(true);
                    delegate = f.get(delegate);
                }
            } catch (NoSuchFieldException e) {
                // extra step for JDK6u24; zip internals have changed
                Field f = delegate.getClass().getDeclaredField("is");
                f.setAccessible(true);
                delegate = f.get(delegate);
            }
            Field f = delegate.getClass().getDeclaredField("this$0");
            f.setAccessible(true);
            ZipFile zipFile = (ZipFile) f.get(delegate);
            return new File(zipFile.getName());
        } catch (Throwable e) {
            // something must have changed in JBoss5. fall through
            LOGGER.log(Level.FINE, "Failed to resolve vfszip into a jar location", e);
        } finally {
            is.close();
        }
    }
    if (resURL.startsWith("vfs:") || resURL.startsWith("vfsfile:")) {
        // JBoss6
        try {
            String resource = '/' + clazz.getName().replace('.', '/');
            resURL = resURL.substring(0, resURL.lastIndexOf(resource));
            Object content = new URL(res, resURL).getContent();
            if (content instanceof File) {
                return (File) content;
            }
            Method m = content.getClass().getMethod("getPhysicalFile");
            return (File) m.invoke(content);
        } catch (Throwable e) {
            // something must have changed in JBoss6. fall through
            LOGGER.log(Level.FINE, "Failed to resolve vfs/vfsfile into a jar location", e);
        }
    }
    if (resURL.startsWith("bundleresource:") || resURL.startsWith("bundle:")) {
        // Equinox/Felix/etc.
        try {
            URLConnection con = res.openConnection();
            Method m = con.getClass().getDeclaredMethod("getLocalURL");
            m.setAccessible(true);
            res = (URL) m.invoke(con);
        } catch (Throwable e) {
            // something must have changed in Equinox. fall through
            LOGGER.log(Level.FINE, "Failed to resolve bundleresource into a jar location", e);
        }
    }
    URLConnection con = res.openConnection();
    if (con instanceof JarURLConnection) {
        JarURLConnection jcon = (JarURLConnection) con;
        JarFile jarFile = jcon.getJarFile();
        if (jarFile != null) {
            String n = jarFile.getName();
            if (n.length() > 0) {
                // JDK6u10 needs this
                return new File(n);
            } else {
                // so this just keeps getting tricker and trickier...
                try {
                    Field f = ZipFile.class.getDeclaredField("name");
                    f.setAccessible(true);
                    return new File((String) f.get(jarFile));
                } catch (NoSuchFieldException e) {
                    LOGGER.log(Level.INFO, "Failed to obtain the local cache file name of " + clazz, e);
                } catch (IllegalAccessException e) {
                    LOGGER.log(Level.INFO, "Failed to obtain the local cache file name of " + clazz, e);
                }
            }
        }
    }
    throw new IllegalArgumentException(originalURL + " - " + resURL);
}
Also used : InputStream(java.io.InputStream) JarURLConnection(java.net.JarURLConnection) Method(java.lang.reflect.Method) JarFile(java.util.jar.JarFile) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) Field(java.lang.reflect.Field) ZipFile(java.util.zip.ZipFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

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