Search in sources :

Example 1 with AuthResult

use of com.actionworks.flashsale.app.auth.model.AuthResult in project flash-sale by ThoughtsBeta.

the class AuthInterceptor method preHandle.

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    Object userId = request.getAttribute(USER_ID);
    if (userId != null) {
        return true;
    }
    String token = request.getParameter("token");
    AuthResult authResult = authorizationService.auth(token);
    if (authResult.isSuccess()) {
        HttpServletRequestWrapper authRequestWrapper = new HttpServletRequestWrapper(request);
        authRequestWrapper.setAttribute(USER_ID, authResult.getUserId());
    }
    return true;
}
Also used : HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult)

Example 2 with AuthResult

use of com.actionworks.flashsale.app.auth.model.AuthResult in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method onlineFlashItem.

@Override
public AppResult onlineFlashItem(Long userId, Long activityId, Long itemId) {
    logger.info("itemOnline|上线秒杀品|{},{},{}", userId, activityId, itemId);
    if (userId == null || activityId == null || itemId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock itemModificationLock = lockFactoryService.getDistributedLock(getItemModificationLockKey(userId));
    try {
        boolean isLockSuccess = itemModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(LOCK_FAILED_ERROR);
        }
        flashItemDomainService.onlineFlashItem(itemId);
        logger.info("itemOnline|秒杀品已上线");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemOnline|秒杀品已上线失败|{}", userId, e);
        throw new BizException("秒杀品已上线失败");
    } finally {
        itemModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 3 with AuthResult

use of com.actionworks.flashsale.app.auth.model.AuthResult in project flash-sale by ThoughtsBeta.

the class DefaultFlashItemAppService method offlineFlashItem.

@Override
public AppResult offlineFlashItem(Long userId, Long activityId, Long itemId) {
    logger.info("itemOffline|下线秒杀品|{},{},{}", userId, activityId, itemId);
    AuthResult authResult = authorizationService.auth(userId, FLASH_ITEM_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    if (userId == null || activityId == null || itemId == null) {
        throw new BizException(INVALID_PARAMS);
    }
    DistributedLock itemModificationLock = lockFactoryService.getDistributedLock(getItemModificationLockKey(userId));
    try {
        boolean isLockSuccess = itemModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(LOCK_FAILED_ERROR);
        }
        flashItemDomainService.offlineFlashItem(itemId);
        logger.info("itemOffline|秒杀品已下线");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("itemOffline|秒杀品已下线失败|{}", userId, e);
        throw new BizException("秒杀品已下线失败");
    } finally {
        itemModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 4 with AuthResult

use of com.actionworks.flashsale.app.auth.model.AuthResult in project flash-sale by ThoughtsBeta.

the class DefaultActivityAppService method publishFlashActivity.

@Override
public AppResult publishFlashActivity(Long userId, FlashActivityPublishCommand flashActivityPublishCommand) {
    logger.info("activityPublish|发布秒杀活动|{},{}", userId, JSON.toJSONString(flashActivityPublishCommand));
    if (userId == null || flashActivityPublishCommand == null || !flashActivityPublishCommand.validate()) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_CREATE);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock activityCreateLock = lockFactoryService.getDistributedLock(getActivityCreateLockKey(userId));
    try {
        boolean isLockSuccess = activityCreateLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        flashActivityDomainService.publishActivity(userId, toDomain(flashActivityPublishCommand));
        logger.info("activityPublish|活动已发布");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("activityPublish|活动发布失败|{}", userId, e);
        throw new BizException("活动发布失败");
    } finally {
        activityCreateLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Example 5 with AuthResult

use of com.actionworks.flashsale.app.auth.model.AuthResult in project flash-sale by ThoughtsBeta.

the class DefaultActivityAppService method modifyFlashActivity.

@Override
public AppResult modifyFlashActivity(Long userId, Long activityId, FlashActivityPublishCommand flashActivityPublishCommand) {
    logger.info("activityModification|秒杀活动修改|{},{},{}", userId, activityId, JSON.toJSONString(flashActivityPublishCommand));
    if (userId == null || flashActivityPublishCommand == null || !flashActivityPublishCommand.validate()) {
        throw new BizException(INVALID_PARAMS);
    }
    AuthResult authResult = authorizationService.auth(userId, FLASH_ACTIVITY_MODIFICATION);
    if (!authResult.isSuccess()) {
        throw new AuthException(UNAUTHORIZED_ACCESS);
    }
    DistributedLock activityModificationLock = lockFactoryService.getDistributedLock(getActivityModificationLockKey(activityId));
    try {
        boolean isLockSuccess = activityModificationLock.tryLock(500, 1000, TimeUnit.MILLISECONDS);
        if (!isLockSuccess) {
            throw new BizException(FREQUENTLY_ERROR);
        }
        FlashActivity flashActivity = toDomain(flashActivityPublishCommand);
        flashActivity.setId(activityId);
        flashActivityDomainService.modifyActivity(userId, flashActivity);
        logger.info("activityModification|活动已修改");
        return AppResult.buildSuccess();
    } catch (Exception e) {
        logger.error("activityModification|活动修改失败|{},{}", userId, activityId, e);
        throw new BizException("活动修改失败");
    } finally {
        activityModificationLock.unlock();
    }
}
Also used : DistributedLock(com.actionworks.flashsale.lock.DistributedLock) FlashActivity(com.actionworks.flashsale.domain.model.entity.FlashActivity) BizException(com.actionworks.flashsale.app.exception.BizException) AuthException(com.actionworks.flashsale.controller.exception.AuthException) AuthResult(com.actionworks.flashsale.app.auth.model.AuthResult) AuthException(com.actionworks.flashsale.controller.exception.AuthException) BizException(com.actionworks.flashsale.app.exception.BizException)

Aggregations

AuthResult (com.actionworks.flashsale.app.auth.model.AuthResult)9 BizException (com.actionworks.flashsale.app.exception.BizException)7 AuthException (com.actionworks.flashsale.controller.exception.AuthException)7 DistributedLock (com.actionworks.flashsale.lock.DistributedLock)7 FlashActivity (com.actionworks.flashsale.domain.model.entity.FlashActivity)2 FlashItem (com.actionworks.flashsale.domain.model.entity.FlashItem)1 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)1