Search in sources :

Example 6 with TbResource

use of org.thingsboard.server.common.data.TbResource 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 7 with TbResource

use of org.thingsboard.server.common.data.TbResource 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 8 with TbResource

use of org.thingsboard.server.common.data.TbResource 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 9 with TbResource

use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.

the class BaseController method checkResourceId.

TbResource checkResourceId(TbResourceId resourceId, Operation operation) throws ThingsboardException {
    try {
        validateId(resourceId, "Incorrect resourceId " + resourceId);
        TbResource resource = resourceService.findResourceById(getCurrentUser().getTenantId(), resourceId);
        checkNotNull(resource, "Resource with id [" + resourceId + "] is not found");
        accessControlService.checkPermission(getCurrentUser(), Resource.TB_RESOURCE, operation, resourceId, resource);
        return resource;
    } catch (Exception e) {
        throw handleException(e, false);
    }
}
Also used : TbResource(org.thingsboard.server.common.data.TbResource) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) MessagingException(javax.mail.MessagingException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 10 with TbResource

use of org.thingsboard.server.common.data.TbResource in project thingsboard by thingsboard.

the class BaseTbResourceControllerTest method testFindSystemTbResources.

@Test
public void testFindSystemTbResources() throws Exception {
    loginSysAdmin();
    List<TbResourceInfo> resources = new ArrayList<>();
    for (int i = 0; i < 173; i++) {
        TbResource resource = new TbResource();
        resource.setTitle("Resource" + i);
        resource.setResourceType(ResourceType.JKS);
        resource.setFileName(i + DEFAULT_FILE_NAME);
        resource.setData("Test Data");
        resources.add(new TbResourceInfo(save(resource)));
    }
    List<TbResourceInfo> loadedResources = new ArrayList<>();
    PageLink pageLink = new PageLink(24);
    PageData<TbResourceInfo> pageData;
    do {
        pageData = doGetTypedWithPageLink("/api/resource?", new TypeReference<PageData<TbResourceInfo>>() {
        }, pageLink);
        loadedResources.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(resources, idComparator);
    Collections.sort(loadedResources, idComparator);
    Assert.assertEquals(resources, loadedResources);
    for (TbResourceInfo resource : resources) {
        doDelete("/api/resource/" + resource.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(27);
    loadedResources.clear();
    do {
        pageData = doGetTypedWithPageLink("/api/resource?", new TypeReference<PageData<TbResourceInfo>>() {
        }, pageLink);
        loadedResources.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Assert.assertTrue(loadedResources.isEmpty());
}
Also used : TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TbResource(org.thingsboard.server.common.data.TbResource) Test(org.junit.Test)

Aggregations

TbResource (org.thingsboard.server.common.data.TbResource)29 Test (org.junit.Test)19 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)11 DaoSqlTest (org.thingsboard.server.dao.service.DaoSqlTest)11 ArrayList (java.util.ArrayList)5 TbResourceInfo (org.thingsboard.server.common.data.TbResourceInfo)5 PageLink (org.thingsboard.server.common.data.page.PageLink)5 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 TbResourceId (org.thingsboard.server.common.data.id.TbResourceId)3 TenantId (org.thingsboard.server.common.data.id.TenantId)3 UUID (java.util.UUID)2 Tenant (org.thingsboard.server.common.data.Tenant)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteString (com.google.protobuf.ByteString)1 MessagingException (javax.mail.MessagingException)1