Search in sources :

Example 1 with CoAPResponseAttributes

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

the class Client method doRequest.

// TODO add custom timeout
/**
 * Issue a request on a CoAP resource residing on a server.
 * @param builder Builder containing request parameters.
 * @param options The request options.
 * @param handlerBuilder Builder containing the name of the handle when the response is handled asynchronously
 * @return The result of the request including payload and attributes.
 * @throws InternalInvalidHandlerNameException  When the handlerName is not null but does not reference an existing handler.
 * @throws InternalRequestException When the Request could not be issued.
 * @throws InternalResponseException When the received response appears to be invalid and cannot be processed.
 * @throws InternalEndpointException When CoAP communication failed.
 * @throws InternalInvalidRequestCodeException When the request code has invalid value.
 * @throws InternalNoResponseException When timeout has occurred.
 * @throws InternalClientErrorResponseException When response indicates client error.
 * @throws InternalInvalidResponseCodeException When response indicates other error.
 * @throws InternalServerErrorResponseException When response indicates server error.
 * @throws InternalUriException When given request uri parameters are invalid.
 */
Result<InputStream, CoAPResponseAttributes> doRequest(RequestParams builder, RequestOptions options, ResponseHandlerParams handlerBuilder) throws InternalInvalidHandlerNameException, InternalRequestException, InternalResponseException, InternalEndpointException, InternalInvalidRequestCodeException, InternalNoResponseException, InternalClientErrorResponseException, InternalInvalidResponseCodeException, InternalServerErrorResponseException, InternalUriException {
    Result<InputStream, CoAPResponseAttributes> result = null;
    CoapHandler handler = null;
    Request request = new CoapRequestBuilderImpl(builder).build();
    try {
        MessageUtils.copyOptions(options, request.getOptions(), transformationService);
    } catch (InternalInvalidOptionValueException e) {
        throw new InternalRequestException("cannot process request options", e);
    }
    if (handlerBuilder != null) {
        // asynchronous request
        SourceCallback<InputStream, CoAPResponseAttributes> callback;
        callback = getHandler(handlerBuilder.getResponseHandler());
        handler = createCoapHandler(handlerBuilder.getResponseHandler(), request.getURI(), builder.getRequestCode(), callback);
        coapClient.advanced(handler, request);
    // nothing to return
    } else {
        // send out synchronous request
        result = executeSynchronous(builder.getRequestCode(), request);
    }
    return result;
}
Also used : InternalRequestException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalRequestException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CoapHandler(org.eclipse.californium.core.CoapHandler) Request(org.eclipse.californium.core.coap.Request) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)

Example 2 with CoAPResponseAttributes

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

the class Client method startObserver.

/**
 * Start observing a resource on a CoAP server.
 * @param params The observer parameters.
 * @param handlerBuilder The response handler parameters.
 * @throws InternalInvalidObserverException When the observer parameters are invalid.
 * @throws InternalUriException When the uri parameters of the resource to observe are invalid.
 * @throws InternalInvalidHandlerNameException When the handler parameters are invalid.
 */
synchronized void startObserver(ObserverAddParams params, ResponseHandlerParams handlerBuilder) throws InternalInvalidObserverException, InternalUriException, InternalInvalidHandlerNameException {
    SourceCallback<InputStream, CoAPResponseAttributes> handler = getHandler(handlerBuilder.getResponseHandler());
    CoapRequestBuilderImpl requestBuilder = new CoapRequestBuilderImpl(params);
    URI uri = requestBuilder.buildResourceUri();
    ObserveRelation relation = getRelation(uri);
    if (relation != null) {
        // only one observe relation allowed per uri
        // TODO proactive or not, configurable?
        relation.stop();
        removeRelation(uri);
    }
    relation = new ObserveRelation("CoAP Observer { " + getClientName() + "::" + uri + " }", coapClient, requestBuilder, (requestUri, requestCode, response) -> this.processMuleFlow(requestUri, requestCode, response, handler));
    addRelation(uri, relation);
    relation.start();
}
Also used : ResponseCode(org.eclipse.californium.core.coap.CoAP.ResponseCode) Proxy(nl.teslanet.mule.connectors.coap.api.Proxy) DefaultResponseAttributes(nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes) RemoteEndpointConfig(nl.teslanet.mule.connectors.coap.api.RemoteEndpointConfig) AbstractAddressParams(nl.teslanet.mule.connectors.coap.api.AbstractAddressParams) ConnectorException(org.eclipse.californium.elements.exception.ConnectorException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) LoggerFactory(org.slf4j.LoggerFactory) CoAPRequestCode(nl.teslanet.mule.connectors.coap.api.CoAPRequestCode) InternalClientErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalClientErrorResponseException) CoapResponse(org.eclipse.californium.core.CoapResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) LinkFormat(org.eclipse.californium.core.coap.LinkFormat) Map(java.util.Map) InternalInvalidObserverException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidObserverException) URI(java.net.URI) Endpoint(nl.teslanet.mule.connectors.coap.api.config.endpoint.Endpoint) RequestOptions(nl.teslanet.mule.connectors.coap.api.options.RequestOptions) ParameterDsl(org.mule.runtime.extension.api.annotation.dsl.xml.ParameterDsl) Startable(org.mule.runtime.api.lifecycle.Startable) ObserverExistsParams(nl.teslanet.mule.connectors.coap.api.ObserverExistsParams) PingParams(nl.teslanet.mule.connectors.coap.api.PingParams) AbstractEndpoint(nl.teslanet.mule.connectors.coap.api.config.endpoint.AbstractEndpoint) MessageUtils(nl.teslanet.mule.connectors.coap.internal.utils.MessageUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Token(org.eclipse.californium.core.coap.Token) Set(java.util.Set) InternalInvalidByteArrayValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidByteArrayValueException) AbstractQueryParams(nl.teslanet.mule.connectors.coap.api.AbstractQueryParams) RequestParams(nl.teslanet.mule.connectors.coap.api.RequestParams) Request(org.eclipse.californium.core.coap.Request) WebLink(org.eclipse.californium.core.WebLink) Sources(org.mule.runtime.extension.api.annotation.Sources) AbstractResourceParams(nl.teslanet.mule.connectors.coap.api.AbstractResourceParams) AbstractResourceRequestParams(nl.teslanet.mule.connectors.coap.api.AbstractResourceRequestParams) InternalRequestException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalRequestException) MediaType(org.mule.runtime.api.metadata.MediaType) Expression(org.mule.runtime.extension.api.annotation.Expression) InternalEndpointException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException) Summary(org.mule.runtime.extension.api.annotation.param.display.Summary) CoapClient(org.eclipse.californium.core.CoapClient) Operations(org.mule.runtime.extension.api.annotation.Operations) ObserverRemoveParams(nl.teslanet.mule.connectors.coap.api.ObserverRemoveParams) InternalUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriException) ProxyConfig(nl.teslanet.mule.connectors.coap.api.ProxyConfig) InternalInvalidRequestCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidRequestCodeException) Configuration(org.mule.runtime.extension.api.annotation.Configuration) InternalInvalidHandlerNameException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidHandlerNameException) Inject(javax.inject.Inject) Placement(org.mule.runtime.extension.api.annotation.param.display.Placement) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException) MuleException(org.mule.runtime.api.exception.MuleException) CoAPRequestType(nl.teslanet.mule.connectors.coap.api.CoAPRequestType) RemoteEndpoint(nl.teslanet.mule.connectors.coap.api.RemoteEndpoint) InternalInvalidResponseCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException) DefaultResponseOptionsAttributes(nl.teslanet.mule.connectors.coap.internal.options.DefaultResponseOptionsAttributes) MediaTypeRegistry(org.eclipse.californium.core.coap.MediaTypeRegistry) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) EndpointConstructionException(nl.teslanet.mule.connectors.coap.internal.exceptions.EndpointConstructionException) InternalNoResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalNoResponseException) Optional(org.mule.runtime.extension.api.annotation.param.Optional) Parameter(org.mule.runtime.extension.api.annotation.param.Parameter) ParameterGroup(org.mule.runtime.extension.api.annotation.param.ParameterGroup) Type(org.eclipse.californium.core.coap.CoAP.Type) Disposable(org.mule.runtime.api.lifecycle.Disposable) SchedulerConfig(org.mule.runtime.api.scheduler.SchedulerConfig) OperationalEndpoint(nl.teslanet.mule.connectors.coap.internal.OperationalEndpoint) TransformationService(org.mule.runtime.api.transformation.TransformationService) ObserverAddParams(nl.teslanet.mule.connectors.coap.api.ObserverAddParams) SourceCallback(org.mule.runtime.extension.api.runtime.source.SourceCallback) Logger(org.slf4j.Logger) SourceCallbackContext(org.mule.runtime.extension.api.runtime.source.SourceCallbackContext) ExpressionSupport(org.mule.runtime.api.meta.ExpressionSupport) ResponseHandlerParams(nl.teslanet.mule.connectors.coap.api.ResponseHandlerParams) MediaTypeMediator(nl.teslanet.mule.connectors.coap.internal.options.MediaTypeMediator) IOException(java.io.IOException) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) InternalUnexpectedResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUnexpectedResponseException) Result(org.mule.runtime.extension.api.runtime.operation.Result) InternalResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException) TypedValue(org.mule.runtime.api.metadata.TypedValue) Code(org.eclipse.californium.core.coap.CoAP.Code) InternalServerErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalServerErrorResponseException) CoapHandler(org.eclipse.californium.core.CoapHandler) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) RefName(org.mule.runtime.extension.api.annotation.param.RefName) CoAPConnector(nl.teslanet.mule.connectors.coap.internal.CoAPConnector) AttributeUtils(nl.teslanet.mule.connectors.coap.internal.attributes.AttributeUtils) DiscoverParams(nl.teslanet.mule.connectors.coap.api.DiscoverParams) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) URI(java.net.URI)

Example 3 with CoAPResponseAttributes

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

the class ObserveTest method testPermanentObserve.

/**
 * Test permanent observe
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testPermanentObserve() throws Exception {
    final MuleEventSpy spy = new MuleEventSpy("permanent");
    spy.clear();
    // let asynchronous work happen
    pauze();
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        Event result = flowRunner("do_put_permanent").withPayload(contents.get(i)).run();
        Message response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await().atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size();
    });
    int obsOffset = 0;
    for (int i = 0; i < spy.getEvents().size(); i++) {
        Message response = (Message) spy.getEvents().get(i).getContent();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        if (i == 0)
            obsOffset = attributes.getOptions().getObserve().intValue();
        assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload());
        assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
        assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
        assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 4 with CoAPResponseAttributes

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

the class ObserveTest method testTemporaryObserve.

/**
 * Test temporary observe
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testTemporaryObserve() throws Exception {
    Event result;
    Message response;
    MuleEventSpy spy = new MuleEventSpy("temporary");
    spy.clear();
    // let asynchronous work happen
    pauze();
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
    result = flowRunner("start_temporary").withPayload("nothing_important").run();
    response = result.getMessage();
    await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size();
    });
    result = flowRunner("stop_temporary").withPayload("nothing_important").run();
    response = result.getMessage();
    await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    int obsOffset = 0;
    for (int i = 1; i < contents.size(); i++) {
        response = (Message) spy.getEvents().get(i).getContent();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        if (i == 1)
            obsOffset = attributes.getOptions().getObserve().intValue() - 1;
        assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
        assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
        assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
        assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Example 5 with CoAPResponseAttributes

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

the class ObserveTest method testTemporaryObserveNon.

/**
 * Test temporary observe using NON requests
 * @throws Exception should not happen in this test
 */
@Test(timeout = 100000L)
public void testTemporaryObserveNon() throws Exception {
    Event result;
    Message response;
    MuleEventSpy spy = new MuleEventSpy("temporary");
    spy.clear();
    // let asynchronous work happen
    pauze();
    assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
    result = flowRunner("start_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
    response = result.getMessage();
    await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size();
    });
    result = flowRunner("stop_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
    response = result.getMessage();
    await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    for (int i = 1; i < contents.size(); i++) {
        pauze();
        result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
        response = result.getMessage();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
    }
    await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
        return spy.getEvents().size() == contents.size() + 1;
    });
    int obsOffset = 0;
    for (int i = 1; i < contents.size(); i++) {
        response = (Message) spy.getEvents().get(i).getContent();
        assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
        CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
        if (i == 1)
            obsOffset = attributes.getOptions().getObserve().intValue() - 1;
        assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
        assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
        assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
        assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
    }
}
Also used : Message(org.mule.runtime.api.message.Message) Event(org.mule.runtime.api.event.Event) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) MuleEventSpy(nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy) Test(org.junit.Test)

Aggregations

CoAPResponseAttributes (nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes)51 Test (org.junit.Test)46 Message (org.mule.runtime.api.message.Message)35 MuleEventSpy (nl.teslanet.mule.connectors.coap.test.utils.MuleEventSpy)29 Event (org.mule.runtime.api.event.Event)26 CursorStreamProvider (org.mule.runtime.api.streaming.bytes.CursorStreamProvider)16 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)12 LinkedList (java.util.LinkedList)10 AbstractQueryParam (nl.teslanet.mule.connectors.coap.api.query.AbstractQueryParam)10 QueryParam (nl.teslanet.mule.connectors.coap.api.query.QueryParam)10 InMemoryCursorStreamProvider (org.mule.runtime.core.api.streaming.bytes.InMemoryCursorStreamProvider)10 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 InternalInvalidOptionValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)4 UniqueObject (nl.teslanet.mule.connectors.coap.test.utils.UniqueObject)3 OptionSet (org.eclipse.californium.core.coap.OptionSet)3 IOException (java.io.IOException)2 SocketException (java.net.SocketException)2 URI (java.net.URI)2 Set (java.util.Set)2