Search in sources :

Example 1 with InvalidETagException

use of nl.teslanet.mule.connectors.coap.api.error.InvalidETagException in project mule-coap-connector by teslanet-nl.

the class Listener method onSuccess.

@OnSuccess
@MediaType(value = "*/*", strict = false)
public void onSuccess(@Optional @NullSafe @Alias("response") @Placement(tab = "Response", order = 1) ResponseParams response, @Optional @NullSafe @Alias("response-options") @Expression(ExpressionSupport.SUPPORTED) @Summary("The CoAP options to send with the response.") @Placement(tab = "Response", order = 2) ResponseOptions responseOptions, SourceCallbackContext callbackContext) throws InternalInvalidByteArrayValueException, InternalInvalidResponseCodeException, IOException, InvalidETagException, InternalInvalidOptionValueException {
    CoAPResponseCode defaultCoapResponseCode = (CoAPResponseCode) callbackContext.getVariable("defaultCoAPResponseCode").get();
    Response coapResponse = new Response(AttributeUtils.toResponseCode(response.getResponseCode(), defaultCoapResponseCode));
    // TODO give user control
    TypedValue<Object> responsePayload = response.getResponsePayload();
    coapResponse.getOptions().setContentFormat(MediaTypeMediator.toContentFormat(responsePayload.getDataType().getMediaType()));
    if (responseOptions != null) {
        MessageUtils.copyOptions(responseOptions, coapResponse.getOptions(), transformationService);
    }
    // TODO add streaming & blockwise cooperation
    try {
        coapResponse.setPayload(MessageUtils.toBytes(responsePayload, transformationService));
    } catch (Exception e) {
        throw new InternalInvalidByteArrayValueException("Cannot convert payload to byte[]", e);
    }
    ((CoapExchange) callbackContext.getVariable("CoapExchange").get()).respond(coapResponse);
}
Also used : Response(org.eclipse.californium.core.coap.Response) EmitsResponse(org.mule.runtime.extension.api.annotation.source.EmitsResponse) InternalInvalidByteArrayValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidByteArrayValueException) CoAPResponseCode(nl.teslanet.mule.connectors.coap.api.CoAPResponseCode) InvalidETagException(nl.teslanet.mule.connectors.coap.api.error.InvalidETagException) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException) MuleException(org.mule.runtime.api.exception.MuleException) InternalInvalidResponseCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException) InternalResourceUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException) IOException(java.io.IOException) InternalInvalidByteArrayValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidByteArrayValueException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) InternalUriPatternException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriPatternException) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) OnSuccess(org.mule.runtime.extension.api.annotation.execution.OnSuccess) MediaType(org.mule.runtime.extension.api.annotation.param.MediaType)

Example 2 with InvalidETagException

use of nl.teslanet.mule.connectors.coap.api.error.InvalidETagException in project mule-coap-connector by teslanet-nl.

the class ETagTest method testConstructorETagLargeString.

@Test
public void testConstructorETagLargeString() throws InvalidETagException {
    String etagValue1 = "112233445566778899";
    InvalidETagException e = assertThrows(InvalidETagException.class, () -> {
        @SuppressWarnings("unused") ETag etag1 = new ETag(etagValue1, 16);
    });
    assertTrue("exception has wrong message", e.getMessage().contains("Cannot construct etag value"));
    assertTrue("exception has wrong message", e.getMessage().contains(etagValue1));
}
Also used : InvalidETagException(nl.teslanet.mule.connectors.coap.api.error.InvalidETagException) ETag(nl.teslanet.mule.connectors.coap.api.options.ETag) Test(org.junit.Test)

Example 3 with InvalidETagException

use of nl.teslanet.mule.connectors.coap.api.error.InvalidETagException in project mule-coap-connector by teslanet-nl.

the class ETagTest method testConstructorETagLargeString2.

@Test
public void testConstructorETagLargeString2() throws InvalidETagException {
    String etagValue1 = "this is too large to fit into an etag";
    InvalidETagException e = assertThrows(InvalidETagException.class, () -> {
        @SuppressWarnings("unused") ETag etag1 = new ETag(etagValue1);
    });
    assertTrue("exception has wrong message", e.getMessage().contains("Cannot construct etag value"));
    assertTrue("exception has wrong message", e.getMessage().contains(etagValue1));
}
Also used : InvalidETagException(nl.teslanet.mule.connectors.coap.api.error.InvalidETagException) ETag(nl.teslanet.mule.connectors.coap.api.options.ETag) Test(org.junit.Test)

Example 4 with InvalidETagException

use of nl.teslanet.mule.connectors.coap.api.error.InvalidETagException in project mule-coap-connector by teslanet-nl.

the class ETagTest method testConstructorETagLargeByteArray.

@Test
public void testConstructorETagLargeByteArray() throws InvalidETagException {
    byte[] etagValue1 = new byte[9];
    for (int i = 0; i < 9; i++) {
        etagValue1[i] = (byte) i;
    }
    InvalidETagException e = assertThrows(InvalidETagException.class, () -> {
        @SuppressWarnings("unused") ETag etag1 = new ETag(etagValue1);
    });
    assertTrue("exception has wrong message", e.getMessage().contains("Given length is: 9"));
}
Also used : InvalidETagException(nl.teslanet.mule.connectors.coap.api.error.InvalidETagException) ETag(nl.teslanet.mule.connectors.coap.api.options.ETag) Test(org.junit.Test)

Example 5 with InvalidETagException

use of nl.teslanet.mule.connectors.coap.api.error.InvalidETagException in project mule-coap-connector by teslanet-nl.

the class ETagTest method testConstructorInvalidString.

@Test
public void testConstructorInvalidString() throws InvalidETagException {
    String etagValue1 = "10aaZZ";
    InvalidETagException e = assertThrows(InvalidETagException.class, () -> {
        @SuppressWarnings("unused") ETag etag1 = new ETag(etagValue1, 16);
    });
    assertTrue("exception has wrong message", e.getMessage().contains("Cannot construct etag value"));
    assertTrue("exception has wrong message", e.getMessage().contains(etagValue1));
}
Also used : InvalidETagException(nl.teslanet.mule.connectors.coap.api.error.InvalidETagException) ETag(nl.teslanet.mule.connectors.coap.api.options.ETag) Test(org.junit.Test)

Aggregations

InvalidETagException (nl.teslanet.mule.connectors.coap.api.error.InvalidETagException)5 ETag (nl.teslanet.mule.connectors.coap.api.options.ETag)4 Test (org.junit.Test)4 IOException (java.io.IOException)1 CoAPResponseCode (nl.teslanet.mule.connectors.coap.api.CoAPResponseCode)1 InternalInvalidByteArrayValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidByteArrayValueException)1 InternalInvalidOptionValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)1 InternalInvalidResponseCodeException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException)1 InternalResourceUriException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException)1 InternalUriPatternException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriPatternException)1 Response (org.eclipse.californium.core.coap.Response)1 CoapExchange (org.eclipse.californium.core.server.resources.CoapExchange)1 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)1 MuleException (org.mule.runtime.api.exception.MuleException)1 OnSuccess (org.mule.runtime.extension.api.annotation.execution.OnSuccess)1 MediaType (org.mule.runtime.extension.api.annotation.param.MediaType)1 EmitsResponse (org.mule.runtime.extension.api.annotation.source.EmitsResponse)1