@Override public void onWebSocketConnect(WebSocketSessionContext context) { final ProvisioningConnection connection = new ProvisioningConnection(context.getClient()); final ProvisioningAddress provisioningAddress = ProvisioningAddress.generate(); pubSubManager.subscribe(provisioningAddress, connection); context.addListener(new WebSocketSessionContext.WebSocketEventListener() { @Override public void onWebSocketClose(WebSocketSessionContext context, int statusCode, String reason) { pubSubManager.unsubscribe(provisioningAddress, connection); } }); } }
@Timed @GET public Response getKeepAlive(@Auth Account account, @WebSocketSession WebSocketSessionContext context) { if (account != null) { WebsocketAddress address = new WebsocketAddress(account.getNumber(), account.getAuthenticatedDevice().get().getId()); if (!pubSubManager.hasLocalSubscription(address)) { logger.warn("***** No local subscription found for: " + address); context.getClient().close(1000, "OK"); } } return Response.ok().build(); }
@Override public void onWebSocketConnect(WebSocketSessionContext context) { if (context.getAuthenticated() != null) { final Account account = context.getAuthenticated(Account.class); final Device device = account.getAuthenticatedDevice().get(); final String connectionId = String.valueOf(new SecureRandom().nextLong()); final WebSocketConnection connection = new WebSocketConnection(pushSender, receiptSender, messagesManager, account, device, context.getClient(), connectionId); final PubSubMessage connectMessage = PubSubMessage.newBuilder().setType(PubSubMessage.Type.CONNECTED) .setContent(ByteString.copyFrom(connectionId.getBytes())) pubSubManager.subscribe(address, connection); context.addListener(new WebSocketSessionContext.WebSocketEventListener() { @Override public void onWebSocketClose(WebSocketSessionContext context, int statusCode, String reason) { } else { final Timer.Context timer = unauthenticatedDurationTimer.time(); context.addListener((context1, statusCode, reason) -> timer.stop());
@Override public Object provide() { Principal principal = getContainerRequest().getSecurityContext().getUserPrincipal(); if (principal != null && !(principal instanceof WebSocketServletRequest.ContextPrincipal)) { throw new IllegalArgumentException("Can't inject non-ContextPrincipal into request"); } if (principal == null) return Optional.empty(); else return Optional.ofNullable(((WebSocketServletRequest.ContextPrincipal)principal).getContext().getAuthenticated()); } }
@Override public void onWebSocketConnect(Session session) { this.session = session; this.remoteEndpoint = session.getRemote(); this.context = new WebSocketSessionContext(new WebSocketClient(session, remoteEndpoint, messageFactory, requestMap)); this.context.setAuthenticated(authenticated); this.session.setIdleTimeout(idleTimeoutMillis); if (connectListener.isPresent()) { connectListener.get().onWebSocketConnect(this.context); } }
public synchronized void notifyClosed(int statusCode, String reason) { for (WebSocketEventListener listener : closeListeners) { listener.onWebSocketClose(this, statusCode, reason); } closed = true; }
@Override public void onWebSocketClose(int statusCode, String reason) { if (context != null) { context.notifyClosed(statusCode, reason); for (long requestId : requestMap.keySet()) { SettableFuture outstandingRequest = requestMap.remove(requestId); if (outstandingRequest != null) { outstandingRequest.setException(new IOException("Connection closed!")); } } } }
public WebSocketSessionContext provide() { Principal principal = getContainerRequest().getSecurityContext().getUserPrincipal(); if (principal == null) { throw new IllegalStateException("Cannot inject a custom principal into unauthenticated request"); } if (!(principal instanceof WebSocketServletRequest.ContextPrincipal)) { throw new IllegalArgumentException("Cannot inject a non-WebSocket AuthPrincipal into request"); } return ((WebSocketServletRequest.ContextPrincipal)principal).getContext(); } };
public WebSocketResourceProviderFactory(WebSocketEnvironment environment) throws ServletException { this.environment = environment; environment.jersey().register(new WebSocketSessionContextValueFactoryProvider.Binder()); environment.jersey().register(new WebSocketAuthValueFactoryProvider.Binder()); environment.jersey().register(new JacksonMessageBodyProvider(environment.getObjectMapper())); }
@GET @Path("/prompt") public Response askMe(@Auth HelloAccount account, @WebSocketSession WebSocketSessionContext context) { ListenableFuture<WebSocketResponseMessage> response = context.getClient().sendRequest("GET", "/hello", new LinkedList<>(), Optional.empty()); Futures.addCallback(response, new FutureCallback<WebSocketResponseMessage>() { @Override public void onSuccess(WebSocketResponseMessage result) { logger.warn("Got response: " + new String(result.getBody().orElse(null))); } @Override public void onFailure(Throwable t) { logger.warn("Request error", t); } }); return Response.ok().build(); }
@Override public Object provide() { Principal principal = getContainerRequest().getSecurityContext().getUserPrincipal(); if (principal == null) { throw new IllegalStateException("Cannot inject a custom principal into unauthenticated request"); } if (!(principal instanceof WebSocketServletRequest.ContextPrincipal)) { throw new IllegalArgumentException("Cannot inject a non-WebSocket AuthPrincipal into request"); } Object authenticated = ((WebSocketServletRequest.ContextPrincipal)principal).getContext().getAuthenticated(); if (authenticated == null) { throw new WebApplicationException("Authenticated resource", 401); } if (!parameter.getRawType().isAssignableFrom(authenticated.getClass())) { throw new IllegalArgumentException("Authenticated principal is of the wrong type: " + authenticated.getClass() + " looking for: " + parameter.getRawType()); } return parameter.getRawType().cast(authenticated); } }
public synchronized void addListener(WebSocketEventListener listener) { if (!closed) this.closeListeners.add(listener); else listener.onWebSocketClose(this, 1000, "Closed"); }