Example usage for javax.websocket EncodeException EncodeException

List of usage examples for javax.websocket EncodeException EncodeException

Introduction

In this page you can find the example usage for javax.websocket EncodeException EncodeException.

Prototype

public EncodeException(Object object, String message, Throwable cause) 

Source Link

Usage

From source file:de.kaojo.chat.TextMessageEncoder.java

@Override
public String encode(Message object) throws EncodeException {
    ObjectMapper mapper = new ObjectMapper();
    try {//w  w  w.  j  a v a2 s.  c  om
        return mapper.writeValueAsString(object);
    } catch (JsonProcessingException ex) {
        Logger.getLogger(TextMessageEncoder.class.getName()).log(Level.SEVERE, null, ex);
        throw new EncodeException(object, "Could not encode Message", ex);
    }
}

From source file:org.webpda.server.war.ServerMessageEncoder.java

@Override
public String encode(IServerMessage object) throws EncodeException {
    try {//ww  w  .  j  ava 2s .  com
        return object.toJson();
    } catch (JsonProcessingException e) {
        throw new EncodeException(object, "Failed to encode " + object, e);
    }
}

From source file:hr.ws4is.websocket.WebsocketEncoder.java

@Override
public final String encode(final WebSocketResponse data) throws EncodeException {
    String response = null;//from   ww  w.jav a 2s. c om
    try {
        if (mapper != null) {
            response = mapper.writeValueAsString(data);
        }
    } catch (IOException exception) {
        LOGGER.error(exception.getMessage(), exception);
        throw new EncodeException(data, exception.getMessage(), exception);
    }
    if (response == null) {
        response = "";
    }
    return response;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeJsonQueryHandler.java

@Override
public String encode(final WebsockQuery query) throws EncodeException {
    String result = null;/*  w  w  w .ja va 2  s.  c  o m*/

    try {
        final JSONObject obj = JsonConverter.toJson(query);
        result = obj.toString();

        if (fDebug) {
            fTotalBytesOut += result.getBytes().length;
            fLogger.log(Level.FINEST, "encoded JSON message: " + result.getBytes().length + " bytes\n"
                    + "total bytes sent: " + fTotalBytesOut);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new EncodeException(query, "failed to encode JSON", e);
    }

    return result;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.logging.LoggingTSafeJsonQueryHandler.java

@Override
public String encode(final WebsockQuery query) throws EncodeException {
    String result = null;/*from  w w  w.j  ava2 s  . c  om*/

    try {
        long time = System.nanoTime();
        final JSONObject obj = JsonConverter.toJson(query);
        result = obj.toString();
        time = System.nanoTime() - time;

        if (fDebug) {
            fTotalBytesOut += result.getBytes().length;
            fLogger.log(Level.FINEST, "encoded JSON message: " + result.getBytes().length + " bytes\n"
                    + "total bytes sent: " + fTotalBytesOut);
        }

        //store query type
        final String type = query.getPayload().toString();
        synchronized (QUERY_TYPES) {
            QUERY_TYPES.put(query.getId(), type);
        }

        if (LOGGING_ENABLED) {
            //store size of query
            List<Integer> sizes = null;
            synchronized (LOGGED_OUT_SIZES) {
                sizes = LOGGED_OUT_SIZES.get(type);

                if (sizes == null) {
                    sizes = new LinkedList<Integer>();
                    LOGGED_OUT_SIZES.put(type, sizes);
                }
            }

            synchronized (sizes) {
                sizes.add(result.getBytes().length);
            }

            //store time taken
            List<Long> times = null;
            synchronized (LOGGED_OUT_TIMES) {
                times = LOGGED_OUT_TIMES.get(type);

                if (times == null) {
                    times = new LinkedList<Long>();
                    LOGGED_OUT_TIMES.put(type, times);
                }
            }

            synchronized (times) {
                times.add(time);
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new EncodeException(query, "failed to encode JSON", e);
    }

    return result;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java

@Override
public ByteBuffer encode(final WebsockQuery query) throws EncodeException {
    ByteBuffer result = null;//from ww w. j av  a2  s.co m

    try {
        final JSONObject obj = JsonConverter.toJson(query);
        byte[] data = obj.toString().getBytes();

        //compress
        final Deflater deflater = new Deflater(fCompression, true);
        deflater.setInput(data);

        int read = 0;
        int totalSize = 0;
        final List<byte[]> buffers = new LinkedList<byte[]>();

        final byte[] buffer = new byte[BUFFER_SIZE];
        read = deflater.deflate(buffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
        while (read > 0) {
            totalSize += read;
            buffers.add(Arrays.copyOf(buffer, read));
            read = deflater.deflate(buffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
        }

        result = fuse(buffers, totalSize);

        if (fDebug) {
            fTotalBytesOut += totalSize;
            fLogger.log(Level.FINEST, "encoded compressed JSON message: " + totalSize + " bytes\n"
                    + "total bytes sent: " + fTotalBytesOut);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new EncodeException(query, "failed to encode JSON", e);
    }

    return result;
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java

@Override
public ByteBuffer encode(final WebsockQuery query) throws EncodeException {
    ByteBuffer result = null;/*from  w ww  .  ja  va  2 s .c  om*/

    try {
        final JSONObject obj = JsonConverter.toJson(query);
        byte[] data = obj.toString().getBytes();

        //compress
        final Deflater deflater = new Deflater(fCompression, true);
        deflater.setInput(data);
        deflater.finish();

        int totalSize = 0;

        int read = deflater.deflate(fBuffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
        while (true) {
            totalSize += read;

            if (deflater.finished()) {
                //if finished, directly add buffer
                fBuffers.add(fBuffer);
                break;
            } else {
                //make a copy, reuse buffer
                fBuffers.add(Arrays.copyOf(fBuffer, read));
                read = deflater.deflate(fBuffer, 0, BUFFER_SIZE, Deflater.SYNC_FLUSH);
            }
        }

        result = fuse(totalSize);

        deflater.end();

        if (fDebug) {
            fTotalBytesOut += totalSize;
            fLogger.log(Level.FINEST, "encoded compressed JSON message: " + totalSize + " bytes\n"
                    + "total bytes sent: " + fTotalBytesOut);
        }
    } catch (JSONException e) {
        e.printStackTrace();
        throw new EncodeException(query, "failed to encode JSON", e);
    }

    return result;
}

From source file:ru.schernolyas.websockettest.testproject.ws.EnvelopeEncoder.java

@Override
public String encode(Envelope env) throws EncodeException {
    JSONObject obj = new JSONObject();
    obj.put("type", env.getType());
    obj.put("sequence_id", env.getSequenceId());
    JSONParser parser = new JSONParser();
    try {//from w w  w  . j av  a 2  s.c o  m
        JSONObject jsonData = (JSONObject) parser.parse(env.getData());
        obj.put("data", jsonData.toJSONString());
    } catch (ParseException pe) {
        throw new EncodeException(env, "Can not parse JSON", pe);
    }

    return obj.toJSONString();
}