use of nl.teslanet.mule.connectors.coap.internal.exceptions.InternalEndpointException 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;
}
Aggregations