|
| 1 | +package io.ipinfo.spring; |
| 2 | + |
| 3 | +import io.ipinfo.api.IPinfoPlus; |
| 4 | +import io.ipinfo.api.model.IPResponsePlus; |
| 5 | +import io.ipinfo.spring.strategies.attribute.AttributeStrategy; |
| 6 | +import io.ipinfo.spring.strategies.attribute.SessionAttributeStrategy; |
| 7 | +import io.ipinfo.spring.strategies.interceptor.BotInterceptorStrategy; |
| 8 | +import io.ipinfo.spring.strategies.interceptor.InterceptorStrategy; |
| 9 | +import io.ipinfo.spring.strategies.ip.IPStrategy; |
| 10 | +import io.ipinfo.spring.strategies.ip.SimpleIPStrategy; |
| 11 | +import jakarta.servlet.http.HttpServletRequest; |
| 12 | +import jakarta.servlet.http.HttpServletResponse; |
| 13 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 14 | + |
| 15 | +public class IPinfoPlusSpring implements HandlerInterceptor { |
| 16 | + |
| 17 | + public static final String ATTRIBUTE_KEY = |
| 18 | + "IPinfoOfficialSparkWrapper.IPResponsePlus"; |
| 19 | + private final IPinfoPlus ii; |
| 20 | + private final AttributeStrategy attributeStrategy; |
| 21 | + private final IPStrategy ipStrategy; |
| 22 | + private final InterceptorStrategy interceptorStrategy; |
| 23 | + |
| 24 | + IPinfoPlusSpring( |
| 25 | + IPinfoPlus ii, |
| 26 | + AttributeStrategy attributeStrategy, |
| 27 | + IPStrategy ipStrategy, |
| 28 | + InterceptorStrategy interceptorStrategy |
| 29 | + ) { |
| 30 | + this.ii = ii; |
| 31 | + this.attributeStrategy = attributeStrategy; |
| 32 | + this.ipStrategy = ipStrategy; |
| 33 | + this.interceptorStrategy = interceptorStrategy; |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String... args) { |
| 37 | + System.out.println( |
| 38 | + "This library is not meant to be run as a standalone jar." |
| 39 | + ); |
| 40 | + System.exit(0); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public boolean preHandle( |
| 45 | + HttpServletRequest request, |
| 46 | + HttpServletResponse response, |
| 47 | + Object handler |
| 48 | + ) throws Exception { |
| 49 | + if (!interceptorStrategy.shouldRun(request)) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + // Don't waste an API call if we already have it. |
| 54 | + // This should only happen for RequestAttributeStrategy and potentially |
| 55 | + // other implementations. |
| 56 | + if (attributeStrategy.hasPlusAttribute(request)) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + String ip = ipStrategy.getIPAddress(request); |
| 61 | + if (ip == null) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + IPResponsePlus ipResponse = ii.lookupIP(ip); |
| 66 | + attributeStrategy.storePlusAttribute(request, ipResponse); |
| 67 | + |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + public static class Builder { |
| 72 | + |
| 73 | + private IPinfoPlus ii = new IPinfoPlus.Builder().build(); |
| 74 | + private AttributeStrategy attributeStrategy = |
| 75 | + new SessionAttributeStrategy(); |
| 76 | + private IPStrategy ipStrategy = new SimpleIPStrategy(); |
| 77 | + private InterceptorStrategy interceptorStrategy = |
| 78 | + new BotInterceptorStrategy(); |
| 79 | + |
| 80 | + public Builder setIPinfo(IPinfoPlus ii) { |
| 81 | + this.ii = ii; |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public Builder attributeStrategy(AttributeStrategy attributeStrategy) { |
| 86 | + this.attributeStrategy = attributeStrategy; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + public Builder ipStrategy(IPStrategy ipStrategy) { |
| 91 | + this.ipStrategy = ipStrategy; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + public Builder interceptorStrategy( |
| 96 | + InterceptorStrategy interceptorStrategy |
| 97 | + ) { |
| 98 | + this.interceptorStrategy = interceptorStrategy; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public IPinfoPlusSpring build() { |
| 103 | + return new IPinfoPlusSpring( |
| 104 | + ii, |
| 105 | + attributeStrategy, |
| 106 | + ipStrategy, |
| 107 | + interceptorStrategy |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments