All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nightcode.milter.net.SessionInitializer Maven / Gradle / Ivy

The newest version!
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.nightcode.milter.net;

import java.util.function.Supplier;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.nightcode.milter.codec.Int32LenFrameDecoder;
import org.nightcode.milter.codec.Int32LenFrameEncoder;
import org.nightcode.milter.codec.MilterPacketDecoder;
import org.nightcode.milter.codec.MilterPacketEncoder;

import static org.nightcode.milter.MilterOptions.NETTY_LOGGING_ENABLED;
import static org.nightcode.milter.MilterOptions.NETTY_LOG_LEVEL;
import static org.nightcode.milter.util.Properties.getBoolean;
import static org.nightcode.milter.util.Properties.getString;

/**
 * Implementation of ChannelInitializer.
 */
public class SessionInitializer extends ChannelInitializer {

  private final boolean loggingEnabled;
  private final String  logLevel;

  private final Supplier responseHandler;

  public SessionInitializer(Supplier responseHandler) {
    this.responseHandler = responseHandler;

    loggingEnabled = getBoolean(NETTY_LOGGING_ENABLED, false);
    logLevel       = getString(NETTY_LOG_LEVEL, "DEBUG");
  }

  @Override protected void initChannel(Channel channel) {
    ChannelPipeline pipeline = channel.pipeline();

    if (loggingEnabled) {
      pipeline.addLast("logger", new LoggingHandler(MilterGatewayManager.class.getName(), LogLevel.valueOf(logLevel)));
    }

    pipeline.addLast("frameDecoder", new Int32LenFrameDecoder());
    pipeline.addLast("frameEncoder", new Int32LenFrameEncoder());

    pipeline.addLast("milterPacketDecoder", new MilterPacketDecoder());
    pipeline.addLast("milterPacketEncoder", new MilterPacketEncoder());

    pipeline.addLast("milterResponseHandler", responseHandler.get());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy