use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.
the class BaseRuleServiceTest method findTenantRules.
@Test
public void findTenantRules() throws Exception {
TenantId tenantIdA = new TenantId(UUIDs.timeBased());
TenantId tenantIdB = new TenantId(UUIDs.timeBased());
PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt());
PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt());
pluginService.savePlugin(pluginA);
pluginService.savePlugin(pluginB);
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
TextPageData<RuleMetaData> foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100));
Assert.assertEquals(3, foundA.getData().size());
TextPageData<RuleMetaData> foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100));
Assert.assertEquals(2, foundB.getData().size());
}
use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.
the class BaseRuleServiceTest method deleteRulesByTenantId.
@Test
public void deleteRulesByTenantId() throws Exception {
TenantId tenantIdA = new TenantId(UUIDs.timeBased());
TenantId tenantIdB = new TenantId(UUIDs.timeBased());
PluginMetaData pluginA = generatePlugin(tenantIdA, "testPluginToken" + ThreadLocalRandom.current().nextInt());
PluginMetaData pluginB = generatePlugin(tenantIdB, "testPluginToken" + ThreadLocalRandom.current().nextInt());
pluginService.savePlugin(pluginA);
pluginService.savePlugin(pluginB);
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
ruleService.saveRule(generateRule(tenantIdA, null, pluginA.getApiToken()));
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
ruleService.saveRule(generateRule(tenantIdB, null, pluginB.getApiToken()));
TextPageData<RuleMetaData> foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100));
Assert.assertEquals(3, foundA.getData().size());
TextPageData<RuleMetaData> foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100));
Assert.assertEquals(2, foundB.getData().size());
ruleService.deleteRulesByTenantId(tenantIdA);
foundA = ruleService.findTenantRules(tenantIdA, new TextPageLink(100));
Assert.assertEquals(0, foundA.getData().size());
foundB = ruleService.findTenantRules(tenantIdB, new TextPageLink(100));
Assert.assertEquals(2, foundB.getData().size());
}
use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.
the class BaseRuleServiceTest method findSystemRules.
@Test
public void findSystemRules() throws Exception {
// system tenant id
TenantId systemTenant = new TenantId(ModelConstants.NULL_UUID);
PluginMetaData plugin = generatePlugin(systemTenant, "testPluginToken" + ThreadLocalRandom.current().nextInt());
pluginService.savePlugin(plugin);
ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
TextPageData<RuleMetaData> found = ruleService.findSystemRules(new TextPageLink(100));
Assert.assertEquals(3, found.getData().size());
}
use of org.thingsboard.server.common.data.plugin.PluginMetaData in project thingsboard by thingsboard.
the class DefaultSystemDataLoaderService method loadPlugins.
private void loadPlugins(Path pluginsDir, TenantId tenantId) throws Exception {
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(pluginsDir, path -> path.toString().endsWith(JSON_EXT))) {
dirStream.forEach(path -> {
try {
JsonNode pluginJson = objectMapper.readTree(path.toFile());
PluginMetaData plugin = objectMapper.treeToValue(pluginJson, PluginMetaData.class);
plugin.setTenantId(tenantId);
if (plugin.getState() == ComponentLifecycleState.ACTIVE) {
plugin.setState(ComponentLifecycleState.SUSPENDED);
PluginMetaData savedPlugin = pluginService.savePlugin(plugin);
pluginService.activatePluginById(savedPlugin.getId());
} else {
pluginService.savePlugin(plugin);
}
} catch (Exception e) {
log.error("Unable to load plugin from json: [{}]", path.toString());
throw new RuntimeException("Unable to load plugin from json", e);
}
});
}
}
use of org.thingsboard.server.common.data.plugin.PluginMetaData 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;
}
Aggregations