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());
}
}
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;
}
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());
}
}
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);
}
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());
}
}
Aggregations