@ -26,7 +26,10 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType ;
import org.springframework.http.ResponseEntity ;
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.web.bind.annotation.DeleteMapping ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.RequestBody ;
import org.springframework.web.bind.annotation.RequestHeader ;
import org.springframework.web.bind.annotation.RequestMapping ;
@ -87,15 +90,15 @@ public class TbResourceController extends BaseController {
@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')" )
@Reques tMapping ( value = "/resource/{resourceId}/download" , method = RequestMethod . GET )
@Ge tMapping ( value = "/resource/{resourceId}/download" )
@ResponseBody
public ResponseEntity < org . springframework . core . io . Resource > downloadResource ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ) throws ThingsboardException {
public ResponseEntity < ByteArray Resource> downloadResource ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ) throws ThingsboardException {
checkParameter ( RESOURCE_ID , strResourceId ) ;
TbResourceId resourceId = new TbResourceId ( toUUID ( strResourceId ) ) ;
TbResource tbResource = checkResourceId ( resourceId , Operation . READ ) ;
ByteArrayResource resource = new ByteArrayResource ( Base64 . getDecoder ( ) . decode ( tbResource . getData ( ) . getBytes ( ) ) ) ;
ByteArrayResource resource = new ByteArrayResource ( tbResource . getData ( ) ) ;
return ResponseEntity . ok ( )
. header ( HttpHeaders . CONTENT_DISPOSITION , "attachment;filename=" + tbResource . getFileName ( ) )
. header ( "x-filename" , tbResource . getFileName ( ) )
@ -106,11 +109,11 @@ public class TbResourceController extends BaseController {
@ApiOperation ( value = "Download LWM2M Resource (downloadLwm2mResourceIfChanged)" , notes = DOWNLOAD_RESOURCE_IF_NOT_CHANGED + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource/lwm2m/{resourceId}/download" , method = RequestMethod . GET , produces = "application/xml" )
@Ge tMapping ( value = "/resource/lwm2m/{resourceId}/download" , produces = "application/xml" )
@ResponseBody
public ResponseEntity < org . springframework . core . io . Resource > downloadLwm2mResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
public ResponseEntity < ByteArray Resource> downloadLwm2mResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
return downloadResourceIfChanged ( ResourceType . LWM2M_MODEL , strResourceId , etag ) ;
}
@ -118,30 +121,30 @@ public class TbResourceController extends BaseController {
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@RequestMapping ( value = "/resource/pkcs12/{resourceId}/download" , method = RequestMethod . GET , produces = "application/x-pkcs12" )
@ResponseBody
public ResponseEntity < org . springframework . core . io . Resource > downloadPkcs12ResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
public ResponseEntity < ByteArray Resource> downloadPkcs12ResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
return downloadResourceIfChanged ( ResourceType . PKCS_12 , strResourceId , etag ) ;
}
@ApiOperation ( value = "Download JKS Resource (downloadJksResourceIfChanged)" ,
notes = DOWNLOAD_RESOURCE_IF_NOT_CHANGED + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource/jks/{resourceId}/download" , method = RequestMethod . GET , produces = "application/x-java-keystore" )
@Ge tMapping ( value = "/resource/jks/{resourceId}/download" , produces = "application/x-java-keystore" )
@ResponseBody
public ResponseEntity < org . springframework . core . io . Resource > downloadJksResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
public ResponseEntity < ByteArray Resource> downloadJksResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
return downloadResourceIfChanged ( ResourceType . JKS , strResourceId , etag ) ;
}
@ApiOperation ( value = "Download JS Resource (downloadJsResourceIfChanged)" , notes = DOWNLOAD_RESOURCE_IF_NOT_CHANGED + AVAILABLE_FOR_ANY_AUTHORIZED_USER )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@Reques tMapping ( value = "/resource/js/{resourceId}/download" , method = RequestMethod . GET , produces = "application/javascript" )
@Ge tMapping ( value = "/resource/js/{resourceId}/download" , produces = "application/javascript" )
@ResponseBody
public ResponseEntity < org . springframework . core . io . Resource > downloadJsResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
public ResponseEntity < ByteArray Resource> downloadJsResourceIfChanged ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ,
@RequestHeader ( name = HttpHeaders . IF_NONE_MATCH , required = false ) String etag ) throws ThingsboardException {
return downloadResourceIfChanged ( ResourceType . JS_MODULE , strResourceId , etag ) ;
}
@ -150,7 +153,7 @@ public class TbResourceController extends BaseController {
RESOURCE_INFO_DESCRIPTION + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH ,
produces = "application/json" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource/info/{resourceId}" , method = RequestMethod . GET )
@Ge tMapping ( value = "/resource/info/{resourceId}" )
@ResponseBody
public TbResourceInfo getResourceInfoById ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ) throws ThingsboardException {
@ -162,15 +165,18 @@ public class TbResourceController extends BaseController {
@ApiOperation ( value = "Get Resource (getResourceById)" ,
notes = "Fetch the Resource object based on the provided Resource Id. " +
RESOURCE_DESCRIPTION + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH ,
produces = "application/json" )
produces = "application/json" , hidden = true )
@Deprecated // resource's data should be fetched with a download request
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource/{resourceId}" , method = RequestMethod . GET )
@Ge tMapping ( value = "/resource/{resourceId}" )
@ResponseBody
public TbResource getResourceById ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( RESOURCE_ID ) String strResourceId ) throws ThingsboardException {
checkParameter ( RESOURCE_ID , strResourceId ) ;
TbResourceId resourceId = new TbResourceId ( toUUID ( strResourceId ) ) ;
return checkResourceId ( resourceId , Operation . READ ) ;
TbResource resource = checkResourceId ( resourceId , Operation . READ ) ;
resource . setBase64Data ( Base64 . getEncoder ( ) . encodeToString ( resource . getData ( ) ) ) ;
return resource ;
}
@ApiOperation ( value = "Create Or Update Resource (saveResource)" ,
@ -184,10 +190,10 @@ public class TbResourceController extends BaseController {
produces = "application/json" ,
consumes = "application/json" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@Reque stMapping ( value = "/resource" , method = RequestMethod . POST )
@Po stMapping ( value = "/resource" )
@ResponseBody
public TbResource saveResource ( @ApiParam ( value = "A JSON value representing the Resource." )
@RequestBody TbResource resource ) throws Exception {
public TbResourceInfo saveResource ( @ApiParam ( value = "A JSON value representing the Resource." )
@RequestBody TbResource resource ) throws Exception {
resource . setTenantId ( getTenantId ( ) ) ;
checkEntity ( resource . getId ( ) , resource , Resource . TB_RESOURCE ) ;
return tbResourceService . save ( resource , getCurrentUser ( ) ) ;
@ -198,7 +204,7 @@ public class TbResourceController extends BaseController {
PAGE_DATA_PARAMETERS + RESOURCE_INFO_DESCRIPTION + SYSTEM_OR_TENANT_AUTHORITY_PARAGRAPH ,
produces = "application/json" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource" , method = RequestMethod . GET )
@Ge tMapping ( value = "/resource" )
@ResponseBody
public PageData < TbResourceInfo > getResources ( @ApiParam ( value = PAGE_SIZE_DESCRIPTION , required = true )
@RequestParam int pageSize ,
@ -230,7 +236,7 @@ public class TbResourceController extends BaseController {
PAGE_DATA_PARAMETERS + LWM2M_OBJECT_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH ,
produces = "application/json" )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource/lwm2m/page" , method = RequestMethod . GET )
@Ge tMapping ( value = "/resource/lwm2m/page" )
@ResponseBody
public List < LwM2mObject > getLwm2mListObjectsPage ( @ApiParam ( value = PAGE_SIZE_DESCRIPTION , required = true )
@RequestParam int pageSize ,
@ -251,7 +257,7 @@ public class TbResourceController extends BaseController {
"You can specify parameters to filter the results. " + LWM2M_OBJECT_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH ,
produces = "application/json" )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN')" )
@Reques tMapping ( value = "/resource/lwm2m" , method = RequestMethod . GET )
@Ge tMapping ( value = "/resource/lwm2m" )
@ResponseBody
public List < LwM2mObject > getLwm2mListObjects ( @ApiParam ( value = SORT_ORDER_DESCRIPTION , allowableValues = SORT_ORDER_ALLOWABLE_VALUES , required = true )
@RequestParam String sortOrder ,
@ -265,7 +271,7 @@ public class TbResourceController extends BaseController {
@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')" )
@Request Mapping ( value = "/resource/{resourceId}" , method = RequestMethod . DELETE )
@Delete Mapping ( value = "/resource/{resourceId}" )
@ResponseBody
public void deleteResource ( @ApiParam ( value = RESOURCE_ID_PARAM_DESCRIPTION )
@PathVariable ( "resourceId" ) String strResourceId ) throws ThingsboardException {
@ -275,7 +281,7 @@ public class TbResourceController extends BaseController {
tbResourceService . delete ( tbResource , getCurrentUser ( ) ) ;
}
private ResponseEntity < org . springframework . core . io . Resource > downloadResourceIfChanged ( ResourceType type , String strResourceId , String etag ) throws ThingsboardException {
private ResponseEntity < ByteArray Resource> downloadResourceIfChanged ( ResourceType type , String strResourceId , String etag ) throws ThingsboardException {
checkParameter ( RESOURCE_ID , strResourceId ) ;
TbResourceId resourceId = new TbResourceId ( toUUID ( strResourceId ) ) ;
@ -289,7 +295,7 @@ public class TbResourceController extends BaseController {
}
TbResource tbResource = checkResourceId ( resourceId , Operation . READ ) ;
ByteArrayResource resource = new ByteArrayResource ( Base64 . getDecoder ( ) . decode ( tbResource . getData ( ) . getBytes ( ) ) ) ;
ByteArrayResource resource = new ByteArrayResource ( tbResource . getData ( ) ) ;
return ResponseEntity . ok ( )
. header ( HttpHeaders . CONTENT_DISPOSITION , "attachment;filename=" + tbResource . getFileName ( ) )