|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | +package com.redhat.devtools.intellij.common.utils; |
| 12 | + |
| 13 | +import java.lang.reflect.Constructor; |
| 14 | +import java.lang.reflect.Method; |
| 15 | +import java.net.Authenticator; |
| 16 | +import java.net.PasswordAuthentication; |
| 17 | +import java.net.Proxy; |
| 18 | +import java.net.ProxySelector; |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URISyntaxException; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +public class IdeProxyAdapter { |
| 25 | + |
| 26 | + public static ProxySelector getProxySelector() { |
| 27 | + ProxySelector selector = Pre243.getProxySelector(); |
| 28 | + if (selector != null) { |
| 29 | + return selector; |
| 30 | + } else { |
| 31 | + return Post243.getProxySelector(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public static Authenticator getAuthenticator() { |
| 36 | + Authenticator authenticator = Pre243.getAuthenticator(); |
| 37 | + if (authenticator != null) { |
| 38 | + return authenticator; |
| 39 | + } else { |
| 40 | + return Post243.getAuthenticator(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * |
| 46 | + * Returns the proxies that exist for the given url. |
| 47 | + * |
| 48 | + * @param url the url to get proxies for |
| 49 | + * @return the proxies for the given url |
| 50 | + * |
| 51 | + */ |
| 52 | + public static List<Proxy> getProxies(String url) { |
| 53 | + List<Proxy> proxies = Pre243.getProxies(url); |
| 54 | + if (proxies != null |
| 55 | + && !proxies.isEmpty()) { |
| 56 | + return proxies; |
| 57 | + } else { |
| 58 | + return Post243.getProxies(url); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static PasswordAuthentication getPasswordAuthentication() { |
| 63 | + PasswordAuthentication authentication = Pre243.getPasswordAuthentication(); |
| 64 | + if (authentication != null) { |
| 65 | + return authentication; |
| 66 | + } else { |
| 67 | + return Post243.getPasswordAuthentication(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static class Pre243 { |
| 72 | + |
| 73 | + static ProxySelector getProxySelector() { |
| 74 | + Object httpConfigurable = httpConfigurable_getInstance(); |
| 75 | + if (httpConfigurable == null) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + return (ProxySelector) ideaWideProxySelector_newInstance(httpConfigurable); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * <code> |
| 83 | + * new IdeaWideAuthenticator(HttpConfigurable) |
| 84 | + * </code> |
| 85 | + */ |
| 86 | + static Authenticator getAuthenticator() { |
| 87 | + Object commonProxy = commonProxy_getInstance(); |
| 88 | + if (commonProxy == null) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + return commonProxy_getAuthenticator(commonProxy); |
| 92 | + } |
| 93 | + |
| 94 | + static List<Proxy> getProxies(String url) { |
| 95 | + List<Proxy> proxies = new ArrayList<>(); |
| 96 | + try { |
| 97 | + Object httpConfigurable = httpConfigurable_getInstance(); |
| 98 | + if (httpConfigurable != null) { |
| 99 | + Object ideaWideProxySelector = ideaWideProxySelector_newInstance(httpConfigurable); |
| 100 | + if (ideaWideProxySelector != null) { |
| 101 | + proxies = ideaWideProxySelector_select(ideaWideProxySelector, new URI(url)); |
| 102 | + } |
| 103 | + } |
| 104 | + } catch (URISyntaxException e) { |
| 105 | + // swallow only |
| 106 | + } |
| 107 | + return proxies; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * <code> |
| 112 | + * new IdeaWideAuthenticator(HttpConfigurable.getInstance()).getPasswordAuthentication() |
| 113 | + * </code> |
| 114 | + */ |
| 115 | + static private PasswordAuthentication getPasswordAuthentication() { |
| 116 | + PasswordAuthentication authentication = null; |
| 117 | + Authenticator authenticator = getAuthenticator(); |
| 118 | + if (authenticator != null) { |
| 119 | + authentication = authenticator_getPasswordAuthentication(authenticator); |
| 120 | + } |
| 121 | + return authentication; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * <code> |
| 126 | + * IdeaWideProxySelector.select(URI) |
| 127 | + * </code> |
| 128 | + */ |
| 129 | + @SuppressWarnings("unchecked") |
| 130 | + private static List<Proxy> ideaWideProxySelector_select(Object ideaWideProxySelector, URI uri) { |
| 131 | + try { |
| 132 | + Method method = ideaWideProxySelector.getClass().getDeclaredMethod("select", URI.class); |
| 133 | + return (List<Proxy>) method.invoke(ideaWideProxySelector, uri); |
| 134 | + } catch (Exception e) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * <code> |
| 141 | + * new IdeaWideProxySelector() |
| 142 | + * </code> |
| 143 | + */ |
| 144 | + @SuppressWarnings("JavaReflectionMemberAccess") |
| 145 | + private static Object ideaWideProxySelector_newInstance(Object httpConfigurable) { |
| 146 | + try { |
| 147 | + Class<?> clazz = Class.forName("com.intellij.util.net.IdeaWideProxySelector"); |
| 148 | + Constructor<?> constructor = clazz.getConstructor(httpConfigurable.getClass()); |
| 149 | + return constructor.newInstance(httpConfigurable); |
| 150 | + } catch (Exception e) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * <code> |
| 157 | + * new IdeaWideAuthenticator(HttpConfigurable) |
| 158 | + * </code> |
| 159 | + */ |
| 160 | + private static Object ideaWideAuthenticator_newInstance(Object httpConfigurable) { |
| 161 | + try { |
| 162 | + Class<?> clazz = httpConfigurable.getClass(); |
| 163 | + Constructor<?> constructor = clazz.getConstructor(httpConfigurable.getClass()); |
| 164 | + return constructor.newInstance(httpConfigurable); |
| 165 | + } catch (Exception e) { |
| 166 | + return null; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * <code> |
| 172 | + * HttpConfigurable.getInstance() |
| 173 | + * </code> |
| 174 | + */ |
| 175 | + private static Object httpConfigurable_getInstance() { |
| 176 | + try { |
| 177 | + Class<?> clazz = Class.forName("com.intellij.util.net.HttpConfigurable"); |
| 178 | + Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); |
| 179 | + return getInstanceMethod.invoke(null); |
| 180 | + } catch (Exception e) { |
| 181 | + return null; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * <code> |
| 187 | + * CommonProxy.getInstance() |
| 188 | + * </code> |
| 189 | + */ |
| 190 | + private static Object commonProxy_getInstance() { |
| 191 | + try { |
| 192 | + Class<?> clazz = Class.forName("com.intellij.util.proxy.CommonProxy"); |
| 193 | + Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); |
| 194 | + return getInstanceMethod.invoke(null); |
| 195 | + } catch (ReflectiveOperationException e) { |
| 196 | + return null; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private static Authenticator commonProxy_getAuthenticator(Object commonProxy) { |
| 201 | + try { |
| 202 | + Class<?> clazz = commonProxy.getClass(); |
| 203 | + Method method = clazz.getDeclaredMethod("getAuthenticator"); |
| 204 | + return (Authenticator) method.invoke(commonProxy); |
| 205 | + } catch (ReflectiveOperationException e) { |
| 206 | + return null; |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + private static class Post243 { |
| 212 | + |
| 213 | + /** |
| 214 | + * <code> |
| 215 | + * JdkProxyProvider.getInstance().getProxySelector() |
| 216 | + * </code> |
| 217 | + */ |
| 218 | + public static ProxySelector getProxySelector() { |
| 219 | + Object provider = jdkProxyProvider_getInstance(); |
| 220 | + if (provider == null) { |
| 221 | + return null; |
| 222 | + } |
| 223 | + return jdkProxyProvider_getProxySelector(provider); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * <code> |
| 228 | + * JdkProxyProvider.getInstance().getAuthenticator() |
| 229 | + * </code> |
| 230 | + */ |
| 231 | + public static Authenticator getAuthenticator() { |
| 232 | + Object proxyAuthentication = jdkProxyProvider_getInstance(); |
| 233 | + if (proxyAuthentication == null) { |
| 234 | + return null; |
| 235 | + } |
| 236 | + return jdkProxyProvider_getAuthenticator(proxyAuthentication); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * <code> |
| 241 | + * JdkProxyProvider.getInstance().getProxySelector().select(URI.create(url)) |
| 242 | + * </code> |
| 243 | + */ |
| 244 | + public static List<Proxy> getProxies(String url) { |
| 245 | + List<Proxy> proxies = new ArrayList<>(); |
| 246 | + try { |
| 247 | + ProxySelector selector = getProxySelector(); |
| 248 | + if (selector != null) { |
| 249 | + proxies = proxySelector_select(selector, new URI(url)); |
| 250 | + } |
| 251 | + } catch (Exception e) { |
| 252 | + // swallow only |
| 253 | + } |
| 254 | + return proxies; |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * <code> |
| 259 | + * JdkProxyProvider.getInstance().getAuthenticator().getPasswordAuthentication(); |
| 260 | + * </code> |
| 261 | + */ |
| 262 | + private static PasswordAuthentication getPasswordAuthentication() { |
| 263 | + PasswordAuthentication authentication = null; |
| 264 | + Authenticator authenticator = getAuthenticator(); |
| 265 | + if (authenticator != null) { |
| 266 | + authentication = authenticator_getPasswordAuthentication(authenticator); |
| 267 | + } |
| 268 | + return authentication; |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * <code> |
| 273 | + * ProxySelector.select(URI) |
| 274 | + * <code> |
| 275 | + */ |
| 276 | + @SuppressWarnings("unchecked") |
| 277 | + private static List<Proxy> proxySelector_select(Object proxySelector, URI uri) { |
| 278 | + try { |
| 279 | + Method method = proxySelector.getClass().getDeclaredMethod("select", URI.class); |
| 280 | + method.setAccessible(true); |
| 281 | + return (List<Proxy>) method.invoke(proxySelector, uri); |
| 282 | + } catch (Exception e) { |
| 283 | + return null; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + /** |
| 288 | + * <code> |
| 289 | + * JdkProxyProvider.getProxySelector() |
| 290 | + * </code> |
| 291 | + */ |
| 292 | + private static ProxySelector jdkProxyProvider_getProxySelector(Object jdkProxyProvider) { |
| 293 | + try { |
| 294 | + Method getInstanceMethod = jdkProxyProvider.getClass().getDeclaredMethod("getProxySelector"); |
| 295 | + return (ProxySelector) getInstanceMethod.invoke(jdkProxyProvider); |
| 296 | + } catch (Exception e) { |
| 297 | + return null; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * <code> |
| 303 | + * JdkProxyProvider.getAuthenticator() |
| 304 | + * </code> |
| 305 | + */ |
| 306 | + private static Authenticator jdkProxyProvider_getAuthenticator(Object jdkProxyProvider) { |
| 307 | + try { |
| 308 | + Method getAuthenticatorMethod = jdkProxyProvider.getClass().getDeclaredMethod("getAuthenticator"); |
| 309 | + return (Authenticator) getAuthenticatorMethod.invoke(jdkProxyProvider); |
| 310 | + } catch (Exception e) { |
| 311 | + return null; |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * <code> |
| 317 | + * JdkProxyProvider.getInstance() |
| 318 | + * </code> |
| 319 | + */ |
| 320 | + private static Object jdkProxyProvider_getInstance() { |
| 321 | + try { |
| 322 | + Class<?> clazz = Class.forName("com.intellij.util.net.JdkProxyProvider"); |
| 323 | + Method getInstanceMethod = clazz.getDeclaredMethod("getInstance"); |
| 324 | + return getInstanceMethod.invoke(null); |
| 325 | + } catch (Exception e) { |
| 326 | + return null; |
| 327 | + } |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + /** |
| 332 | + * <code> |
| 333 | + * Authenticator.getPasswordAuthentication() |
| 334 | + * </code> |
| 335 | + */ |
| 336 | + private static PasswordAuthentication authenticator_getPasswordAuthentication(Object authenticator) { |
| 337 | + try { |
| 338 | + Method method = authenticator.getClass().getDeclaredMethod("getPasswordAuthentication"); |
| 339 | + method.setAccessible(true); |
| 340 | + return (PasswordAuthentication) method.invoke(authenticator); |
| 341 | + } catch (Exception e) { |
| 342 | + return null; |
| 343 | + } |
| 344 | + } |
| 345 | +} |
0 commit comments