Search in sources :

Example 36 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class ReferencingAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    Authenticator cxfauth = auth.get();
    if (cxfauth == null) {
        remove();
    }
    PasswordAuthentication pauth = null;
    if (wrapped != null) {
        try {
            pauth = tryWith(wrapped);
            if (pauth != null) {
                return pauth;
            }
        } catch (Exception e) {
            pauth = null;
        }
    }
    if (cxfauth != null) {
        try {
            pauth = tryWith(cxfauth);
        } catch (Exception e1) {
            pauth = null;
        }
    }
    return pauth;
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 37 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class ReferencingAuthenticator method removeInternal.

private void removeInternal() {
    try {
        for (final Field f : Authenticator.class.getDeclaredFields()) {
            if (f.getType().equals(Authenticator.class)) {
                try {
                    f.setAccessible(true);
                    Authenticator o = (Authenticator) f.get(null);
                    if (o == this) {
                        // this is at the root of any chain of authenticators
                        Authenticator.setDefault(wrapped);
                    } else {
                        removeFromChain(o);
                    }
                } catch (Exception e) {
                // ignore
                }
            }
        }
    } catch (Throwable t) {
    // ignore
    }
}
Also used : Field(java.lang.reflect.Field) Authenticator(java.net.Authenticator)

Example 38 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class ReferencingAuthenticator method check.

public final void check() {
    Authenticator cxfauth = auth.get();
    if (cxfauth == null) {
        remove();
    }
    if (wrapped != null && wrapped.getClass().getName().equals(ReferencingAuthenticator.class.getName())) {
        try {
            Method m = wrapped.getClass().getMethod("check");
            m.setAccessible(true);
            m.invoke(wrapped);
        } catch (Throwable t) {
        // ignore
        }
    }
}
Also used : Method(java.lang.reflect.Method) Authenticator(java.net.Authenticator)

Example 39 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class ReferencingAuthenticator method remove.

private void remove() {
    try {
        // Try Authenticator.getDefault() first, JDK9+
        final MethodHandle mt = MethodHandles.lookup().findStatic(Authenticator.class, "getDefault", MethodType.methodType(Authenticator.class));
        removeInternal((Authenticator) mt.invoke());
    } catch (final NoSuchMethodException | IllegalAccessException ex) {
        removeInternal();
    } catch (Throwable e) {
    // ignore
    }
}
Also used : Authenticator(java.net.Authenticator) MethodHandle(java.lang.invoke.MethodHandle)

Example 40 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class ReferencingAuthenticator method removeFromChain.

private void removeFromChain(Authenticator a) {
    try {
        if (a.getClass().getName().equals(ReferencingAuthenticator.class.getName())) {
            // multiple referencing authenticators, we can remove ourself
            Field f2 = a.getClass().getDeclaredField("wrapped");
            f2.setAccessible(true);
            Authenticator a2 = (Authenticator) f2.get(a);
            if (a2 == this) {
                f2.set(a, wrapped);
            } else {
                removeFromChain(a2);
            }
        }
    } catch (Throwable t) {
    // ignore
    }
}
Also used : Field(java.lang.reflect.Field) Authenticator(java.net.Authenticator)

Aggregations

Authenticator (java.net.Authenticator)83 PasswordAuthentication (java.net.PasswordAuthentication)52 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ServerSocket (java.net.ServerSocket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3