Example usage for javax.servlet.http HttpServletRequest getUserPrincipal

List of usage examples for javax.servlet.http HttpServletRequest getUserPrincipal

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getUserPrincipal.

Prototype

public java.security.Principal getUserPrincipal();

Source Link

Document

Returns a java.security.Principal object containing the name of the current authenticated user.

Usage

From source file:org.opendaylight.controller.web.DaylightWebAdmin.java

@RequestMapping(value = "/users", method = RequestMethod.POST)
@ResponseBody//from  ww w  .  j a va 2  s  .  c om
public Status saveLocalUserConfig(@RequestParam(required = true) String json,
        @RequestParam(required = true) String action, HttpServletRequest request) {

    IUserManager userManager = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, this);
    if (userManager == null) {
        return new Status(StatusCode.NOSERVICE, "User Manager unavailable");
    }

    if (!authorize(userManager, UserLevel.NETWORKADMIN, request)) {
        return new Status(StatusCode.UNAUTHORIZED, "Operation not permitted");
    }

    Gson gson = new Gson();
    UserConfig plainConfig = gson.fromJson(json, UserConfig.class);
    // Recreate using the proper constructor which will hash the password
    UserConfig config = new UserConfig(plainConfig.getUser(), plainConfig.getPassword(),
            plainConfig.getRoles());

    Status result = (action.equals("add")) ? userManager.addLocalUser(config)
            : userManager.removeLocalUser(config);
    if (result.isSuccess()) {
        if (action.equals("add")) {
            DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "added",
                    config.getUser() + " as " + config.getRoles().toString());
        } else {
            DaylightWebUtil.auditlog("User", request.getUserPrincipal().getName(), "removed", config.getUser());
        }
    }
    return result;
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("dealerships") // Updated 10/23
@PUT/*from  w w  w .j a v  a2 s  .co m*/
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response newDealership(@Context HttpServletRequest request, Dealerships dealership) {
    String googleUserId = request.getUserPrincipal().getName();
    int userType = this.dao.getUserByGoogleId(googleUserId).getType();
    if (userType > 2) {
        //          GenericEntity<Dealerships> entity = new GenericEntity<Dealerships>(){};
        return Response.ok(this.dao.newDealership(dealership)).build();
    }
    throw new RuntimeException("you must be of type 3 or higher to make new dealerships");
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("dealerships") // Updated 10/23
@POST/* ww  w .ja v a2 s  . c o m*/
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateDealership(@Context HttpServletRequest request, Dealerships dealership) {
    String googleUserId = request.getUserPrincipal().getName();
    int userType = this.dao.getUserByGoogleId(googleUserId).getType();
    if (userType > 2) {
        //          GenericEntity<Dealerships> entity = new GenericEntity<Dealerships>(){};
        return Response.ok(this.dao.updateDealership(dealership)).build();
    }
    throw new RuntimeException("you must be of type 3 or more to update a dealership");
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("stockNumbers/status")
@POST//w w w  . jav  a 2s.c  om
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateStockNumber(@Context HttpServletRequest request, StockNumbers stockNumber) {
    String googleUserId = request.getUserPrincipal().getName();
    Users user = this.dao.getUserByGoogleId(googleUserId);

    if (user.getType() > 2 || user.getDealershipId() == stockNumber.getDealershipId())
        return Response.ok().entity(this.dao.updateStockNumber(stockNumber)).build();

    return Response.status(400)
            .entity(new ErrorMessage("You must be an sb employee or belong to this dealership to do this"))
            .build();
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("userExists") // Updated 10/23
@PUT//from w  w  w  .  j av  a2s.com
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getUserById(@Context HttpServletRequest request, Users userFromClient) {
    String googleUserId = request.getUserPrincipal().getName();
    Users user = this.dao.getUserByGoogleId(googleUserId);
    if (user == null) {
        userFromClient.setGoogleUserId(googleUserId);
        user = this.dao.getUserById(this.dao.createUser(userFromClient));
    }

    if (user.getRefreshToken().length() > 0
            && !user.getRefreshToken().equals(userFromClient.getRefreshToken())) {
        userFromClient.setId(user.getId());
        this.dao.updateRefreshTokenForUser(userFromClient);
        user.setRefreshToken(userFromClient.getRefreshToken());
        //          user.setRefreshToken("");// clear this out when in production type environments
    }
    //       GenericEntity<Users> entity = new GenericEntity<Users>(user){};
    return Response.ok(user).build();
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("licenses") // Updated 10/24
@PUT//  www .j a v a2s  .  com
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response putLicense(@Context HttpServletRequest request, LicensesFromClient licenseFromClient) {
    String googleUserId = request.getUserPrincipal().getName();
    return Response.ok(this.dao.putLicense(licenseFromClient, googleUserId)).build();
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("licenses") // Added 10/24
@POST/*from w w  w .j a v a  2s.  c o m*/
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateLicense(@Context HttpServletRequest request, LicensesFromClient licenseFromClient) {
    String googleUserId = request.getUserPrincipal().getName();
    //       GenericEntity<LicensesListElement> entity = new GenericEntity<LicensesListElement>(){};
    return Response.ok(this.dao.updateLicense(licenseFromClient, googleUserId)).build();
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("licenses") // updated 10/24, add delete license image if successful TODO ************************************************************************************************
@DELETE//ww w .  j ava  2 s  . c o m
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response deleteLicense(@Context HttpServletRequest request, @QueryParam("licenseid") int licenseId) {
    String googleUserId = request.getUserPrincipal().getName();
    if (this.dao.userOwnsLicenseId(licenseId, googleUserId))
        return Response.ok(this.dao.deleteLicense(licenseId)).build();
    return Response.status(Status.UNAUTHORIZED).build();
}

From source file:com.salesmanBuddy.Controllers.SalesmanBuddy.java

@Path("licenseimage")
@GET//  w  w w  .  j av a 2  s. c o m
@Produces("image/jpeg")
public Response getImageForAnswerId(@Context HttpServletRequest request, @QueryParam("answerid") int answerId) {
    String googleUserId = request.getUserPrincipal().getName();
    if (this.dao.userOwnsQuestionId(answerId, googleUserId)) {
        File file = this.dao.getLicenseImageForAnswerId(answerId);// works 10/13
        Response response = Response.ok((Object) file)
                .header("Content-Disposition", "attachment; filename=" + file.getAbsoluteFile())
                .header("Content-Length", file.length()).build();
        //          file.delete();
        return response;
    }
    return Response.status(Status.UNAUTHORIZED).build();
}

From source file:net.sourceforge.vulcan.web.struts.actions.ClaimBrokenBuildAction.java

public ActionForward claim(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    boolean valid = true;
    final String projectName = request.getParameter("projectName");
    final String buildNumberString = request.getParameter("buildNumber");
    Integer buildNumber = -1;//  w  w  w .j  av a2 s  .c  o  m

    if (StringUtils.isBlank(projectName)) {
        saveError(request, ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("errors.required.with.name", "projectName"));
        valid = false;
    }
    if (StringUtils.isBlank(buildNumberString)) {
        saveError(request, ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("errors.required.with.name", "buildNumber"));
        valid = false;
    } else {
        try {
            buildNumber = Integer.valueOf(buildNumberString);
        } catch (NumberFormatException e) {
            saveError(request, ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.integer"));
            valid = false;
        }
    }

    if (!valid) {
        return mapping.findForward("error");
    }

    boolean claimed;
    try {
        claimed = buildManager.claimBrokenBuild(projectName, buildNumber, request.getUserPrincipal().getName());
    } catch (IllegalArgumentException e) {
        saveError(request, ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("errors.status.not.available.by.build.number", projectName, buildNumber));
        return mapping.findForward("error");
    }

    if (claimed) {
        saveSuccessMessage(request);
    } else {
        saveError(request, ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.already.claimed"));
        return mapping.findForward("error");
    }

    final String referrer = request.getHeader("referer");
    if (StringUtils.isNotBlank(referrer)) {
        response.sendRedirect(referrer);
        return null;
    }

    return mapping.findForward("dashboard");
}