Search in sources :

Example 6 with InvalidDataException

use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.

the class WebSocketImpl method decodeFrames.

private void decodeFrames(ByteBuffer socketBuffer) {
    List<Framedata> frames;
    try {
        frames = draft.translateFrame(socketBuffer);
        for (Framedata f : frames) {
            if (DEBUG) {
                System.out.println("matched frame: " + f);
            }
            Opcode curop = f.getOpcode();
            boolean fin = f.isFin();
            if (curop == Opcode.CLOSING) {
                int code = CloseFrame.NOCODE;
                String reason = "";
                if (f instanceof CloseFrame) {
                    CloseFrame cf = (CloseFrame) f;
                    code = cf.getCloseCode();
                    reason = cf.getMessage();
                }
                if (readystate == READYSTATE.CLOSING) {
                    // complete the close handshake by disconnecting
                    closeConnection(code, reason, true);
                } else // echo close handshake
                if (draft.getCloseHandshakeType() == CloseHandshakeType.TWOWAY) {
                    close(code, reason, true);
                } else {
                    flushAndClose(code, reason, false);
                }
                continue;
            } else if (curop == Opcode.PING) {
                wsl.onWebsocketPing(this, f);
                continue;
            } else if (curop == Opcode.PONG) {
                wsl.onWebsocketPong(this, f);
                continue;
            } else if (!fin || curop == Opcode.CONTINUOUS) {
                if (curop != Opcode.CONTINUOUS) {
                    if (current_continuous_frame_opcode != null) {
                        throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Previous continuous frame sequence not completed.");
                    }
                    current_continuous_frame_opcode = curop;
                } else if (fin) {
                    if (current_continuous_frame_opcode == null) {
                        throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence was not started.");
                    }
                    current_continuous_frame_opcode = null;
                } else if (current_continuous_frame_opcode == null) {
                    throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence was not started.");
                }
                try {
                    wsl.onWebsocketMessageFragment(this, f);
                } catch (RuntimeException e) {
                    wsl.onWebsocketError(this, e);
                }
            } else if (current_continuous_frame_opcode != null) {
                throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "Continuous frame sequence not completed.");
            } else if (curop == Opcode.TEXT) {
                try {
                    wsl.onWebsocketMessage(this, Charsetfunctions.stringUtf8(f.getPayloadData()));
                } catch (RuntimeException e) {
                    wsl.onWebsocketError(this, e);
                }
            } else if (curop == Opcode.BINARY) {
                try {
                    wsl.onWebsocketMessage(this, f.getPayloadData());
                } catch (RuntimeException e) {
                    wsl.onWebsocketError(this, e);
                }
            } else {
                throw new InvalidDataException(CloseFrame.PROTOCOL_ERROR, "non control or continious frame expected");
            }
        }
    } catch (InvalidDataException e1) {
        wsl.onWebsocketError(this, e1);
        close(e1);
        return;
    }
}
Also used : Framedata(org.java_websocket.framing.Framedata) InvalidDataException(org.java_websocket.exceptions.InvalidDataException) Opcode(org.java_websocket.framing.Framedata.Opcode) CloseFrame(org.java_websocket.framing.CloseFrame)

Example 7 with InvalidDataException

use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.

the class Draft_10 method createFrames.

@Override
public List<Framedata> createFrames(ByteBuffer binary, boolean mask) {
    FrameBuilder curframe = new FramedataImpl1();
    try {
        curframe.setPayload(binary);
    } catch (InvalidDataException e) {
        throw new NotSendableException(e);
    }
    curframe.setFin(true);
    curframe.setOptcode(Opcode.BINARY);
    curframe.setTransferemasked(mask);
    return Collections.singletonList((Framedata) curframe);
}
Also used : InvalidDataException(org.java_websocket.exceptions.InvalidDataException) CloseFrameBuilder(org.java_websocket.framing.CloseFrameBuilder) FrameBuilder(org.java_websocket.framing.FrameBuilder) NotSendableException(org.java_websocket.exceptions.NotSendableException) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Example 8 with InvalidDataException

use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.

the class Draft_75 method createFrames.

@Override
public List<Framedata> createFrames(String text, boolean mask) {
    FrameBuilder frame = new FramedataImpl1();
    try {
        frame.setPayload(ByteBuffer.wrap(Charsetfunctions.utf8Bytes(text)));
    } catch (InvalidDataException e) {
        throw new NotSendableException(e);
    }
    frame.setFin(true);
    frame.setOptcode(Opcode.TEXT);
    frame.setTransferemasked(mask);
    return Collections.singletonList((Framedata) frame);
}
Also used : InvalidDataException(org.java_websocket.exceptions.InvalidDataException) FrameBuilder(org.java_websocket.framing.FrameBuilder) NotSendableException(org.java_websocket.exceptions.NotSendableException) FramedataImpl1(org.java_websocket.framing.FramedataImpl1)

Example 9 with InvalidDataException

use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.

the class CloseFrameBuilder method setCodeAndMessage.

private void setCodeAndMessage(int code, String m) throws InvalidDataException {
    if (m == null) {
        m = "";
    }
    // CloseFrame.TLS_ERROR is not allowed to be transfered over the wire
    if (code == CloseFrame.TLS_ERROR) {
        code = CloseFrame.NOCODE;
        m = "";
    }
    if (code == CloseFrame.NOCODE) {
        if (0 < m.length()) {
            throw new InvalidDataException(PROTOCOL_ERROR, "A close frame must have a closecode if it has a reason");
        }
        // empty payload
        return;
    }
    byte[] by = Charsetfunctions.utf8Bytes(m);
    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.putInt(code);
    buf.position(2);
    ByteBuffer pay = ByteBuffer.allocate(2 + by.length);
    pay.put(buf);
    pay.put(by);
    pay.rewind();
    setPayload(pay);
}
Also used : InvalidDataException(org.java_websocket.exceptions.InvalidDataException) ByteBuffer(java.nio.ByteBuffer)

Example 10 with InvalidDataException

use of org.java_websocket.exceptions.InvalidDataException in project quorrabot by GloriousEggroll.

the class Charsetfunctions method stringUtf8.

/*public static String stringUtf8( byte[] bytes, int off, int length ) throws InvalidDataException {
		CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
		decode.onMalformedInput( codingErrorAction );
		decode.onUnmappableCharacter( codingErrorAction );
		//decode.replaceWith( "X" );
		String s;
		try {
			s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
		} catch ( CharacterCodingException e ) {
			throw new InvalidDataException( CloseFrame.NO_UTF8, e );
		}
		return s;
	}*/
public static String stringUtf8(ByteBuffer bytes) throws InvalidDataException {
    CharsetDecoder decode = Charset.forName("UTF8").newDecoder();
    decode.onMalformedInput(codingErrorAction);
    decode.onUnmappableCharacter(codingErrorAction);
    // decode.replaceWith( "X" );
    String s;
    try {
        bytes.mark();
        s = decode.decode(bytes).toString();
        bytes.reset();
    } catch (CharacterCodingException e) {
        throw new InvalidDataException(CloseFrame.NO_UTF8, e);
    }
    return s;
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) InvalidDataException(org.java_websocket.exceptions.InvalidDataException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Aggregations

InvalidDataException (org.java_websocket.exceptions.InvalidDataException)10 FrameBuilder (org.java_websocket.framing.FrameBuilder)4 FramedataImpl1 (org.java_websocket.framing.FramedataImpl1)4 NotSendableException (org.java_websocket.exceptions.NotSendableException)3 CloseFrameBuilder (org.java_websocket.framing.CloseFrameBuilder)3 ByteBuffer (java.nio.ByteBuffer)2 InvalidHandshakeException (org.java_websocket.exceptions.InvalidHandshakeException)2 CharacterCodingException (java.nio.charset.CharacterCodingException)1 CharsetDecoder (java.nio.charset.CharsetDecoder)1 Draft (org.java_websocket.drafts.Draft)1 HandshakeState (org.java_websocket.drafts.Draft.HandshakeState)1 IncompleteHandshakeException (org.java_websocket.exceptions.IncompleteHandshakeException)1 CloseFrame (org.java_websocket.framing.CloseFrame)1 Framedata (org.java_websocket.framing.Framedata)1 Opcode (org.java_websocket.framing.Framedata.Opcode)1 ClientHandshake (org.java_websocket.handshake.ClientHandshake)1 Handshakedata (org.java_websocket.handshake.Handshakedata)1 ServerHandshake (org.java_websocket.handshake.ServerHandshake)1 ServerHandshakeBuilder (org.java_websocket.handshake.ServerHandshakeBuilder)1