Search in sources :

Example 6 with DriverConfig

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

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

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

the class DriverResourceServlet method getDriverIdList.

private List<String> getDriverIdList() {
    List<String> driverList = new ArrayList<>();
    Collection<DriverConfig> driverConfigs = rootConfig.getDrivers();
    for (DriverConfig driverConfig : driverConfigs) {
        driverList.add(driverConfig.getId());
    }
    return driverList;
}
Also used : ArrayList(java.util.ArrayList) DriverConfig(org.openmuc.framework.config.DriverConfig)

Example 9 with DriverConfig

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

the class DriverResourceServlet method getDeviceIdList.

private List<String> getDeviceIdList(String driverId) {
    List<String> deviceList = new ArrayList<>();
    DriverConfig driverConfig = rootConfig.getDriver(driverId);
    Collection<DeviceConfig> deviceConfigs = driverConfig.getDevices();
    for (DeviceConfig deviceConfig : deviceConfigs) {
        deviceList.add(deviceConfig.getId());
    }
    return deviceList;
}
Also used : ArrayList(java.util.ArrayList) DriverConfig(org.openmuc.framework.config.DriverConfig) DeviceConfig(org.openmuc.framework.config.DeviceConfig)

Example 10 with DriverConfig

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

the class DriverResourceServlet method doPut.

@Override
public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString == null) {
        return;
    }
    setConfigAccess();
    String pathInfo = pathAndQueryString[ServletLib.PATH_ARRAY_NR];
    String[] pathInfoArray = ServletLib.getPathInfoArray(pathInfo);
    String driverId = pathInfoArray[0].replace("/", "");
    String json = ServletLib.getJsonText(request);
    if (pathInfoArray.length < 1) {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
    } else {
        DriverConfig driverConfig = rootConfig.getDriver(driverId);
        if (driverConfig != null && pathInfoArray.length == 2 && pathInfoArray[1].equalsIgnoreCase(Const.CONFIGS)) {
            doSetConfigs(driverId, response, json);
        } else if (driverConfig != null && pathInfoArray.length == 2 && pathInfoArray[1].equalsIgnoreCase(Const.SCAN_INTERRUPT)) {
            interruptScanProcess(driverId, response, json);
        } else {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
        }
    }
}
Also used : DriverConfig(org.openmuc.framework.config.DriverConfig)

Aggregations

DriverConfig (org.openmuc.framework.config.DriverConfig)15 DeviceConfig (org.openmuc.framework.config.DeviceConfig)6 ArrayList (java.util.ArrayList)5 ConfigWriteException (org.openmuc.framework.config.ConfigWriteException)3 JsonObject (com.google.gson.JsonObject)2 LinkedList (java.util.LinkedList)2 ChannelConfig (org.openmuc.framework.config.ChannelConfig)2 IdCollisionException (org.openmuc.framework.config.IdCollisionException)2 LogicalDevice (org.openmuc.framework.dataaccess.LogicalDevice)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1