List of usage examples for org.springframework.web.socket CloseStatus CloseStatus
public CloseStatus(int code, @Nullable String reason)
From source file:org.metis.push.PusherBean.java
@Override /**/*from w w w . j a v a2s . co m*/ * This method handles an incoming message from the web socket client. */ public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { if (session == null) { LOG.error(getBeanName() + ": null session"); throw new Exception(getBeanName() + ":handleTextMessage, null session was received"); } // the session should be in the registry WdsSocketSession wdsSession = getWdsSessions().get(session.getId()); if (wdsSession == null) { LOG.error(getBeanName() + ":handleTextMessage, session with this id is not in registry: " + session.getId()); session.close(new CloseStatus(SERVER_ERROR.getCode(), "ERROR, session with this id not in registry: " + session.getId())); return; } // some sort of message should have been received if (message == null) { LOG.error(getBeanName() + ":handleTextMessage, null message parameter"); session.close(new CloseStatus(POLICY_VIOLATION.getCode(), "ERROR, session with this id gave a null message " + "parameter: " + session.getId())); return; } // we're supposed to receive a JSON object String jsonMsg = message.getPayload(); if (jsonMsg == null) { LOG.error(getBeanName() + ":handleTextMessage, getPayload returns null or empty string"); session.close(new CloseStatus(POLICY_VIOLATION.getCode(), "ERROR, session with this id did not return a payload: " + session.getId())); return; } if (jsonMsg.isEmpty()) { LOG.error(getBeanName() + ":handleTextMessage, getPayload returns zero-length string"); session.close(new CloseStatus(POLICY_VIOLATION.getCode(), "ERROR, session with this id returns zero-length payload: " + session.getId())); return; } // dump the request if trace is on if (LOG.isTraceEnabled()) { LOG.trace(getBeanName() + ":***** processing new request *****"); LOG.trace(getBeanName() + ":session id = " + session.getId()); LOG.trace(getBeanName() + ":session remote address = " + session.getRemoteAddress().toString()); LOG.trace(getBeanName() + ":session uri = " + session.getUri().toString()); LOG.trace(getBeanName() + ":session json object = " + jsonMsg); } // parse the json object List<Map<String, String>> jParams = null; try { jParams = Utils.parseJson(jsonMsg); } catch (Exception exc) { LOG.error(getBeanName() + ":caught this " + "exception while parsing json object: " + exc.toString()); LOG.error(getBeanName() + ": exception stack trace follows:"); dumpStackTrace(exc.getStackTrace()); if (exc.getCause() != null) { LOG.error(getBeanName() + ": Caused by " + exc.getCause().toString()); LOG.error(getBeanName() + ": causing exception stack trace follows:"); dumpStackTrace(exc.getCause().getStackTrace()); } session.close(new CloseStatus(SERVER_ERROR.getCode(), "ERROR, got this json parsing exception: " + exc.getMessage())); return; } if (jParams == null || jParams.isEmpty()) { LOG.error(getBeanName() + ":json parser returns null or " + "empty json array"); session.close( new CloseStatus(SERVER_ERROR.getCode(), "ERROR, json parser returns null or empty json array")); return; } // if trace is on, dump the params (if any) to the log if (LOG.isDebugEnabled()) { LOG.debug( getBeanName() + ": handleRequestInternal, received these params: " + jParams.get(0).toString()); } // get the command portion of the json message Map<String, String> map = jParams.get(0); String command = map.remove(WS_COMMAND); if (command == null) { LOG.error(getBeanName() + ":command field not present"); session.close(POLICY_VIOLATION); session.close(new CloseStatus(POLICY_VIOLATION.getCode(), "ERROR, command string not present or improperly set: " + command)); return; } if (!command.equals(WS_SUBSCRIBE) && !command.equals(WS_PING)) { LOG.error(getBeanName() + ":received this unknown command = " + command); session.close(POLICY_VIOLATION); session.close(new CloseStatus(POLICY_VIOLATION.getCode(), "ERROR, received this unknown command = " + command)); return; } // Get the SQL Job, if any, that this session is currently subscribed to SqlJob job = wdsSession.getMyJob(); // if this is a ping command, return session's current subscription if (command.equals(WS_PING)) { LOG.debug(getBeanName() + ":received ping command"); List<Map<String, Object>> response = new ArrayList<Map<String, Object>>(); Map<String, Object> map0 = new HashMap<String, Object>(); if (job != null) { LOG.debug(getBeanName() + ": client is subscribed"); map0.put(WS_STATUS, WS_SUBSCRIBED); map = job.getParams(); if (map != null && !map.isEmpty()) { for (String key : map.keySet()) { map0.put(key, map.get(key)); } } } else { LOG.debug(getBeanName() + ": client is not subscribed"); map0.put(WS_STATUS, WS_OK); } response.add(map0); // send response back to client session.sendMessage(new TextMessage(Utils.generateJson(response))); return; } // find a sql statement that matches the incoming session request's // params SqlStmnt sqlStmnt = (map == null || map.isEmpty()) ? SqlStmnt.getMatch(getSqlStmnts4Get(), null) : SqlStmnt.getMatch(getSqlStmnts4Get(), map.keySet()); // if getMatch could not find a match, then return error to client if (sqlStmnt == null) { LOG.error(getBeanName() + ":ERROR, unable to find sql " + "statement with this map: " + map.toString()); List<Map<String, Object>> response = new ArrayList<Map<String, Object>>(); Map<String, Object> map0 = new HashMap<String, Object>(); if (map != null && !map.isEmpty()) { for (String key : map.keySet()) { map0.put(key, map.get(key)); } } map0.put(WS_STATUS, WS_NOT_FOUND); // send response back to client session.sendMessage(new TextMessage(Utils.generateJson(response))); return; } // other than a ping, the only other command from the client is a // subscription command // Does this session already exist in one of the sql jobs? Note that the // client can switch subscriptions. if (job != null) { // the session pertains to a job, but does that job's map match // that of this session's subscription request if (job.isParamMatch(map)) { // if so, we're done return; } else { // else remove this session from that job - the client is // switching subscriptions job.removeSession(wdsSession.getId()); } } mainLock.lock(); try { // if we've gotten this far, the session does not pertain to a job // or it is a subscription change. so we now need to find an // existing job whose params match that of the incoming session. if // no job was found, then create and start one if (sqlStmnt.findSqlJob(map, wdsSession) == null) { sqlStmnt.createSqlJob(map, wdsSession); } } finally { mainLock.unlock(); } }
From source file:org.kurento.jsonrpc.internal.ws.WebSocketServerSession.java
@Override public void closeNativeSession(String reason) { try {//from w w w. ja v a 2s .co m wsSession.close(new CloseStatus(CloseStatus.NORMAL.getCode(), reason)); } catch (IOException e) { log.warn("Exception closing webSocket session", e); } }
From source file:org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter.java
@OnWebSocketClose public void onWebSocketClose(int statusCode, String reason) { CloseStatus closeStatus = new CloseStatus(statusCode, reason); try {/*from w w w.ja va2s .co m*/ this.webSocketHandler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled exception after connection closed for " + this, ex); } } }
From source file:org.springframework.web.socket.adapter.JettyWebSocketHandlerAdapter.java
@Override public void onWebSocketClose(int statusCode, String reason) { CloseStatus closeStatus = new CloseStatus(statusCode, reason); try {/* w ww. java 2 s . c o m*/ this.webSocketHandler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable t) { logger.error("Unhandled error for " + this.wsSession, t); } }
From source file:org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.java
@Override public void onClose(javax.websocket.Session session, CloseReason reason) { CloseStatus closeStatus = new CloseStatus(reason.getCloseCode().getCode(), reason.getReasonPhrase()); try {/* w w w .java 2 s. c om*/ this.handler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Unhandled on-close exception for " + this.wsSession, ex); } } }
From source file:org.springframework.web.socket.adapter.StandardEndpointAdapter.java
@Override public void onClose(javax.websocket.Session session, CloseReason reason) { CloseStatus closeStatus = new CloseStatus(reason.getCloseCode().getCode(), reason.getReasonPhrase()); try {//from w ww. jav a2 s. co m this.handler.afterConnectionClosed(this.wsSession, closeStatus); } catch (Throwable t) { logger.error("Unhandled error for " + this.wsSession, t); } }
From source file:org.springframework.web.socket.sockjs.AbstractSockJsSession.java
/** * {@inheritDoc}/* www. j a v a 2 s .c o m*/ * <p>Performs cleanup and notifies the {@link SockJsHandler}. */ @Override public final void close() throws IOException { close(new CloseStatus(3000, "Go away!")); }
From source file:org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.java
/** * Return a timeout cleanup task to invoke if the SockJS sessions is not * fully established within the retransmission timeout period calculated in * {@code SockJsRequest} based on the duration of the initial SockJS "Info" * request.//w w w .j a va2s.c o m */ Runnable getTimeoutTask() { return new Runnable() { @Override public void run() { try { closeInternal(new CloseStatus(2007, "Transport timed out")); } catch (Throwable ex) { if (logger.isWarnEnabled()) { logger.warn("Failed to close " + this + " after transport timeout", ex); } } } }; }
From source file:org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.java
private void handleOpenFrame() { if (logger.isDebugEnabled()) { logger.debug("Processing SockJS open frame in " + this); }/* ww w . j a v a2s . c o m*/ if (this.state == State.NEW) { this.state = State.OPEN; try { this.webSocketHandler.afterConnectionEstablished(this); this.connectFuture.set(this); } catch (Throwable ex) { if (logger.isErrorEnabled()) { logger.error("WebSocketHandler.afterConnectionEstablished threw exception in " + this, ex); } } } else { if (logger.isDebugEnabled()) { logger.debug("Open frame received in " + getId() + " but we're not connecting (current state " + this.state + "). The server might have been restarted and lost track of the session."); } silentClose(new CloseStatus(1006, "Server lost session")); } }
From source file:org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.java
private void handleCloseFrame(SockJsFrame frame) { CloseStatus closeStatus = CloseStatus.NO_STATUS_CODE; try {/*from w w w . j a v a 2 s . co m*/ String frameData = frame.getFrameData(); if (frameData != null) { String[] data = getMessageCodec().decode(frameData); if (data != null && data.length == 2) { closeStatus = new CloseStatus(Integer.valueOf(data[0]), data[1]); } if (logger.isDebugEnabled()) { logger.debug("Processing SockJS close frame with " + closeStatus + " in " + this); } } } catch (IOException ex) { if (logger.isErrorEnabled()) { logger.error("Failed to decode data for " + frame + " in " + this, ex); } } silentClose(closeStatus); }