Search in sources :

Example 1 with BasicPluginRestMsg

use of org.thingsboard.server.extensions.api.plugins.rest.BasicPluginRestMsg in project thingsboard by thingsboard.

the class PluginApiController method processRequest.

@SuppressWarnings("rawtypes")
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/{pluginToken}/**")
@ResponseStatus(value = HttpStatus.OK)
public DeferredResult<ResponseEntity> processRequest(@PathVariable("pluginToken") String pluginToken, RequestEntity<byte[]> requestEntity, HttpServletRequest request) throws ThingsboardException {
    log.debug("[{}] Going to process requst uri: {}", pluginToken, requestEntity.getUrl());
    DeferredResult<ResponseEntity> result = new DeferredResult<ResponseEntity>();
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(pluginToken);
    if (pluginMd == null) {
        result.setErrorResult(new PluginNotFoundException("Plugin with token: " + pluginToken + " not found!"));
    } else {
        TenantId tenantId = getCurrentUser().getTenantId();
        CustomerId customerId = getCurrentUser().getCustomerId();
        if (validatePluginAccess(pluginMd, tenantId, customerId)) {
            if (tenantId != null && ModelConstants.NULL_UUID.equals(tenantId.getId())) {
                tenantId = null;
            }
            UserId userId = getCurrentUser().getId();
            String userName = getCurrentUser().getName();
            PluginApiCallSecurityContext securityCtx = new PluginApiCallSecurityContext(pluginMd.getTenantId(), pluginMd.getId(), tenantId, customerId, userId, userName);
            actorService.process(new BasicPluginRestMsg(securityCtx, new RestRequest(requestEntity, request), result));
        } else {
            result.setResult(new ResponseEntity<>(HttpStatus.FORBIDDEN));
        }
    }
    return result;
}
Also used : BasicPluginRestMsg(org.thingsboard.server.extensions.api.plugins.rest.BasicPluginRestMsg) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) CustomerId(org.thingsboard.server.common.data.id.CustomerId) PluginApiCallSecurityContext(org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext) ResponseEntity(org.springframework.http.ResponseEntity) TenantId(org.thingsboard.server.common.data.id.TenantId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) UserId(org.thingsboard.server.common.data.id.UserId) DeferredResult(org.springframework.web.context.request.async.DeferredResult) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 DeferredResult (org.springframework.web.context.request.async.DeferredResult)1 CustomerId (org.thingsboard.server.common.data.id.CustomerId)1 TenantId (org.thingsboard.server.common.data.id.TenantId)1 UserId (org.thingsboard.server.common.data.id.UserId)1 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)1 PluginApiCallSecurityContext (org.thingsboard.server.extensions.api.plugins.PluginApiCallSecurityContext)1 BasicPluginRestMsg (org.thingsboard.server.extensions.api.plugins.rest.BasicPluginRestMsg)1 RestRequest (org.thingsboard.server.extensions.api.plugins.rest.RestRequest)1