CoapResource
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
Properties
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 attributes for /.well-known/core.
The child resources, in the order they were added.
Whether /.well-known/core lists this resource (RFC 6690 §4: which links to include is up to the server).
Whether GET or FETCH with Observe registers observers (RFC 7641 §4.1); see ObservableResource.
The parent resource, or null for a root or a detached resource.
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
Adds child under this resource and returns it.
The child called name, or null.
Dispatches to the handler for the request method (template method). Override it to handle methods generically, for example to proxy.
DELETE (RFC 7252 §5.8.4).
FETCH (RFC 8132 §2); observable like GET (RFC 8132 §2.4).
GET (RFC 7252 §5.8.1); also produces every Observe notification of an observable resource.
iPATCH (RFC 8132 §3).
PATCH (RFC 8132 §3).
POST (RFC 7252 §5.8.2).
PUT (RFC 7252 §5.8.3).
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).