Search in sources :

Example 1 with InternalInvalidOptionValueException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException 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 InternalInvalidOptionValueException

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

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException 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 4 with InternalInvalidOptionValueException

use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException 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 5 with InternalInvalidOptionValueException

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

the class MessageUtils method copyOptions.

/**
 * Copy options from {@link ResponseOptions} to {@link OptionSet}.
 * @param responseOptions to copy from
 * @param optionSet to copy to
 * @throws InternalInvalidOptionValueException
 * @throws InvalidETagException when an option containes invalid ETag value
 * @throws IOException when an option stream throws an error.
 */
public static void copyOptions(ResponseOptions responseOptions, OptionSet optionSet, TransformationService transformationService) throws InternalInvalidOptionValueException, IOException, InvalidETagException {
    if (responseOptions.getEtag() != null) {
        ETag etag = MessageUtils.toETag(responseOptions.getEtag(), transformationService);
        if (etag.isEmpty())
            throw new InternalInvalidOptionValueException("ETag", "empty etag is not valid");
        optionSet.addETag(etag.getValue());
    }
    if (responseOptions.getContentFormat() != null) {
        optionSet.setContentFormat(responseOptions.getContentFormat());
    }
    if (responseOptions.getMaxAge() != null) {
        optionSet.setMaxAge(responseOptions.getMaxAge());
    }
    if (responseOptions.getLocationPath() != null) {
        optionSet.setLocationPath(responseOptions.getLocationPath());
    }
    if (responseOptions.getLocationQuery() != null) {
        for (AbstractQueryParam param : responseOptions.getLocationQuery()) {
            optionSet.addLocationQuery(param.toString());
        }
    }
    if (responseOptions.getResponseSize() != null) {
        optionSet.setSize2(responseOptions.getResponseSize());
    }
    if (responseOptions.getAcceptableRequestSize() != null) {
        optionSet.setSize1(responseOptions.getAcceptableRequestSize());
    }
    for (OtherOption otherOption : responseOptions.getOtherResponseOptions()) {
        try {
            optionSet.addOption(new Option(otherOption.getNumber(), toBytes(otherOption.getValue(), transformationService)));
        } catch (InternalInvalidByteArrayValueException e) {
            throw new InternalInvalidOptionValueException("Other-Option", "Number { " + otherOption.getNumber() + " }", e);
        }
    }
}
Also used : OtherOption(nl.teslanet.mule.connectors.coap.api.options.OtherOption) ETag(nl.teslanet.mule.connectors.coap.api.options.ETag) InternalInvalidByteArrayValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidByteArrayValueException) AbstractQueryParam(nl.teslanet.mule.connectors.coap.api.query.AbstractQueryParam) BlockOption(org.eclipse.californium.core.coap.BlockOption) OtherOption(nl.teslanet.mule.connectors.coap.api.options.OtherOption) Option(org.eclipse.californium.core.coap.Option) InternalInvalidOptionValueException(nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)

Aggregations

InternalInvalidOptionValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidOptionValueException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 CoAPResponseAttributes (nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes)3 InternalInvalidResponseCodeException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidResponseCodeException)3 IOException (java.io.IOException)2 DefaultResponseAttributes (nl.teslanet.mule.connectors.coap.internal.attributes.DefaultResponseAttributes)2 InternalInvalidByteArrayValueException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalInvalidByteArrayValueException)2 InternalResponseException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResponseException)2 CoAPResponseCode (nl.teslanet.mule.connectors.coap.api.CoAPResponseCode)1 InvalidETagException (nl.teslanet.mule.connectors.coap.api.error.InvalidETagException)1 ETag (nl.teslanet.mule.connectors.coap.api.options.ETag)1 OtherOption (nl.teslanet.mule.connectors.coap.api.options.OtherOption)1 AbstractQueryParam (nl.teslanet.mule.connectors.coap.api.query.AbstractQueryParam)1 InternalEndpointException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException)1 InternalRequestException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalRequestException)1 InternalResourceUriException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalResourceUriException)1 InternalUriPatternException (nl.teslanet.mule.connectors.coap.internal.exceptions.InternalUriPatternException)1 CoapHandler (org.eclipse.californium.core.CoapHandler)1 CoapResponse (org.eclipse.californium.core.CoapResponse)1