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