Search in sources :

Example 1 with CoAPRequestAttributes

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);
    }
}
Also used : Response(org.eclipse.californium.core.coap.Response) CoAPRequestAttributes(nl.teslanet.mule.connectors.coap.api.CoAPRequestAttributes) ByteArrayInputStream(java.io.ByteArrayInputStream) SourceCallbackContext(org.mule.runtime.extension.api.runtime.source.SourceCallbackContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)

Example 2 with CoAPRequestAttributes

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()));
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) CoAPRequestAttributes(nl.teslanet.mule.connectors.coap.api.CoAPRequestAttributes) Message(org.mule.runtime.api.message.Message) Request(org.eclipse.californium.core.coap.Request) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Aggregations

CoAPRequestAttributes (nl.teslanet.mule.connectors.coap.api.CoAPRequestAttributes)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InternalInvalidOptionValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)1 MuleEventSpy (nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy)1 CoapResponse (org.eclipse.californium.core.CoapResponse)1 Request (org.eclipse.californium.core.coap.Request)1 Response (org.eclipse.californium.core.coap.Response)1 Test (org.junit.Test)1 Message (org.mule.runtime.api.message.Message)1 SourceCallbackContext (org.mule.runtime.extension.api.runtime.source.SourceCallbackContext)1