Search in sources :

Example 1 with HttpSessionCtx

use of org.thingsboard.server.transport.http.session.HttpSessionCtx 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;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpSessionCtx(org.thingsboard.server.transport.http.session.HttpSessionCtx) JsonObject(com.google.gson.JsonObject) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) DeferredResult(org.springframework.web.context.request.async.DeferredResult) JsonParser(com.google.gson.JsonParser)

Example 2 with HttpSessionCtx

use of org.thingsboard.server.transport.http.session.HttpSessionCtx 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;
}
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)

Example 3 with HttpSessionCtx

use of org.thingsboard.server.transport.http.session.HttpSessionCtx 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;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) JsonSyntaxException(com.google.gson.JsonSyntaxException) HttpSessionCtx(org.thingsboard.server.transport.http.session.HttpSessionCtx) JsonObject(com.google.gson.JsonObject) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) DeferredResult(org.springframework.web.context.request.async.DeferredResult) JsonParser(com.google.gson.JsonParser)

Example 4 with HttpSessionCtx

use of org.thingsboard.server.transport.http.session.HttpSessionCtx 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;
}
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)

Example 5 with HttpSessionCtx

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

the class DeviceApiController method getDeviceAttributes.

@RequestMapping(value = "/{deviceToken}/attributes", method = RequestMethod.GET, produces = "application/json")
public DeferredResult<ResponseEntity> getDeviceAttributes(@PathVariable("deviceToken") String deviceToken, @RequestParam(value = "clientKeys", required = false, defaultValue = "") String clientKeys, @RequestParam(value = "sharedKeys", required = false, defaultValue = "") String sharedKeys, HttpServletRequest httpRequest) {
    DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
    if (quotaExceeded(httpRequest, responseWriter)) {
        return responseWriter;
    }
    HttpSessionCtx ctx = getHttpSessionCtx(responseWriter);
    if (ctx.login(new DeviceTokenCredentials(deviceToken))) {
        GetAttributesRequest request;
        if (StringUtils.isEmpty(clientKeys) && StringUtils.isEmpty(sharedKeys)) {
            request = new BasicGetAttributesRequest(0);
        } else {
            Set<String> clientKeySet = !StringUtils.isEmpty(clientKeys) ? new HashSet<>(Arrays.asList(clientKeys.split(","))) : null;
            Set<String> sharedKeySet = !StringUtils.isEmpty(sharedKeys) ? new HashSet<>(Arrays.asList(sharedKeys.split(","))) : null;
            request = new BasicGetAttributesRequest(0, clientKeySet, sharedKeySet);
        }
        process(ctx, request);
    } else {
        responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED));
    }
    return responseWriter;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpSessionCtx(org.thingsboard.server.transport.http.session.HttpSessionCtx) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

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