Search in sources :

Example 1 with RestUserConfig

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

Example 2 with RestUserConfig

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

the class UserServlet method doDelete.

@Override
public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString != null) {
        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();
            String userID = userConfig.getId();
            if (authenticationService.contains(userID)) {
                authenticationService.delete(userID);
            } else {
                ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "Requested user does not exist.", " User = ", userID);
            }
        } else {
            ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, REST_PATH, request.getPathInfo());
        }
    } 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)

Example 3 with RestUserConfig

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

the class UserServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(APPLICATION_JSON);
    String[] pathAndQueryString = checkIfItIsACorrectRest(request, response, logger);
    if (pathAndQueryString != null) {
        setServices();
        String pathInfo = pathAndQueryString[ServletLib.PATH_ARRAY_NR];
        ToJson json = new ToJson();
        if (pathInfo.equals("/")) {
            Set<String> userSet = authenticationService.getAllUsers();
            List<String> userList = new ArrayList<>();
            userList.addAll(userSet);
            json.addStringList(Const.USERS, userList);
        } else {
            String[] pathInfoArray = ServletLib.getPathInfoArray(pathInfo);
            if (pathInfoArray.length == 1) {
                String userId = pathInfoArray[0].replace("/", "");
                if (userId.equalsIgnoreCase(Const.GROUPS)) {
                    List<String> groupList = new ArrayList<>();
                    // TODO: add real groups, if groups exists in OpenMUC
                    groupList.add("");
                    json.addStringList(Const.GROUPS, groupList);
                } else if (authenticationService.contains(userId)) {
                    json.addUserConfig(new RestUserConfig(userId));
                } else {
                    ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "User does not exist.", " User = ", userId);
                }
            } else {
                ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, REQUESTED_REST_PATH_IS_NOT_AVAILABLE, " Path Info = ", request.getPathInfo());
            }
        }
        sendJson(json, response);
    }
}
Also used : ToJson(org.openmuc.framework.lib.rest.ToJson) ArrayList(java.util.ArrayList) RestUserConfig(org.openmuc.framework.lib.rest.objects.RestUserConfig)

Example 4 with RestUserConfig

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

the class UserServlet 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) {
        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 (userConfig.getPassword() == null || userConfig.getPassword().isEmpty()) {
                ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_PRECONDITION_FAILED, logger, "Password is mandatory.");
            } else if (userConfig.getOldPassword() == null || userConfig.getOldPassword().isEmpty()) {
                ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_PRECONDITION_FAILED, logger, "Old password is mandatory.");
            } else if (authenticationService.contains(userConfig.getId())) {
                String id = userConfig.getId();
                if (authenticationService.login(id, userConfig.getOldPassword())) {
                    authenticationService.registerNewUser(id, userConfig.getPassword());
                } else {
                    ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_UNAUTHORIZED, logger, "Old password is wrong.");
                }
            } else {
                ServletLib.sendHTTPErrorAndLogDebug(response, HttpServletResponse.SC_NOT_FOUND, logger, "User does not exist.", " User = ", userConfig.getId());
            }
        } 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

RestUserConfig (org.openmuc.framework.lib.rest.objects.RestUserConfig)4 FromJson (org.openmuc.framework.lib.rest.FromJson)3 ArrayList (java.util.ArrayList)1 ToJson (org.openmuc.framework.lib.rest.ToJson)1