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

RequestProcessor

trait RequestProcessor extends RequestHandler

Provides utility for processing incoming request.

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

Abstract Value Members

  1. abstract def process(request: HttpRequest): HttpResponse

    Processes incoming request.

    Processes incoming request.

    The processor returns a response that satisfies the request.

    request

    incoming request

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. def apply(request: HttpRequest): Either[HttpRequest, HttpResponse]

    Handles incoming request.

    Handles incoming request.

    If handler satisfies request, then it returns a response. Otherwise, it returns a request, which may be the original request or an alternate one.

    Definition Classes
    RequestProcessorRequestHandler
  5. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  6. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )
  7. def compose(other: RequestHandler): RequestHandler

    Composes this handler with other, using this as a fallback.

    Composes this handler with other, using this as a fallback.

    If other returns a request, then the request is passed to this. Otherwise, if other returns a response, then this is not invoked.

    other

    initial handler

    Definition Classes
    RequestHandler
  8. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  12. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  13. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  15. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  16. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  17. def orElse(other: RequestHandler): RequestHandler

    Composes this handler with other, using other as a fallback.

    Composes this handler with other, using other as a fallback.

    If this returns a request, then the request is passed to other. Otherwise, if this returns a response, then other is not invoked.

    other

    fallback handler

    Definition Classes
    RequestHandler
  18. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  19. def toString(): String
    Definition Classes
    AnyRef → Any
  20. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @native() @throws( ... )

Inherited from RequestHandler

Inherited from AnyRef

Inherited from Any

Ungrouped