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.RemoveUserFromTeamAction.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 w w  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.setTeam(null);
        hpc.removeFromTeamManager(team.getIdTeam(), user);
        List<Team> teams = hpc.getTeamsManagedBy(user);
        if (teams.isEmpty() && user.getRole().equals(Constants.ROLE_TEAM_MANAGER)) {
            user.setRole(Constants.ROLE_USER);
        }
        hpc.updateUserInfo(user);
        MessageGenerator.sendSuccessMessage(response);
    } else {
        MessageGenerator.sendErrorMessage("NotFound", response);
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.RenameTeamAction.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 jsonTeamNAme = json.get(Constants.ACTION_PARAM_NAME);
    String name = jsonTeamNAme.getAsString();

    Team team = hpc.getTeam(teamId, sessionUser.getManagedOrganizations());
    if (null == team) {
        MessageGenerator.sendErrorMessage("NotFound", response);
        return;//from   www  . j a  v a 2s.c om
    }
    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;
        }
    }

    Boolean success = hpc.renameTeam(teamId, name);
    if (success) {
        MessageGenerator.sendSuccessMessage(response);
    } else {
        MessageGenerator.sendErrorMessage("NotFound", response);
    }
}

From source file:com.remediatetheflag.global.actions.auth.management.team.UpdateChallengeAction.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 idElement = json.get(Constants.ACTION_PARAM_ID);
    JsonElement nameElement = json.get(Constants.ACTION_PARAM_NAME);
    JsonElement detailsElement = json.get(Constants.ACTION_PARAM_DETAILS);
    JsonElement startElement = json.get(Constants.ACTION_PARAM_START_DATE);
    JsonElement endElement = json.get(Constants.ACTION_PARAM_END_DATE);
    JsonElement scoringElement = json.get(Constants.ACTION_PARAM_SCORING_MODE);
    JsonElement firstPlaceElement = json.get(Constants.ACTION_PARAM_SCORING_FIRST_PLACE);
    JsonElement secondPlaceElement = json.get(Constants.ACTION_PARAM_SCORING_SECOND_PLACE);
    JsonElement thirdPlaceElement = json.get(Constants.ACTION_PARAM_SCORING_THIRD_PLACE);

    Challenge c = hpc.getChallengeWithDetails(idElement.getAsInt(), user.getManagedOrganizations());
    if (null == c) {
        MessageGenerator.sendErrorMessage("NotFound", response);
        return;//www .ja v  a  2 s.  co  m
    }
    if (!nameElement.getAsString().equals(c.getName())) {
        Challenge existingChallenge = hpc.getChallengeFromName(nameElement.getAsString());
        if (existingChallenge != null) {
            MessageGenerator.sendErrorMessage("NameNotAvailable", response);
            return;
        }
    }
    Type listIntegerType = new TypeToken<List<Integer>>() {
    }.getType();
    Type listStringType = new TypeToken<List<String>>() {
    }.getType();

    Gson gson = new Gson();
    Set<User> challengeUsers = new HashSet<User>();
    Set<AvailableExercise> challengeExercises = new HashSet<AvailableExercise>();

    try {
        List<String> users = gson.fromJson(json.get(Constants.ACTION_PARAM_USERID_LIST), listStringType);
        for (String username : users) {
            User tmpUser = hpc.getUserFromUsername(username);
            if (null != tmpUser && tmpUser.getDefaultOrganization().equals(c.getOrganization()))
                challengeUsers.add(tmpUser);
        }
        List<Integer> exercises = gson.fromJson(json.get(Constants.ACTION_PARAM_EXERCISE_LIST),
                listIntegerType);
        for (Integer idExercise : exercises) {
            AvailableExercise tmpExercise = hpc.getAvailableExerciseDetails(idExercise, c.getOrganization());
            if (null != tmpExercise)
                challengeExercises.add(tmpExercise);
        }
    } catch (Exception e) {
        MessageGenerator.sendErrorMessage("ListsParseError", response);
        return;
    }

    c.setDetails(detailsElement.getAsString());

    SimpleDateFormat parser = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
    c.setEndDate(parser.parse(endElement.getAsString()));
    c.setStartDate(parser.parse(startElement.getAsString()));

    c.setFirstInFlag(firstPlaceElement.getAsInt());
    c.setSecondInFlag(secondPlaceElement.getAsInt());
    c.setThirdInFlag(thirdPlaceElement.getAsInt());

    if (c.getEndDate().before(c.getStartDate())) {
        MessageGenerator.sendErrorMessage("DatesError", response);
        return;
    }

    c.setExercises(challengeExercises);
    c.setLastActivity(new Date());
    c.setName(nameElement.getAsString());
    try {
        ExerciseScoringMode scoring = ExerciseScoringMode.getStatusFromStatusCode(scoringElement.getAsInt());
        c.setScoring(scoring);
    } catch (Exception e) {
        c.setScoring(ExerciseScoringMode.MANUAL_REVIEW);
    }
    c.setUsers(challengeUsers);

    if (c.getStartDate().before(new Date()))
        c.setStatus(ChallengeStatus.IN_PROGRESS);
    else
        c.setStatus(ChallengeStatus.NOT_STARTED);

    Double completion = 0.0;

    Integer nrFlags = 0;
    for (AvailableExercise e : c.getExercises()) {
        nrFlags += e.getFlags().size();
    }
    Integer runFlags = 0;
    for (ExerciseInstance i : c.getRunExercises()) {
        runFlags += i.getResults().size();
    }

    if (null != c.getExercises() && !c.getExercises().isEmpty() && null != c.getUsers()
            && !c.getUsers().isEmpty() && null != c.getRunExercises() && !c.getRunExercises().isEmpty())
        completion = (double) ((double) runFlags / ((double) nrFlags * (double) c.getUsers().size())) * 100.0;
    c.setCompletion(completion);

    Boolean result = hpc.updateChallenge(c);
    if (result)
        MessageGenerator.sendSuccessMessage(response);
    else
        MessageGenerator.sendErrorMessage("ChallengeNotUpdate", response);

}

From source file:com.remediatetheflag.global.actions.auth.user.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();
    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);
    AvailableExercise exercise = hpc.getAvailableExerciseDetails(idExercise,
            sessionUser.getDefaultOrganization());
    if (null != exercise)
        MessageGenerator.sendExerciseInfoMessage(exercise, response);
    else {/*from  w  w w  .  j  a  va2 s  .  c o m*/
        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.user.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();
    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    AvailableExercise exercise = hpc.getAvailableExerciseWithReferenceFile(idExercise,
            sessionUser.getDefaultOrganization());
    if (null != exercise && null != exercise.getReferenceFile()
            && null != exercise.getReferenceFile().getFile()) {
        MessageGenerator.sendExerciseReferenceFileMessage(exercise, response);
    } else {//from www . j a  va 2  s  . c  o m
        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.user.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();

    ExerciseInstance instance = hpc.getCompletedExerciseInstanceWithSolution(sessionUser.getIdUser(),
            idExercise);// w  ww  . ja v  a 2 s . c o m
    if (null != instance) {
        MessageGenerator.sendExerciseSolutionFileMessage(instance, response);
    } else {
        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.user.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<RTFGateway> regions = hpc.getAllActiveGateways();
    List<ExerciseRegion> availableRegions = new LinkedList<ExerciseRegion>();
    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);

    for (RTFGateway gw : regions) {
        if (null == gw.getRegion())
            continue;
        RTFECSTaskDefinition task = hpc.getTaskDefinitionForExerciseInRegion(idExercise, gw.getRegion(),
                sessionUser.getDefaultOrganization());
        if (null == task) {
            AvailableExercise exercise = hpc.getAvailableExercise(idExercise,
                    sessionUser.getDefaultOrganization());
            if (null != exercise)
                task = hpc.getTaskDefinitionFromUUID(exercise.getUuid(), gw.getRegion());
        }/*from  w ww .  ja v a 2s  .co  m*/
        if (null != task) {
            ExerciseRegion r = new ExerciseRegion();
            r.setFqdn(gw.getFqdn());
            r.setName(gw.getRegion().toString());
            r.setPing(-1);
            availableRegions.add(r);
        } else {
            logger.warn("Exercise " + idExercise + " doesn't have a TaskDefinition in region "
                    + gw.getRegion().getName());
        }
    }
    MessageGenerator.sendExerciseRegionsMessage(availableRegions, response);
}

From source file:com.remediatetheflag.global.actions.auth.user.GetChallengeDetailsAction.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 idChallenge = jsonElement.getAsInt();

    User sessionUser = (User) request.getSession().getAttribute(Constants.ATTRIBUTE_SECURITY_CONTEXT);
    Challenge challenge = hpc.getChallengeWithDetailsForUser(idChallenge, sessionUser.getIdUser());
    MessageGenerator.sendChallengeDetailsMessage(challenge, response);

}

From source file:com.remediatetheflag.global.actions.auth.user.GetExerciseHistoryDetailsAction.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 instance = hpc.getCompletedExerciseInstanceForUser(sessionUser.getIdUser(), idExercise);
    if (null != instance) {
        if (instance.isResultsAvailable() || (null != instance.getScoring()
                && !instance.getScoring().equals(ExerciseScoringMode.MANUAL_REVIEW))) {
            instance.setLatestDateResultsLastReviewed(new Date());
            if (instance.getCountResultsReviewedByUser() == null)
                instance.setCountResultsReviewedByUser(1);
            else/*from   ww  w  .j a v a2  s.  c  o m*/
                instance.setCountResultsReviewedByUser(instance.getCountResultsReviewedByUser() + 1);
            hpc.updateExerciseInstance(instance);
        }
        MessageGenerator.sendUserHistoryDetailMessage(instance, response);
    } else {
        logger.error("ExerciseInstance " + idExercise + " not found for user " + sessionUser.getIdUser());
        MessageGenerator.sendErrorMessage("NotFound", response);
        return;
    }
}

From source file:com.remediatetheflag.global.actions.auth.user.GetExerciseHistoryZipFileAction.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 instance = hpc.getCompletedExerciseInstanceWithFileForUser(sessionUser.getIdUser(),
            idExercise);/*from  w  w w .  j av  a 2s .c o  m*/
    if (null != instance) {
        MessageGenerator.sendFileMessage(instance, response);
    } else {
        logger.error("Exercise " + idExercise + " not found for user " + sessionUser.getIdUser());
        MessageGenerator.sendErrorMessage("NotFound", response);
    }
}