Search in sources :

Example 1 with ToErrorResponseEntity

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);
}
Also used : ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)

Example 2 with ToErrorResponseEntity

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;
}
Also used : ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity) ResponseEntity(org.springframework.http.ResponseEntity) LocalRequestMetaData(org.thingsboard.server.extensions.core.plugin.rpc.LocalRequestMetaData) PluginContext(org.thingsboard.server.extensions.api.plugins.PluginContext) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) ToDeviceRpcRequestBody(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody) ServletException(javax.servlet.ServletException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ToErrorResponseEntity(org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)2 ToErrorResponseEntity (org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)1 ToDeviceRpcRequest (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest)1 ToDeviceRpcRequestBody (org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequestBody)1 LocalRequestMetaData (org.thingsboard.server.extensions.core.plugin.rpc.LocalRequestMetaData)1