public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = split2(message.body); if (split.length < 2) { throw new IllegalCommandException("Wrong income message format."); } int dashId = Integer.parseInt(split[0]); String deviceString = split[1]; if (deviceString == null || deviceString.isEmpty()) { throw new IllegalCommandException("Income device message is empty."); } DashBoard dash = user.profile.getDashByIdOrThrow(dashId); Device newDevice = JsonParser.parseDevice(deviceString, message.id); log.debug("Updating new device {}.", deviceString); if (newDevice.isNotValid()) { throw new IllegalCommandException("Income device message is not valid."); } Device existingDevice = user.profile.getDeviceById(dash, newDevice.id); if (existingDevice == null) { log.debug("Attempt to update device with non existing id."); ctx.writeAndFlush(illegalCommandBody(message.id), ctx.voidPromise()); return; } existingDevice.update(newDevice); dash.updatedAt = System.currentTimeMillis(); user.lastModifiedTs = dash.updatedAt; ctx.writeAndFlush(ok(message.id), ctx.voidPromise()); }
public static void messageReceived(Holder holder, ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = split2(message.body); int dashId = Integer.parseInt(split[0]); DashBoard dash = user.profile.getDashByIdOrThrow(dashId); String deviceString = split[1]; if (deviceString == null || deviceString.isEmpty()) { throw new IllegalCommandException("Income device message is empty."); } Device temporaryDevice = JsonParser.parseDevice(deviceString, message.id); if (temporaryDevice.isNotValid()) { throw new IllegalCommandException("Income device message is not valid."); } for (Device device : dash.devices) { if (device.id == temporaryDevice.id) { throw new NotAllowedException("Device with same id already exists.", message.id); } } String tempToken = TokenGeneratorUtil.generateNewToken(); holder.tokenManager.assignToken(user, dash, temporaryDevice, tempToken, true); if (ctx.channel().isWritable()) { ctx.writeAndFlush(makeASCIIStringMessage(GET_PROVISION_TOKEN, message.id, temporaryDevice.toString()), ctx.voidPromise()); } }
if (newDevice.isNotValid()) { throw new IllegalCommandException("Income device message is not valid.");