use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.
the class DefaultActorServiceTest method testBasicPostWithSyncSession.
@Test
public void testBasicPostWithSyncSession() throws Exception {
SessionContext ssnCtx = mock(SessionContext.class);
KvEntry entry1 = new StringDataEntry("key1", "value1");
KvEntry entry2 = new StringDataEntry("key2", "value2");
BasicTelemetryUploadRequest telemetry = new BasicTelemetryUploadRequest();
long ts = 42;
telemetry.add(ts, entry1);
telemetry.add(ts, entry2);
BasicAdaptorToSessionActorMsg msg = new BasicAdaptorToSessionActorMsg(ssnCtx, telemetry);
DeviceId deviceId = new DeviceId(UUID.randomUUID());
DeviceCredentialsFilter filter = new DeviceTokenCredentials("token1");
Device device = mock(Device.class);
when(device.getId()).thenReturn(deviceId);
when(device.getTenantId()).thenReturn(tenantId);
when(ssnCtx.getSessionId()).thenReturn(new DummySessionID("session1"));
when(ssnCtx.getSessionType()).thenReturn(SessionType.SYNC);
when(deviceAuthService.process(filter)).thenReturn(DeviceAuthResult.of(deviceId));
when(deviceService.findDeviceById(deviceId)).thenReturn(device);
ObjectMapper ruleMapper = new ObjectMapper();
when(ruleMock.getFilters()).thenReturn(ruleMapper.readTree(FILTERS_CONFIGURATION));
when(ruleMock.getAction()).thenReturn(ruleMapper.readTree(ACTION_CONFIGURATION));
ComponentDescriptor filterComp = new ComponentDescriptor();
filterComp.setClazz("org.thingsboard.server.extensions.core.filter.MsgTypeFilter");
filterComp.setType(ComponentType.FILTER);
when(componentService.getComponent("org.thingsboard.server.extensions.core.filter.MsgTypeFilter")).thenReturn(Optional.of(filterComp));
ComponentDescriptor actionComp = new ComponentDescriptor();
actionComp.setClazz("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction");
actionComp.setType(ComponentType.ACTION);
when(componentService.getComponent("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction")).thenReturn(Optional.of(actionComp));
ObjectMapper pluginMapper = new ObjectMapper();
JsonNode pluginAdditionalInfo = pluginMapper.readTree(PLUGIN_CONFIGURATION);
when(pluginMock.getConfiguration()).thenReturn(pluginAdditionalInfo);
when(pluginMock.getClazz()).thenReturn(TelemetryStoragePlugin.class.getName());
when(attributesService.findAll(deviceId, DataConstants.CLIENT_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
when(attributesService.findAll(deviceId, DataConstants.SHARED_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
when(attributesService.findAll(deviceId, DataConstants.SERVER_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
initActorSystem();
Thread.sleep(1000);
actorService.process(new BasicToDeviceActorSessionMsg(device, msg));
// Check that device data was saved to DB;
List<TsKvEntry> expected = new ArrayList<>();
expected.add(new BasicTsKvEntry(ts, entry1));
expected.add(new BasicTsKvEntry(ts, entry2));
verify(tsService, Mockito.timeout(5000)).save(deviceId, expected, 0L);
}
use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.
the class DeviceApiController method replyToCommand.
@RequestMapping(value = "/{deviceToken}/rpc/{requestId}", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> replyToCommand(@PathVariable("deviceToken") String deviceToken, @PathVariable("requestId") Integer requestId, @RequestBody String json, HttpServletRequest request) {
DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
if (quotaExceeded(request, responseWriter)) {
return responseWriter;
}
HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
try {
JsonObject response = new JsonParser().parse(json).getAsJsonObject();
process(ctx, new ToDeviceRpcResponseMsg(requestId, response.toString()));
} catch (IllegalStateException | JsonSyntaxException ex) {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
} else {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
}
return responseWriter;
}
use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.
the class DeviceApiController method postDeviceAttributes.
@RequestMapping(value = "/{deviceToken}/attributes", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> postDeviceAttributes(@PathVariable("deviceToken") String deviceToken, @RequestBody String json, HttpServletRequest request) {
DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
if (quotaExceeded(request, responseWriter)) {
return responseWriter;
}
HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
try {
process(ctx, JsonConverter.convertToAttributes(new JsonParser().parse(json)));
} catch (IllegalStateException | JsonSyntaxException ex) {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
} else {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
}
return responseWriter;
}
use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.
the class DeviceApiController method postRpcRequest.
@RequestMapping(value = "/{deviceToken}/rpc", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> postRpcRequest(@PathVariable("deviceToken") String deviceToken, @RequestBody String json, HttpServletRequest httpRequest) {
DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
if (quotaExceeded(httpRequest, responseWriter)) {
return responseWriter;
}
HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
try {
JsonObject request = new JsonParser().parse(json).getAsJsonObject();
process(ctx, new ToServerRpcRequestMsg(0, request.get("method").getAsString(), request.get("params").toString()));
} catch (IllegalStateException | JsonSyntaxException ex) {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
} else {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
}
return responseWriter;
}
use of org.thingsboard.server.common.data.security.DeviceTokenCredentials in project thingsboard by thingsboard.
the class DeviceApiController method subscribe.
private DeferredResult<ResponseEntity> subscribe(String deviceToken, long timeout, FromDeviceMsg msg, HttpServletRequest httpRequest) {
DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
if (quotaExceeded(httpRequest, responseWriter)) {
return responseWriter;
}
HttpSessionCtx ctx = getHttpSessionCtx(responseWriter, timeout);
if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
try {
process(ctx, msg);
} catch (IllegalStateException | JsonSyntaxException ex) {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
} else {
responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
}
return responseWriter;
}
Aggregations