storing fields in Java ee websocket

123 views Asked by At

i have created a javaee websocket endpoint using a PathParam. The parameter and the session are each stored in a field @OnOpen.

The websocket listens to some event and send some data out to session (using the path parameter).

The problem is that both fields are null at the moment event is caught. What is the cause of this? Are there multiple instances of endpoint per session? I read the spec : "If the developer uses the default ServerEndpointConfig.Configurator, there will be precisely one endpoint instance per active client connection. " Here is example code:

@ServerEndpoint(value = "/allbookings/{projectId}", encoders = {JSONEncoder.class},
                configurator = WebSocketUserConfig.class)
public class BookingsByProject {

    private Session session;
    private String  projectId;

    @OnOpen
    public void open(Session session, EndpointConfig conf, @PathParam(PROJECT_ID_KEY) String projectId) {
        if(projectId!=null){
            this.session = session;
            this.projectId = projectId;
        } else {
            session.close();
        }
    }

    @OnClose
    public void close(Session session, CloseReason reason) {
        log.debug("Session closed.");
    }

    public void createdEventFired(@Observes @Created Booking entity) {
        if(session.isOpen){
            session.getAsyncRemote().sendObject(converter.from(null, repo.getBookingsByProjectId(projectId)));
        }
    }

}

The problem is that on createdEventFired session and projectId are always null! Why?

For sake of completeness here the configurator (only addin a user property):

public class WebSocketUserConfig extends ServerEndpointConfig.Configurator {
    /**
     * Key used in config to mark if requesting user is admin.
     */
    public static final String IS_ADMIN_ADMIN = "isAdmin";

    @Override
    public void modifyHandshake(ServerEndpointConfig config, HandshakeRequest request, HandshakeResponse response) {
        config.getUserProperties().put(IS_ADMIN_ADMIN, request.isUserInRole(UserRole.ADMIN.name()));
        super.modifyHandshake(config, request, response);
    }

}
0

There are 0 answers