Search in sources :

Example 1 with IdCollisionException

use of org.openmuc.framework.config.IdCollisionException in project OpenMUC by isc-konstanz.

the class DriverConfigImpl method addDriverFromDomNode.

static void addDriverFromDomNode(Node driverConfigNode, RootConfigImpl parentConfig) throws ParseException {
    String id = ChannelConfigImpl.getAttributeValue(driverConfigNode, "id");
    if (id == null) {
        throw new ParseException("driver has no id attribute");
    }
    DriverConfigImpl config;
    try {
        config = parentConfig.addDriver(id);
    } catch (IdCollisionException e) {
        throw new ParseException(e);
    }
    parseDiverNode(driverConfigNode, config);
}
Also used : IdCollisionException(org.openmuc.framework.config.IdCollisionException) ParseException(org.openmuc.framework.config.ParseException)

Example 2 with IdCollisionException

use of org.openmuc.framework.config.IdCollisionException in project OpenMUC by isc-konstanz.

the class ChannelResourceServlet method doPostConfigs.

private synchronized boolean doPostConfigs(String channelId, HttpServletResponse response, FromJson json) throws JsonSyntaxException, ConfigWriteException, RestConfigIsNotCorrectException, Error, MissingJsonObjectException, IllegalStateException {
    boolean ok = false;
    DeviceConfig deviceConfig;
    ChannelConfig channelConfig = rootConfig.getChannel(channelId);
    JsonObject jso = json.getJsonObject();
    JsonElement jsonElement = jso.get(Const.DEVICE);
    if (jsonElement == null) {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_BAD_REQUEST, logger, "Wrong json message syntax. Device statement is missing.");
    }
    String deviceID = jsonElement.getAsString();
    if (deviceID != null) {
        deviceConfig = rootConfig.getDevice(deviceID);
    } else {
        throw new Error("No device ID in JSON");
    }
    if (deviceConfig == null) {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_CONFLICT, logger, "Device does not exists: ", deviceID);
    } else if (channelConfig != null) {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_CONFLICT, logger, "Channel already exists: ", channelId);
    } else {
        try {
            channelConfig = deviceConfig.addChannel(channelId);
            json.setChannelConfig(channelConfig, channelId);
            if ((channelConfig.getValueType() == ValueType.STRING || channelConfig.getValueType() == ValueType.BYTE_ARRAY) && channelConfig.getValueTypeLength() == null) {
                ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_NOT_ACCEPTABLE, logger, "Channel ", channelId, " with value type ", channelConfig.getValueType().toString(), ", missing valueTypeLength.");
                channelConfig.delete();
            } else {
                configService.setConfig(rootConfig);
                configService.writeConfigToFile();
            }
        } catch (IdCollisionException e) {
        }
        response.setStatus(HttpServletResponse.SC_OK);
        ok = true;
    }
    return ok;
}
Also used : ChannelConfig(org.openmuc.framework.config.ChannelConfig) JsonElement(com.google.gson.JsonElement) IdCollisionException(org.openmuc.framework.config.IdCollisionException) JsonObject(com.google.gson.JsonObject) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 3 with IdCollisionException

use of org.openmuc.framework.config.IdCollisionException in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method doPostConfigs.

private synchronized boolean doPostConfigs(String deviceId, HttpServletResponse response, FromJson json) throws JsonSyntaxException, ConfigWriteException, RestConfigIsNotCorrectException, Error, MissingJsonObjectException, IllegalStateException {
    boolean ok = false;
    DriverConfig driverConfig;
    DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
    JsonObject jso = json.getJsonObject();
    String driverId = jso.get(Const.DRIVER).getAsString();
    if (driverId != null) {
        driverConfig = rootConfig.getDriver(driverId);
    } else {
        throw new Error("No driver ID in JSON");
    }
    if (driverConfig == null) {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_CONFLICT, logger, "Driver does not exists: ", driverId);
    } else if (deviceConfig != null) {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_CONFLICT, logger, "Device already exists: ", deviceId);
    } else {
        try {
            deviceConfig = driverConfig.addDevice(deviceId);
            json.setDeviceConfig(deviceConfig, deviceId);
        } catch (IdCollisionException e) {
        }
        configService.setConfig(rootConfig);
        configService.writeConfigToFile();
        response.setStatus(HttpServletResponse.SC_OK);
        ok = true;
    }
    return ok;
}
Also used : IdCollisionException(org.openmuc.framework.config.IdCollisionException) DriverConfig(org.openmuc.framework.config.DriverConfig) JsonObject(com.google.gson.JsonObject) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 4 with IdCollisionException

use of org.openmuc.framework.config.IdCollisionException in project OpenMUC by isc-konstanz.

the class DriverResourceServlet method doSetConfigs.

private synchronized boolean doSetConfigs(String driverId, HttpServletResponse response, String json) {
    boolean ok = false;
    try {
        DriverConfig driverConfig = rootConfig.getDriver(driverId);
        if (driverConfig != null) {
            try {
                FromJson fromJson = new FromJson(json);
                fromJson.setDriverConfig(driverConfig, driverId);
            } catch (IdCollisionException e) {
            }
            configService.setConfig(rootConfig);
            configService.writeConfigToFile();
            response.setStatus(HttpServletResponse.SC_OK);
            ok = true;
        } else {
            ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to access to driver ", driverId);
        }
    } catch (JsonSyntaxException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_CONFLICT, logger, "JSON syntax is wrong.");
    } catch (MissingJsonObjectException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, e.getMessage());
    } catch (ConfigWriteException e) {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_CONFLICT, logger, "Could not write driver \"", driverId, "\".");
        logger.debug(e.getMessage());
    } catch (RestConfigIsNotCorrectException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_ACCEPTABLE, logger, "Not correct formed driver config json.", " JSON = ", json);
    } catch (IllegalStateException e) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_CONFLICT, logger, e.getMessage());
    }
    return ok;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) ConfigWriteException(org.openmuc.framework.config.ConfigWriteException) IdCollisionException(org.openmuc.framework.config.IdCollisionException) MissingJsonObjectException(org.openmuc.framework.lib.rest.exceptions.MissingJsonObjectException) FromJson(org.openmuc.framework.lib.rest.FromJson) DriverConfig(org.openmuc.framework.config.DriverConfig) RestConfigIsNotCorrectException(org.openmuc.framework.lib.rest.exceptions.RestConfigIsNotCorrectException)

Example 5 with IdCollisionException

use of org.openmuc.framework.config.IdCollisionException in project OpenMUC by isc-konstanz.

the class DeviceResourceServlet method doPutConfigs.

private synchronized boolean doPutConfigs(String deviceId, HttpServletResponse response, FromJson json) throws JsonSyntaxException, ConfigWriteException, RestConfigIsNotCorrectException, MissingJsonObjectException, IllegalStateException {
    boolean ok = false;
    DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
    if (deviceConfig != null) {
        try {
            json.setDeviceConfig(deviceConfig, deviceId);
        } catch (IdCollisionException e) {
        }
        configService.setConfig(rootConfig);
        configService.writeConfigToFile();
        response.setStatus(HttpServletResponse.SC_OK);
        ok = true;
    } else {
        ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to access to device ", deviceId);
    }
    return ok;
}
Also used : IdCollisionException(org.openmuc.framework.config.IdCollisionException) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Aggregations

IdCollisionException (org.openmuc.framework.config.IdCollisionException)6 DeviceConfig (org.openmuc.framework.config.DeviceConfig)3 JsonObject (com.google.gson.JsonObject)2 ChannelConfig (org.openmuc.framework.config.ChannelConfig)2 DriverConfig (org.openmuc.framework.config.DriverConfig)2 JsonElement (com.google.gson.JsonElement)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ConfigWriteException (org.openmuc.framework.config.ConfigWriteException)1 ParseException (org.openmuc.framework.config.ParseException)1 FromJson (org.openmuc.framework.lib.rest.FromJson)1 MissingJsonObjectException (org.openmuc.framework.lib.rest.exceptions.MissingJsonObjectException)1 RestConfigIsNotCorrectException (org.openmuc.framework.lib.rest.exceptions.RestConfigIsNotCorrectException)1