@OnMessage public WebsocketMessageWrapper doLogin(WebsocketMessageWrapper<Customer> message) { Customer customer = loginBean.findCustomer(message.getData().getEmail(), message.getData().getPassword()); if (customer == null) { throw WebSocketServerLogger.LOG.customerNotFoundException( message.getData().getEmail(), message.getSequenceId()); } ApiToken apiToken = loginBean.findValidToken(customer); if (apiToken == null) { apiToken = loginBean.createToken(customer); } else { apiToken = loginBean.refreshToken(apiToken); } // loginBean.refreshToken(customer.getEmail(), customer.getPassword(), message.getSequenceId()); WebsocketMessageWrapper<ApiToken> token = new WebsocketMessageWrapper<>(); token.setType(WebsocketMessageType.CUSTOMER_API_TOKEN); token.setSequenceId(message.getSequenceId()); token.setData(apiToken); return token; }
public ApiToken refreshToken(ApiToken apiToken) { Calendar expiration = Calendar.getInstance(); expiration.add(Calendar.HOUR, TOKEN_LIFETIME_HOURS); apiToken.setExpiration(expiration.getTime()); apiToken = entityManager.merge(apiToken); WebSocketServerLogger.LOG.refreshValidTokenDebug( apiToken.getToken(), apiToken.getExpiration().toString()); return apiToken; }
public ApiToken createToken(Customer customer) { Calendar expiration = Calendar.getInstance(); expiration.add(Calendar.HOUR, TOKEN_LIFETIME_HOURS); ApiToken apiToken = new ApiToken(); apiToken.setCustomer(customer); apiToken.setToken(UUID.randomUUID().toString()); apiToken.setExpiration(expiration.getTime()); entityManager.persist(apiToken); WebSocketServerLogger.LOG.newTokenCreatedDebug( apiToken.getToken(), apiToken.getExpiration().toString()); return apiToken; }
@OnError public void errorHandle(Session session, Throwable error) { if (session != null && session.isOpen()) { WebsocketMessageWrapper<Error> response = new WebsocketMessageWrapper<>(); response.setType(WebsocketMessageType.CUSTOMER_ERROR); response.setData(new Error()); if (error instanceof CustomerNotFoundException) { response.setSequenceId(((CustomerNotFoundException) error).getSequenceId()); response.getData().setCode("customer.notFound"); response.getData().setDescription("Customer not found"); } else { WebSocketServerLogger.LOG.handleWebsocketServerError(error); response.setSequenceId(UUID.randomUUID().toString()); response.getData().setCode(error.getClass().getName()); response.getData().setDescription("Handled exception"); } session.getAsyncRemote().sendObject(response); } }
@OnClose public void closeLoginEndPoint(Session session) { WebSocketServerLogger.LOG.closeWebsocketServerSessionDebug(session.getId()); }
@OnOpen public void openLoginEndPoint(Session session) { WebSocketServerLogger.LOG.openWebsocketServerSessionDebug(session.getId()); }