jsr356-experts@websocket-spec.java.net

[jsr356-experts] Re: [jsr356-users] Tweaks to EndpointConfiguration and endpoint creation

From: Scott Ferguson <ferg_at_caucho.com>
Date: Wed, 28 Nov 2012 15:19:52 -0800

On 11/28/12 2:48 PM, Danny Coward wrote:
> Hi folks,
>
> For a few reasons that have come up over the last couple weeks: the
> change to one Endpoint instance per peer, Scott's suggestion that
> developers be able to customize the creation of (server-side) endpoint
> instances so they can share state (like an open database connection),
> and an older requirement to be able to customize the handshake
> HttpRequest and HttpResponse, I'm proposing some smallish changes to
> the Endpoint/EndpointConfig and deployment semantics.
>
> 1) Introduce a new type so developers can customize the creation of
> their endpoints if they need to:-
>
> public interface EndpointFactory {
> // return an instance of an endpoint (Endpoint or POJO) when the
> container
> // needs a new instance because a new peer is connecting
> Object createEndpoint();
> }
>
> 2) Reverse the ownership Endpoint <-> EndpointConfiguration because
> there should be only one config instance for all the endpoint
> instances, and work the endpoint factory into the API:-
> a) Remove getEndpointConfiguration() from Endpoint,
> b) Add ServerEndpointConfiguration -> public EndpointFactory
> getEndpointFactory(), default implementation returns a factory that
> uses the no arg constructor of the Endpoint class.
> c) Add an attribute Class<? extends EndpointFactory> factory() to
> @WebSocketEndpoint so that POJOs can declare an EndpointFactory.
>
> 3) Rework the server deployment mechanism to take an
> EndpointConfiguration instead of an Endpoint class (for programmatic
> endpoints).

I'll need to work through a refactor, but it sounds reasonable.

Do you want to parameterize EndpointFactory<T> so the server can
introspect the class at publish time, rather than instantiating and
introspecting on the fly?

>
> 4) Add the ability to intercept the client side of the opening
> handshake (e.g. to insert or read custom headers).
> a) Add public void beforeRequest(HandshakeRequest request) and public
> void afterResponse(HandshakeResponse response) to
> ClientEndpointConfiguration. The implementation calls these new
> methods before the handshake request is sent and after the handshake
> response is received but before the connection is opened,
> respectively. The developer may subclass ClientEndpointConfiguration
> in order to customize the default implementations which do nothing

Yes. This is an issue on the server side for the annotated endpoint. It
can't get the remote IP address, for example, from the session, which
might be desired for IP/DoS throttling or just for logging. (Unless
there's some way of getting that information that I'm missing here.)

-- Scott



>
> Let me know if you see any issues with these changes.
>
> Thanks,
>
> - Danny
> --
> <http://www.oracle.com> *Danny Coward *
> Java EE
> Oracle Corporation
>