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

← View file content

File Content at Commit 188fc92

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

Commits

Commit Author Date Message File SHA Actions
f0438c2 <f69e50@finnacloud.com> 1766443042 +0300 12/22/2025, 10:37:22 PM increment once more fd20b56 View
188fc92 <f69e50@finnacloud.com> 1766442998 +0300 12/22/2025, 10:36:38 PM increment fd20b56 Hide
4617f76 <f69e50@finnacloud.com> 1766442953 +0300 12/22/2025, 10:35:53 PM rename branch from main to master oops fd20b56 View
e6d1548 <f69e50@finnacloud.com> 1766442769 +0300 12/22/2025, 10:32:49 PM add initial test workflow file fd20b56 View
9c24ca4 <f69e50@finnacloud.com> 1766442705 +0300 12/22/2025, 10:31:45 PM add CI configuration and test script for Jenkins build fd20b56 View
690c1f6 <f69e50@finnacloud.com> 1766368110 +0300 12/22/2025, 1:48:30 AM initialize backend structure with controllers, DTOs, and configuration files fd20b56 View