Router

scamper.http.server.Router
trait Router

Defines router for request handling.

import scala.language.implicitConversions

import scamper.http.ResponseStatus.Registry.{ BadRequest, NotFound, Ok }
import scamper.http.server.{ ParameterNotConvertible, ServerApplication, toServerHttpRequest }
import scamper.http.stringToEntity

val app = ServerApplication()

// Mount router to /api
app.route("/api") { router =>
 val messages = Map(1 -> "Hello, world!", 2 -> "Goodbye, cruel world!")

 // Map handler to /api/messages
 router.get("/messages") { req =>
   Ok(messages.mkString("\r\n"))
 }

 // Map handler to /api/messages/:id
 router.get("/messages/:id") { req =>
   val id = req.pathParams.getInt("id")
   messages.get(id)
    .map(Ok(_))
    .getOrElse(NotFound())
 }

 router.recover { req =>
   { case _: ParameterNotConvertible => BadRequest(req.target.toString) }
 }
}

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes

Members list

Value members

Abstract methods

def incoming(handler: RequestHandler): Router.this.type

Adds supplied request handler.

Adds supplied request handler.

The handler is appended to existing request handler chain.

Value parameters

handler

request handler

Attributes

Returns

this router

Note

If request handler implements LifecycleHook, it is also added as a lifecycle hook.

def incoming(path: String, methods: RequestMethod*)(handler: RequestHandler): Router.this.type

Adds supplied handler for requests with given router path and any of specified request methods.

Adds supplied handler for requests with given router path and any of specified request methods.

The handler is appended to existing request handler chain.

Value parameters

handler

request handler

methods

request methods

path

router path

Attributes

Returns

this router

Note

If no request methods are specified, then matches are limited to path only.

If request handler implements LifecycleHook, it is also added as a lifecycle hook.

def mountPath: String

Gets mount path.

Gets mount path.

Attributes

def outgoing(filter: ResponseFilter): Router.this.type

Adds supplied response filter.

Adds supplied response filter.

The filter is appended to existing response filter chain.

Value parameters

filter

response filter

Attributes

Returns

this router

Note

If response filter implements LifecycleHook, it is also added as a lifecycle hook.

def recover(handler: ErrorHandler): Router.this.type

Adds error handler.

Adds error handler.

The handler is appended to existing error handler chain.

Value parameters

handler

error handler

Attributes

Returns

this router

Note

If error handler implements LifecycleHook, it is also added as a lifecycle hook.

def reset(): Router.this.type

Resets router.

Resets router.

Attributes

Returns

this router

def trigger(hook: LifecycleHook): Router.this.type

Adds server lifecycle hook.

Adds server lifecycle hook.

Value parameters

hook

lifecycle hook

Attributes

Returns

this router

Note

On startup, hooks are called in the order they are added; on shutdown, they are called in reverse order.

Concrete methods

def delete(path: String)(handler: RequestHandler): Router.this.type

Adds supplied handler for DELETE requests to given router path.

Adds supplied handler for DELETE requests to given router path.

The handler is appended to existing request handler chain.

Value parameters

handler

request handler

path

router path

Attributes

Returns

this router

Note

If request handler implements LifecycleHook, it is also added as a lifecycle hook.

def files(path: String, source: File, defaults: String*): Router.this.type

Mounts file server at given path.

Mounts file server at given path.

At request time, the mount path is stripped from the router path, and the remaining path is used to locate a file in the source directory.

File Mapping Examples

Mount Path Source Directory Router Path Maps to
/images /tmp /images/logo.png /tmp/logo.png
/images /tmp /images/icons/warning.png /tmp/icons/warning.png

Value parameters

defaults

default file names used when request matches directory

path

router path at which directory is mounted

source

directory from which files are served

Attributes

Returns

this router

Note

If a request matches a directory, and if a file with one of the default file names exists in that directory, the server sends 303 (See Other) with a Location header value set to path of default file.

def get(path: String)(handler: RequestHandler): Router.this.type

Adds supplied handler for GET requests to given router path.

Adds supplied handler for GET requests to given router path.

The handler is appended to existing request handler chain.

Value parameters

handler

request handler

path

router path

Attributes

Returns

this router

Note

If request handler implements LifecycleHook, it is also added as a lifecycle hook.

def post(path: String)(handler: RequestHandler): Router.this.type

Adds supplied handler for POST requests to given router path.

Adds supplied handler for POST requests to given router path.

The handler is appended to existing request handler chain.

Value parameters

handler

request handler

path

router path

Attributes

Returns

this router

Note

If request handler implements LifecycleHook, it is also added as a lifecycle hook.

def put(path: String)(handler: RequestHandler): Router.this.type

Adds supplied handler for PUT requests to given router path.

Adds supplied handler for PUT requests to given router path.

The handler is appended to existing request handler chain.

Value parameters

handler

request handler

path

router path

Attributes

Returns

this router

Note

If request handler implements LifecycleHook, it is also added as a lifecycle hook.

def route(path: String)(app: RouterApplication): Router.this.type

Mounts router application at given path.

Mounts router application at given path.

Value parameters

app

router application

path

router path at which application is mounted

Attributes

Returns

this router

Note

If router app implements LifecycleHook, it is also added as a lifecycle hook.

def toAbsolutePath(path: String): String

Expands supplied router path to its absolute path.

Expands supplied router path to its absolute path.

Value parameters

path

router path

Attributes

Throws
java.lang.IllegalArgumentException

if router path is not * and does not begin with / or if it escapes mount path

Note

If * is supplied as router path, its absolute path is also *.

def websocket(path: String)(app: WebSocketApplication[_]): Router.this.type

Mounts WebSocket application at given path.

Mounts WebSocket application at given path.

Value parameters

app

WebSocket application

path

router path at which application is mounted

Attributes

Returns

this router

Note

If WebSocket app implements LifecycleHook, it is also added as a lifecycle hook.