Mule flows use the CoapClient Connector to do requests on resources made available by CoAP servers. For each type of CoAP request ( GET, PUT, POST, DELETE) the connector has a mule message-processor available. The message-processors are described in the sections below .
The message-processors reference a <coap-client:config/>
element, that
configures a number of CoAP communication parameters that will be used when issuing
CoAP requests. On the message-processors the CoAP resource is defined that will be
requested during execution of the Mule flow. The resource is addressed using server
host and port, the
path of the resource and - optionally - query-parameters. The host
and port need only to be set when they differ from the
defaults in the <coap-client:config/>
element. The confirmable attribute defines whether the server must confirm the
request..
The payload of the CoAP request is obtained from the Mule-message payload. Its type
will determine what conversion is done. A byte array type will be used as is, where
other payload types are transformed to byte array equivalent to calling
toString().getBytes( UTF_8 )
.
The message-processors return the response of the CoAP request. The payload of the
Mule-message contains the payload of the CoAP response as byte array - if any. When the
server has responded on the request within time-out, the inbound-property
coap.response.code
contains the response category retuned by the CoAP
server. The complete list of response codes can be found in the section called “Response codes”. When no timeout occurs and the response code
indicates a successful processing of the request, the inbound-property
coap.response.success
has the value true, otherwise the value will be false. The Mule-flow can use these properties to act appropriately on the
different responses.
The CoAP protocol provides for options that can be added to the CoAP messages. The CoapClient Connectors message-processors supports these. CoAP Options on a request can be set by using outbound-properties. The Mule-flow has to set these on the Mule-message before the message-processor is called. Options contained in CoAP responses are made available as inbound-properties in the Mule-message returned.
For a complete list of message properties, see the section called “Message Properties”.
The Get message-processor requests the contents of a CoAP resource.
In the example below the contents of the resource /query is
requested. Some optional query parameters are defined on the request. The
content-type that the Mule-flow wishes to receive is given in the outbound-property
coap.opt.accept
.
The response is evaluated by the Choice-Router that checks whether the request
succeeded using the coap.response.success
and
coap.response.code
inbound-properties.
Example 5. Get example
<flow name="get_example"> ... <set-payload value="#[NullPayload.getInstance()]" /> <set-property propertyName="coap.opt.accept" value="0" /> <coap-client:get config-ref="my_CoAP_Client" path="/query" > <coap-client:query-parameters> <coap-client:query-parameter>hello=there</coap-client:query-parameter> <coap-client:query-parameter>how_are=you</coap-client:query-parameter> </coap-client:query-parameters> </coap-client:get> <choice > <when expression= "#[ message.inboundProperties['coap.response.success'] and message.inboundProperties['coap.response.code'] == '2.05' ]"> <byte-array-to-string-transformer /> <set-payload value="Content is : #[payload]" mimeType="text/plain" /> </when> <otherwise> <set-payload value="No Content received!" mimeType="text/plain" /> </otherwise> </choice> ... </flow>
The Put message-processor requests a server to set new contents on a CoAP resource.
In the example below new contents for the resource /test is
requested. The content-type of the request payload is given in the outbound-property
coap.opt.content_format
.
The response is evaluated by the Choice-Router, which checks whether the request
succeeded using the coap.response.success
and
coap.response.code
inbound-properties. The
2.04 value of the response code indicates that the server
has changed the resource.
Example 6. Put example
<flow name="put_example"> ... <set-payload value="This is new content." /> <set-property propertyName="coap.opt.content_format" value="0" /> <coap-client:put config-ref="my_CoAP_Client" path="/test"/> <logger level="INFO" /> <choice> <when expression="#[ message.inboundProperties['coap.response.success'] and message.inboundProperties['coap.response.code'] == '2.04' ]"> <byte-array-to-string-transformer /> <set-payload value="Content is delivered" mimeType="text/plain" /> </when> <otherwise> <set-payload value="No Content delivered" mimeType="text/plain" /> </otherwise> </choice> ... </flow>
The Post message-processor requests to create a new resource on a CoAP server. The message-payload will be used as the contents of the resource that is created.
In the example below a request is issued to add a new child resource to the
/test resource. The content-type of the request payload,
that should become the resources content, is given in the outbound-property
coap.opt.content_format
.
The response is evaluated by the Choice-Router that checks whether the request
succeeded using the coap.response.success
and
coap.response.code
inbound-properties. The
2.01 value of the response code indicates that the server
has created the resource.
Example 7. Post example
<flow name="post_example"> ... <set-payload value="This is the new resources content." /> <set-property propertyName="coap.opt.content_format" value="0" /> <coap-client:post config-ref="my_CoAP_Client" path="/test" /> <logger level="INFO" /> <choice> <when expression="#[ message.inboundProperties['coap.response.success'] and message.inboundProperties['coap.response.code'] == '2.01' ]"> <byte-array-to-string-transformer /> <set-payload value="Resource created." mimeType="text/plain" /> </when> <otherwise> <set-payload value="No resource created!" mimeType="text/plain" /> </otherwise> </choice> ... </flow>
The Delete message-processor requests to delete a resource on a CoAP server.
In the example below a request is issued to delete the /test resource.
The response is evaluated by the Choice-Router that checks whether the request
succeeded using the coap.response.success
and
coap.response.code
inbound-properties. The
2.02 value of the response code indicates that the server
has deleted the resource.
Example 8. Delete example
<flow name="delete_example"> ... <set-payload value="#[NullPayload.getInstance()]" /> <coap-client:delete config-ref="my_CoAP_Client" path="/test" /> <logger level="INFO" /> <choice> <when expression="#[ message.inboundProperties['coap.response.success'] and message.inboundProperties['coap.response.code'] == '2.02' ]"> <byte-array-to-string-transformer /> <set-payload value="Resource is deleted" mimeType="text/plain" /> </when> <otherwise> <set-payload value="No resource deleted!" mimeType="text/plain" /> </otherwise> </choice> ... </flow>