Search in sources :

Example 1 with InternalResponseException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException in project mule-coap-connector by teslanet-nl.

the class Client method processMuleFlow.

/**
 * Passes asynchronous response to the muleflow.
 * @param requestUri The Uri of the request that caused the response.
 * @param requestCode The code of the request that caused the response.
 * @param response The coap response to process.
 * @param callback The callback method of the muleflow.
 * @throws InternalResponseException When the received CoAP response contains values that cannot be processed.
 */
void processMuleFlow(String requestUri, CoAPRequestCode requestCode, CoapResponse response, SourceCallback<InputStream, CoAPResponseAttributes> callback) throws InternalResponseException {
    DefaultResponseAttributes responseAttributes;
    try {
        responseAttributes = createReceivedResponseAttributes(requestUri, requestCode, response);
    } catch (InternalInvalidOptionValueException | InternalInvalidResponseCodeException e) {
        throw new InternalResponseException("cannot proces received response", e);
    }
    SourceCallbackContext requestcontext = callback.createContext();
    // not needed yet: requestcontext.addVariable( "CoapExchange", exchange );
    if (response != null) {
        byte[] responsePayload = response.getPayload();
        if (responsePayload != null) {
            callback.handle(Result.<InputStream, CoAPResponseAttributes>builder().output(new ByteArrayInputStream(responsePayload)).attributes(responseAttributes).mediaType(MediaTypeMediator.toMediaType(response.getOptions().getContentFormat())).build(), requestcontext);
        } else {
            callback.handle(Result.<InputStream, CoAPResponseAttributes>builder().attributes(responseAttributes).mediaType(MediaTypeMediator.toMediaType(response.getOptions().getContentFormat())).build(), requestcontext);
        }
    } else {
        callback.handle(Result.<InputStream, CoAPResponseAttributes>builder().attributes(responseAttributes).mediaType(MediaType.ANY).build(), requestcontext);
    }
}
Also used : InternalInvalidResponseCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException) ByteArrayInputStream(java.io.ByteArrayInputStream) SourceCallbackContext(org.mule.runtime.extension.api.runtime.source.SourceCallbackContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) InternalResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException) DefaultResponseAttributes(nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)

Example 2 with InternalResponseException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException in project mule-coap-connector by teslanet-nl.

the class Client method createCoapHandler.

/**
 * Create a Handler of CoAP responses.
 * @param uri
 * @param client  The Coap client that produced the response
 * @param callback The Listening Messageprocessor that needs to be called
 * @param requestCode The coap request code from the request context
 * @return
 */
private CoapHandler createCoapHandler(final String handlerName, final String requestUri, final CoAPRequestCode requestCode, final SourceCallback<InputStream, CoAPResponseAttributes> callback) {
    final Client thisClient = this;
    final String handlerDescription = "Handler { " + thisClient.getClientName() + "::" + handlerName + " } ";
    return new CoapHandler() {

        /**
         * Callback for errors that occur on an asynchronous request or response.
         */
        @Override
        public void onError() {
            try {
                thisClient.processMuleFlow(requestUri, requestCode, null, callback);
            } catch (InternalResponseException e) {
                // this should never happen
                logger.error(handlerDescription + "cannot proces an error on asynchronous request or response", e);
            }
        }

        /**
         * Callback for delivering an asynchronous response.
         * @param response The response received.
         */
        @Override
        public void onLoad(CoapResponse response) {
            try {
                thisClient.processMuleFlow(requestUri, requestCode, response, callback);
            } catch (InternalResponseException e) {
                logger.error(handlerDescription + "cannot proces an asynchronous response", e);
                try {
                    thisClient.processMuleFlow(requestUri, requestCode, null, callback);
                } catch (InternalResponseException e1) {
                    // this should never happen
                    logger.error(handlerDescription + "cannot proces an error on asynchronous response", e);
                }
            }
        }
    };
}
Also used : CoapResponse(org.eclipse.californium.core.CoapResponse) CoapHandler(org.eclipse.californium.core.CoapHandler) InternalResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException) CoapClient(org.eclipse.californium.core.CoapClient)

Example 3 with InternalResponseException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException in project mule-coap-connector by teslanet-nl.

the class Client method executeSynchronous.

/**
 * Execute the request synchronously.
 * @param request The request to execute.
 * @return The result.
 * @throws InternalEndpointException The Endpoint cannot execute the request.
 * @throws InternalResponseException  The response could not be interpreted.
 * @throws InternalServerErrorResponseException The response received indicates server error.
 * @throws InternalInvalidResponseCodeException The response contained an invalid response code.
 * @throws InternalClientErrorResponseException The response received indicates client error.
 * @throws InternalNoResponseException No response was received within exchange lifetime.
 */
private Result<InputStream, CoAPResponseAttributes> executeSynchronous(CoAPRequestCode code, Request request) throws InternalEndpointException, InternalNoResponseException, InternalClientErrorResponseException, InternalInvalidResponseCodeException, InternalServerErrorResponseException, InternalResponseException {
    CoapResponse response;
    try {
        response = coapClient.advanced(request);
    } catch (ConnectorException | IOException e) {
        throw new InternalEndpointException("CoAP request failed", e);
    }
    throwExceptionWhenNeeded(throwExceptionOnErrorResponse, response);
    DefaultResponseAttributes responseAttributes;
    try {
        responseAttributes = createReceivedResponseAttributes(request.getURI(), code, response);
    } catch (InternalInvalidOptionValueException | InternalInvalidResponseCodeException e) {
        throw new InternalResponseException("CoAP response cannot be processed", e);
    }
    Result<InputStream, CoAPResponseAttributes> result;
    if (response == null) {
        result = Result.<InputStream, CoAPResponseAttributes>builder().output(null).attributes(responseAttributes).mediaType(MediaType.ANY).build();
    } else {
        byte[] payload = response.getPayload();
        if (payload != null) {
            result = Result.<InputStream, CoAPResponseAttributes>builder().output(new ByteArrayInputStream(response.getPayload())).length(payload.length).attributes(responseAttributes).mediaType(MediaTypeMediator.toMediaType(response.getOptions().getContentFormat())).build();
        } else {
            result = Result.<InputStream, CoAPResponseAttributes>builder().output(null).attributes(responseAttributes).mediaType(MediaTypeMediator.toMediaType(response.getOptions().getContentFormat())).build();
        }
    }
    return result;
}
Also used : InternalEndpointException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException) CoapResponse(org.eclipse.californium.core.CoapResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) InternalResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException) InternalInvalidResponseCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConnectorException(org.eclipse.californium.elements.exception.ConnectorException) CoAPResponseAttributes(nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes) DefaultResponseAttributes(nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)

Example 4 with InternalResponseException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException in project mule-coap-connector by teslanet-nl.

the class ClientOperations method discover.

/**
 * The Discover processor retrieves information about CoAP resources of a
 * server.
 *
 * @param client         The client to use to issue the request.
 * @param discoverParams The attributes of the discover request
 * @return The description of resources on the server that have been discovered.
 */
@Throws({ DiscoverErrorProvider.class })
public Set<DiscoveredResource> discover(@Config Client client, @ParameterGroup(name = "Discover address") DiscoverParams discoverParams) {
    Set<WebLink> links = null;
    try {
        links = client.discover(discoverParams);
    } catch (IOException | ConnectorException e) {
        throw new EndpointException(client + discoverErrorMsg, e);
    } catch (InternalUriException e) {
        throw new UriException(client + discoverErrorMsg, e);
    } catch (InternalUnexpectedResponseException | InternalInvalidResponseCodeException | InternalResponseException e) {
        throw new ResponseException(client + discoverErrorMsg, e);
    } catch (InternalNoResponseException e) {
        throw new NoResponseException(client + discoverErrorMsg, e);
    } catch (InternalClientErrorResponseException e) {
        throw new ResponseException(client + discoverErrorMsg, e);
    } catch (InternalServerErrorResponseException e) {
        throw new ServerErrorResponseException(client + discoverErrorMsg, e);
    } catch (InternalInvalidRequestCodeException e) {
        throw new InvalidRequestCodeException(client + discoverErrorMsg, e);
    } catch (InternalRequestException e) {
        throw new RequestException(client + discoverErrorMsg, e);
    }
    TreeSet<DiscoveredResource> resultSet = new TreeSet<DiscoveredResource>();
    for (WebLink link : links) {
        resultSet.add(new DiscoveredResource(link.getURI(), link.getAttributes().hasObservable(), link.getAttributes().getTitle(), link.getAttributes().getInterfaceDescriptions(), link.getAttributes().getResourceTypes(), link.getAttributes().getMaximumSizeEstimate(), link.getAttributes().getContentTypes()));
    }
    return Collections.unmodifiableSortedSet(resultSet);
}
Also used : InternalClientErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalClientErrorResponseException) InternalClientErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalClientErrorResponseException) ClientErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ClientErrorResponseException) NoResponseException(nl.teslanet.mule.connectors.coap.api.error.NoResponseException) ResponseException(nl.teslanet.mule.connectors.coap.api.error.ResponseException) ServerErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ServerErrorResponseException) InternalNoResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalNoResponseException) InternalUnexpectedResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUnexpectedResponseException) InternalResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException) InternalServerErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalServerErrorResponseException) InternalInvalidRequestCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidRequestCodeException) InternalServerErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalServerErrorResponseException) RequestException(nl.teslanet.mule.connectors.coap.api.error.RequestException) InternalRequestException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalRequestException) ServerErrorResponseException(nl.teslanet.mule.connectors.coap.api.error.ServerErrorResponseException) InternalServerErrorResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalServerErrorResponseException) DiscoveredResource(nl.teslanet.mule.connectors.coap.api.DiscoveredResource) InternalInvalidResponseCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException) InternalUnexpectedResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUnexpectedResponseException) TreeSet(java.util.TreeSet) UriException(nl.teslanet.mule.connectors.coap.api.error.UriException) InternalUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriException) InternalRequestException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalRequestException) IOException(java.io.IOException) InternalResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException) InternalNoResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalNoResponseException) WebLink(org.eclipse.californium.core.WebLink) InternalEndpointException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException) EndpointException(nl.teslanet.mule.connectors.coap.api.error.EndpointException) ConnectorException(org.eclipse.californium.elements.exception.ConnectorException) NoResponseException(nl.teslanet.mule.connectors.coap.api.error.NoResponseException) InternalNoResponseException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalNoResponseException) InternalUriException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriException) InternalInvalidRequestCodeException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidRequestCodeException) InvalidRequestCodeException(nl.teslanet.mule.connectors.coap.api.error.InvalidRequestCodeException) Throws(org.mule.runtime.extension.api.annotation.error.Throws)

Aggregations

InternalResponseException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException)4 InternalInvalidResponseCodeException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 CoAPResponseAttributes (nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes)2 DefaultResponseAttributes (nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes)2 InternalEndpointException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException)2 InternalInvalidOptionValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)2 CoapResponse (org.eclipse.californium.core.CoapResponse)2 ConnectorException (org.eclipse.californium.elements.exception.ConnectorException)2 TreeSet (java.util.TreeSet)1 DiscoveredResource (nl.teslanet.mule.connectors.coap.api.DiscoveredResource)1 ClientErrorResponseException (nl.teslanet.mule.connectors.coap.api.error.ClientErrorResponseException)1 EndpointException (nl.teslanet.mule.connectors.coap.api.error.EndpointException)1 InvalidRequestCodeException (nl.teslanet.mule.connectors.coap.api.error.InvalidRequestCodeException)1 NoResponseException (nl.teslanet.mule.connectors.coap.api.error.NoResponseException)1 RequestException (nl.teslanet.mule.connectors.coap.api.error.RequestException)1 ResponseException (nl.teslanet.mule.connectors.coap.api.error.ResponseException)1 ServerErrorResponseException (nl.teslanet.mule.connectors.coap.api.error.ServerErrorResponseException)1