Search in sources :

Example 1 with DefaultResponseAttributes

use of nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes in project mule-coap-connector by teslanet-nl.

the class Client method createReceivedResponseAttributes.

/**
 * Create response attributes that describe a response that is received.
 * @param request the request that caused the response.
 * @param response the response that is received from the server.
 * @return the created attributes derived from given request and response.
 * @throws InternalInvalidOptionValueException
 * @throws InternalInvalidResponseCodeException When responseCode is unknown.
 */
private DefaultResponseAttributes createReceivedResponseAttributes(String requestUri, CoAPRequestCode requestCode, CoapResponse response) throws InternalInvalidOptionValueException, InternalInvalidResponseCodeException {
    DefaultResponseAttributes attributes = new DefaultResponseAttributes();
    attributes.setRequestCode(requestCode.name());
    attributes.setLocalAddress(operationalEndpoint.getCoapEndpoint().getAddress().toString());
    attributes.setRequestUri(requestUri);
    if (response == null) {
        attributes.setSuccess(false);
    } else {
        attributes.setSuccess(response.isSuccess());
        attributes.setResponseCode(AttributeUtils.toResponseCodeAttribute(response.getCode()).name());
        attributes.setConfirmable(response.advanced().isConfirmable());
        attributes.setRemoteAddress(response.advanced().getSourceContext().getPeerAddress().toString());
        attributes.setNotification(response.advanced().isNotification());
        attributes.setOptions(new DefaultResponseOptionsAttributes(response.getOptions()));
        attributes.setLocationUri(MessageUtils.uriString(attributes.getOptions().getLocationPath(), attributes.getOptions().getLocationQuery()));
    }
    return attributes;
}
Also used : DefaultResponseOptionsAttributes(nl.teslanet.mule.connectors.coap.internal.options.DefaultResponseOptionsAttributes) DefaultResponseAttributes(nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes)

Example 2 with DefaultResponseAttributes

use of nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes 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 3 with DefaultResponseAttributes

use of nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes 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)

Aggregations

DefaultResponseAttributes (nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 CoAPResponseAttributes (nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes)2 InternalInvalidOptionValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)2 InternalInvalidResponseCodeException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException)2 InternalResponseException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException)2 IOException (java.io.IOException)1 InternalEndpointException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException)1 DefaultResponseOptionsAttributes (nl.teslanet.mule.connectors.coap.internal.options.DefaultResponseOptionsAttributes)1 CoapResponse (org.eclipse.californium.core.CoapResponse)1 ConnectorException (org.eclipse.californium.elements.exception.ConnectorException)1 SourceCallbackContext (org.mule.runtime.extension.api.runtime.source.SourceCallbackContext)1