Example usage for java.security Principal getName

List of usage examples for java.security Principal getName

Introduction

In this page you can find the example usage for java.security Principal getName.

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:org.magnum.mobilecloud.video.controller.VideoController.java

@RequestMapping(value = VIDEO_SVC_PATH + "/{id}" + "/like", method = RequestMethod.POST)
public @ResponseBody Video likeVideo(@PathVariable("id") long id, HttpServletResponse response, Principal p) {

    String username = p.getName();

    Video v = videos.findById(id);//w w w .j a  v a2 s. c  o  m

    if (v != null) {

        HashSet<String> videoLikes = v.getUserLikes();

        if (videoLikes.contains(username)) {

            response.setStatus(400);
            return v;

        } else {

            System.out.print("These are the videoLikes: " + videoLikes);

            videoLikes.add(username);

            System.out.print("These are the videoLikes: " + videoLikes);

            v.setLikes((long) (v.getLikes() + 1));

            v.setUserLikes(videoLikes);

            response.setStatus(200);

            videos.save(v);

            return v;

        }

    }

    response.setStatus(404);
    return null;

}

From source file:org.magnum.mobilecloud.video.controller.VideoController.java

@RequestMapping(value = VIDEO_SVC_PATH + "/{id}" + "/unlike", method = RequestMethod.POST)
public @ResponseBody Video unlikeVideo(@PathVariable("id") long id, HttpServletResponse response, Principal p) {

    String username = p.getName();

    Video v = videos.findById(id);//from  w  w w  .  j  a v  a 2s. co m

    if (v != null) {

        HashSet<String> videoLikes = v.getUserLikes();

        if (videoLikes.contains(username)) {

            System.out.print("These are the videoLikes: " + videoLikes);

            videoLikes.remove(username);

            System.out.print("These are the videoLikes: " + videoLikes);

            v.setLikes((long) (v.getLikes() - 1));

            v.setUserLikes(videoLikes);

            response.setStatus(200);

            videos.save(v);

            return v;

        } else {

            response.setStatus(400);
            return v;

        }

    }

    response.setStatus(404);
    return null;

}

From source file:org.starfishrespect.myconsumption.server.business.controllers.StatController.java

@RequestMapping(value = "/sensor/{sensorId}", method = RequestMethod.GET)
public List<StatDTO> getAllStats(Principal principal, @PathVariable String sensorId) {
    List<User> users = mUserRepository.findBySensorId(sensorId);

    // Check if this user can access this resource
    boolean allowed = false;

    for (User user : users) {
        if (principal.getName().equals(user.getName()))
            allowed = true;/*  ww  w  . j a  va2 s. c  om*/
    }

    if (!allowed)
        return null;

    // Get all stats
    List<PeriodStat> periodStats = mPeriodStatRepository.findBySensorId(sensorId);
    List<StatDTO> stats = new ArrayList<>();

    Mapper mapper = new DozerBeanMapper();

    for (PeriodStat periodStat : periodStats) {
        // If the period stat is complete (= all the days are present)
        if (periodStat.getDaysInPeriod().size() == StatUtils.getNumberOfDaysInPeriod(periodStat.getPeriod())) {
            stats.add(mapper.map(periodStat, StatDTO.class));
        }
    }

    return stats;
}

From source file:org.geosdi.geoplatform.experimental.dropwizard.resources.secure.server.GPSecureServerResource.java

@DELETE
@Path(value = GPServiceRSPathConfig.DELETE_SERVER_PATH)
@Override/* w  w w.j av  a  2  s  .c om*/
public Boolean deleteServer(@Auth Principal principal, @QueryParam(value = "serverID") Long serverID)
        throws Exception {
    logger.debug("\n\n@@@@@@@@@@@@@@Executing secure deleteServer - " + "Principal : {}\n\n",
            principal.getName());
    return super.deleteServer(serverID);
}

From source file:com.epam.ta.reportportal.ws.controller.impl.UserController.java

@Override
@RequestMapping(value = "/{login}", method = DELETE)
@ResponseBody//from  ww w  . j  av a2  s .  co  m
@PreAuthorize(ADMIN_ONLY)
@ApiOperation("Delete specified user")
public OperationCompletionRS deleteUser(@PathVariable String login, Principal principal) {
    return deleteUserMessageHandler.deleteUser(EntityUtils.normalizeUsername(login), principal.getName());
}

From source file:org.encos.flydown.Flydown.java

private void beforeRequestRate(JoinPoint joinPoint, RateLimitData rateLimitData) throws RateExceededException {

    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    String methodKey = getMethodKey(methodSignature);

    String identifier = null;/*w ww .  ja va 2s .  co  m*/

    switch (rateLimitData.getFlydownDevil()) {
    case CONTEXT_VAR:
        String context = rateCache.getFromContext(rateLimitData.getContextKey());

        if (context == null || "".equals(context.trim())) {
            //todo invalid or unset paramindex
        }

        identifier = MessageFormat.format("{0}[{1}]", rateLimitData.getContextKey(), context);
        break;
    case PRINCIPAL:
        Object objectPrincipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (!(objectPrincipal instanceof Principal)) {
            throw new FlydownRuntimeException(String.format(
                    "Your application principal doesn't implement class %s", Principal.class.getName()));
        }

        Principal principal = (Principal) objectPrincipal;
        identifier = principal.getName();
        break;

    case PARAM:
        Object[] arguments = joinPoint.getArgs();
        int rateParamIndex = rateLimitData.getParamIndex();
        if (rateParamIndex < 0) {
            //todo invalid or unset paramindex
        }
        if (arguments.length < rateParamIndex - 1) {
            log.warn("not enough arguments for picking up param {} from method {}", rateParamIndex,
                    methodSignature.getMethod().getName());
            return;
        }

        //fixme what if more method have the same params?
        methodKey = MessageFormat.format("{0}[{1}]", getMethodKey(methodSignature), rateParamIndex);
        identifier = arguments[rateParamIndex].toString();
        break;
    }

    if (rateLimitData.getFlydownEvent() == FlydownEvent.REQUEST) {
        limiter.cacheRequest(rateLimitData, methodKey, identifier);
    } else {
        //exception, not storing anything, just checking suspension
        limiter.checkSuspension(rateLimitData, methodKey, identifier);
    }

}

From source file:org.geosdi.geoplatform.experimental.dropwizard.resources.secure.server.GPSecureServerResource.java

@GET
@Path(value = GPServiceRSPathConfig.GET_SERVER_DETAIL_PATH)
@Override//from w  ww  .  ja va 2s  .com
public GeoPlatformServer getServerDetail(@Auth Principal principal,
        @PathParam(value = "serverID") Long serverID) throws Exception {
    logger.debug("\n\n@@@@@@@@@@@@@@Executing secure getServerDetail - " + "Principal : {}\n\n",
            principal.getName());
    return super.getServerDetail(serverID);
}

From source file:org.magnum.mobilecloud.video.VideoService.java

@RequestMapping(value = VideoSvcApi.VIDEO_SVC_PATH + "/{id}/unlike", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)//from w w  w  .  j  a va 2s . c  om
public void unlikeVideo(@PathVariable("id") long id, HttpServletResponse response, Principal p) {
    Video v = videos.findOne(id);
    if (v == null) {
        sendError(response, HttpServletResponse.SC_NOT_FOUND, "Video not found");
        return;
    }
    v.unlikeVideo(p.getName());
    videos.save(v);
}

From source file:alfio.controller.api.AdminApiController.java

@RequestMapping(value = "/events/{eventName}/pending-payments/{reservationId}", method = DELETE)
public String deletePendingPayment(@PathVariable("eventName") String eventName,
        @PathVariable("reservationId") String reservationId, Principal principal) {
    eventManager.deletePendingOfflinePayment(eventName, reservationId, principal.getName());
    return OK;// w  ww .ja v a  2 s .c  o  m
}

From source file:org.fcrepo.http.commons.session.SessionFactory.java

/**
 * Retrieve a JCR session from an active transaction
 *
 * @param servletRequest the servlet request
 * @param txId the transaction id/*ww w.  ja  v  a 2s . c o  m*/
 * @return a JCR session that is associated with the transaction
 */
protected Session getSessionFromTransaction(final HttpServletRequest servletRequest, final String txId) {

    final Principal userPrincipal = servletRequest.getUserPrincipal();

    String userName = null;
    if (userPrincipal != null) {
        userName = userPrincipal.getName();
    }

    final Transaction transaction = transactionService.getTransaction(txId, userName);
    LOGGER.debug("Returning a session in the transaction {} for user {}", transaction, userName);
    return transaction.getSession();

}