CoapResource

abstract class CoapResource(val name: String)(source)

A resource of a CoapServer, identified by its path from the root. Subclass it and override the handlers for the methods it supports; the others answer 4.05 Method Not Allowed (RFC 7252 §5.9.2.6). For resources defined inline, see the DSL in ResourceBuilder.

class Temperature : CoapResource("temperature") {
override val attributes = ResourceAttributes(resourceTypes = listOf("temperature-c"))
override suspend fun handleGet(context: RequestContext) = CoapResponse.content("22.5")
}

Handlers run concurrently on the server's coroutines, one per request, so a resource must be thread safe. A handler answers with an error either by returning it or, from deeper in the application's code, by throwing CoapResponseException with it, which is logged at DEBUG only. A handler that throws anything else produces 5.00 Internal Server Error, logged at ERROR, and so does a failure anywhere else in the server's pipeline, the security layer and the application callbacks behind it included (see CoapServer); an error while producing a notification, thrown or returned, ends the observation (RFC 7641 §3.2) and removes its observer. To map an application's own exceptions to responses in one place, override handle in a base class of its resources. Children can be added and removed at any time, for example to create resources on PUT or POST.

Inheritors

Constructors

Link copied to clipboard
constructor(name: String)

Properties

Link copied to clipboard
open val access: AccessPolicy?

Who may use this resource and, unless a child has a policy of its own, everything below it (ADR 0008). Null inherits the parent's, and the root's falls back to CoapServerConfig.defaultAccess; see AccessPolicy for when the server asks it.

Link copied to clipboard

Link attributes for /.well-known/core.

Link copied to clipboard

The child resources, in the order they were added.

Link copied to clipboard

Whether /.well-known/core lists this resource (RFC 6690 §4: which links to include is up to the server).

Link copied to clipboard

Whether GET or FETCH with Observe registers observers (RFC 7641 §4.1); see ObservableResource.

Link copied to clipboard

the Uri-Path segment of this resource; the root has the empty name.

Link copied to clipboard

The parent resource, or null for a root or a detached resource.

Link copied to clipboard

"/a/b", or "/" for the root.

Link copied to clipboard

Critical options this resource processes itself, beyond those the server implements (RFC 7252 §5.4.1). A request with any other unrecognized critical option gets 4.02 Bad Option before a handler runs. For example, a resource that honours conditional requests adds If-Match and If-None-Match (see RequestContext.preconditionsHold).

Functions

Link copied to clipboard

Adds child under this resource and returns it.

Link copied to clipboard

The child called name, or null.

Link copied to clipboard
fun find(segments: List<String>): CoapResource?

The resource at segments below this one, or null.

Link copied to clipboard
open suspend fun handle(context: RequestContext): CoapResponse

Dispatches to the handler for the request method (template method). Override it to handle methods generically, for example to proxy.

Link copied to clipboard
open suspend fun handleDelete(context: RequestContext): CoapResponse

DELETE (RFC 7252 §5.8.4).

Link copied to clipboard
open suspend fun handleFetch(context: RequestContext): CoapResponse

FETCH (RFC 8132 §2); observable like GET (RFC 8132 §2.4).

Link copied to clipboard
open suspend fun handleGet(context: RequestContext): CoapResponse

GET (RFC 7252 §5.8.1); also produces every Observe notification of an observable resource.

Link copied to clipboard
open suspend fun handleIPatch(context: RequestContext): CoapResponse

iPATCH (RFC 8132 §3).

Link copied to clipboard
open suspend fun handlePatch(context: RequestContext): CoapResponse

PATCH (RFC 8132 §3).

Link copied to clipboard
open suspend fun handlePost(context: RequestContext): CoapResponse

POST (RFC 7252 §5.8.2).

Link copied to clipboard
open suspend fun handlePut(context: RequestContext): CoapResponse

PUT (RFC 7252 §5.8.3).

Link copied to clipboard

Removes and returns the child called name. Its observers, and those of its children, get a 4.04 notification and are removed (RFC 7641 §3.2).

Link copied to clipboard
open override fun toString(): String