javax.websocket.CloseReason.CloseCode Java Examples

The following examples show how to use javax.websocket.CloseReason.CloseCode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WsSession.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.sendMessageBlock(Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException | WritePendingException e) {
        // Failed to send close message. Close the socket and let the caller
        // deal with the Exception
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("wsSession.sendCloseFail", id), e);
        }
        wsRemoteEndpoint.close();
        // Failure to send a close message is not unexpected in the case of
        // an abnormal closure (usually triggered by a failure to read/write
        // from/to the client. In this case do not trigger the endpoint's
        // error handling
        if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
            localEndpoint.onError(this, e);
        }
    } finally {
        webSocketContainer.unregisterSession(getSessionMapKey(), this);
    }
}
 
Example #2
Source File: TesterWsClient.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void sendCloseFrame(CloseCode closeCode) throws IOException {
    int code = closeCode.getCode();
    byte[] codeBytes = new byte[2];
    codeBytes[0] = (byte) (code >> 8);
    codeBytes[1] = (byte) code;
    write(createFrame(true, 8, codeBytes));
}
 
Example #3
Source File: TestClose.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public static void awaitOnClose(CloseCode... codes) {
    Set<CloseCode> set = new HashSet<>();
    for (CloseCode code : codes) {
        set.add(code);
    }
    awaitOnClose(set);
}
 
Example #4
Source File: WsSession.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.startMessageBlock(
                Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException ioe) {
        // Failed to send close message. Close the socket and let the caller
        // deal with the Exception
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("wsSession.sendCloseFail", id), ioe);
        }
        wsRemoteEndpoint.close();
        // Failure to send a close message is not unexpected in the case of
        // an abnormal closure (usually triggered by a failure to read/write
        // from/to the client. In this case do not trigger the endpoint's
        // error handling
        if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
            localEndpoint.onError(this, ioe);
        }
    } finally {
        webSocketContainer.unregisterSession(localEndpoint, this);
    }
}
 
Example #5
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void sendCloseMessage(CloseReason closeReason) {
    // 125 is maximum size for the payload of a control message
    ByteBuffer msg = ByteBuffer.allocate(125);
    CloseCode closeCode = closeReason.getCloseCode();
    // CLOSED_ABNORMALLY should not be put on the wire
    if (closeCode == CloseCodes.CLOSED_ABNORMALLY) {
        // PROTOCOL_ERROR is probably better than GOING_AWAY here
        msg.putShort((short) CloseCodes.PROTOCOL_ERROR.getCode());
    } else {
        msg.putShort((short) closeCode.getCode());
    }

    String reason = closeReason.getReasonPhrase();
    if (reason != null && reason.length() > 0) {
        appendCloseReasonWithTruncation(msg, reason);
    }
    msg.flip();
    try {
        wsRemoteEndpoint.startMessageBlock(Constants.OPCODE_CLOSE, msg, true);
    } catch (IOException ioe) {
        handleCloseException(ioe, closeCode);
    } catch (WritePendingException wpe) {
        handleCloseException(wpe, closeCode);
    } finally {
        webSocketContainer.unregisterSession(localEndpoint, this);
    }
}
 
Example #6
Source File: WsSession.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private void handleCloseException(Exception e, CloseCode closeCode) {
    // Failed to send close message. Close the socket and let the caller
    // deal with the Exception
    if (log.isDebugEnabled()) {
        log.debug(sm.getString("wsSession.sendCloseFail", id), e);
    }
    wsRemoteEndpoint.close();
    // Failure to send a close message is not unexpected in the case of
    // an abnormal closure (usually triggered by a failure to read/write
    // from/to the client. In this case do not trigger the endpoint's
    // error handling
    if (closeCode != CloseCodes.CLOSED_ABNORMALLY) {
        localEndpoint.onError(this, e);
    }
}
 
Example #7
Source File: TesterWsCloseClient.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void sendCloseFrame(CloseCode closeCode) throws IOException {
    int code = closeCode.getCode();
    byte[] codeBytes = new byte[2];
    codeBytes[0] = (byte) (code >> 8);
    codeBytes[1] = (byte) code;
    write(createFrame(true, 8, codeBytes));
}
 
Example #8
Source File: TestClose.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public static void awaitOnClose(CloseCode... codes) {
    Set<CloseCode> set = new HashSet<CloseCode>();
    for (CloseCode code : codes) {
        set.add(code);
    }
    awaitOnClose(set);
}
 
Example #9
Source File: GuacamoleWebSocketTunnelEndpoint.java    From guacamole-client with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the numeric Guacaomle Status Code and Web Socket
 * code and closes the connection.
 *
 * @param session
 *     The outbound WebSocket connection to close.
 *
 * @param guacamoleStatusCode
 *     The numeric Guacamole status to send.
 *
 * @param webSocketCode
 *     The numeric WebSocket status to send.
 */
private void closeConnection(Session session, int guacamoleStatusCode,
        int webSocketCode) {

    try {
        CloseCode code = CloseReason.CloseCodes.getCloseCode(webSocketCode);
        String message = Integer.toString(guacamoleStatusCode);
        session.close(new CloseReason(code, message));
    }
    catch (IOException e) {
        logger.debug("Unable to close WebSocket connection.", e);
    }

}
 
Example #10
Source File: GuacamoleWebSocketTunnelEndpoint.java    From guacamole-client with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the numeric Guacaomle Status Code and Web Socket
 * code and closes the connection.
 *
 * @param session
 *     The outbound WebSocket connection to close.
 *
 * @param guacamoleStatusCode
 *     The numeric Guacamole status to send.
 *
 * @param webSocketCode
 *     The numeric WebSocket status to send.
 */
private void closeConnection(Session session, int guacamoleStatusCode,
        int webSocketCode) {

    try {
        CloseCode code = CloseReason.CloseCodes.getCloseCode(webSocketCode);
        String message = Integer.toString(guacamoleStatusCode);
        session.close(new CloseReason(code, message));
    }
    catch (IOException e) {
        logger.debug("Unable to close WebSocket connection.", e);
    }

}
 
Example #11
Source File: Util.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
static CloseCode getCloseCode(int code) {
    if (code > 2999 && code < 5000) {
        return CloseCodes.getCloseCode(code);
    }
    switch (code) {
        case 1000:
            return CloseCodes.NORMAL_CLOSURE;
        case 1001:
            return CloseCodes.GOING_AWAY;
        case 1002:
            return CloseCodes.PROTOCOL_ERROR;
        case 1003:
            return CloseCodes.CANNOT_ACCEPT;
        case 1004:
            // Should not be used in a close frame
            // return CloseCodes.RESERVED;
            return CloseCodes.PROTOCOL_ERROR;
        case 1005:
            // Should not be used in a close frame
            // return CloseCodes.NO_STATUS_CODE;
            return CloseCodes.PROTOCOL_ERROR;
        case 1006:
            // Should not be used in a close frame
            // return CloseCodes.CLOSED_ABNORMALLY;
            return CloseCodes.PROTOCOL_ERROR;
        case 1007:
            return CloseCodes.NOT_CONSISTENT;
        case 1008:
            return CloseCodes.VIOLATED_POLICY;
        case 1009:
            return CloseCodes.TOO_BIG;
        case 1010:
            return CloseCodes.NO_EXTENSION;
        case 1011:
            return CloseCodes.UNEXPECTED_CONDITION;
        case 1012:
            // Not in RFC6455
            // return CloseCodes.SERVICE_RESTART;
            return CloseCodes.PROTOCOL_ERROR;
        case 1013:
            // Not in RFC6455
            // return CloseCodes.TRY_AGAIN_LATER;
            return CloseCodes.PROTOCOL_ERROR;
        case 1015:
            // Should not be used in a close frame
            // return CloseCodes.TLS_HANDSHAKE_FAILURE;
            return CloseCodes.PROTOCOL_ERROR;
        default:
            return CloseCodes.PROTOCOL_ERROR;
    }
}
 
Example #12
Source File: TestClose.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public static void awaitOnClose(Set<CloseCode> codes) {
    awaitLatch(events.onCloseCalled, "onClose not called");
    CloseCode received = events.closeReason.getCloseCode();
    Assert.assertTrue("Rx: " + received, codes.contains(received));
}
 
Example #13
Source File: Util.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
static CloseCode getCloseCode(int code) {
    if (code > 2999 && code < 5000) {
        return CloseCodes.getCloseCode(code);
    }
    switch (code) {
        case 1000:
            return CloseCodes.NORMAL_CLOSURE;
        case 1001:
            return CloseCodes.GOING_AWAY;
        case 1002:
            return CloseCodes.PROTOCOL_ERROR;
        case 1003:
            return CloseCodes.CANNOT_ACCEPT;
        case 1004:
            // Should not be used in a close frame
            // return CloseCodes.RESERVED;
            return CloseCodes.PROTOCOL_ERROR;
        case 1005:
            // Should not be used in a close frame
            // return CloseCodes.NO_STATUS_CODE;
            return CloseCodes.PROTOCOL_ERROR;
        case 1006:
            // Should not be used in a close frame
            // return CloseCodes.CLOSED_ABNORMALLY;
            return CloseCodes.PROTOCOL_ERROR;
        case 1007:
            return CloseCodes.NOT_CONSISTENT;
        case 1008:
            return CloseCodes.VIOLATED_POLICY;
        case 1009:
            return CloseCodes.TOO_BIG;
        case 1010:
            return CloseCodes.NO_EXTENSION;
        case 1011:
            return CloseCodes.UNEXPECTED_CONDITION;
        case 1012:
            // Not in RFC6455
            // return CloseCodes.SERVICE_RESTART;
            return CloseCodes.PROTOCOL_ERROR;
        case 1013:
            // Not in RFC6455
            // return CloseCodes.TRY_AGAIN_LATER;
            return CloseCodes.PROTOCOL_ERROR;
        case 1015:
            // Should not be used in a close frame
            // return CloseCodes.TLS_HANDSHAKE_FAILURE;
            return CloseCodes.PROTOCOL_ERROR;
        default:
            return CloseCodes.PROTOCOL_ERROR;
    }
}
 
Example #14
Source File: Util.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
static CloseCode getCloseCode(int code) {
    if (code > 2999 && code < 5000) {
        return CloseCodes.getCloseCode(code);
    }
    switch (code) {
        case 1000:
            return CloseCodes.NORMAL_CLOSURE;
        case 1001:
            return CloseCodes.GOING_AWAY;
        case 1002:
            return CloseCodes.PROTOCOL_ERROR;
        case 1003:
            return CloseCodes.CANNOT_ACCEPT;
        case 1004:
            // Should not be used in a close frame
            // return CloseCodes.RESERVED;
            return CloseCodes.PROTOCOL_ERROR;
        case 1005:
            // Should not be used in a close frame
            // return CloseCodes.NO_STATUS_CODE;
            return CloseCodes.PROTOCOL_ERROR;
        case 1006:
            // Should not be used in a close frame
            // return CloseCodes.CLOSED_ABNORMALLY;
            return CloseCodes.PROTOCOL_ERROR;
        case 1007:
            return CloseCodes.NOT_CONSISTENT;
        case 1008:
            return CloseCodes.VIOLATED_POLICY;
        case 1009:
            return CloseCodes.TOO_BIG;
        case 1010:
            return CloseCodes.NO_EXTENSION;
        case 1011:
            return CloseCodes.UNEXPECTED_CONDITION;
        case 1012:
            // Not in RFC6455
            // return CloseCodes.SERVICE_RESTART;
            return CloseCodes.PROTOCOL_ERROR;
        case 1013:
            // Not in RFC6455
            // return CloseCodes.TRY_AGAIN_LATER;
            return CloseCodes.PROTOCOL_ERROR;
        case 1015:
            // Should not be used in a close frame
            // return CloseCodes.TLS_HANDSHAKE_FAILURE;
            return CloseCodes.PROTOCOL_ERROR;
        default:
            return CloseCodes.PROTOCOL_ERROR;
    }
}
 
Example #15
Source File: TestClose.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public static void awaitOnClose(Set<CloseCode> codes) {
    awaitLatch(events.onCloseCalled, "onClose not called");
    CloseCode received = events.closeReason.getCloseCode();
    Assert.assertTrue("Rx: " + received, codes.contains(received));
}