Search in sources :

Example 1 with FromJson

use of org.openmuc.framework.lib.rest.FromJson in project OpenMUC by isc-konstanz.

the class ChannelResourceServlet method doPost.

@Override
public void doPost(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);
    FromJson json = ServletLib.getFromJson(request, logger, response);
    if (json == null) {
        return;
    }
    if (pathInfoArray.length == 1) {
        String channelId = pathInfoArray[0];
        doSetConfigs(channelId, response, json, false);
    } else {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
    }
}
Also used : FromJson(org.openmuc.framework.lib.rest.FromJson)

Example 2 with FromJson

use of org.openmuc.framework.lib.rest.FromJson 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 FromJson

use of org.openmuc.framework.lib.rest.FromJson in project OpenMUC by isc-konstanz.

the class RestChannelScanner method scan.

@Override
public void scan(List<ChannelScanInfo> channelScanInfos) throws ArgumentSyntaxException, ScanException, ConnectionException {
    try (RestConnection connection = new RestConnection((RestConfigs) getContext())) {
        FromJson json = new FromJson(connection.get(""));
        List<RestChannel> channels = json.getRestChannelList();
        for (RestChannel channel : channels) {
            // TODO: get channel config list with valueTypeLength, description, ...
            ChannelScanInfo channelScanInfo = new ChannelScanInfo(channel.getId(), "", channel.getValueType(), 0);
            channelScanInfos.add(channelScanInfo);
        }
    } catch (IOException e) {
        throw new ConnectionException(e.getMessage());
    }
}
Also used : ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) FromJson(org.openmuc.framework.lib.rest.FromJson) RestChannel(org.openmuc.framework.lib.rest.objects.RestChannel) IOException(java.io.IOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException)

Example 4 with FromJson

use of org.openmuc.framework.lib.rest.FromJson in project OpenMUC by isc-konstanz.

the class JsonWrapper method toRecord.

public Record toRecord(InputStream stream, ValueType valueType) throws IOException {
    String jsonString = getStringFromInputStream(stream);
    FromJson fromJson = new FromJson(jsonString);
    return fromJson.getRecord(valueType);
}
Also used : FromJson(org.openmuc.framework.lib.rest.FromJson)

Example 5 with FromJson

use of org.openmuc.framework.lib.rest.FromJson in project OpenMUC by isc-konstanz.

the class UserServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString == null) {
        return;
    }
    setServices();
    String pathInfo = pathAndQueryString[ServletLib.PATH_ARRAY_NR];
    FromJson json = ServletLib.getFromJson(request, logger, response);
    if (json == null) {
        return;
    }
    if (pathInfo.equals("/")) {
        RestUserConfig userConfig = json.getRestUserConfig();
        if (authenticationService.contains(userConfig.getId())) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "User already exists.", " User = ", userConfig.getId());
        } else if (userConfig.getPassword() == null || userConfig.getPassword().isEmpty()) {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_PRECONDITION_FAILED, logger, "Password is mandatory.");
        } else {
            authenticationService.registerNewUser(userConfig.getId(), userConfig.getPassword());
        }
    } else {
        ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
    }
}
Also used : FromJson(org.openmuc.framework.lib.rest.FromJson) RestUserConfig(org.openmuc.framework.lib.rest.objects.RestUserConfig)

Aggregations

FromJson (org.openmuc.framework.lib.rest.FromJson)16 RestUserConfig (org.openmuc.framework.lib.rest.objects.RestUserConfig)3 JsonElement (com.google.gson.JsonElement)2 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)2 Record (org.openmuc.framework.data.Record)2 RestChannel (org.openmuc.framework.lib.rest.objects.RestChannel)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 ChannelConfig (org.openmuc.framework.config.ChannelConfig)1 ConfigWriteException (org.openmuc.framework.config.ConfigWriteException)1 DeviceConfig (org.openmuc.framework.config.DeviceConfig)1 DriverConfig (org.openmuc.framework.config.DriverConfig)1 IdCollisionException (org.openmuc.framework.config.IdCollisionException)1 ValueType (org.openmuc.framework.data.ValueType)1 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)1 MissingJsonObjectException (org.openmuc.framework.lib.rest.exceptions.MissingJsonObjectException)1 RestConfigIsNotCorrectException (org.openmuc.framework.lib.rest.exceptions.RestConfigIsNotCorrectException)1 RestRecord (org.openmuc.framework.lib.rest.objects.RestRecord)1