Search in sources :

Example 16 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class DefaultTestHelper method waitForResult.

@Override
public void waitForResult(Function<String, Boolean> continueFunction, Integer interationWaitMilis, Integer iterationCount) {
    int maxCounter = (iterationCount == null) ? 50 : (iterationCount > 300) ? 300 : iterationCount;
    int waitTime = interationWaitMilis == null ? 300 : interationWaitMilis;
    // 
    int counter = 0;
    while ((continueFunction == null ? true : continueFunction.apply(null)) && (counter < maxCounter)) {
        counter++;
        try {
            Thread.sleep(waitTime);
        } catch (InterruptedException ex) {
            throw new CoreException(ex);
        }
    }
    ;
    // 
    if (continueFunction != null && continueFunction.apply(null)) {
        throw new IllegalStateException("Continue function is defined and timeout exceeded before function returned false (~ before complete).");
    }
}
Also used : CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException)

Example 17 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class DefaultSchedulerManager method find.

@Override
public Page<Task> find(TaskFilter filter, Pageable pageable) {
    try {
        List<Task> tasks = new ArrayList<>();
        // load scheduled tasks
        for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(DEFAULT_GROUP_NAME))) {
            Task task = getTask(jobKey);
            // 
            if (passFilter(task, filter)) {
                tasks.add(task);
            }
        }
        // 
        // pageable is required internally
        Pageable internalPageable;
        if (pageable == null) {
            internalPageable = PageRequest.of(0, Integer.MAX_VALUE);
        } else {
            internalPageable = pageable;
        }
        // apply "naive" sort and pagination
        tasks = tasks.stream().sorted((taskOne, taskTwo) -> {
            Sort sort = internalPageable.getSort();
            if (internalPageable.getSort() == null) {
                return 0;
            }
            int compareAscValue = 0;
            boolean asc = true;
            // "naive" sort implementation
            Order orderForTaskType = sort.getOrderFor(Task.PROPERTY_TASK_TYPE);
            if (orderForTaskType != null) {
                asc = orderForTaskType.isAscending();
                compareAscValue = taskOne.getTaskType().getSimpleName().compareTo(taskTwo.getTaskType().getSimpleName());
            }
            Order orderForDescription = sort.getOrderFor(Task.PROPERTY_DESCRIPTION);
            if (orderForDescription != null) {
                asc = orderForDescription.isAscending();
                compareAscValue = StringUtils.compare(taskOne.getDescription(), taskTwo.getDescription());
            }
            Order orderForInstance = sort.getOrderFor(Task.PROPERTY_INSTANCE_ID);
            if (orderForInstance != null) {
                asc = orderForInstance.isAscending();
                compareAscValue = StringUtils.compare(taskOne.getInstanceId(), taskTwo.getInstanceId());
            }
            // 
            return asc ? compareAscValue : compareAscValue * -1;
        }).collect(Collectors.toList());
        // "naive" pagination
        int first = internalPageable.getPageNumber() * internalPageable.getPageSize();
        int last = internalPageable.getPageSize() + first;
        List<Task> taskPage = tasks.subList(first < tasks.size() ? first : tasks.size() > 0 ? tasks.size() - 1 : 0, last < tasks.size() ? last : tasks.size());
        // 
        return new PageImpl<>(taskPage, internalPageable, tasks.size());
    } catch (org.quartz.SchedulerException ex) {
        throw new CoreException(ex);
    }
}
Also used : Order(org.springframework.data.domain.Sort.Order) PageImpl(org.springframework.data.domain.PageImpl) Task(eu.bcvsolutions.idm.core.scheduler.api.dto.Task) ArrayList(java.util.ArrayList) JobKey(org.quartz.JobKey) Pageable(org.springframework.data.domain.Pageable) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Sort(org.springframework.data.domain.Sort)

Example 18 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class DefaultEntityEventManagerIntergationTest method testNextProcessorIsNotExecutedAfterException.

@Test
public void testNextProcessorIsNotExecutedAfterException() {
    EntityEvent<TestContent> event = new CoreEvent<>(CoreEventType.CREATE, new TestContent());
    event.getContent().setException(3);
    try {
        manager.process(event);
    } catch (CoreException ex) {
    // ok
    }
    Assert.assertEquals("2", event.getContent().getText());
}
Also used : CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) CoreEvent(eu.bcvsolutions.idm.core.api.event.CoreEvent) TestContent(eu.bcvsolutions.idm.core.event.TestContent) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 19 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class AbstractReadWriteDtoControllerRestTest method testFindByCodeable.

/**
 * Test search by external identifier, if DTO implements {@link ExternalIdentifiable}
 *
 * @throws Exception
 */
@Test
public void testFindByCodeable() {
    if (!DataFilter.class.isAssignableFrom(getController().getFilterClass())) {
        LOG.warn("Controller [{}] doesn't support DataFilter. Find by codeable will not be tested.", getController().getClass());
        return;
    }
    // 
    DTO dto = prepareDto();
    if (!(dto instanceof Codeable)) {
        // ignore test
        return;
    }
    Codeable codeable = (Codeable) dto;
    if (StringUtils.isEmpty(codeable.getCode())) {
        throw new CoreException("Code has to be set by #prepareDto method, its required by default");
    }
    // 
    DTO createdDto = createDto(dto);
    // mock dto
    createDto(prepareDto());
    // 
    MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
    parameters.set(DataFilter.PARAMETER_CODEABLE_IDENTIFIER, codeable.getCode());
    // 
    List<DTO> results = find(parameters);
    // 
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(createdDto.getId(), results.get(0).getId());
    // 
    if (supportsAutocomplete()) {
        results = autocomplete(parameters);
        // 
        Assert.assertEquals(1, results.size());
        Assert.assertEquals(createdDto.getId(), results.get(0).getId());
    } else {
        LOG.info("Controller [{}] doesn't support autocomplete method. Method will not be tested.", getController().getClass());
    }
    // 
    Assert.assertEquals(1, count(parameters));
}
Also used : DataFilter(eu.bcvsolutions.idm.core.api.dto.filter.DataFilter) Codeable(eu.bcvsolutions.idm.core.api.domain.Codeable) ExternalCodeable(eu.bcvsolutions.idm.core.api.domain.ExternalCodeable) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 20 with CoreException

use of eu.bcvsolutions.idm.core.api.exception.CoreException in project CzechIdMng by bcvsolutions.

the class AdUserConnectorType method getCertificateFromAD.

/**
 * Try to find authority for this certificate in AD (we want to return certificate with the biggest validity).
 */
private X509Certificate getCertificateFromAD(X509Certificate serverCrt, String port, String host, String user, String password) {
    X509Certificate resultCertificate = null;
    DirContext ldapContext = null;
    try {
        // Init LDAP context.
        Hashtable<String, String> ldapEnv = getAdEnvironment(host, "389", user, password, false);
        ldapContext = new InitialDirContext(ldapEnv);
        boolean continueSearching = true;
        resultCertificate = serverCrt;
        while (continueSearching) {
            X509Certificate authorityCrtOnAD = findAuthorityCrtOnAD(resultCertificate, ldapContext);
            if (authorityCrtOnAD != null) {
                // Validate certificate by found authority.
                try {
                    CertificateUtils.verifyCertificate(resultCertificate, authorityCrtOnAD);
                } catch (CertificateException ex) {
                    throw new ResultCodeException(AccResultCode.WIZARD_AD_CONNECTOR_CRT_NOT_TRUSTED, ImmutableMap.of("serialNumber", authorityCrtOnAD.getSerialNumber().toString(16).toUpperCase()), ex);
                }
            }
            if (authorityCrtOnAD == null) {
                // No authority certificate was found, previous certificate is result.
                continueSearching = false;
            } else if (authorityCrtOnAD.getIssuerDN() != null && resultCertificate.getIssuerDN() != null && resultCertificate.getIssuerDN().getName().equals(authorityCrtOnAD.getIssuerDN().getName())) {
                // Issuer name in previous and returned authority certificate is same -> returned certificate is result.
                resultCertificate = authorityCrtOnAD;
                continueSearching = false;
            } else if (authorityCrtOnAD.getIssuerDN() == null || Strings.isBlank(authorityCrtOnAD.getIssuerDN().getName())) {
                // Found authority certificate doesn't have issuer -> it is result.
                resultCertificate = authorityCrtOnAD;
                continueSearching = false;
            } else {
                // Next round.
                resultCertificate = authorityCrtOnAD;
            }
        }
    } catch (CommunicationException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_COMMUNICATION_EXCEPTION, ImmutableMap.of("host", host), ex);
    } catch (NamingException ex) {
        throw new ResultCodeException(AccResultCode.WIZARD_AD_OPERATION_FAILED, ImmutableMap.of("dn", serverCrt != null ? serverCrt.getSubjectDN().getName() : host), ex);
    } catch (CertificateException ex) {
        throw new CoreException(ex.getLocalizedMessage(), ex);
    } finally {
        if (ldapContext != null) {
            try {
                ldapContext.close();
            } catch (NamingException e) {
                // Only log it.
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }
    return resultCertificate;
}
Also used : CommunicationException(javax.naming.CommunicationException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CertificateException(java.security.cert.CertificateException) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) InitialDirContext(javax.naming.directory.InitialDirContext) X509Certificate(java.security.cert.X509Certificate)

Aggregations

CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)64 UUID (java.util.UUID)16 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)15 Test (org.junit.Test)14 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)10 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)9 Field (java.lang.reflect.Field)9 Embedded (eu.bcvsolutions.idm.core.api.domain.Embedded)8 AbstractDto (eu.bcvsolutions.idm.core.api.dto.AbstractDto)8 BaseEntity (eu.bcvsolutions.idm.core.api.entity.BaseEntity)8 List (java.util.List)8 Requestable (eu.bcvsolutions.idm.core.api.domain.Requestable)7 BaseDto (eu.bcvsolutions.idm.core.api.dto.BaseDto)7 IdmRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRequestDto)7 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)7 RequestOperationType (eu.bcvsolutions.idm.core.api.domain.RequestOperationType)6 IntrospectionException (java.beans.IntrospectionException)6