Search in sources :

Example 86 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException 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 87 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException 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 88 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project thingsboard by thingsboard.

the class GatewaySessionCtx method onDeviceRpcResponse.

public void onDeviceRpcResponse(MqttPublishMessage mqttMsg) throws AdaptorException {
    JsonElement json = validateJsonPayload(gatewaySessionId, mqttMsg.payload());
    if (json.isJsonObject()) {
        JsonObject jsonObj = json.getAsJsonObject();
        String deviceName = checkDeviceConnected(jsonObj.get(DEVICE_PROPERTY).getAsString());
        Integer requestId = jsonObj.get("id").getAsInt();
        String data = jsonObj.get("data").toString();
        GatewayDeviceSessionCtx deviceSessionCtx = devices.get(deviceName);
        processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), new BasicAdaptorToSessionActorMsg(deviceSessionCtx, new ToDeviceRpcResponseMsg(requestId, data))));
    } else {
        throw new JsonSyntaxException(CAN_T_PARSE_VALUE + json);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) JsonObject(com.google.gson.JsonObject) BasicAdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.BasicAdaptorToSessionActorMsg)

Example 89 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class ElementTypeStatementSlot method deserialize0.

@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    if (!json.properties.containsKey("size[0]")) {
        json.properties.put("size[0]", "18");
    }
    if (!json.properties.containsKey("size[1]")) {
        json.properties.put("size[1]", "18");
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    String source;
    if (json.properties.containsKey("source")) {
        source = json.properties.get("source");
    } else {
        source = resolveEquation(json, "source_expression", ctx);
        if (source == null) {
            throw new JsonSyntaxException("Expected either 'source' or 'source_expression' for " + NAME);
        }
    }
    boolean draw = !"false".equals(json.properties.get("draw"));
    FullStatement<?> fullStatement = gui.properties.get(source, FullStatement.class);
    if (fullStatement == null) {
        throw new JsonSyntaxException("Can't find a statement called '" + source + "'");
    }
    StatementContext<?> context = gui.properties.get(source, StatementContext.class);
    return new GuiElementStatement<>(gui, area, fullStatement, context, draw);
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) JsonSyntaxException(com.google.gson.JsonSyntaxException) FunctionContext(buildcraft.lib.expression.FunctionContext) GuiElementStatement(buildcraft.lib.gui.statement.GuiElementStatement)

Example 90 with JsonSyntaxException

use of com.google.gson.JsonSyntaxException in project BuildCraft by BuildCraft.

the class ElementTypeSlot method deserialize0.

// pos: the position of the slot
// slot: The slot to be moved
// index: If the slot was an InventorySlotHolder then this is the index of the slot in the list
// visible: If false then the slot won't be visible
@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    String slotName = json.properties.get("slot");
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    Slot slot = gui.properties.get(slotName, Slot.class);
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    if (slot != null) {
        return new GuiElementSlotMover(gui, pos, visible, slot);
    }
    InventorySlotHolder holder = gui.properties.get(slotName, InventorySlotHolder.class);
    if (holder == null) {
        throw new JsonSyntaxException("Unknown slot '" + slotName + "'");
    }
    int index = resolveEquationInt(json, "index", ctx);
    if (index < 0 || index >= holder.slots.length) {
        throw new JsonSyntaxException("Invalid slot index! (" + index + ", min = 0, max = " + (holder.slots.length - 1) + ")");
    }
    return new GuiElementSlotMover(gui, pos, visible, holder.slots[index]);
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) Slot(net.minecraft.inventory.Slot) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition) GuiElementSlotMover(buildcraft.lib.gui.elem.GuiElementSlotMover)

Aggregations

JsonSyntaxException (com.google.gson.JsonSyntaxException)379 Gson (com.google.gson.Gson)169 IOException (java.io.IOException)83 HashMap (java.util.HashMap)68 JsonElement (com.google.gson.JsonElement)62 JsonObject (com.google.gson.JsonObject)58 ArrayList (java.util.ArrayList)46 JsonParser (com.google.gson.JsonParser)42 GsonBuilder (com.google.gson.GsonBuilder)38 Cursor (android.database.Cursor)33 InputStreamReader (java.io.InputStreamReader)30 Map (java.util.Map)30 BadRequestException (co.cask.cdap.common.BadRequestException)28 JsonArray (com.google.gson.JsonArray)28 Path (javax.ws.rs.Path)28 Reader (java.io.Reader)26 Type (java.lang.reflect.Type)23 JsonIOException (com.google.gson.JsonIOException)21 FileReader (java.io.FileReader)21 File (java.io.File)19