Search in sources :

Example 1 with TbResourceId

use of org.thingsboard.server.common.data.id.TbResourceId in project thingsboard by thingsboard.

the class TbResourceEntity method toData.

@Override
public TbResource toData() {
    TbResource resource = new TbResource(new TbResourceId(id));
    resource.setCreatedTime(createdTime);
    resource.setTenantId(TenantId.fromUUID(tenantId));
    resource.setTitle(title);
    resource.setResourceType(ResourceType.valueOf(resourceType));
    resource.setResourceKey(resourceKey);
    resource.setSearchText(searchText);
    resource.setFileName(fileName);
    resource.setData(data);
    return resource;
}
Also used : TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) TbResource(org.thingsboard.server.common.data.TbResource)

Example 2 with TbResourceId

use of org.thingsboard.server.common.data.id.TbResourceId in project thingsboard by thingsboard.

the class TbResourceController method downloadResource.

@ApiOperation(value = "Download Resource (downloadResource)", notes = "Download Resource based on the provided Resource Id." + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/resource/{resourceId}/download", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<org.springframework.core.io.Resource> downloadResource(@ApiParam(value = RESOURCE_ID_PARAM_DESCRIPTION) @PathVariable(RESOURCE_ID) String strResourceId) throws ThingsboardException {
    checkParameter(RESOURCE_ID, strResourceId);
    try {
        TbResourceId resourceId = new TbResourceId(toUUID(strResourceId));
        TbResource tbResource = checkResourceId(resourceId, Operation.READ);
        ByteArrayResource resource = new ByteArrayResource(Base64.getDecoder().decode(tbResource.getData().getBytes()));
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + tbResource.getFileName()).header("x-filename", tbResource.getFileName()).contentLength(resource.contentLength()).contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) ByteArrayResource(org.springframework.core.io.ByteArrayResource) TbResource(org.thingsboard.server.common.data.TbResource) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with TbResourceId

use of org.thingsboard.server.common.data.id.TbResourceId in project thingsboard by thingsboard.

the class TbResourceController method deleteResource.

@ApiOperation(value = "Delete Resource (deleteResource)", notes = "Deletes the Resource. Referencing non-existing Resource Id will cause an error." + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/resource/{resourceId}", method = RequestMethod.DELETE)
@ResponseBody
public void deleteResource(@ApiParam(value = RESOURCE_ID_PARAM_DESCRIPTION) @PathVariable("resourceId") String strResourceId) throws ThingsboardException {
    checkParameter(RESOURCE_ID, strResourceId);
    try {
        TbResourceId resourceId = new TbResourceId(toUUID(strResourceId));
        TbResource tbResource = checkResourceId(resourceId, Operation.DELETE);
        resourceService.deleteResource(getTenantId(), resourceId);
        tbClusterService.onResourceDeleted(tbResource, null);
        logEntityAction(resourceId, tbResource, null, ActionType.DELETED, null, strResourceId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.TB_RESOURCE), null, null, ActionType.DELETED, e, strResourceId);
        throw handleException(e);
    }
}
Also used : TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) TbResource(org.thingsboard.server.common.data.TbResource) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with TbResourceId

use of org.thingsboard.server.common.data.id.TbResourceId in project thingsboard by thingsboard.

the class AccessValidator method validateResource.

private void validateResource(SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
    ListenableFuture<TbResourceInfo> resourceFuture = resourceService.findResourceInfoByIdAsync(currentUser.getTenantId(), new TbResourceId(entityId.getId()));
    Futures.addCallback(resourceFuture, getCallback(callback, resource -> {
        if (resource == null) {
            return ValidationResult.entityNotFound("Resource with requested id wasn't found!");
        } else {
            try {
                accessControlService.checkPermission(currentUser, Resource.TB_RESOURCE, operation, entityId, resource);
            } catch (ThingsboardException e) {
                return ValidationResult.accessDenied(e.getMessage());
            }
            return ValidationResult.ok(resource);
        }
    }), executor);
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) HttpValidationCallback(org.thingsboard.server.controller.HttpValidationCallback) Customer(org.thingsboard.server.common.data.Customer) Autowired(org.springframework.beans.factory.annotation.Autowired) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) ToErrorResponseEntity(org.thingsboard.server.service.telemetry.exception.ToErrorResponseEntity) PreDestroy(javax.annotation.PreDestroy) TenantService(org.thingsboard.server.dao.tenant.TenantService) Rpc(org.thingsboard.server.common.data.rpc.Rpc) ResourceService(org.thingsboard.server.dao.resource.ResourceService) RpcService(org.thingsboard.server.dao.rpc.RpcService) ApiUsageStateService(org.thingsboard.server.dao.usagerecord.ApiUsageStateService) EntityViewService(org.thingsboard.server.dao.entityview.EntityViewService) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) EdgeId(org.thingsboard.server.common.data.id.EdgeId) AssetService(org.thingsboard.server.dao.asset.AssetService) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) EdgeService(org.thingsboard.server.dao.edge.EdgeService) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) ApiUsageStateId(org.thingsboard.server.common.data.id.ApiUsageStateId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) Executors(java.util.concurrent.Executors) EntityView(org.thingsboard.server.common.data.EntityView) PostConstruct(javax.annotation.PostConstruct) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Operation(org.thingsboard.server.service.security.permission.Operation) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) OtaPackageService(org.thingsboard.server.dao.ota.OtaPackageService) UserService(org.thingsboard.server.dao.user.UserService) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AssetId(org.thingsboard.server.common.data.id.AssetId) Tenant(org.thingsboard.server.common.data.Tenant) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) User(org.thingsboard.server.common.data.User) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) DeviceService(org.thingsboard.server.dao.device.DeviceService) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) CustomerService(org.thingsboard.server.dao.customer.CustomerService) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) RpcId(org.thingsboard.server.common.data.id.RpcId) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) FutureCallback(com.google.common.util.concurrent.FutureCallback) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) DeviceProfileService(org.thingsboard.server.dao.device.DeviceProfileService) HttpStatus(org.springframework.http.HttpStatus) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) UserId(org.thingsboard.server.common.data.id.UserId) TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Resource(org.thingsboard.server.service.security.permission.Resource) ResponseEntity(org.springframework.http.ResponseEntity) Asset(org.thingsboard.server.common.data.asset.Asset) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException)

Example 5 with TbResourceId

use of org.thingsboard.server.common.data.id.TbResourceId in project thingsboard by thingsboard.

the class TbResourceInfoEntity method toData.

@Override
public TbResourceInfo toData() {
    TbResourceInfo resource = new TbResourceInfo(new TbResourceId(id));
    resource.setCreatedTime(createdTime);
    resource.setTenantId(TenantId.fromUUID(tenantId));
    resource.setTitle(title);
    resource.setResourceType(ResourceType.valueOf(resourceType));
    resource.setResourceKey(resourceKey);
    resource.setSearchText(searchText);
    return resource;
}
Also used : TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId)

Aggregations

TbResourceId (org.thingsboard.server.common.data.id.TbResourceId)5 TbResource (org.thingsboard.server.common.data.TbResource)3 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)3 ApiOperation (io.swagger.annotations.ApiOperation)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 TbResourceInfo (org.thingsboard.server.common.data.TbResourceInfo)2 Function (com.google.common.base.Function)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 BiConsumer (java.util.function.BiConsumer)1 Nullable (javax.annotation.Nullable)1 PostConstruct (javax.annotation.PostConstruct)1 PreDestroy (javax.annotation.PreDestroy)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1