WebSocket client API
Michael McMahon
michael.x.mcmahon at oracle.com
Tue Sep 8 10:26:33 UTC 2015
On 03/09/15 09:28, Andrej Golovnin wrote:
> Hi Michael,
>
>> Can you explain why you need j.n.Proxy rather than the
>> String/InetSocketAddress
>> combination proposed for HttpClient?
> String is not type safe. And allowing to define proxy only for the
> specific protocol is not enough from my experience. Sometimes you have
> to choose proxy based on the host of the URL you try to connect to.
> What HttpClient.Builder.proxy() should really take as parameter, is
> the j.n.ProxySelector. The most code, I have seen so far, does not
> really care about proxies. But everyone expects that it still works
> with proxies. j.n.URL.openConnection() gives us this magic because at
> some point in sun.net.www.protocol.http.HttpURLConnection the method
> ProxySelector.getDefault().select(URI) is called for the URL and the
> connection is established using the proxy. The same behaviour I expect
> from HttpClient and WebSocket. Therefore I think you have to deal with
> j.n.Proxy and j.n.ProxySelector in the HttpClient code anyway. And
> that's why I think you should use them in the Public API of HttpClient
> and WebSocket as well.
>
> Here is a small summary of the API I would like to see:
>
> /**
> * Defines a selector to be used by this client to select the proxy
> server for a request.
> * If no selector is defined for this client, the client will use
> {@link j.n.ProxySelector.getDefault()} as it's selector.
> *
> * @param selector the selector to be used by this client; may not be null.
> *
> * @throws IllegalArgumentException if selector is null.
> */
> j.n.HttpClient.Builder.proxySelector(j.n.ProxySelector selector)
>
> /**
> * Defines the proxy to be used by this request.
> * If no proxy is defined for this request, the selector from the
> HttpClient will be used to select the proxy.
> *
> * @param proxy the proxy to be used by this request; may not be null.
> *
> * @throws IllegalArgumentException if proxy is null.
> */
> j.n.HttpRequest.Builder.proxy(j.n.Proxy proxy)
>
> /**
> * Defines the proxy to be used by this WebSocket.
> * If no proxy is defined for this WebSocket, the default selector
> {@link j.n.ProxySelector.getDefault()} will be used to select the
> proxy.
> *
> * @param proxy the proxy to be used by this WebSocket; may not be null.
> *
> * @throws IllegalArgumentException if proxy is null.
> */
> j.n.WebSocket.Builder.proxy(j.n.Proxy proxy)
>
>
> As always fell free to use it or to ignore it. My code is already in
> production and works. :-)
>
> I hope this helps.
>
> Best regards,
> Andrej Golovnin
I was reluctant initially to use ProxySelector because of its complexity,
and the fact that SOCKS won't be supported,
but I think hooking into the default proxy selection mechanism is useful
(especially where system proxies are just expected to work)
I'll add this to the list of changes we'll consider after the initial
integration
but before JDK 9 is released.
Thanks,
Michael
More information about the net-dev
mailing list