获取对Java的默认http(s)URLStreamHandler的引用 [英] Getting a reference to Java's default http(s) URLStreamHandler

查看:168
本文介绍了获取对Java的默认http(s)URLStreamHandler的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我需要在我的一个项目中使用的库,不幸的是它注册了自己的 URLStreamHandler 来处理http - URL 。有没有办法获得对Java的默认http-和https - URLStreamHandlers 的引用,所以我可以在 URL中指定其中一个用于在不使用库覆盖的协议的情况下打开标准http连接的构造函数?

I have a library that I need to use in one of my projects, which unfortunately registers its own URLStreamHandler to handle http-URLs. Is there a way to get a reference to Java's default http- and https-URLStreamHandlers, so I can specify one of them in the URL's constructor to open a standard http connection without using the protocol overridden by the library?

推荐答案

找到它:

sun.net.www.protocol.http.Handler

有了这个,我现在可以做到:

With that, I can now do:

URL url = new URL(null, "http://...", new sun.net.www.protocol.http.Handler());
HttpURLConnection cxn = (HttpURLConnection) url.openConnection();

我得到一个普通的Java HttpURLConnection而不是库提供的那个。

And I get a normal Java HttpURLConnection and not the one provided by the library.

更新:

我发现了另一个,更多一般,方法:删除库的 URLStreamHandlerFactory

I discovered another, more general, approach: Remove the library's URLStreamHandlerFactory!

这有点棘手,因为URL类技术上没有不允许你设置工厂多次或者用官方函数调用来清除它,但是有一点反射魔法,我们无论如何都可以做到:

This is a bit tricky, since the URL-class technically doesn't allow you to set the factory more than once or to clear it with an official function call, but with a bit of reflection-magic, we can do it anyways:

public static String unsetURLStreamHandlerFactory() {
    try {
        Field f = URL.class.getDeclaredField("factory");
        f.setAccessible(true);
        Object curFac = f.get(null);
        f.set(null, null);
        URL.setURLStreamHandlerFactory(null);
        return curFac.getClass().getName();
    } catch (Exception e) {
        return null;
    }
}

此方法抓取静态字段 factory URL -class中,使其可访问,获取其当前值并将其更改为 null 。之后,它调用 URL.setStreamHandlerFactory( null (现在完成且没有错误)使此设置为正式,即让函数有机会进行其可能需要的任何其他清理去做。然后它返回先前注册的工厂的类名,仅供参考。如果出现任何问题,它会吞下异常(我知道,坏主意......)并返回 null

This method grabs a hold of the static field factory in the URL-class, makes it accessible, grabs its current value and changes it to null. Afterwards, it calls URL.setStreamHandlerFactory(null) (which now completes without error) to make this setting "official", i.e. give the function a chance to do any other clean-up that it might want to do. Then it returns the previously registered factory's class name, just for reference. If anything goes wrong, it swallows the exception (I know, bad idea...) and returns null.

供参考:这是 URL.java的相关源代码

注意:这种方法可能比使用内部太阳更危险类(就可移植性而言)因为它依赖于URL类的特定内部结构(即 factory -field的存在和确切函数),但它确实有一个优点,我不需要通过我的所有代码来查找所有的URL构造函数并添加handler-parameter ...此外,它可能会破坏依赖于其注册处理程序的库的某些功能。幸运的是,这两个问题(可移植性和部分损坏的库功能)都是与我的情况相关的问题。

Note: This approach might be even more risky than using the internal sun-classes (as far as portability goes) since it relies on the specific internal structure of the URL-class (namely the existence and exact function of the factory-field), but it does have the advantage that I don't need to go through all of my code to find all URL-constructors and add the handler-parameter... Also, it might break some functionality of the library that relies on their registered handlers. Luckily, neither issue (portability and partially broken library functionality) are issues that are relevant in my case.

更新:#2

我们正在使用反射:这是获得对默认处理程序的引用的最安全的方法:

While we're using reflection: Here is the probably safest way to get a reference to the default handler:

public static URLStreamHandler getURLStreamHandler(String protocol) {
    try {
        Method method = URL.class.getDeclaredMethod("getURLStreamHandler", String.class);
        method.setAccessible(true);
        return (URLStreamHandler) method.invoke(null, protocol);        
    } catch (Exception e) {
        return null;
    }
}

您可以简单地称之为:

URLStreamHandler hander = getURLStreamHandler("http");

注意:此通话需要才能库注册了 URLStreamHandlerFactory ,否则你最终会得到对其处理程序的引用。

Note: This call needs to happen before the library registers its URLStreamHandlerFactory, otherwise you will end up with a reference to their handler.

为什么我会认为这是最安全的方法吗?因为 URL.getURLStreamHandler(...)不是完全私有的方法,而只是package-private。因此,修改其调用签名可能会破坏同一包中的其他代码。此外,除了我们正在寻找的东西之外,它的名字并没有留下太多空间来归还任何东西。因此,我认为不可能(尽管仍然不是不可能) URL -class的不同/未来实现与此处的假设不兼容。

Why would I consider this the safest approach? Because URL.getURLStreamHandler(...) is not a fully private method, but only package-private. So, modifying its call-signature could break other code in the same package. Also, its name doesn't really leave much room for returning anything other than what we are looking for. Thus, I would expect it to be unlikely (albeit still not impossible) that a different/future implementation of the URL-class would be incompatible with the assumptions made here.

这篇关于获取对Java的默认http(s)URLStreamHandler的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆