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

jp.co.cyberagent.aeromock.api.controller.AeromockApiController.scala Maven / Gradle / Ivy

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

import jp.co.cyberagent.aeromock.core.http.ParsedRequest
import jp.co.cyberagent.aeromock.server.http.HttpResponseWriter
import io.netty.handler.codec.http.HttpResponse
import io.netty.handler.codec.http.HttpResponseStatus._
import io.netty.channel.ChannelHandlerContext
import jp.co.cyberagent.aeromock.{AeromockTemplateNotFoundException, AeromockConfigurationException, AeromockTemplateParseException, AeromockApiBadRequestException}
import org.slf4j.LoggerFactory
import scaldi.Injectable

trait AeromockApiController extends HttpResponseWriter with Injectable {

  val LOG = LoggerFactory.getLogger(classOf[AeromockApiController])

  def render(request: ParsedRequest)(implicit context: ChannelHandlerContext): HttpResponse = {

    try {
      renderJson(Map("status" -> OK.code()) ++ renderJson(request), OK)
    } catch {
      case e: AeromockApiBadRequestException => renderJson(Map("status" -> BAD_REQUEST.code(), "message" -> e.getMessage()), BAD_REQUEST)
      case e: AeromockTemplateParseException => renderJson(Map("status" -> BAD_REQUEST.code(), "message" -> e.getMessage()), BAD_REQUEST)
      case e: AeromockTemplateNotFoundException => renderJson(Map("status" -> BAD_REQUEST.code(), "message" -> e.getMessage()), BAD_REQUEST)
      case e: AeromockConfigurationException => renderJson(Map("status" -> FORBIDDEN.code(), "message" -> e.getMessage()), FORBIDDEN)
      case e: Exception => {
        e.printStackTrace()
        LOG.error(e.getMessage(), e.getCause())
        renderJson(
          Map(
            "status" -> INTERNAL_SERVER_ERROR.code(),
            "message" -> e.getMessage()), INTERNAL_SERVER_ERROR)
      }
    }
  }

  def renderJson(request: ParsedRequest): Map[String, Any]
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy