Commit: 690c1f6

Commit Details

SHA690c1f6ce426c62c29e87ac132dda0f8125192ff
Tree5a67fd55b56735ab4e469c7d06c1c73c4c2b4c20
Author<f69e50@finnacloud.com> 1766368110 +0300
Committer<f69e50@finnacloud.com> 1766368110 +0300
Message
initialize backend structure with controllers, DTOs, and configuration files
GPG Signature
-----BEGIN PGP SIGNATURE-----

iQJSBAABCAA8FiEEWJb139mJI+vZ81KkoAIVSUsXI0oFAmlIo24eHHNvcGhpYS5l
cmFzbGFuQGZpbm5hY2xvdWQuY29tAAoJEKACFUlLFyNKwXYP/RvWx8mxXoZbKEVA
wQFC9UnzcoL/lElB5QMr9opKzRv4uGgFkKMDhbSnqE6NoET5H5VanOFQ9u5a4Khi
9PBTLIEBjbEqA1trC+aTDk3EplVtQYYbSn19CdMSCW7FXJNSg0IiyWKA44iH8Ts0
Xcxh59m6WcwvRDhxQDy6hCXqUa9ISNNk75KRnJS/qRGIEy94DwUYxVfCJpAfzyVu
VmdinE6kZM2GDj8MBTPQTzi6hMf/e9CcAg51tf4oNtd9tnW8QKpTsPsFy434VUDh
Vxtv/oAJ5tuTIprs2BSnyZ6Kb8RDwDdmQyuKjZMUIwZTH/TCE85SZFf5sa5tpYOH
2KekT52ffZgKw/DUtpNosW6qeHgCJlSvwY7BW7M90X5xJuahrKS04lEDBO04cIRn
bg0ayPFTp7Idhl1OuTRhWS6e344g44mJ/9sZK1sXd/0U8OKBbytk35AnCIPCEdSX
8vgjqBR9Wt3A/Kel5j2VcUFDhrAR72a9lJiQHNBicvcVu9Nd41vDnEwUDdNQv6Uc
6omEz3pkVkq+89/eW1KQM8LvrIuGQ/wIUgykvCNSCQ5oba2fjtAXzI+SmxpeCWOz
jTKZOEJyhIQE7uvaUj6/0D2JwlxbMG27fcUN7N3aKv6mSVP7hYnHaUbRLQ+f8/xU
VfU776FkUXS+4CfSooHtu+ioul9O
=ZuqE
-----END PGP SIGNATURE-----

✓ Verified

File: src/main/java/com/paymentlink/config/LocalizationInterceptor.java

1 package com.paymentlink.config;
2
3 import com.paymentlink.exception.RegionBlockedException;
4 import com.paymentlink.model.dto.CountryInfo;
5 import com.paymentlink.service.GeoIpService;
6 import com.paymentlink.service.RegionService;
7 import jakarta.servlet.http.HttpServletRequest;
8 import jakarta.servlet.http.HttpServletResponse;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.stereotype.Component;
13 import org.springframework.web.servlet.HandlerInterceptor;
14
15 @Component
16 public class LocalizationInterceptor implements HandlerInterceptor {
17
18 private static final Logger logger = LoggerFactory.getLogger(LocalizationInterceptor.class);
19
20 private final GeoIpService geoIpService;
21 private final RegionService regionService;
22
23 @Value("${region.blocking.enabled:true}")
24 private boolean regionBlockingEnabled;
25
26 public LocalizationInterceptor(GeoIpService geoIpService, RegionService regionService) {
27 this.geoIpService = geoIpService;
28 this.regionService = regionService;
29 }
30
31 @Override
32 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
33 // Check if client already has region preference (from cookie or header)
34 String clientRegion = request.getHeader("X-User-Region");
35
36 CountryInfo countryInfo;
37
38 if (clientRegion != null && !clientRegion.isEmpty()) {
39 // Client already selected a region, use that instead of GeoIP
40 logger.debug("Using client-selected region: {}", clientRegion);
41 countryInfo = regionService.getRegion(clientRegion) != null
42 ? buildCountryInfoFromRegion(regionService.getRegion(clientRegion))
43 : geoIpService.getCountryFromRequest(request);
44 } else {
45 // Only do GeoIP lookup if no region preference exists
46 // This typically happens once on first visit
47 countryInfo = geoIpService.getCountryFromRequest(request);
48 logger.debug("GeoIP lookup performed for first-time visitor from IP");
49 }
50
51 // Set country info in request attributes for controllers to use
52 request.setAttribute("countryCode", countryInfo.getCountryCode());
53 request.setAttribute("countryName", countryInfo.getCountryName());
54 request.setAttribute("languageCode", countryInfo.getLanguageCode());
55 request.setAttribute("currencyCode", countryInfo.getCurrencyCode());
56
57 logger.debug("Request from country: {} ({}), language: {}, currency: {}",
58 countryInfo.getCountryName(),
59 countryInfo.getCountryCode(),
60 countryInfo.getLanguageCode(),
61 countryInfo.getCurrencyCode());
62
63 // Check if region blocking is enabled
64 if (regionBlockingEnabled) {
65 // Check if country is enabled
66 if (countryInfo.getEnabled() == null || !countryInfo.getEnabled()) {
67 boolean isEnabled = regionService.isCountryEnabled(countryInfo.getCountryCode());
68 if (!isEnabled) {
69 logger.warn("Blocked request from disabled region: {} ({})",
70 countryInfo.getCountryName(),
71 countryInfo.getCountryCode());
72 throw new RegionBlockedException(countryInfo.getCountryCode(), countryInfo.getCountryName());
73 }
74 }
75 }
76
77 return true;
78 }
79
80 private CountryInfo buildCountryInfoFromRegion(com.paymentlink.model.entity.Region region) {
81 return CountryInfo.builder()
82 .countryCode(region.getCountryCode())
83 .countryName(region.getCountryName())
84 .languageCode(region.getLanguageCode())
85 .currencyCode(region.getCurrencyCode())
86 .enabled(region.getEnabled())
87 .build();
88 }
89 }
90