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

com.gu.management.Http.scala Maven / Gradle / Ivy

The newest version!
package com.gu.management

import scala.collection.mutable
import scala.xml.Elem
import org.json4s._
import org.json4s.jackson.JsonMethods._

sealed abstract class Method
case object GET extends Method
case object POST extends Method

object Method {
  def apply(method: String): Method = method.toUpperCase match {
    case "GET" => GET
    case "POST" => POST
    // Only GET and POST are supported for Management URLs
  }
}

sealed trait ResponseBody {
  def toText: String
  def length: Int = toText.length
}
case class TextResponseBody(text: String) extends ResponseBody { lazy val toText = text }
case class HtmlResponseBody(html: Elem) extends ResponseBody { lazy val toText = html.toString() }
case class XmlResponseBody(xml: Elem) extends ResponseBody { lazy val toText = xml.toString() }
case class JsonResponseBody(json: JValue) extends ResponseBody { lazy val toText = pretty(render(json)) }

/*
 * requestURI: the part of this request's URL from the protocol name up to the query string
 *             in the first line of the HTTP request.
 */
case class HttpRequest(
    method: Method,
    path: String,
    requestURI: String,
    parameters: Map[String, List[String]]) {

  def getParameter(key: String): Option[String] = (parameters get key) flatMap { _.headOption }
}

trait HttpResponse {
  var encoding: String = "UTF-8"
  val headers: mutable.Map[String, String] = mutable.Map()

  var contentType: String

  var status: Int
  var body: Option[ResponseBody] = None

  def send()
  def sendError(code: Int, message: String)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy