Search in sources :

Example 6 with HttpSessionCtx

use of org.thingsboard.server.transport.http.session.HttpSessionCtx in project thingsboard by thingsboard.

the class DeviceApiController method postTelemetry.

@RequestMapping(value = "/{deviceToken}/telemetry", method = RequestMethod.POST)
public DeferredResult<ResponseEntity> postTelemetry(@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.convertToTelemetry(new JsonParser().parse(json)));
        } catch (IllegalStateException | JsonSyntaxException ex) {
            responseWriter.setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
        }
    } else {
        responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
    }
    return responseWriter;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpSessionCtx(org.thingsboard.server.transport.http.session.HttpSessionCtx) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) DeferredResult(org.springframework.web.context.request.async.DeferredResult) JsonParser(com.google.gson.JsonParser)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)6 DeferredResult (org.springframework.web.context.request.async.DeferredResult)6 DeviceTokenCredentials (org.thingsboard.server.common.data.security.DeviceTokenCredentials)6 HttpSessionCtx (org.thingsboard.server.transport.http.session.HttpSessionCtx)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 JsonParser (com.google.gson.JsonParser)4 JsonObject (com.google.gson.JsonObject)2