Packages

  • package root
    Definition Classes
    root
  • package scamper

    At the core Scamper is HttpMessage, which is a trait that defines the fundamental characteristics of an HTTP message.

    HTTP Messages

    At the core Scamper is HttpMessage, which is a trait that defines the fundamental characteristics of an HTTP message. HttpRequest and HttpResponse extend the specification to define characteristics specific to their respective message types.

    An HttpRequest can be created using one of the factory methods defined in its companion object. Or you can start with a RequestMethod and use builder methods to further define the request.

    import scamper.Header
    import scamper.ImplicitConverters.stringToUri
    import scamper.RequestMethods.GET
    
    val request = GET("/motd").withHeaders(
      Header("Host", "localhost:8080"),
      Header("Accept", "text/plain")
    )
    
    printf("Request Method: %s%n", request.method)
    printf("Target URI: %s%n", request.target)
    
    request.headers.foreach(println)
    
    val host: Option[String] = request.getHeaderValue("Host")

    An HttpResponse can be created using one of the factory methods defined in its companion object. Or you can start with a ResponseStatus and use builder methods to further define the response.

    import scamper.{ BodyParsers, Header }
    import scamper.ImplicitConverters.stringToEntity
    import scamper.ResponseStatuses.Ok
    
    val response = Ok("There is an answer.").withHeaders(
      Header("Content-Type", "text/plain"),
      Header("Connection", "close")
    )
    
    printf("Status Code: %s%n", response.status.code)
    printf("Reason Phrase: %s%n", response.status.reason)
    
    response.headers.foreach(println)
    
    val contentType: Option[String] = response.getHeaderValue("Content-Type")
    
    implicit val parser = BodyParsers.text()
    
    printf("Body: %s%n", response.as[String])
    Definition Classes
    root
  • package server

    Provides HTTP server implementation.

    Provides HTTP server implementation.

    Building HTTP Server

    To build a server, you begin with ServerApplication. This is a mutable structure to which you apply changes to configure the server. Once the desired settings are applied, you invoke one of several methods to create the server.

    import java.io.File
    import java.util.zip.DeflaterInputStream
    import scamper.BodyParsers
    import scamper.ImplicitConverters.{ stringToEntity, inputStreamToEntity }
    import scamper.ResponseStatuses.{ NotFound, Ok }
    import scamper.headers.TransferEncoding
    import scamper.server.HttpServer
    import scamper.server.Implicits.ServerHttpRequestType
    import scamper.types.ImplicitConverters.stringToTransferCoding
    
    // Get server application
    val app = HttpServer.app()
    
    // Add request handler to log all requests
    app.request { req =>
      println(req.startLine)
      req
    }
    
    // Add request handler to specific request method and path
    app.get("/about") { req => Ok("This server is powered by Scamper.") }
    
    // Add request handler using path parameter
    app.put("/data/:id") { req =>
      def update(id: Int, data: String): Boolean = ???
    
      implicit val parser = BodyParsers.text()
    
      // Get path parameter
      val id = req.params.getInt("id")
    
      update(id, req.as[String]) match {
        case true  => Ok()
        case false => NotFound()
      }
    }
    
    // Serve static files
    app.files("/main", new File("/path/to/public"))
    
    // Add response filter to deflate response
    app.response { res =>
      res.withBody(new DeflaterInputStream(res.body.getInputStream))
        .withTransferEncoding("deflate", "chunked")
    }
    
    // Create server
    val server = app.create(8080)
    
    printf("Host: %s%n", server.host)
    printf("Port: %d%n", server.port)
    
    Thread.sleep(60 * 1000)
    
    // Close server when done
    server.close()
    Definition Classes
    scamper
  • ErrorHandler
  • HttpServer
  • Implicits
  • ParameterNotConvertible
  • ParameterNotFound
  • RequestFilter
  • RequestHandler
  • RequestParameters
  • RequestProcessor
  • ResponseAborted
  • ResponseFilter
  • Router
  • ServerApplication
t

scamper.server

ErrorHandler

trait ErrorHandler extends AnyRef

Provides utility for handling error when servicing request.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ErrorHandler
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def apply(error: Throwable, request: HttpRequest): HttpResponse

    Creates response for given error.

    Creates response for given error.

    error

    error generated when servicing request

    request

    request for which error was generated

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped