de.cubeisland.engine.core.webapi.TextWebSocketFrameEncoder.java Source code

Java tutorial

Introduction

Here is the source code for de.cubeisland.engine.core.webapi.TextWebSocketFrameEncoder.java

Source

/**
 * This file is part of CubeEngine.
 * CubeEngine is licensed under the GNU General Public License Version 3.
 *
 * CubeEngine is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CubeEngine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CubeEngine.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.cubeisland.engine.core.webapi;

import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class TextWebSocketFrameEncoder extends MessageToMessageEncoder<Object> {
    private ObjectMapper objectMapper;

    public TextWebSocketFrameEncoder(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
        if (msg instanceof String) {
            ObjectNode node = objectMapper.createObjectNode();
            node.put("desc", (String) msg);
            out.add(new TextWebSocketFrame(node.toString()));
        } else if (msg instanceof JsonNode) {
            out.add(new TextWebSocketFrame(msg.toString()));
        } else {
            out.add(msg);
        }
    }
}