Skip to content
Snippets Groups Projects
Commit c8b0ad8a authored by Lukasz Lopatowski's avatar Lukasz Lopatowski
Browse files

Merge remote-tracking branch 'origin/develop' into develop

parents f38bf380 5b0a7e7d
No related branches found
No related tags found
1 merge request!273Release 1.8.0 update
Pipeline #95364 passed
......@@ -11,8 +11,10 @@ import net.geant.nmaas.portal.persistent.entity.Role;
import net.geant.nmaas.portal.persistent.entity.User;
import net.geant.nmaas.portal.persistent.repositories.ContentRepository;
import net.geant.nmaas.portal.persistent.repositories.UserRepository;
import net.geant.nmaas.portal.service.ApplicationService;
import net.geant.nmaas.portal.service.ConfigurationManager;
import net.geant.nmaas.portal.service.DomainService;
import net.geant.nmaas.portal.service.impl.FormioSanitizerService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -157,6 +159,9 @@ public class PortalConfig {
@Autowired
private ConfigurationManager configurationManager;
@Autowired
private ApplicationService applicationService;
@Override
public void afterPropertiesSet() {
ConfigurationView configurationView = ConfigurationView.builder()
......@@ -179,6 +184,8 @@ public class PortalConfig {
} catch (OnlyOneConfigurationSupportedException e) {
log.debug("Portal configuration already exists. Skipping initialization.");
}
applicationService.checkAllFormioTemplate();
}
};
}
......
......@@ -326,6 +326,7 @@ public class ApplicationController extends AppBaseController {
application.setCreationDate(LocalDateTime.now());
this.applicationService.setMissingProperties(application, appId);
ApplicationServiceImpl.clearIds(application);
this.applicationService.checkAndUpdateFormioTemplate(application);
this.applicationService.update(application);
// create, add and persist new application version
......
......@@ -36,4 +36,8 @@ public interface ApplicationService {
boolean exists(String name, String version);
Application checkAndUpdateFormioTemplate(Application application);
void checkAllFormioTemplate();
}
......@@ -2,6 +2,7 @@ package net.geant.nmaas.portal.service.impl;
import freemarker.template.Configuration;
import freemarker.template.Template;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.geant.nmaas.nmservice.configuration.entities.ConfigFileTemplate;
......@@ -21,7 +22,6 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import jakarta.transaction.Transactional;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.Comparator;
......@@ -41,12 +41,15 @@ public class ApplicationServiceImpl implements ApplicationService {
private final ApplicationRepository applicationRepository;
private final ApplicationEventPublisher eventPublisher;
private final FormioSanitizerService formioSanitizerService;
@Override
@Transactional
@CachePut("applicationBaseS")
public Application update(Application application) {
checkApp(application);
checkAndUpdateFormioTemplate(application);
Application saved = applicationRepository.save(application);
generateApplicationListUpdatedEvent(saved, UPDATED);
return saved;
......@@ -221,4 +224,25 @@ public class ApplicationServiceImpl implements ApplicationService {
return versions;
}
@Override
public Application checkAndUpdateFormioTemplate(Application application) {
application.getConfigWizardTemplate().setTemplate(formioSanitizerService.sanitizeFormioJson(application.getConfigWizardTemplate().getTemplate()));
return application;
}
@Override
public void checkAllFormioTemplate() {
log.warn("Checking formio template for # in keys ");
this.findAll().forEach(app -> {
log.warn("Sanitize formio wizards keys for app {}", app.getId());
if(app.getConfigWizardTemplate() != null) {
app.getConfigWizardTemplate().setTemplate(formioSanitizerService.sanitizeFormioJson(app.getConfigWizardTemplate().getTemplate()));
log.warn("sanitezed done", app.getConfigWizardTemplate().getTemplate());
}
if(app.getConfigUpdateWizardTemplate() != null) {
app.getConfigUpdateWizardTemplate().setTemplate(formioSanitizerService.sanitizeFormioJson(app.getConfigUpdateWizardTemplate().getTemplate()));
}
applicationRepository.save(app);
});
}
}
package net.geant.nmaas.portal.service.impl;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.stereotype.Service;
@Service
public class FormioSanitizerService {
private final ObjectMapper objectMapper;
public FormioSanitizerService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public String sanitizeFormioJson(String json) {
try {
JsonNode root = objectMapper.readTree(json);
sanitizeKeysRecursively(root);
return objectMapper.writeValueAsString(root);
} catch (Exception e) {
throw new RuntimeException("Error parsing formio", e);
}
}
/**
* Iterate over JSON and change # to _dot_ in key fields
*/
private void sanitizeKeysRecursively(JsonNode node) {
if (node.isObject()) {
ObjectNode objNode = (ObjectNode) node;
if (objNode.has("key")) {
String key = objNode.get("key").asText();
if (key.contains("#")) {
objNode.put("key", key.replace("#", "_dot_"));
}
}
objNode.fields().forEachRemaining(entry -> {
sanitizeKeysRecursively(entry.getValue());
});
} else if (node.isArray()) {
for (JsonNode item : node) {
sanitizeKeysRecursively(item);
}
}
}
}
......@@ -41,12 +41,13 @@ public class ApplicationServiceImplTest {
ApplicationRepository applicationRepository = mock(ApplicationRepository.class);
ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
FormioSanitizerService formioSanitizerService = mock(FormioSanitizerService.class);
ApplicationServiceImpl applicationService;
@BeforeEach
void setup(){
applicationService = new ApplicationServiceImpl(applicationRepository, eventPublisher);
applicationService = new ApplicationServiceImpl(applicationRepository, eventPublisher, formioSanitizerService);
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment