Mule flows receive CoAP client requests through a listener messagesource. A listener
is configured by adding the <coap-server:listen/>
element at the
beginning of the Mule flow. The resource that is listened on is defined by the uri attribute, for example <coap-server:listen
uri="/hello/there"/>
. Wildcards can be used to listen on a group of
resources. For example uri="/*"
will listen on all resources and
uri="/somewhat/deeper/*"
listens on the children of the "deeper"
resources. When multiple listeners apply, the listener with the most specific uri will
receive the requests.
The listener delivers CoAP client requests as a MuleMessage to the flow. The Mule flow
processes this message with its payload that contains the payload of the received CoAP
request as byte[] . The payload at the end of the flow is returned as payload of the
CoAP response. The CoAP operation, the resource uri, and all CoAP options contained in
the request are made available in the inbound scope of the MuleMessage. The properties
of the CoAP response that is returned by the listener are taken from the MuleMessage
outbound scope. The CoAP response code can be set in outbound property
coap.response.code
. Setting the response code is optional and defaults
to a value depending on the kind of request, see table below. The response code property
should be set to the CoAP response or error condition as is appripriate for the use case
at hand. The complete list of codes can be found in the section called “Response codes”.
Request code | Default Response Code |
---|---|
GET | CONTENT |
PUT | CREATED |
POST | CHANGED |
DELETE | DELETED |
CoAP Options also are passed through in inbound and outbound scope. For a
complete list of message properties see the section called “Message Properties”.
Response payloads that are of type byte[] are returned as is, where other payload types are transformed to byte[] by calling toString().getBytes( UTF_8 ).
<flow name="coap-serverFlow1"> <coap-server:listen uri="/alphabet/*" config-ref="CoAP_Server_Configuration"/> <set-variable variableName="method" value="#[message.inboundProperties['coap.request.code']]"/> <set-variable variableName="uri" value="#[message.inboundProperties['coap.request.uri']]"/> <byte-array-to-string-transformer/> <logger level="INFO" message="#[payload]"/> <set-payload value="<my_response>Hi!</my_response>" encoding="UTF-8" mimeType="application/xml"/> <set-property propertyName="coap.response.code" value="CONTENT" /> </flow>