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

jp.co.cyberagent.aeromock.server.AeromockServerHandler.scala Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package jp.co.cyberagent.aeromock.server

import java.net.InetSocketAddress
import java.util.Locale

import jp.co.cyberagent.aeromock.config.ConfigHolder
import jp.co.cyberagent.aeromock.core.http.HttpRequestProcessor
import jp.co.cyberagent.aeromock.data.DynamicMethodValueStore
import jp.co.cyberagent.aeromock.server.http.{HttpRequestProcessorSelector, ServerExceptionHandler}
import io.netty.channel.{ChannelHandlerContext, SimpleChannelInboundHandler}
import io.netty.handler.codec.http.FullHttpRequest

class AeromockServerHandler(val useSendFile: Boolean)
  extends SimpleChannelInboundHandler[FullHttpRequest] {

  override def channelRead0(context: ChannelHandlerContext, request: FullHttpRequest) {
    implicit val con = context

    AccessLog.initialize(request)

    ConfigHolder.initialize
    ConfigHolder.getUserConfig.language match {
      case Some(locale) => Locale.setDefault(locale)
      case None =>
    }

    DynamicMethodValueStore.initialize
    val requestContainer = HttpRequestProcessor.execute(request,
      context.channel().remoteAddress().asInstanceOf[InetSocketAddress])
    val finalizedRequest = requestContainer.finalizedRequest

    val delegator = HttpRequestProcessorSelector.select(ConfigHolder.getProject, finalizedRequest)
    val response = delegator.delegate(finalizedRequest)

    AccessLog.writeAccessLog(response.getStatus.code())
  }

  override def exceptionCaught(context: ChannelHandlerContext, cause: Throwable) {
    implicit val con = context

    if (context.channel().isActive()) {
      ServerExceptionHandler.handle(cause)
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy