mmo.server.DataToHttpEncoder.java Source code

Java tutorial

Introduction

Here is the source code for mmo.server.DataToHttpEncoder.java

Source

/*
 * Copyright 2016 Florian Hassanen
 *
 * This file is part of mmo-server.
 *
 * mmo-server is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * mmo-server 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with mmo-server.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

package mmo.server;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import mmo.server.data.Data;

import javax.inject.Inject;
import java.util.List;

@ChannelHandler.Sharable
public class DataToHttpEncoder extends MessageToMessageEncoder<Data> {
    private final ObjectMapper mapper;

    @Inject
    public DataToHttpEncoder(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    protected void encode(ChannelHandlerContext ctx, Data msg, List<Object> out) throws Exception {
        ByteBuf buf = Unpooled.wrappedBuffer(mapper.writeValueAsBytes(msg));
        HttpResponse res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
        HttpHeaders.setHeader(res, HttpHeaders.Names.CONTENT_TYPE, "text/plain; encoding=utf-8");
        HttpHeaders.setContentLength(res, buf.readableBytes());

        out.add(res);
    }
}