Example usage for java.net URLConnection getClass

List of usage examples for java.net URLConnection getClass

Introduction

In this page you can find the example usage for java.net URLConnection getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.qedeq.base.io.UrlUtility.java

/**
 * Make local copy of an URL./*from  w w w .  j a va 2  s  . c o  m*/
 *
 * @param   url             Save this URL.
 * @param   f               Save into this file. An existing file is overwritten.
 * @param   proxyHost       Use this proxy host.
 * @param   proxyPort       Use this port at proxy host.
 * @param   nonProxyHosts   This are hosts not to be proxied.
 * @param   connectTimeout  Connection timeout.
 * @param   readTimeout     Read timeout.
 * @param   listener        Here completion events are fired.
 * @throws  IOException     Saving failed.
 */
public static void saveUrlToFile(final String url, final File f, final String proxyHost, final String proxyPort,
        final String nonProxyHosts, final int connectTimeout, final int readTimeout,
        final LoadingListener listener) throws IOException {
    final String method = "saveUrlToFile()";
    Trace.begin(CLASS, method);

    // if we are not web started and running under Java 1.4 we use apache commons
    // httpclient library (so we can set timeouts)
    if (!isSetConnectionTimeOutSupported() && !IoUtility.isWebStarted()) {
        saveQedeqFromWebToBufferApache(url, f, proxyHost, proxyPort, nonProxyHosts, connectTimeout, readTimeout,
                listener);
        Trace.end(CLASS, method);
        return;
    }

    // set proxy properties according to kernel configuration (if not webstarted)
    if (!IoUtility.isWebStarted()) {
        if (proxyHost != null) {
            System.setProperty("http.proxyHost", proxyHost);
        }
        if (proxyPort != null) {
            System.setProperty("http.proxyPort", proxyPort);
        }
        if (nonProxyHosts != null) {
            System.setProperty("http.nonProxyHosts", nonProxyHosts);
        }
    }

    FileOutputStream out = null;
    InputStream in = null;
    try {
        final URLConnection connection = new URL(url).openConnection();

        if (connection instanceof HttpURLConnection) {
            final HttpURLConnection httpConnection = (HttpURLConnection) connection;
            // if we are running at least under Java 1.5 the following code should be executed
            if (isSetConnectionTimeOutSupported()) {
                try {
                    YodaUtility.executeMethod(httpConnection, "setConnectTimeout", new Class[] { Integer.TYPE },
                            new Object[] { new Integer(connectTimeout) });
                } catch (NoSuchMethodException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setConnectTimeout was previously found", e);
                } catch (InvocationTargetException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setConnectTimeout throwed an error", e);
                }
            }
            // if we are running at least under Java 1.5 the following code should be executed
            if (isSetReadTimeoutSupported()) {
                try {
                    YodaUtility.executeMethod(httpConnection, "setReadTimeout", new Class[] { Integer.TYPE },
                            new Object[] { new Integer(readTimeout) });
                } catch (NoSuchMethodException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setReadTimeout was previously found", e);
                } catch (InvocationTargetException e) {
                    Trace.fatal(CLASS, method, "URLConnection.setReadTimeout throwed an error", e);
                }
            }
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == 200) {
                in = httpConnection.getInputStream();
            } else {
                in = httpConnection.getErrorStream();
                final String errorText = IoUtility.loadStreamWithoutException(in, 1000);
                throw new IOException("Response code from HTTP server was " + responseCode
                        + (errorText.length() > 0 ? "\nResponse  text from HTTP server was:\n" + errorText
                                : ""));
            }
        } else {
            Trace.paramInfo(CLASS, method, "connection.getClass", connection.getClass().toString());
            in = connection.getInputStream();
        }

        if (!url.equals(connection.getURL().toString())) {
            throw new FileNotFoundException(
                    "\"" + url + "\" was substituted by " + "\"" + connection.getURL() + "\" from server");
        }
        final double maximum = connection.getContentLength();
        IoUtility.createNecessaryDirectories(f);
        out = new FileOutputStream(f);
        final byte[] buffer = new byte[4096];
        int bytesRead; // bytes read during one buffer read
        int position = 0; // current reading position within the whole document
        // continue writing
        while ((bytesRead = in.read(buffer)) != -1) {
            position += bytesRead;
            out.write(buffer, 0, bytesRead);
            if (maximum > 0) {
                double completeness = position / maximum;
                if (completeness < 0) {
                    completeness = 0;
                }
                if (completeness > 100) {
                    completeness = 1;
                }
                listener.loadingCompletenessChanged(completeness);
            }
        }
        listener.loadingCompletenessChanged(1);
    } finally {
        IoUtility.close(out);
        out = null;
        IoUtility.close(in);
        in = null;
        Trace.end(CLASS, method);
    }
}

From source file:uk.ac.sanger.cgp.dbcon.util.resources.UrlResource.java

/**
 * Used to provide a clean up procedure when dealing with URLs
 *//*from www .  jav a 2 s  .  c  o  m*/
private void cleanUpHttpURLConnection(URLConnection connection) throws DbConException {

    if (connection == null) {
        getLog().fatal("Input HttpURLConnection was null ... will not perform cleanup");
    } else if (HttpURLConnection.class.isAssignableFrom(connection.getClass())) {
        InputStream errorStream = null;
        try {
            HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
            errorStream = httpUrlConnection.getErrorStream();
            if (errorStream != null) {
                byte[] errorOutput = IOUtils.toByteArray(errorStream);
                if (getLog().isDebugEnabled()) {
                    String lengthOfError = (errorOutput == null) ? "NULL STREAM"
                            : Integer.toString(errorOutput.length);
                    getLog().debug("Length of error stream (in bytes): " + lengthOfError);
                }
            }

            int responseCode = httpUrlConnection.getResponseCode();
            if (getLog().isDebugEnabled()) {
                getLog().debug("Detected response code: " + responseCode);
            }

        } catch (IOException e) {
            throw new DbConException("Detected IOException when retriving errorStream", e);
        } finally {
            InputOutputUtils.closeQuietly(errorStream);
        }
    } else {
        getLog().debug("Input URLConnection was not a HttpURLConnection; " + "will not perform cleanup");
    }
}