Example usage for com.google.gson JsonElement getAsInt

List of usage examples for com.google.gson JsonElement getAsInt

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsInt.

Prototype

public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer value.

Usage

From source file:com.remediatetheflag.global.actions.auth.management.team.GetAvailableExerciseDetailsAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();
    AvailableExercise exercise = hpc.getAvailableExerciseDetails(idExercise);
    if (null != exercise)
        MessageGenerator.sendExerciseInfoMessageWithHints(exercise, response);
    else {/*from w  w  w  .ja va  2s  .  com*/
        MessageGenerator.sendErrorMessage("NotFound", response);
        User user = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);
        logger.error("User " + user.getIdUser() + " requested AvailableExercise " + idExercise
                + " details, exercise is inactive/not found");
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetAvailableExerciseReferenceFileAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();

    AvailableExercise exercise = hpc.getAvailableExerciseWithReferenceFile(idExercise);
    if (null != exercise && null != exercise.getReferenceFile()
            && null != exercise.getReferenceFile().getFile()) {
        MessageGenerator.sendExerciseReferenceFileMessage(exercise, response);
    } else {/*from w w  w. j  ava2 s . c  om*/
        User user = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);
        logger.error("Could not retrieve reference file for AvailableExercise: " + idExercise + " for user: "
                + user.getIdUser());
        MessageGenerator.sendErrorMessage("NotFound", response);
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetAvailableExerciseSolutionFileAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();

    AvailableExercise exercise = hpc.getAvailableExerciseWithSolution(idExercise);
    if (null != exercise && null != exercise.getSolutionFile()
            && null != exercise.getSolutionFile().getFile()) {
        MessageGenerator.sendExerciseSolutionFileMessage(exercise.getSolutionFile(), response);
    } else {/*from ww w  .j av a  2  s. c om*/
        logger.error("Solution file not found for exerciseInstance " + idExercise + " for user "
                + sessionUser.getIdUser());
        MessageGenerator.sendErrorMessage("NotFound", response);
    }

}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetAvailableRegionsForExerciseAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();

    List<RTFECSTaskDefinitionForExerciseInRegion> task = new LinkedList<RTFECSTaskDefinitionForExerciseInRegion>();
    task.addAll(hpc.getAllTaskDefinitionsForExercise(idExercise));

    MessageGenerator.sendExerciseTaskDefinitionsMessage(task, response);
}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetReviewDetailsAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();
    ExerciseInstance pending = hpc.getManagementExerciseInstance(idExercise,
            sessionUser.getManagedOrganizations());

    if (sessionUser.getRole().equals(Constants.ROLE_TEAM_MANAGER)) {
        List<User> users = hpc.getUsersInTeamManagedBy(sessionUser);
        if (!users.contains(pending.getUser())) {
            MessageGenerator.sendErrorMessage("NotFound", response);
            return;
        }/*from  w w w  . j a v  a2  s .co m*/
    }

    if (null != pending)
        MessageGenerator.sendExerciseDetails(pending, response);
    else {
        MessageGenerator.sendErrorMessage("NotFound", response);
        logger.error("Exercise " + idExercise + " not found");
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetReviewFileAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();
    ExerciseInstance pending = hpc.getManagementExerciseInstanceWithFile(idExercise,
            sessionUser.getManagedOrganizations());

    if (sessionUser.getRole().equals(Constants.ROLE_TEAM_MANAGER)) {
        List<User> users = hpc.getUsersInTeamManagedBy(sessionUser);
        if (!users.contains(pending.getUser())) {
            MessageGenerator.sendErrorMessage("NotFound", response);
            return;
        }//from  w  w w. j  a va2s.  c  o  m
    }
    if (null != pending) {
        MessageGenerator.sendFileMessage(pending, response);
    } else {
        MessageGenerator.sendErrorMessage("NotFound", response);
        logger.error("Review File for ExerciseInstance " + idExercise + " not found");
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetUserFeedbackForExerciseAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();
    Feedback feedback = hpc.getUserFeedbackForExercise(idExercise, sessionUser.getManagedOrganizations());
    if (sessionUser.getRole().equals(Constants.ROLE_TEAM_MANAGER)) {
        List<User> users = hpc.getUsersInTeamManagedBy(sessionUser);
        if (!users.contains(feedback.getUser())) {
            MessageGenerator.sendErrorMessage("NotFound", response);
            return;
        }/*from   w  w  w  .j  av  a 2  s  .  c  o  m*/
    }
    if (null != feedback)
        MessageGenerator.sendUserFeedbackMessage(feedback, response);
    else {
        MessageGenerator.sendErrorMessage("NotFound", response);
    }

}

From source file:com.remediatetheflag.global.actions.auth.management.team.GetUsersInOrganization.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    User user = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement orgElement = json.get(Constants.ACTION_PARAM_ORG_ID);
    Organization org = hpc.getOrganizationById(orgElement.getAsInt());
    if (null == org || !user.getManagedOrganizations().contains(org)) {
        MessageGenerator.sendErrorMessage("NotAuthorized", response);
        return;//from   w  w  w  .  ja va2  s.c o m
    }

    @SuppressWarnings("serial")
    List<User> users = hpc.getManagementAllUsers(new HashSet<Organization>() {
        {
            add(org);
        }
    });
    MessageGenerator.sendUsersListMessage(users, response);

}

From source file:com.remediatetheflag.global.actions.auth.management.team.MakeUserTeamManagerAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonTeamId = json.get(Constants.ACTION_PARAM_TEAM_ID);
    Integer teamId = jsonTeamId.getAsInt();
    JsonElement jsonUser = json.get(Constants.ACTION_PARAM_USERNAME);
    String username = jsonUser.getAsString();

    Team team = hpc.getTeam(teamId, sessionUser.getManagedOrganizations());
    if (null == team) {
        MessageGenerator.sendErrorMessage("NotFound", response);
        return;/*from   ww  w.  j  ava  2  s . c o  m*/
    }
    if (sessionUser.getRole().equals(Constants.ROLE_TEAM_MANAGER)) {
        boolean isManager = false;
        for (User u : team.getManagers()) {
            if (u.getIdUser().equals(sessionUser.getIdUser())) {
                isManager = true;
                break;
            }
        }
        if (!isManager) {
            MessageGenerator.sendErrorMessage("NotFound", response);
            return;
        }
    }
    User user = hpc.getUserFromUsername(username, sessionUser.getManagedOrganizations());
    if (null != user && user.getDefaultOrganization().getId().equals(team.getOrganization().getId())) {
        if (user.getRole().equals(Constants.ROLE_USER)) {
            user.setRole(Constants.ROLE_TEAM_MANAGER);
            hpc.updateUserInfo(user);
            logger.debug("User " + user.getUsername() + " upgraded to Role 'Team Manager'");
        }
        boolean alreadyManaging = false;
        for (Organization managed : user.getManagedOrganizations()) {
            if (managed.getId().equals(team.getOrganization().getId())) {
                alreadyManaging = true;
                break;
            }
        }
        if (!alreadyManaging) {
            user.getManagedOrganizations().add(team.getOrganization());
            hpc.updateUserInfo(user);
            logger.debug(
                    "User " + user.getUsername() + " added to managing organization " + team.getOrganization());
        }
        if (hpc.addToTeamManager(team.getIdTeam(), user))
            MessageGenerator.sendSuccessMessage(response);
        else
            MessageGenerator.sendErrorMessage("Error", response);
    } else {
        MessageGenerator.sendErrorMessage("NotFound", response);
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.MarkExerciseAsCancelledAction.java

License:Apache License

@Override
public void doAction(HttpServletRequest request, HttpServletResponse response) throws Exception {

    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    JsonObject json = (JsonObject) request.getAttribute(Constants.REQUEST_JSON);
    JsonElement jsonElement = json.get(Constants.ACTION_PARAM_ID);
    Integer idExercise = jsonElement.getAsInt();

    ExerciseInstance reviewedInstance = hpc.getManagementCompletedExerciseInstance(idExercise,
            sessionUser.getManagedOrganizations());
    if (null == reviewedInstance) {
        logger.error("ExerciseInstance " + idExercise + " not found from reviewer " + sessionUser.getIdUser());
        MessageGenerator.sendErrorMessage("NotFound", response);
        return;//from w  w  w. ja v a2 s  . c o  m
    }
    if (sessionUser.getRole().equals(Constants.ROLE_TEAM_MANAGER)) {
        List<User> users = hpc.getUsersInTeamManagedBy(sessionUser);
        if (!users.contains(reviewedInstance.getUser())) {
            MessageGenerator.sendErrorMessage("NotFound", response);
            return;
        }
    }
    if (reviewedInstance.getStatus().equals(ExerciseStatus.REVIEWED)
            || reviewedInstance.getStatus().equals(ExerciseStatus.CANCELLED)) {
        logger.error("ExerciseInstance " + idExercise
                + " has already reviewer. Current attempt is from reviewer " + sessionUser.getIdUser());
        MessageGenerator.sendErrorMessage("NotFound", response);
        return;
    }
    Integer idFile = null;
    if (null != reviewedInstance.getResultFile()) {
        idFile = reviewedInstance.getResultFile().getId();
        reviewedInstance.setResultFile(null);
    }
    Integer idScore = reviewedInstance.getScore().getIdExerciseScore();
    List<Integer> idResults = new LinkedList<Integer>();
    for (ExerciseResult er : reviewedInstance.getResults()) {
        idResults.add(er.getIdResult());
    }

    reviewedInstance.setStatus(ExerciseStatus.CANCELLED);
    reviewedInstance.setResultsAvailable(false);
    reviewedInstance.getResults().clear();
    reviewedInstance.setScore(null);
    reviewedInstance.setReviewer(sessionUser.getIdUser());
    reviewedInstance.setReviewedDate(new Date());

    hpc.updateExerciseInstance(reviewedInstance);
    hpc.managementCleanCancelledInstance(idFile, idScore, idResults);

    logger.debug("Exercise " + idExercise + " marked as cancelled from reviewer " + sessionUser.getIdUser());
    MessageGenerator.sendSuccessMessage(response);
}