Search in sources :

Example 1 with DecodeException

use of javax.websocket.DecodeException in project tomcat by apache.

the class PojoMessageHandlerWholeBinary method decode.

@Override
protected Object decode(ByteBuffer message) throws DecodeException {
    for (Decoder decoder : decoders) {
        if (decoder instanceof Binary) {
            if (((Binary<?>) decoder).willDecode(message)) {
                return ((Binary<?>) decoder).decode(message);
            }
        } else {
            byte[] array = new byte[message.limit() - message.position()];
            message.get(array);
            ByteArrayInputStream bais = new ByteArrayInputStream(array);
            try {
                return ((BinaryStream<?>) decoder).decode(bais);
            } catch (IOException ioe) {
                throw new DecodeException(message, sm.getString("pojoMessageHandlerWhole.decodeIoFail"), ioe);
            }
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryStream(javax.websocket.Decoder.BinaryStream) Binary(javax.websocket.Decoder.Binary) IOException(java.io.IOException) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 2 with DecodeException

use of javax.websocket.DecodeException in project jetty.project by eclipse.

the class BinaryWholeMessage method messageComplete.

@SuppressWarnings("unchecked")
@Override
public void messageComplete() {
    super.finished = true;
    byte[] data = out.toByteArray();
    DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
    Decoder.Binary<Object> binaryDecoder = (Binary<Object>) decoder.getDecoder();
    try {
        Object obj = binaryDecoder.decode(BufferUtil.toBuffer(data));
        wholeHandler.onMessage(obj);
    } catch (DecodeException e) {
        throw new WebSocketException("Unable to decode binary data", e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) DecoderFactory(org.eclipse.jetty.websocket.jsr356.DecoderFactory) Binary(javax.websocket.Decoder.Binary) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 3 with DecodeException

use of javax.websocket.DecodeException in project jetty.project by eclipse.

the class BadDualDecoder method decode.

@Override
public Fruit decode(String s) throws DecodeException {
    Pattern pat = Pattern.compile("([^|]*)|([^|]*)");
    Matcher mat = pat.matcher(s);
    if (!mat.find()) {
        throw new DecodeException(s, "Unable to find Fruit reference encoded in text message");
    }
    Fruit fruit = new Fruit();
    fruit.name = mat.group(1);
    fruit.color = mat.group(2);
    return fruit;
}
Also used : Pattern(java.util.regex.Pattern) Fruit(org.eclipse.jetty.websocket.jsr356.samples.Fruit) Matcher(java.util.regex.Matcher) DecodeException(javax.websocket.DecodeException)

Example 4 with DecodeException

use of javax.websocket.DecodeException in project jetty.project by eclipse.

the class JsrCallable method init.

public void init(JsrSession session) {
    // Default for the session.
    // Session is an optional parameter (always)
    idxSession = findIndexForRole(Param.Role.SESSION);
    if (idxSession >= 0) {
        args[idxSession] = session;
    }
    // Optional EndpointConfig
    idxConfig = findIndexForRole(Param.Role.ENDPOINT_CONFIG);
    if (idxConfig >= 0) {
        args[idxConfig] = session.getEndpointConfig();
    }
    // Default for the path parameters
    // PathParam's are optional parameters (always)
    Map<String, String> pathParams = session.getPathParameters();
    if ((pathParams != null) && (pathParams.size() > 0)) {
        for (Param param : params) {
            if (param.role == Role.PATH_PARAM) {
                int idx = param.index;
                String rawvalue = pathParams.get(param.getPathParamName());
                Decoder decoder = session.getDecoderFactory().getDecoderFor(param.type);
                if (decoder instanceof Decoder.Text<?>) {
                    Decoder.Text<?> textDecoder = (Decoder.Text<?>) decoder;
                    try {
                        args[idx] = textDecoder.decode(rawvalue);
                    } catch (DecodeException e) {
                        session.notifyError(e);
                    }
                } else {
                    throw new InvalidWebSocketException("PathParam decoders must use Decoder.Text");
                }
            }
        }
    }
}
Also used : InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 5 with DecodeException

use of javax.websocket.DecodeException in project jetty.project by eclipse.

the class TextWholeMessage method messageComplete.

@SuppressWarnings("unchecked")
@Override
public void messageComplete() {
    finished = true;
    DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
    Decoder.Text<Object> textDecoder = (Decoder.Text<Object>) decoder.getDecoder();
    try {
        Object obj = textDecoder.decode(utf.toString());
        wholeHandler.onMessage(obj);
    } catch (DecodeException e) {
        throw new WebSocketException("Unable to decode text data", e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) DecoderFactory(org.eclipse.jetty.websocket.jsr356.DecoderFactory) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Aggregations

DecodeException (javax.websocket.DecodeException)7 Decoder (javax.websocket.Decoder)4 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Binary (javax.websocket.Decoder.Binary)2 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)2 DecoderFactory (org.eclipse.jetty.websocket.jsr356.DecoderFactory)2 Fruit (org.eclipse.jetty.websocket.jsr356.samples.Fruit)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 BinaryStream (javax.websocket.Decoder.BinaryStream)1 InvalidWebSocketException (org.eclipse.jetty.websocket.api.InvalidWebSocketException)1