Commit: f0438c2

Commit Details

SHAf0438c2cbc2d838bb66a4d8129dd25b5d48d28ee
Treeb8e53d050248c8b632fa3de86003489acae4670b
Author<f69e50@finnacloud.com> 1766443042 +0300
Committer<f69e50@finnacloud.com> 1766443042 +0300
Message
increment once more
GPG Signature
-----BEGIN PGP SIGNATURE-----

iQJSBAABCAA8FiEEWJb139mJI+vZ81KkoAIVSUsXI0oFAmlJyCIeHHNvcGhpYS5l
cmFzbGFuQGZpbm5hY2xvdWQuY29tAAoJEKACFUlLFyNKtrgP/04FZPSa4Hmx9WpR
gOamzjIXc+1+pBvGQDairLZU6rtDSgNkoryrsqOOLIXZWkkxZ70q8/aXQBFThr+t
YyDIz85u9GiwPOV8o1X4sxzF7bupOd6YmOOPqS2vco1aibpB8w9N9EwlMzW95Otw
vnQ/h0kz8MTp0wfXeRJqvLg8DVPnLgW70ly1TZQe19jEA4NwBZOyK2ksTis8iycX
HUM4WW8Velo8O+OtygAbwISr2dILKvkclTLn9kgfpN5esvBvJu4K+xA5T5alDchc
PY3FFeZteQf3GX4v0EH8K3c/q8OcQFMxuRGO31uYGqNbjbOB7zdZVs3N0B1m1efv
vhCgZ6gyxlz0kiozgO6Sx5WFOzHuTlguCK+x0JOtzCokRL/VLiPzmW5Umt280eJz
AlD7u7Dah3khoqDkzXfCL1lxch9tRMVMUYWRMayxC3iculCFM6OCLgzY9hay5y1j
WP1ZOdy/x9jKPoLv5USnw0KlH4rZIHH+aDMDEzWmWUWYDpShylG7sKafXg75bnho
5LbAfCucnDrkjN+rx1dxaKrZswQlIJ8iZ5Q6DzHnDzl4TaFcN1E3exS+xp46H7va
SbR94UfpCyjpTQ2hHdOS7XHINFUDIzWF3mUZ8gqyEANiqpgJ+RcOGX66s69oILpr
vD20SIdMHWDqJH3G6GMNsg1g5/QU
=YrcG
-----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