com.chiorichan.http.HttpInitializer.java Source code

Java tutorial

Introduction

Here is the source code for com.chiorichan.http.HttpInitializer.java

Source

/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 2016 Chiori Greene a.k.a. Chiori-chan <me@chiorichan.com>
 * All Right Reserved.
 */
package com.chiorichan.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

import java.lang.ref.WeakReference;
import java.util.List;

import com.google.common.collect.Lists;

public class HttpInitializer extends ChannelInitializer<SocketChannel> {
    public static final List<WeakReference<SocketChannel>> activeChannels = Lists.newCopyOnWriteArrayList();

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();

        p.addLast("decoder", new HttpRequestDecoder());
        p.addLast("aggregator", new HttpObjectAggregator(104857600)); // One Hundred Megabytes
        p.addLast("encoder", new HttpResponseEncoder());
        p.addLast("deflater", new HttpContentCompressor());
        p.addLast("handler", new HttpHandler(false));

        activeChannels.add(new WeakReference<SocketChannel>(ch));
    }
}