Search in sources :

Example 1 with CheckLongPollingResult

use of eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult in project CzechIdMng by bcvsolutions.

the class IdmIdentityController method checkUnresolvedRequests.

/**
 * Long polling for check unresolved identity role-requests
 *
 * @param backendId - applicant ID
 *
 * @return DeferredResult<OperationResultDto>, where:
 *
 * - EXECUTED = All requests for this identity are resolved,
 * - RUNNING = Requests are not resolved, but some request was changed (since previous check).
 * - NOT_EXECUTED = Deferred-result expired
 */
@ResponseBody
@RequestMapping(value = "{backendId}/check-unresolved-request", method = RequestMethod.GET)
@PreAuthorize("hasAuthority('" + CoreGroupPermission.ROLE_REQUEST_READ + "')" + " and hasAuthority('" + CoreGroupPermission.IDENTITY_READ + "')")
@ApiOperation(value = "Check changes of unresloved requests for the identity (Long-polling request).", nickname = "checkUnresolvedRequests", tags = { IdmIdentityController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.ROLE_REQUEST_READ, description = ""), @AuthorizationScope(scope = CoreGroupPermission.IDENTITY_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.ROLE_REQUEST_READ, description = ""), @AuthorizationScope(scope = CoreGroupPermission.IDENTITY_READ, description = "") }) })
public DeferredResult<OperationResultDto> checkUnresolvedRequests(@ApiParam(value = "Identity's uuid identifier or username.", required = true) @PathVariable @NotNull String backendId) {
    IdmIdentityDto dto = getDto(backendId);
    if (dto == null) {
        throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", backendId));
    }
    UUID identityId = dto.getId();
    DeferredResultWrapper result = new // 
    DeferredResultWrapper(// 
    identityId, // 
    dto.getClass(), new // 
    DeferredResult<OperationResultDto>(30000l, // 
    new OperationResultDto(OperationState.NOT_EXECUTED)));
    // 
    result.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    // If isn't long polling enabled, then Blocked response will be sent.
    if (!longPollingManager.isLongPollingEnabled()) {
        result.getResult().setResult(new OperationResultDto(OperationState.BLOCKED));
        return result.getResult();
    }
    longPollingManager.addSuspendedResult(result);
    return result.getResult();
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with CheckLongPollingResult

use of eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManagerTest method testExecuteDeferredResultOnDeleteRoleRequest.

@Test
public void testExecuteDeferredResultOnDeleteRoleRequest() {
    DefaultLongPollingManager defaultPollingManager = (DefaultLongPollingManager) longPollingManager;
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
    IdmIdentityDto identityOne = this.getHelper().createIdentity();
    IdmRoleRequestDto roleRequest = this.getHelper().createRoleRequest(identityOne, this.getHelper().createRole());
    DeferredResult<OperationResultDto> result = new DeferredResult<OperationResultDto>(10000l, new OperationResultDto(OperationState.NOT_EXECUTED));
    DeferredResultWrapper wrapper = new DeferredResultWrapper(identityOne.getId(), identityOne.getClass(), result);
    wrapper.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    Queue<DeferredResultWrapper> suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    longPollingManager.addSuspendedResult(wrapper);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    // Delete of the role-request -> must be detected
    roleRequestService.delete(roleRequest);
    // Check must be called twice, because first detect the change and second remove
    // ended deferred result (from some reason is not invoked method
    // result.onCompleted)
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
}
Also used : LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 3 with CheckLongPollingResult

use of eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManagerTest method testExecuteDeferredResultOnUpdateRoleRequest.

@Test
public void testExecuteDeferredResultOnUpdateRoleRequest() {
    DefaultLongPollingManager defaultPollingManager = (DefaultLongPollingManager) longPollingManager;
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
    IdmIdentityDto identityOne = this.getHelper().createIdentity();
    IdmRoleRequestDto roleRequest = this.getHelper().createRoleRequest(identityOne, this.getHelper().createRole());
    DeferredResult<OperationResultDto> result = new DeferredResult<OperationResultDto>(10000l, new OperationResultDto(OperationState.NOT_EXECUTED));
    DeferredResultWrapper wrapper = new DeferredResultWrapper(identityOne.getId(), identityOne.getClass(), result);
    wrapper.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    Queue<DeferredResultWrapper> suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    longPollingManager.addSuspendedResult(wrapper);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(1, suspendedRequests.size());
    // Update of role-request -> must be detected
    roleRequest.setState(RoleRequestState.IN_PROGRESS);
    roleRequestService.save(roleRequest);
    // Check must be called twice, because first detect the change and second remove
    // ended deferred result (from some reason is not invoked method
    // result.onCompleted)
    getHelper().waitForResult(null, 1, 1);
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    getHelper().waitForResult(null, 1, 1);
    longPollingManager.checkDeferredRequests(IdmIdentityDto.class);
    suspendedRequests = defaultPollingManager.getSuspendedRequests();
    assertEquals(0, suspendedRequests.size());
    // Clear deferred result and subscribers
    defaultPollingManager.getSuspendedRequests().clear();
    defaultPollingManager.getRegistredSubscribers().clear();
}
Also used : LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 4 with CheckLongPollingResult

use of eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult in project CzechIdMng by bcvsolutions.

the class SysSystemController method checkRunningSyncs.

/**
 * Long polling for check sync in progress for given system
 *
 * @param backendId - system ID
 *
 * @return DeferredResult<OperationResultDto>, where:
 *
 * - RUNNING = Some sync are not resolved, but some sync was changed (since previous check).
 * - NOT_EXECUTED = Deferred-result expired
 * - BLOCKED - Long polling is disabled
 */
@ResponseBody
@RequestMapping(value = "{backendId}/check-running-sync", method = RequestMethod.GET)
@PreAuthorize("hasAuthority('" + AccGroupPermission.SYSTEM_READ + "')")
@ApiOperation(value = "Check changes of unresloved sync for the system (Long-polling request).", nickname = "checkRunningSyncs", tags = { SysSystemController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = AccGroupPermission.SYSTEM_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = AccGroupPermission.SYSTEM_READ, description = "") }) })
public DeferredResult<OperationResultDto> checkRunningSyncs(@ApiParam(value = "System's uuid identifier.", required = true) @PathVariable @NotNull String backendId) {
    SysSystemDto dto = getDto(backendId);
    if (dto == null) {
        throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", backendId));
    }
    UUID systemId = dto.getId();
    DeferredResultWrapper result = new // 
    DeferredResultWrapper(// 
    systemId, // 
    dto.getClass(), new // 
    DeferredResult<OperationResultDto>(30000l, // 
    new OperationResultDto(OperationState.NOT_EXECUTED)));
    // 
    result.onCheckResultCallback(new CheckLongPollingResult() {

        @Override
        public void checkDeferredResult(DeferredResult<OperationResultDto> result, LongPollingSubscriber subscriber) {
            checkDeferredRequest(result, subscriber);
        }
    });
    // If isn't long polling enabled, then Blocked response will be sent.
    if (!longPollingManager.isLongPollingEnabled()) {
        result.getResult().setResult(new OperationResultDto(OperationState.BLOCKED));
        return result.getResult();
    }
    longPollingManager.addSuspendedResult(result);
    return result.getResult();
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) UUID(java.util.UUID) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with CheckLongPollingResult

use of eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult in project CzechIdMng by bcvsolutions.

the class DefaultLongPollingManager method checkDeferredRequests.

@Override
public void checkDeferredRequests(Class<? extends AbstractDto> type) {
    Assert.notNull(type, "Class type cannot be null!");
    this.suspendedRequests.stream().filter(request -> request.getResult().isSetOrExpired()).forEach(request -> {
        this.suspendedRequests.remove(request);
    });
    // 
    this.suspendedRequests.stream().filter(// 
    request -> type.equals(request.getType())).forEach(request -> {
        // 
        LongPollingSubscriber subscriber = null;
        if (this.registredSubscribers.containsKey(request.getEntityId())) {
            subscriber = this.registredSubscribers.get(request.getEntityId());
        } else {
            subscriber = new LongPollingSubscriber(request.getEntityId(), type);
        }
        CheckLongPollingResult checkResultCallback = request.getCheckResultCallback();
        if (checkResultCallback != null) {
            subscriber.setLastUsingSubscriber(ZonedDateTime.now());
            checkResultCallback.checkDeferredResult(request.getResult(), subscriber);
        }
    });
}
Also used : ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) DeferredResult(org.springframework.web.context.request.async.DeferredResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Scheduled(org.springframework.scheduling.annotation.Scheduled) ModifiedFromFilter(eu.bcvsolutions.idm.core.api.dto.filter.ModifiedFromFilter) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) AbstractDto(eu.bcvsolutions.idm.core.api.dto.AbstractDto) ReadDtoService(eu.bcvsolutions.idm.core.api.service.ReadDtoService) Service(org.springframework.stereotype.Service) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult) Map(java.util.Map) LongPollingManager(eu.bcvsolutions.idm.core.model.service.api.LongPollingManager) Sort(org.springframework.data.domain.Sort) Direction(org.springframework.data.domain.Sort.Direction) DeferredResultWrapper(eu.bcvsolutions.idm.core.rest.DeferredResultWrapper) Logger(org.slf4j.Logger) AbstractEntity_(eu.bcvsolutions.idm.core.api.entity.AbstractEntity_) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PageRequest(org.springframework.data.domain.PageRequest) OperationState(eu.bcvsolutions.idm.core.api.domain.OperationState) UUID(java.util.UUID) LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Lazy(org.springframework.context.annotation.Lazy) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Assert(org.springframework.util.Assert) LongPollingSubscriber(eu.bcvsolutions.idm.core.rest.LongPollingSubscriber) CheckLongPollingResult(eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult)

Aggregations

OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)9 CheckLongPollingResult (eu.bcvsolutions.idm.core.model.service.api.CheckLongPollingResult)9 DeferredResultWrapper (eu.bcvsolutions.idm.core.rest.DeferredResultWrapper)9 LongPollingSubscriber (eu.bcvsolutions.idm.core.rest.LongPollingSubscriber)9 DeferredResult (org.springframework.web.context.request.async.DeferredResult)6 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)5 Test (org.junit.Test)5 AbstractCoreWorkflowIntegrationTest (eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest)4 UUID (java.util.UUID)4 ApiOperation (io.swagger.annotations.ApiOperation)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)2 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)2 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)2 AbstractSysSyncConfigDto (eu.bcvsolutions.idm.acc.dto.AbstractSysSyncConfigDto)1 SysSyncActionLogDto (eu.bcvsolutions.idm.acc.dto.SysSyncActionLogDto)1 SysSyncItemLogDto (eu.bcvsolutions.idm.acc.dto.SysSyncItemLogDto)1 SysSyncLogDto (eu.bcvsolutions.idm.acc.dto.SysSyncLogDto)1