Search in sources :

Example 1 with ConfigWriteException

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

the class DriverResourceServlet method doDelete.

@Override
public synchronized void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString != null) {
        setConfigAccess();
        String pathInfo = pathAndQueryString[0];
        String driverId = null;
        String[] pathInfoArray = ServletLib.getPathInfoArray(pathInfo);
        driverId = pathInfoArray[0].replace("/", "");
        DriverConfig driverConfig = rootConfig.getDriver(driverId);
        if (pathInfoArray.length != 1) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
        } else if (driverConfig == null) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "Driver \"" + driverId + "\" does not exist.");
        } else {
            try {
                driverConfig.delete();
                configService.setConfig(rootConfig);
                configService.writeConfigToFile();
                if (rootConfig.getDriver(driverId) == null) {
                    response.setStatus(HttpServletResponse.SC_OK);
                } else {
                    ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to delete driver ", driverId);
                }
            } catch (ConfigWriteException e) {
                ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to write into config.");
                e.printStackTrace();
            }
        }
    }
}
Also used : ConfigWriteException(org.openmuc.framework.config.ConfigWriteException) DriverConfig(org.openmuc.framework.config.DriverConfig)

Example 2 with ConfigWriteException

use of org.openmuc.framework.config.ConfigWriteException 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 3 with ConfigWriteException

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

the class DeviceResourceServlet method doDelete.

@Override
public synchronized void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString != null) {
        setConfigAccess();
        String pathInfo = pathAndQueryString[0];
        String deviceId = null;
        String[] pathInfoArray = ServletLib.getPathInfoArray(pathInfo);
        deviceId = pathInfoArray[0];
        DeviceConfig deviceConfig = rootConfig.getDevice(deviceId);
        if (pathInfoArray.length != 1) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
        } else if (deviceConfig == null) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "Device \"" + deviceId + "\" does not exist.");
        } else {
            try {
                deviceConfig.delete();
                configService.setConfig(rootConfig);
                configService.writeConfigToFile();
                if (rootConfig.getDriver(deviceId) == null) {
                    response.setStatus(HttpServletResponse.SC_OK);
                } else {
                    ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to delete driver ", deviceId);
                }
            } catch (ConfigWriteException e) {
                ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to write into config.");
            }
        }
    }
}
Also used : ConfigWriteException(org.openmuc.framework.config.ConfigWriteException) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 4 with ConfigWriteException

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

the class ChannelResourceServlet method doDelete.

@Override
public synchronized void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString != null) {
        setConfigAccess();
        String pathInfo = pathAndQueryString[0];
        String channelId;
        String[] pathInfoArray = ServletLib.getPathInfoArray(pathInfo);
        ChannelConfig channelConfig;
        channelId = pathInfoArray[0];
        channelConfig = getChannelConfig(channelId, response);
        if (channelConfig == null) {
            return;
        }
        if (pathInfoArray.length != 1) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, " Path Info = ", request.getPathInfo());
        } else {
            try {
                channelConfig.delete();
                configService.setConfig(rootConfig);
                configService.writeConfigToFile();
                if (rootConfig.getDriver(channelId) == null) {
                    response.setStatus(HttpServletResponse.SC_OK);
                } else {
                    ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to delete channel ", channelId);
                }
            } catch (ConfigWriteException e) {
                ServletLib.sendHTTPErrorAndLogErr(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, logger, "Not able to write into config.");
                logger.warn("Failed to write config.", e);
            }
        }
    }
}
Also used : ConfigWriteException(org.openmuc.framework.config.ConfigWriteException) ChannelConfig(org.openmuc.framework.config.ChannelConfig)

Aggregations

ConfigWriteException (org.openmuc.framework.config.ConfigWriteException)4 DriverConfig (org.openmuc.framework.config.DriverConfig)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 ChannelConfig (org.openmuc.framework.config.ChannelConfig)1 DeviceConfig (org.openmuc.framework.config.DeviceConfig)1 IdCollisionException (org.openmuc.framework.config.IdCollisionException)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