| 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 |
|
| 34 |
String clientRegion = request.getHeader("X-User-Region"); |
| 35 |
|
| 36 |
CountryInfo countryInfo; |
| 37 |
|
| 38 |
if (clientRegion != null && !clientRegion.isEmpty()) { |
| 39 |
|
| 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 |
|
| 46 |
|
| 47 |
countryInfo = geoIpService.getCountryFromRequest(request); |
| 48 |
logger.debug("GeoIP lookup performed for first-time visitor from IP"); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 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 |
|
| 64 |
if (regionBlockingEnabled) { |
| 65 |
|
| 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 |
|