Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c0956e6e authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

[URGENT] Fix locale if folder not exist on file path

parent 00db8155
No related branches found
No related tags found
No related merge requests found
Pipeline #197728 failed
package eu.europa.ec.edelivery.smp.ui.external; package eu.europa.ec.edelivery.smp.ui.external;
import eu.europa.ec.edelivery.smp.i18n.SMPLocale; import eu.europa.ec.edelivery.smp.i18n.SMPLocale;
import eu.europa.ec.edelivery.smp.logging.SMPLogger;
import eu.europa.ec.edelivery.smp.logging.SMPLoggerFactory;
import eu.europa.ec.edelivery.smp.services.SMPLocaleService; import eu.europa.ec.edelivery.smp.services.SMPLocaleService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.nio.file.Path;
import static eu.europa.ec.edelivery.smp.ui.ResourceConstants.CONTEXT_PATH_PUBLIC_LOCALE; import static eu.europa.ec.edelivery.smp.ui.ResourceConstants.CONTEXT_PATH_PUBLIC_LOCALE;
/** /**
* Provides support for returning locale files required by the ngx-translation Angular library. * Provides support for returning locale files required by the ngx-translation Angular library.
* *
* @since 5.1
* @author Sebastian-Ion TINCU * @author Sebastian-Ion TINCU
* @since 5.1
*/ */
@RestController @RestController
@RequestMapping(value = CONTEXT_PATH_PUBLIC_LOCALE) @RequestMapping(value = CONTEXT_PATH_PUBLIC_LOCALE)
public class LocaleController { public class LocaleController {
private static final SMPLogger LOG = SMPLoggerFactory.getLogger(LocaleController.class);
private static final String DEFAULT_LOCALE_RESOURCE = "/META-INF/resources/ui/assets/i18n/en.json";
private final SMPLocaleService smpLocaleService; private final SMPLocaleService smpLocaleService;
public LocaleController(SMPLocaleService smpLocaleService) { public LocaleController(SMPLocaleService smpLocaleService) {
...@@ -26,7 +33,21 @@ public class LocaleController { ...@@ -26,7 +33,21 @@ public class LocaleController {
@GetMapping(value = "/{code}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE) @GetMapping(value = "/{code}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
@ResponseBody @ResponseBody
public FileSystemResource getLocale(@PathVariable("code") String code) { public Resource getLocale(@PathVariable("code") String code) {
return new FileSystemResource(smpLocaleService.getLocaleFile(SMPLocale.fromCodeDefaultingToEnglish(code))); Path langResourcePath = smpLocaleService.getLocaleFile(SMPLocale.fromCodeDefaultingToEnglish(code));
if (langResourcePath.toFile().exists()) {
LOG.debug("Returning locale file [{}]", langResourcePath.toAbsolutePath());
return new FileSystemResource(langResourcePath);
} else {
LOG.warn("Locale file [{}] does not exist. Return default translation!", langResourcePath.toAbsolutePath());
ClassPathResource defResource = new ClassPathResource(DEFAULT_LOCALE_RESOURCE);
if (defResource.exists()) {
return defResource;
} else {
LOG.error("Default locale file [{}] does not exist in classpath!", DEFAULT_LOCALE_RESOURCE);
return null;
}
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment