Search in sources :

Example 21 with ResultCodeException

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

the class AbstractRecoverableService method deploy.

@Override
@Transactional
public List<DTO> deploy(IdmAttachmentDto attachment, BasePermission... permission) {
    Assert.notNull(attachment, "Attachment is required.");
    UUID attachmentId = attachment.getId();
    Assert.notNull(attachmentId, "Persisted attachment is required.");
    InputStream attachmentData = attachmentManager.getAttachmentData(attachmentId);
    Assert.notNull(attachmentData, "Attachment data is required.");
    // 
    String attachmentName = attachment.getName();
    if (attachment.getMimetype().equals(MediaType.APPLICATION_XML_VALUE) || attachment.getMimetype().equals(MediaType.TEXT_XML_VALUE)) {
        LOG.debug("Single resource [{}] will extracted and deployed.", attachmentName);
        return Lists.newArrayList(deploy(attachmentName, attachmentData));
    }
    // 
    LOG.debug("Archive [{}] will extracted and deployed.", attachmentName);
    Map<String, DTO> deploedResources = new HashMap<>();
    File zipFile = attachmentManager.createTempFile();
    Path zipFolder = attachmentManager.createTempDirectory(null);
    try {
        // copy archive
        Files.copy(attachmentData, zipFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        // and extract
        ZipUtils.extract(zipFile, zipFolder.toString());
        // 
        File[] listFiles = zipFolder.toFile().listFiles();
        LOG.debug("Found [{}] resources on location [{}]", listFiles == null ? 0 : listFiles.length, attachmentName);
        // 
        if (ArrayUtils.isEmpty(listFiles)) {
            return Lists.newArrayList();
        }
        for (File resource : listFiles) {
            try {
                DTO deployedResource = deploy(resource.getName(), new FileInputStream(resource), permission);
                String resourceCode = deployedResource.getCode();
                // log error, if resource with the same code was found twice in one resource
                if (deploedResources.containsKey(resourceCode)) {
                    LOG.error("More templates with code [{}] found on the same location [{}].", resourceCode, attachmentName);
                }
                // last one wins
                deploedResources.put(resourceCode, deployedResource);
            } catch (IOException ex) {
                LOG.error("Failed get input stream from, file name [{}].", resource.getName(), ex);
            }
        }
        // 
        LOG.info("Redeployed [{}] resources from location [{}]", deploedResources.size(), attachmentName);
        return Lists.newArrayList(deploedResources.values());
    } catch (Exception ex) {
        throw new ResultCodeException(CoreResultCode.DEPLOY_ERROR, ImmutableMap.of("path", attachmentName), ex);
    } finally {
        FileUtils.deleteQuietly(zipFile);
        FileUtils.deleteQuietly(zipFolder.toFile());
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) UUID(java.util.UUID) File(java.io.File) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with ResultCodeException

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

the class AbstractRecoverableService method backup.

@Override
public File backup(DTO dto) {
    Marshaller jaxbMarshaller = initJaxbMarshaller();
    // 
    String directory = getBackupFolder();
    File backupFolder = new File(directory);
    if (!backupFolder.exists()) {
        boolean success = backupFolder.mkdirs();
        // if make dir after check if exist, throw error.
        if (!success) {
            LOG.error("Backup for resource [{}] failed, backup folder path: [{}] can't be created.", dto.getCode(), backupFolder.getAbsolutePath());
            throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()));
        }
    }
    // 
    T type = toType(dto);
    File file = new File(getBackupFileName(directory, dto));
    try {
        String xsdLocation = getXsdLocation();
        if (StringUtils.isNotBlank(xsdLocation)) {
            jaxbMarshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, xsdLocation);
        }
        jaxbMarshaller.marshal(type, file);
        // 
        return file;
    } catch (JAXBException e) {
        LOG.error("Backup for template: {} failed", dto.getCode());
        throw new ResultCodeException(CoreResultCode.BACKUP_FAIL, ImmutableMap.of("code", dto.getCode()), e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) File(java.io.File)

Example 23 with ResultCodeException

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

the class ParameterConverter method toBigDecimal.

/**
 * Converts parameter to {@code Double} from given parameters.
 *
 * @param parameters
 * @param parameterName
 * @return
 * @since 10.3.0
 */
public BigDecimal toBigDecimal(Map<String, Object> parameters, String parameterName) {
    Assert.notNull(parameters, "Input parameters are required.");
    Assert.notNull(parameterName, "Parameter name is required.");
    // 
    Object value = parameters.get(parameterName);
    if (value == null) {
        return null;
    }
    if (value instanceof BigDecimal) {
        return (BigDecimal) value;
    }
    // 
    String valueAsString = toString(value);
    if (StringUtils.isNotEmpty(valueAsString)) {
        try {
            return new BigDecimal((String) valueAsString);
        } catch (NumberFormatException ex) {
            throw new ResultCodeException(CoreResultCode.BAD_VALUE, ImmutableMap.of(parameterName, valueAsString), ex);
        }
    }
    return null;
}
Also used : ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) BigDecimal(java.math.BigDecimal)

Example 24 with ResultCodeException

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

the class ContractGuaranteeSaveProcessor method checkControlledBySlices.

/**
 * Test if contract of the given contract guarantee has some slices.
 *
 * @param guarantee
 * @return
 */
private void checkControlledBySlices(EntityEvent<IdmContractGuaranteeDto> event) {
    IdmContractGuaranteeDto guarantee = event.getContent();
    if (getBooleanProperty(ContractSliceManager.SKIP_CHECK_FOR_SLICES, event.getProperties())) {
        return;
    }
    UUID contract = guarantee.getIdentityContract();
    IdmContractSliceFilter sliceFilter = new IdmContractSliceFilter();
    sliceFilter.setParentContract(contract);
    if (contract != null && sliceService.count(sliceFilter) > 0) {
        throw new ResultCodeException(CoreResultCode.CONTRACT_IS_CONTROLLED_GUARANTEE_CANNOT_BE_MODIFIED, ImmutableMap.of("contractId", contract));
    }
}
Also used : IdmContractGuaranteeDto(eu.bcvsolutions.idm.core.api.dto.IdmContractGuaranteeDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmContractSliceFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmContractSliceFilter) UUID(java.util.UUID)

Example 25 with ResultCodeException

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

the class DelegationDefinitionSaveProcessor method process.

@Override
public EventResult<IdmDelegationDefinitionDto> process(EntityEvent<IdmDelegationDefinitionDto> event) {
    IdmDelegationDefinitionDto dto = event.getContent();
    if (!service.isNew(dto)) {
        throw new ResultCodeException(CoreResultCode.DELEGATION_DEFINITION_CANNOT_BE_UPDATED);
    }
    // Validations
    UUID delegateId = dto.getDelegate();
    UUID delegatorId = dto.getDelegator();
    Assert.notNull(delegateId, "Delegate ID cannot be null!");
    Assert.notNull(delegatorId, "Delegator ID cannot be null!");
    if (delegateId.equals(delegatorId)) {
        throw new ResultCodeException(CoreResultCode.DELEGATION_DEFINITION_DELEGATOR_AND_DELEGATE_ARE_SAME, ImmutableMap.of("identity", delegateId));
    }
    dto = service.saveInternal(dto);
    event.setContent(dto);
    return new DefaultEventResult<>(event, this);
}
Also used : IdmDelegationDefinitionDto(eu.bcvsolutions.idm.core.api.dto.IdmDelegationDefinitionDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) UUID(java.util.UUID)

Aggregations

ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)430 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)107 ApiOperation (io.swagger.annotations.ApiOperation)104 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)101 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)99 UUID (java.util.UUID)90 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)89 Test (org.junit.Test)70 Transactional (org.springframework.transaction.annotation.Transactional)54 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)53 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)53 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)49 IOException (java.io.IOException)48 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)47 ResponseEntity (org.springframework.http.ResponseEntity)43 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)38 ArrayList (java.util.ArrayList)33 HashMap (java.util.HashMap)31 IdmPasswordPolicyDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordPolicyDto)27 OperationResult (eu.bcvsolutions.idm.core.api.entity.OperationResult)26