Example usage for java.net HttpURLConnection getClass

List of usage examples for java.net HttpURLConnection getClass

Introduction

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

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.spotify.helios.client.DefaultHttpConnector.java

private static void setRequestMethod(final HttpURLConnection connection, final String method,
        final boolean isHttps) {
    // Nasty workaround for ancient HttpURLConnection only supporting few methods
    final Class<?> httpURLConnectionClass = connection.getClass();
    try {//from   ww  w  . j  a v  a2s  .  c om
        Field methodField;
        HttpURLConnection delegate;
        if (isHttps) {
            final Field delegateField = httpURLConnectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            delegate = (HttpURLConnection) delegateField.get(connection);
            methodField = delegate.getClass().getSuperclass().getSuperclass().getSuperclass()
                    .getDeclaredField("method");
        } else {
            delegate = connection;
            methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
        }

        methodField.setAccessible(true);
        methodField.set(delegate, method);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.screenslicer.common.CommonUtil.java

public static void setMethod(HttpURLConnection httpURLConnection, String method) {
    try {//from w  w  w  .  j  av a  2 s .  c  om
        httpURLConnection.setRequestMethod(method);
        // Check whether we are running on a buggy JRE
    } catch (final ProtocolException pe) {
        Class<?> connectionClass = httpURLConnection.getClass();
        Field delegateField = null;
        try {
            delegateField = connectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            HttpURLConnection delegateConnection = (HttpURLConnection) delegateField.get(httpURLConnection);
            setMethod(delegateConnection, method);
        } catch (NoSuchFieldException e) {
            // Ignore for now, keep going
        } catch (IllegalArgumentException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        try {
            Field methodField;
            while (connectionClass != null) {
                try {
                    methodField = connectionClass.getDeclaredField("method");
                } catch (NoSuchFieldException e) {
                    connectionClass = connectionClass.getSuperclass();
                    continue;
                }
                methodField.setAccessible(true);
                methodField.set(httpURLConnection, method);
                break;
            }
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.spotify.helios.client.DefaultRequestDispatcher.java

private void setRequestMethod(final HttpURLConnection connection, final String method, final boolean isHttps) {
    // Nasty workaround for ancient HttpURLConnection only supporting few methods
    final Class<?> httpURLConnectionClass = connection.getClass();
    try {//from   w  w  w.  j a va2s  .c  o m
        Field methodField;
        HttpURLConnection delegate;
        if (isHttps) {
            final Field delegateField = httpURLConnectionClass.getDeclaredField("delegate");
            delegateField.setAccessible(true);
            delegate = (HttpURLConnection) delegateField.get(connection);
            methodField = delegate.getClass().getSuperclass().getSuperclass().getSuperclass()
                    .getDeclaredField("method");
        } else {
            delegate = connection;
            methodField = httpURLConnectionClass.getSuperclass().getDeclaredField("method");
        }

        methodField.setAccessible(true);
        methodField.set(delegate, method);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.kohsuke.github.Requester.java

private void setRequestMethod(HttpURLConnection uc) throws IOException {
    try {//from w  w  w  . j  a va  2  s . c om
        uc.setRequestMethod(method);
    } catch (ProtocolException e) {
        // JDK only allows one of the fixed set of verbs. Try to override that
        try {
            Field $method = HttpURLConnection.class.getDeclaredField("method");
            $method.setAccessible(true);
            $method.set(uc, method);
        } catch (Exception x) {
            throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
        }
        // sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
        try {
            Field $delegate = uc.getClass().getDeclaredField("delegate");
            $delegate.setAccessible(true);
            Object delegate = $delegate.get(uc);
            if (delegate instanceof HttpURLConnection) {
                HttpURLConnection nested = (HttpURLConnection) delegate;
                setRequestMethod(nested);
            }
        } catch (NoSuchFieldException x) {
            // no problem
        } catch (IllegalAccessException x) {
            throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
        }
    }
    if (!uc.getRequestMethod().equals(method))
        throw new IllegalStateException("Failed to set the request method to " + method);
}

From source file:org.apache.jmeter.util.JsseSSLManager.java

/**
 * Sets the Context attribute of the JsseSSLManager object
 *
 * @param conn/*from w  w w  .jav  a 2  s . com*/
 *            The new Context value
 */
@Override
public void setContext(HttpURLConnection conn) {
    if (conn instanceof HttpsURLConnection) {
        /*
         * No point doing this on a per-connection basis, as there is currently no way to configure it.
         * So we leave it to the defaults set up in the SSL Context
         *
         */
        //          HttpsURLConnection secureConn = (HttpsURLConnection) conn;
        //          secureConn.setSSLSocketFactory(this.getContext().getSocketFactory());
    } else {
        log.warn("Unexpected HttpURLConnection class: " + conn.getClass().getName());
    }
}