use of nl.teslanet.mule.connectors.coap.api.CoAPRequestAttributes in project mule-coap-connector by teslanet-nl.
the class ServedResource method handleRequest.
/**
* Generic handler for processing requests.
* @param exchange the CoAP exchange context of the request.
* @param defaultResponseCode the response code that will be used when the Mule flow hasn't set one.
*/
private void handleRequest(SourceCallback<InputStream, CoAPRequestAttributes> callback, CoapExchange exchange, CoAPResponseCode defaultCoAPResponseCode) {
if (callback == null) {
exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "NO LISTENER");
return;
}
if (earlyAck) {
exchange.accept();
}
CoAPRequestAttributes requestAttributes;
try {
requestAttributes = createRequestAttributes(exchange);
} catch (Exception e1) {
// cannot process the request when an option is not valid,
// probably Californium has already dealt with this, so shouldn't occur
Response response = new Response(ResponseCode.BAD_OPTION);
response.setPayload(e1.getMessage());
exchange.respond(response);
return;
}
SourceCallbackContext requestcontext = callback.createContext();
requestcontext.addVariable("defaultCoAPResponseCode", defaultCoAPResponseCode);
requestcontext.addVariable("CoapExchange", exchange);
// TODO add streaming & blockwise cooperation
byte[] requestPayload = exchange.getRequestPayload();
if (requestPayload != null) {
callback.handle(Result.<InputStream, CoAPRequestAttributes>builder().output(new ByteArrayInputStream(requestPayload)).length(requestPayload.length).attributes(requestAttributes).mediaType(MediaTypeMediator.toMediaType(exchange.getRequestOptions().getContentFormat())).build(), requestcontext);
} else {
callback.handle(Result.<InputStream, CoAPRequestAttributes>builder().attributes(requestAttributes).output(null).mediaType(MediaTypeMediator.toMediaType(exchange.getRequestOptions().getContentFormat())).build(), requestcontext);
}
}
use of nl.teslanet.mule.connectors.coap.api.CoAPRequestAttributes in project mule-coap-connector by teslanet-nl.
the class QueryTest method testQuery.
@Test
public void testQuery() throws Exception {
// spyRequestMessage();
MuleEventSpy spy = new MuleEventSpy("query", null, "OK");
spy.clear();
Request request = new Request(requestCode);
request.setURI(uri);
CoapResponse response = client.advanced(request);
assertNotNull("no response", response);
assertTrue("response indicates failure: " + response.getCode() + " msg: " + response.getResponseText(), response.isSuccess());
assertEquals("wrong spy activation count", 1, spy.getEvents().size());
Message received = (Message) spy.getEvents().get(0).getContent();
assertTrue("wrong attributes class", received.getAttributes().getValue() instanceof CoAPRequestAttributes);
CoAPRequestAttributes attributes = (CoAPRequestAttributes) received.getAttributes().getValue();
assertEquals("wrong request code", requestCode.name(), attributes.getRequestCode());
assertEquals("wrong request uri", expectedUri, attributes.getRequestUri());
assertEquals("wrong query params", expectedQuery, queryString(attributes.getOptions().getUriQuery()));
}
Aggregations