use of java.net.Authenticator in project tomee 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;
}
use of java.net.Authenticator in project tomee by apache.
the class ReferencingAuthenticator method tryWithInternal.
private PasswordAuthentication tryWithInternal(Authenticator a) throws Exception {
if (a == null) {
return null;
}
final Field[] fields;
if (SKIPCHECK) {
fields = Authenticator.class.getDeclaredFields();
} else {
fields = AccessController.doPrivileged((PrivilegedAction<Field[]>) () -> Authenticator.class.getDeclaredFields());
}
for (final Field f : fields) {
if (!Modifier.isStatic(f.getModifiers())) {
f.setAccessible(true);
Object o = f.get(this);
f.set(a, o);
}
}
Method method;
if (SKIPCHECK) {
method = Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
method.setAccessible(true);
} else {
method = AccessController.doPrivileged((PrivilegedAction<Method>) () -> {
try {
return Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
});
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
method.setAccessible(true);
return null;
});
}
return (PasswordAuthentication) method.invoke(a);
}
use of java.net.Authenticator in project tomee 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
}
}
use of java.net.Authenticator in project tomee 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
}
}
use of java.net.Authenticator in project cxf by apache.
the class AbstractCodegenMojo method configureProxyServerSettings.
protected void configureProxyServerSettings() throws MojoExecutionException {
Proxy proxy = mavenSession.getSettings().getActiveProxy();
if (proxy != null) {
getLog().info("Using proxy server configured in maven.");
if (proxy.getHost() == null) {
throw new MojoExecutionException("Proxy in settings.xml has no host");
}
if (proxy.getHost() != null) {
System.setProperty(HTTP_PROXY_HOST, proxy.getHost());
}
if (String.valueOf(proxy.getPort()) != null) {
System.setProperty(HTTP_PROXY_PORT, String.valueOf(proxy.getPort()));
}
if (proxy.getNonProxyHosts() != null) {
System.setProperty(HTTP_NON_PROXY_HOSTS, proxy.getNonProxyHosts());
}
if (!StringUtils.isEmpty(proxy.getUsername()) && !StringUtils.isEmpty(proxy.getPassword())) {
final String authUser = proxy.getUsername();
final String authPassword = proxy.getPassword();
Authenticator.setDefault(new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(authUser, authPassword.toCharArray());
}
});
System.setProperty(HTTP_PROXY_USER, authUser);
System.setProperty(HTTP_PROXY_PASSWORD, authPassword);
}
}
}
Aggregations