use of io.gravitee.am.service.exception.CertificateNotFoundException in project gravitee-access-management by gravitee-io.
the class CertificateServiceImpl method update.
@Override
public Single<Certificate> update(String domain, String id, UpdateCertificate updateCertificate, User principal) {
LOGGER.debug("Update a certificate {} for domain {}", id, domain);
return certificateRepository.findById(id).switchIfEmpty(Maybe.error(new CertificateNotFoundException(id))).flatMapSingle(new Function<Certificate, SingleSource<CertificateWithSchema>>() {
@Override
public SingleSource<CertificateWithSchema> apply(Certificate certificate) throws Exception {
return certificatePluginService.getSchema(certificate.getType()).switchIfEmpty(Maybe.error(new CertificatePluginSchemaNotFoundException(certificate.getType()))).flatMapSingle(new Function<String, SingleSource<? extends CertificateWithSchema>>() {
@Override
public SingleSource<? extends CertificateWithSchema> apply(String schema) throws Exception {
return Single.just(new CertificateWithSchema(certificate, objectMapper.readValue(schema, CertificateSchema.class)));
}
});
}
}).flatMap(oldCertificate -> {
Single<Certificate> certificateSingle = Single.create(emitter -> {
Certificate certificateToUpdate = new Certificate(oldCertificate.getCertificate());
certificateToUpdate.setName(updateCertificate.getName());
try {
CertificateSchema certificateSchema = oldCertificate.getSchema();
JsonNode oldCertificateConfiguration = objectMapper.readTree(oldCertificate.getCertificate().getConfiguration());
JsonNode certificateConfiguration = objectMapper.readTree(updateCertificate.getConfiguration());
certificateSchema.getProperties().entrySet().stream().filter(map -> map.getValue().getWidget() != null && "file".equals(map.getValue().getWidget())).map(map -> map.getKey()).forEach(key -> {
try {
String oldFileInformation = oldCertificateConfiguration.get(key).asText();
String fileInformation = certificateConfiguration.get(key).asText();
// file has changed, let's update it
if (!oldFileInformation.equals(fileInformation)) {
JsonNode file = objectMapper.readTree(certificateConfiguration.get(key).asText());
byte[] data = Base64.getDecoder().decode(file.get("content").asText());
certificateToUpdate.setMetadata(Collections.singletonMap(CertificateMetadata.FILE, data));
// update configuration to set the file path
((ObjectNode) certificateConfiguration).put(key, file.get("name").asText());
updateCertificate.setConfiguration(objectMapper.writeValueAsString(certificateConfiguration));
}
} catch (IOException ex) {
LOGGER.error("An error occurs while trying to update certificate binaries", ex);
emitter.onError(ex);
}
});
certificateToUpdate.setConfiguration(updateCertificate.getConfiguration());
certificateToUpdate.setUpdatedAt(new Date());
} catch (Exception ex) {
LOGGER.error("An error occurs while trying to update certificate configuration", ex);
emitter.onError(ex);
}
emitter.onSuccess(certificateToUpdate);
});
return certificateSingle.flatMap(certificate -> certificateRepository.update(certificate)).flatMap(certificate1 -> {
Event event = new Event(Type.CERTIFICATE, new Payload(certificate1.getId(), ReferenceType.DOMAIN, certificate1.getDomain(), Action.UPDATE));
return eventService.create(event).flatMap(__ -> Single.just(certificate1));
}).onErrorResumeNext(ex -> {
LOGGER.error("An error occurs while trying to update a certificate", ex);
throw new TechnicalManagementException("An error occurs while trying to update a certificate", ex);
});
});
}
Aggregations