Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the IPinfoSpringFilter to handle IP data processing earlier in the request cycle. #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/main/java/io/ipinfo/spring/filters/IPinfoSpringFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.ipinfo.spring.filters;

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.model.IPResponse;
import io.ipinfo.spring.strategies.attribute.AttributeStrategy;
import io.ipinfo.spring.strategies.ip.IPStrategy;
import io.ipinfo.spring.strategies.interceptor.InterceptorStrategy;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.logging.Logger;

public class IPinfoSpringFilter implements Filter {
private static final Logger logger = Logger.getLogger(IPinfoSpringFilter.class.getName());

private final IPinfo ii;
private final AttributeStrategy attributeStrategy;
private final IPStrategy ipStrategy;
private final InterceptorStrategy interceptorStrategy;

public IPinfoSpringFilter(IPinfo ii, AttributeStrategy attributeStrategy, IPStrategy ipStrategy, InterceptorStrategy interceptorStrategy) {
this.ii = ii;
this.attributeStrategy = attributeStrategy;
this.ipStrategy = ipStrategy;
this.interceptorStrategy = interceptorStrategy;
}

@Override
public void init(FilterConfig filterConfig) {
logger.info("Initializing IPinfoSpringFilter");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;

if (!interceptorStrategy.shouldRun(httpRequest)) {
chain.doFilter(request, response);
return;
}

if (attributeStrategy.hasAttribute(httpRequest)) {
chain.doFilter(request, response);
return;
}

String ip = ipStrategy.getIPAddress(httpRequest);
if (ip == null) {
chain.doFilter(request, response);
return;
}

try {
IPResponse ipResponse = ii.lookupIP(ip);
attributeStrategy.storeAttribute(httpRequest, ipResponse);
} catch (Exception e) {
logger.severe("Error while fetching IP information: " + e.getMessage());
}

chain.doFilter(request, response);
}

@Override
public void destroy() {
logger.info("Destroying IPinfoSpringFilter");
}
}