List of usage examples for javax.websocket Session getPathParameters
Map<String, String> getPathParameters();
From source file:io.apicurio.hub.editing.EditApiDesignEndpoint.java
@OnClose public void onCloseSession(Session session, CloseReason reason) { String designId = session.getPathParameters().get("designId"); logger.debug("Closing a WebSocket due to: {}", reason.getReasonPhrase()); logger.debug("\tdesignId: {}", designId); // Call 'leave' on the concurrent editing session for this user ApiDesignEditingSession editingSession = editingSessionManager.getEditingSession(designId); String userId = editingSession.getUser(session); editingSession.leave(session);/*from ww w .j a v a 2 s . c om*/ if (editingSession.isEmpty()) { // TODO race condition - the session may no longer be empty here! editingSessionManager.closeEditingSession(editingSession); try { rollupCommands(userId, designId); } catch (NotFoundException | StorageException | OaiCommandException e) { logger.error("Failed to rollup commands for API with id: " + designId, "Rollup error: ", e); } } else { editingSession.sendLeaveToOthers(session, userId); } }
From source file:io.apicurio.hub.editing.EditApiDesignEndpoint.java
/** * Called when a web socket connection is made. The format for the web socket URL endpoint is: * //from w ww .j ava2 s .com * /designs/{designId}?uuid={uuid}&user={user}&secret={secret} * * The uuid, user, and secret query parameters must be present for a connection to be * successfully made. * * @param session */ @OnOpen public void onOpenSession(Session session) { String designId = session.getPathParameters().get("designId"); logger.debug("WebSocket opened: {}", session.getId()); logger.debug("\tdesignId: {}", designId); String queryString = session.getQueryString(); Map<String, String> queryParams = parseQueryString(queryString); String uuid = queryParams.get("uuid"); String userId = queryParams.get("user"); String secret = queryParams.get("secret"); this.metrics.socketConnected(designId, userId); logger.debug("\tuuid: {}", uuid); logger.debug("\tuser: {}", userId); ApiDesignEditingSession editingSession = null; try { long contentVersion = editingSessionManager.validateSessionUuid(uuid, designId, userId, secret); // Join the editing session (or create a new one) for the API Design editingSession = this.editingSessionManager.getOrCreateEditingSession(designId); Set<Session> otherSessions = editingSession.getSessions(); if (editingSession.isEmpty()) { this.metrics.editingSessionCreated(designId); } editingSession.join(session, userId); // Send "join" messages for each user already in the session for (Session otherSession : otherSessions) { String otherUser = editingSession.getUser(otherSession); editingSession.sendJoinTo(session, otherUser, otherSession.getId()); } // Send any commands that have been created since the user asked to join the editing session. List<ApiDesignCommand> commands = this.storage.listContentCommands(userId, designId, contentVersion); for (ApiDesignCommand command : commands) { String cmdData = command.getCommand(); StringBuilder builder = new StringBuilder(); builder.append("{"); builder.append("\"contentVersion\": "); builder.append(command.getContentVersion()); builder.append(", "); builder.append("\"type\": \"command\", "); builder.append("\"command\": "); builder.append(cmdData); builder.append("}"); logger.debug("Sending command to client (onOpenSession): {}", builder.toString()); session.getBasicRemote().sendText(builder.toString()); } editingSession.sendJoinToOthers(session, userId); } catch (ServerError | StorageException | IOException e) { if (editingSession != null) { editingSession.leave(session); } logger.error("Error validating editing session UUID for API Design ID: " + designId, e); try { session.close(new CloseReason(CloseCodes.CANNOT_ACCEPT, "Error opening editing session: " + e.getMessage())); } catch (IOException e1) { logger.error( "Error closing web socket session (attempted to close due to error validating editing session UUID).", e1); } } }
From source file:io.apicurio.hub.editing.EditApiDesignEndpoint.java
/** * Called when a message is received on a web socket connection. All messages must * be of the following (JSON) format://from w w w .j a va 2s .c o m * * <pre> * { * "type": "command|...", * "command": { * <marshalled OAI command goes here> * } * } * </pre> * * @param session * @param message */ @OnMessage public void onMessage(Session session, JsonNode message) { String designId = session.getPathParameters().get("designId"); ApiDesignEditingSession editingSession = editingSessionManager.getEditingSession(designId); String msgType = message.get("type").asText(); logger.debug("Received a \"{}\" message from a client.", msgType); logger.debug("\tdesignId: {}", designId); if (msgType.equals("command")) { String user = editingSession.getUser(session); long localCommandId = -1; if (message.has("commandId")) { localCommandId = message.get("commandId").asLong(); } String content; long cmdContentVersion; this.metrics.contentCommand(designId); logger.debug("\tuser:" + user); try { content = mapper.writeValueAsString(message.get("command")); } catch (JsonProcessingException e) { logger.error("Error writing command as string.", e); // TODO do something sensible here - send a msg to the client? return; } try { cmdContentVersion = storage.addContent(user, designId, ApiContentType.Command, content); } catch (StorageException e) { logger.error("Error storing the command.", e); // TODO do something sensible here - send a msg to the client? return; } // Send an ack message back to the user ApiDesignCommandAck ack = new ApiDesignCommandAck(); ack.setCommandId(localCommandId); ack.setContentVersion(cmdContentVersion); editingSession.sendAckTo(session, ack); logger.debug("ACK sent back to client."); // Now propagate the command to all other clients ApiDesignCommand command = new ApiDesignCommand(); command.setCommand(content); command.setContentVersion(cmdContentVersion); editingSession.sendCommandToOthers(session, user, command); logger.debug("Command propagated to 'other' clients."); return; } else if (msgType.equals("selection")) { String user = editingSession.getUser(session); String selection = null; if (message.has("selection")) { JsonNode node = message.get("selection"); if (node != null) { selection = node.asText(); } } logger.debug("\tuser:" + user); logger.debug("\tselection:" + selection); editingSession.sendUserSelectionToOthers(session, user, selection); logger.debug("User selection propagated to 'other' clients."); return; } else if (msgType.equals("ping")) { logger.debug("PING message received."); return; } logger.error("Unknown message type: {}", msgType); // TODO something went wrong if we got here - report an error of some kind }