use of org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity in project thingsboard by thingsboard.
the class TelemetryRestMsgHandler method handleError.
private void handleError(Exception e, PluginRestMsg msg, HttpStatus defaultErrorStatus) {
ResponseEntity responseEntity;
if (e != null && e instanceof ToErrorResponseEntity) {
responseEntity = ((ToErrorResponseEntity) e).toErrorResponseEntity();
} else if (e != null && e instanceof IllegalArgumentException) {
responseEntity = new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
} else {
responseEntity = new ResponseEntity<>(defaultErrorStatus);
}
msg.getResponseHolder().setResult(responseEntity);
}
use of org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity in project thingsboard by thingsboard.
the class RpcRestMsgHandler method handleDeviceRPCRequest.
private boolean handleDeviceRPCRequest(PluginContext ctx, final PluginRestMsg msg, TenantId tenantId, DeviceId deviceId, RpcRequest cmd, boolean oneWay) throws JsonProcessingException {
long timeout = System.currentTimeMillis() + (cmd.getTimeout() != null ? cmd.getTimeout() : defaultTimeout);
ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(cmd.getMethodName(), cmd.getRequestData());
ctx.checkAccess(deviceId, new PluginCallback<Void>() {
@Override
public void onSuccess(PluginContext ctx, Void value) {
ToDeviceRpcRequest rpcRequest = new ToDeviceRpcRequest(UUID.randomUUID(), msg.getSecurityCtx(), tenantId, deviceId, oneWay, timeout, body);
rpcManager.process(ctx, new LocalRequestMetaData(rpcRequest, msg.getResponseHolder()));
}
@Override
public void onFailure(PluginContext ctx, Exception e) {
ResponseEntity response;
if (e instanceof ToErrorResponseEntity) {
response = ((ToErrorResponseEntity) e).toErrorResponseEntity();
} else {
response = new ResponseEntity(HttpStatus.UNAUTHORIZED);
}
ctx.logRpcRequest(msg.getSecurityCtx(), deviceId, body, oneWay, Optional.empty(), e);
msg.getResponseHolder().setResult(response);
}
});
return true;
}
Aggregations