Search in sources :

Example 1 with JsonBuilderFactory

use of jakarta.json.JsonBuilderFactory in project openmq by eclipse-ee4j.

the class JSONWebSocket method doSend.

@Override
protected void doSend(StompFrameMessage frame) throws Exception {
    JsonBuilderFactory jsonfactory = Json.createBuilderFactory(null);
    JsonObjectBuilder obuilder = jsonfactory.createObjectBuilder();
    JsonObjectBuilder hbuilder = jsonfactory.createObjectBuilder();
    JsonObjectBuilder bbuilder = jsonfactory.createObjectBuilder();
    obuilder = obuilder.add(JsonMessage.Key.COMMAND, frame.getCommand().toString());
    Iterator<Map.Entry<String, String>> itr = frame.getHeaders().entrySet().iterator();
    Map.Entry<String, String> pair;
    String key, val;
    while (itr.hasNext()) {
        pair = itr.next();
        key = pair.getKey();
        val = pair.getValue();
        hbuilder.add(key, val);
    }
    obuilder.add(JsonMessage.Key.HEADERS, hbuilder.build());
    if (frame.getCommand().equals(StompFrameMessage.Command.MESSAGE)) {
        if (frame.isTextMessage()) {
            String body = frame.getBodyText();
            bbuilder.add(JsonMessage.BodySubKey.TYPE, JsonMessage.BODY_TYPE_TEXT);
            if (body != null) {
                bbuilder.add(JsonMessage.BodySubKey.TEXT, body);
            } else {
                bbuilder.add(JsonMessage.BodySubKey.TEXT, "");
            }
        } else {
            byte[] body = frame.getBody();
            bbuilder.add(JsonMessage.BodySubKey.TYPE, JsonMessage.BODY_TYPE_BYTES);
            bbuilder.add(JsonMessage.BodySubKey.ENCODER, JsonMessage.ENCODER_BASE64);
            String textbody = "";
            if (body != null) {
                if (base64Class == null) {
                    BASE64Encoder encoder = new BASE64Encoder();
                    textbody = encoder.encode(body);
                } else {
                    Method gm = base64Class.getMethod("getEncoder", (new Class[] {}));
                    Object o = gm.invoke(null);
                    Method em = o.getClass().getMethod("encodeToString", (new Class[] { byte[].class }));
                    textbody = (String) em.invoke(o, body);
                }
            }
            bbuilder.add(JsonMessage.BodySubKey.TEXT, textbody);
        }
    } else {
        byte[] body = frame.getBody();
        bbuilder.add(JsonMessage.BodySubKey.TYPE, JsonMessage.BODY_TYPE_TEXT);
        if (body != null) {
            bbuilder.add(JsonMessage.BodySubKey.TEXT, new String(body, "UTF-8"));
        } else {
            bbuilder.add(JsonMessage.BodySubKey.TEXT, "");
        }
    }
    JsonObject jo = obuilder.add(JsonMessage.Key.BODY, bbuilder.build()).build();
    send(jo.toString());
    if (DEBUG) {
        logger.log(logger.INFO, toString() + " SENT JsonObject[" + jo + "]");
    }
}
Also used : BASE64Encoder(com.sun.messaging.jmq.util.BASE64Encoder) JsonObject(jakarta.json.JsonObject) JsonString(jakarta.json.JsonString) Method(java.lang.reflect.Method) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) JsonObject(jakarta.json.JsonObject) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) Map(java.util.Map)

Aggregations

BASE64Encoder (com.sun.messaging.jmq.util.BASE64Encoder)1 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)1 JsonObject (jakarta.json.JsonObject)1 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)1 JsonString (jakarta.json.JsonString)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1