001/**
002 * Copyright (C) 2006-2019 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.junit.http.internal.impl;
017
018import org.talend.sdk.component.junit.http.api.HttpApiHandler;
019
020import io.netty.channel.ChannelInboundHandlerAdapter;
021import io.netty.channel.ChannelInitializer;
022import io.netty.channel.ChannelPipeline;
023import io.netty.channel.socket.SocketChannel;
024import io.netty.handler.codec.http.HttpContentCompressor;
025import io.netty.handler.codec.http.HttpContentDecompressor;
026import io.netty.handler.codec.http.HttpObjectAggregator;
027import io.netty.handler.codec.http.HttpRequestDecoder;
028import io.netty.handler.codec.http.HttpResponseEncoder;
029import io.netty.handler.codec.http.HttpServerKeepAliveHandler;
030import io.netty.handler.logging.LogLevel;
031import io.netty.handler.logging.LoggingHandler;
032import io.netty.handler.stream.ChunkedWriteHandler;
033
034import lombok.AllArgsConstructor;
035
036@AllArgsConstructor
037public class ProxyInitializer extends ChannelInitializer<SocketChannel> {
038
039    private final HttpApiHandler api;
040
041    @Override
042    protected void initChannel(final SocketChannel channel) {
043        final ChannelPipeline pipeline = channel.pipeline();
044        final ChannelInboundHandlerAdapter handler;
045        final boolean degzip;
046        if (Handlers.isActive("capture")) {
047            degzip = true;
048            handler = new DefaultResponseLocatorCapturingHandler(api);
049        } else if (Handlers.isActive("passthrough")) {
050            degzip = false;
051            handler = new PassthroughHandler(api);
052        } else {
053            degzip = true;
054            handler = new ServingProxyHandler(api);
055        }
056        pipeline
057                .addLast("logging", new LoggingHandler(LogLevel.valueOf(api.getLogLevel())))
058                .addLast("http-decoder", new HttpRequestDecoder());
059        if (degzip) {
060            pipeline.addLast("gzip-decompressor", new HttpContentDecompressor());
061        }
062        pipeline
063                .addLast("http-encoder", new HttpResponseEncoder())
064                .addLast("gzip-compressor", new HttpContentCompressor())
065                .addLast("http-keepalive", new HttpServerKeepAliveHandler())
066                .addLast("aggregator", new HttpObjectAggregator(Integer.MAX_VALUE))
067                .addLast("chunked-writer", new ChunkedWriteHandler())
068                .addLast("talend-junit-api-server", handler);
069    }
070}