From b288f1262a5f07e8a099b35140c0798147b14d5a Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Sat, 27 Jul 2024 15:17:39 +0530 Subject: [PATCH 1/7] exposed javax filters --- .../gjex/core/filter/http/HttpFilter.java | 8 ----- .../core/filter/http/JavaxFilterParams.java | 22 +++++++++++++ .../helloworld/guice/HelloWorldModule.java | 4 +++ .../web/javaxfilter/ExampleJavaxFilter.java | 31 +++++++++++++++++++ .../flipkart/gjex/grpc/service/ApiServer.java | 9 +++++- .../com/flipkart/gjex/guice/GuiceBundle.java | 6 +++- 6 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java create mode 100644 examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java diff --git a/core/src/main/java/com/flipkart/gjex/core/filter/http/HttpFilter.java b/core/src/main/java/com/flipkart/gjex/core/filter/http/HttpFilter.java index 51411b9c..d2415ee5 100644 --- a/core/src/main/java/com/flipkart/gjex/core/filter/http/HttpFilter.java +++ b/core/src/main/java/com/flipkart/gjex/core/filter/http/HttpFilter.java @@ -17,17 +17,9 @@ import com.flipkart.gjex.core.filter.Filter; import com.flipkart.gjex.core.filter.RequestParams; -import com.flipkart.gjex.core.filter.grpc.GrpcFilter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Collections; -import java.util.HashSet; import java.util.Set; /** diff --git a/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java b/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java new file mode 100644 index 00000000..5754c87a --- /dev/null +++ b/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java @@ -0,0 +1,22 @@ +package com.flipkart.gjex.core.filter.http; + +import lombok.Builder; +import lombok.Data; + +import javax.servlet.Filter; + +/** + * + * @author ajay.jalgaonkar + */ + +@Data +@Builder +public class JavaxFilterParams { + + // The filter instance to be applied. + private final Filter filter; + + // The URL pattern(s) the filter applies to. + private final String pathSpec; +} diff --git a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/guice/HelloWorldModule.java b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/guice/HelloWorldModule.java index 148e40c5..071ecd47 100644 --- a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/guice/HelloWorldModule.java +++ b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/guice/HelloWorldModule.java @@ -17,6 +17,7 @@ import com.flipkart.gjex.core.filter.grpc.GrpcFilter; import com.flipkart.gjex.core.filter.http.HttpFilterParams; +import com.flipkart.gjex.core.filter.http.JavaxFilterParams; import com.flipkart.gjex.core.tracing.TracingSampler; import com.flipkart.gjex.examples.helloworld.filter.AuthFilter; import com.flipkart.gjex.examples.helloworld.filter.LoggingFilter; @@ -24,6 +25,7 @@ import com.flipkart.gjex.examples.helloworld.tracing.AllWhitelistTracingSampler; import com.flipkart.gjex.examples.helloworld.web.HelloWorldResourceConfig; import com.flipkart.gjex.examples.helloworld.web.httpfilter.ExampleHttpFilter; +import com.flipkart.gjex.examples.helloworld.web.javaxfilter.ExampleJavaxFilter; import com.google.inject.AbstractModule; import com.google.inject.name.Names; import io.grpc.BindableService; @@ -55,5 +57,7 @@ protected void configure() { bind(ResourceConfig.class).annotatedWith(Names.named("HelloWorldResourceConfig")).to(HelloWorldResourceConfig.class); bind(HttpFilterParams.class).annotatedWith(Names.named("ExampleHttpFilterParams")) .toInstance(HttpFilterParams.builder().filter(new ExampleHttpFilter()).pathSpec("/*").build()); + bind(JavaxFilterParams.class).annotatedWith(Names.named("ExampleJavaxFilter")) + .toInstance(JavaxFilterParams.builder().filter(new ExampleJavaxFilter()).pathSpec("/*").build()); } } diff --git a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java new file mode 100644 index 00000000..de34b67a --- /dev/null +++ b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java @@ -0,0 +1,31 @@ +package com.flipkart.gjex.examples.helloworld.web.javaxfilter; + +import com.flipkart.gjex.core.logging.Logging; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; + +public class ExampleJavaxFilter implements Logging, Filter { + public AtomicInteger number = new AtomicInteger(); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + info("printing from ExampleJavaxFilter: " + number.getAndIncrement()); + } + + @Override + public void destroy() { + + } +} diff --git a/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java b/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java index d5413b44..6175e337 100644 --- a/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java +++ b/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java @@ -18,6 +18,7 @@ import com.flipkart.gjex.core.filter.http.AccessLogHttpFilter; import com.flipkart.gjex.core.filter.http.HttpFilterConfig; import com.flipkart.gjex.core.filter.http.HttpFilterParams; +import com.flipkart.gjex.core.filter.http.JavaxFilterParams; import com.flipkart.gjex.core.logging.Logging; import com.flipkart.gjex.core.service.AbstractService; import com.flipkart.gjex.core.service.Service; @@ -67,10 +68,16 @@ public void registerResources(List resourceConfigs) { this.resourceConfigs.addAll(resourceConfigs); } - public void registerFilters(List httpFilterParamsList, HttpFilterConfig httpFilterConfig){ + public void registerFilters(List httpFilterParamsList, + List javaxFilterParamsList, + HttpFilterConfig httpFilterConfig){ configureAccessLog(httpFilterParamsList, httpFilterConfig); httpFilterInterceptor.registerFilters(httpFilterParamsList); context.addFilter(new FilterHolder(httpFilterInterceptor), "/*", EnumSet.of(DispatcherType.REQUEST)); + for (JavaxFilterParams javaxFilterParams: javaxFilterParamsList){ + context.addFilter(new FilterHolder(javaxFilterParams.getFilter()), + javaxFilterParams.getPathSpec(), EnumSet.of(DispatcherType.REQUEST)); + } } private void configureAccessLog(List httpFilterParamsList, HttpFilterConfig httpFilterConfig){ diff --git a/guice/src/main/java/com/flipkart/gjex/guice/GuiceBundle.java b/guice/src/main/java/com/flipkart/gjex/guice/GuiceBundle.java index d6423543..22bbc6c5 100644 --- a/guice/src/main/java/com/flipkart/gjex/guice/GuiceBundle.java +++ b/guice/src/main/java/com/flipkart/gjex/guice/GuiceBundle.java @@ -19,6 +19,7 @@ import com.flipkart.gjex.core.GJEXConfiguration; import com.flipkart.gjex.core.filter.grpc.GrpcFilter; import com.flipkart.gjex.core.filter.http.HttpFilterParams; +import com.flipkart.gjex.core.filter.http.JavaxFilterParams; import com.flipkart.gjex.core.job.ScheduledJob; import com.flipkart.gjex.core.logging.Logging; import com.flipkart.gjex.core.service.Service; @@ -73,6 +74,7 @@ public class GuiceBundle implements private List scheduledJobs; private List resourceConfigs; private List httpFilterParamsList; + private List javaxFilterParamsList; private Optional> configurationClass; private GJEXEnvironmentModule gjexEnvironmentModule; @@ -176,7 +178,9 @@ public void run(T configuration, U configMap, Environment environment) { // Add all custom http filters httpFilterParamsList = getInstances(baseInjector, HttpFilterParams.class); - apiServer.registerFilters(httpFilterParamsList, configuration.getApiService().getHttpFilterConfig()); + javaxFilterParamsList = getInstances(baseInjector, JavaxFilterParams.class); + apiServer.registerFilters(httpFilterParamsList, + javaxFilterParamsList, configuration.getApiService().getHttpFilterConfig()); } @SuppressWarnings("unchecked") From 90c2fc1054512437e4c912867d1632e0ff49eab5 Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Sat, 27 Jul 2024 15:18:57 +0530 Subject: [PATCH 2/7] exposed javax filters --- .../helloworld/web/javaxfilter/ExampleJavaxFilter.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java index de34b67a..e7fd7d4b 100644 --- a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java +++ b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java @@ -11,6 +11,10 @@ import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; +/** + * Example filter extending {@link Filter} + * @author ajay.jalgaonkar + */ public class ExampleJavaxFilter implements Logging, Filter { public AtomicInteger number = new AtomicInteger(); From 6f1675cb97748f60ca542465f9646f21017a3f2b Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Mon, 29 Jul 2024 11:34:54 +0530 Subject: [PATCH 3/7] deprecated javax filters --- .../com/flipkart/gjex/core/filter/http/JavaxFilterParams.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java b/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java index 5754c87a..987493ea 100644 --- a/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java +++ b/core/src/main/java/com/flipkart/gjex/core/filter/http/JavaxFilterParams.java @@ -12,6 +12,7 @@ @Data @Builder +@Deprecated public class JavaxFilterParams { // The filter instance to be applied. From a9e55d91e6e63ef43e0e0d6fc6705222aa17ed8b Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Mon, 29 Jul 2024 11:45:40 +0530 Subject: [PATCH 4/7] moved --- .../gjex/http/interceptor/HttpFilterInterceptor.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java index 3eac84ec..0b407881 100644 --- a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java +++ b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java @@ -54,14 +54,13 @@ public void registerFilters(List httpFilterParamsList) { } filtersMap.get(httpFilterParams.getPathSpec()).filters.add(httpFilterParams.getFilter()); } + for (ServletPathFiltersHolder servletPathFiltersHolder : filtersMap.values()){ + pathSpecToFilterMap.computeIfAbsent(servletPathFiltersHolder.spec, + k-> new ArrayList<>()).addAll(servletPathFiltersHolder.filters); + } } - public void init(FilterConfig filterConfig) throws ServletException { - for (ServletPathFiltersHolder servletPathFiltersHolder : filtersMap.values()){ - pathSpecToFilterMap.computeIfAbsent(servletPathFiltersHolder.spec, - k-> new ArrayList<>()).addAll(servletPathFiltersHolder.filters); - } - } + public void init(FilterConfig filterConfig) throws ServletException {} /** * The core method that processes incoming requests and responses. It captures the request and response objects, From 13cf3a74a78ffe6bd80628ec4ca4dd240f0ac3c4 Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Mon, 29 Jul 2024 11:47:41 +0530 Subject: [PATCH 5/7] test case fix --- .../gjex/http/interceptor/HttpFilterInterceptor.java | 4 ++-- .../gjex/http/interceptor/HttpFilterInterceptorTest.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java index 0b407881..9bd49ab5 100644 --- a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java +++ b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java @@ -108,7 +108,7 @@ public final void doFilter(ServletRequest request, ServletResponse response, * @param request The ServletRequest object containing the client's request * @return The real IP address of the client */ - private String getClientIp(ServletRequest request) { + protected String getClientIp(ServletRequest request) { String remoteAddr = request.getRemoteAddr(); String xForwardedFor = ((HttpServletRequest) request).getHeader("X-Forwarded-For"); if (xForwardedFor != null) { @@ -117,7 +117,7 @@ private String getClientIp(ServletRequest request) { return remoteAddr; } - private List getMatchingFilters(String path) { + protected List getMatchingFilters(String path) { return pathSpecToFilterMap.keySet().stream().filter(key -> key.matches(path)) .map(k-> pathSpecToFilterMap.get(k)).flatMap(List::stream) .map(filter -> filter.getInstance()).collect(Collectors.toList()); diff --git a/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java b/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java index 4a19a213..dfb30f85 100644 --- a/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java +++ b/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java @@ -14,11 +14,13 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class HttpFilterInterceptorTest { private HttpFilterInterceptor interceptor; + String pathSpec = "/test/*"; @Before public void setUp() { @@ -27,19 +29,19 @@ public void setUp() { @Test public void registerFiltersAddsFiltersToMap() { - String pathSpec = "/test/*"; List filters = new ArrayList<>(); filters.add(new AccessLogHttpFilter()); assertEquals(1, filters.size()); List httpFilterParamsList = new ArrayList<>(); httpFilterParamsList.add(HttpFilterParams.builder().pathSpec(pathSpec).filter(new AccessLogHttpFilter()).build()); interceptor.registerFilters(httpFilterParamsList); + assertEquals(1, interceptor.getMatchingFilters("/test/path").size()); } @Test public void testRegexSpec(){ ServletPathSpec spec = new ServletPathSpec("/test/*"); - assertEquals(true, spec.matches("/test/path")); + assertTrue(spec.matches("/test/path")); } } From 735083e0c06a158f073aa263f8f0582b537ed3b6 Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Mon, 29 Jul 2024 15:24:29 +0530 Subject: [PATCH 6/7] ddofilter updated --- .../helloworld/web/javaxfilter/ExampleJavaxFilter.java | 1 + .../main/java/com/flipkart/gjex/grpc/service/ApiServer.java | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java index e7fd7d4b..37ba4044 100644 --- a/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java +++ b/examples/src/main/java/com/flipkart/gjex/examples/helloworld/web/javaxfilter/ExampleJavaxFilter.java @@ -26,6 +26,7 @@ public void init(FilterConfig filterConfig) throws ServletException { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { info("printing from ExampleJavaxFilter: " + number.getAndIncrement()); + chain.doFilter(request, response); } @Override diff --git a/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java b/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java index 6175e337..70090236 100644 --- a/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java +++ b/guice/src/main/java/com/flipkart/gjex/grpc/service/ApiServer.java @@ -75,8 +75,7 @@ public void registerFilters(List httpFilterParamsList, httpFilterInterceptor.registerFilters(httpFilterParamsList); context.addFilter(new FilterHolder(httpFilterInterceptor), "/*", EnumSet.of(DispatcherType.REQUEST)); for (JavaxFilterParams javaxFilterParams: javaxFilterParamsList){ - context.addFilter(new FilterHolder(javaxFilterParams.getFilter()), - javaxFilterParams.getPathSpec(), EnumSet.of(DispatcherType.REQUEST)); + context.addFilter(new FilterHolder(javaxFilterParams.getFilter()), javaxFilterParams.getPathSpec(), EnumSet.of(DispatcherType.REQUEST)); } } From 6b39d78324efd9f99bababf0ce7d0eda639c3d59 Mon Sep 17 00:00:00 2001 From: "ajay.jalgaonkar" Date: Mon, 29 Jul 2024 15:37:39 +0530 Subject: [PATCH 7/7] udpated HttpFilterInterceptor --- .../interceptor/HttpFilterInterceptor.java | 32 ++++++------------- .../HttpFilterInterceptorTest.java | 14 ++++++-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java index 9bd49ab5..2bfd6560 100644 --- a/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java +++ b/guice/src/main/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptor.java @@ -17,10 +17,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -30,33 +28,23 @@ public class HttpFilterInterceptor implements javax.servlet.Filter { private static class ServletPathFiltersHolder { ServletPathSpec spec; - List filters; + HttpFilter filter; - public ServletPathFiltersHolder(ServletPathSpec spec, List filters) { + public ServletPathFiltersHolder(ServletPathSpec spec, HttpFilter filter) { this.spec = spec; - this.filters = filters; + this.filter = filter; } } /** * Map of Filter instances mapped to Service and its method */ - @SuppressWarnings("rawtypes") - private Map filtersMap = new HashMap<>(); - private Map> pathSpecToFilterMap = new HashMap<>(); + private final List pathFiltersHolders = new ArrayList<>(); public void registerFilters(List httpFilterParamsList) { - for (HttpFilterParams httpFilterParams: httpFilterParamsList){ - if (!filtersMap.containsKey(httpFilterParams.getPathSpec())){ - ServletPathSpec spec = new ServletPathSpec(httpFilterParams.getPathSpec()); - filtersMap.put(httpFilterParams.getPathSpec(), new ServletPathFiltersHolder(spec, - new ArrayList<>())); - } - filtersMap.get(httpFilterParams.getPathSpec()).filters.add(httpFilterParams.getFilter()); - } - for (ServletPathFiltersHolder servletPathFiltersHolder : filtersMap.values()){ - pathSpecToFilterMap.computeIfAbsent(servletPathFiltersHolder.spec, - k-> new ArrayList<>()).addAll(servletPathFiltersHolder.filters); + for(HttpFilterParams p : httpFilterParamsList){ + ServletPathSpec spec = new ServletPathSpec(p.getPathSpec()); + pathFiltersHolders.add(new ServletPathFiltersHolder(spec, p.getFilter())); } } @@ -118,9 +106,9 @@ protected String getClientIp(ServletRequest request) { } protected List getMatchingFilters(String path) { - return pathSpecToFilterMap.keySet().stream().filter(key -> key.matches(path)) - .map(k-> pathSpecToFilterMap.get(k)).flatMap(List::stream) - .map(filter -> filter.getInstance()).collect(Collectors.toList()); + return pathFiltersHolders.stream().filter(t -> t.spec.matches(path)) + .map(t-> t.filter) + .map(HttpFilter::getInstance).collect(Collectors.toList()); } @Override diff --git a/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java b/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java index dfb30f85..68010b2d 100644 --- a/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java +++ b/guice/src/test/java/com/flipkart/gjex/http/interceptor/HttpFilterInterceptorTest.java @@ -29,9 +29,6 @@ public void setUp() { @Test public void registerFiltersAddsFiltersToMap() { - List filters = new ArrayList<>(); - filters.add(new AccessLogHttpFilter()); - assertEquals(1, filters.size()); List httpFilterParamsList = new ArrayList<>(); httpFilterParamsList.add(HttpFilterParams.builder().pathSpec(pathSpec).filter(new AccessLogHttpFilter()).build()); interceptor.registerFilters(httpFilterParamsList); @@ -44,4 +41,15 @@ public void testRegexSpec(){ assertTrue(spec.matches("/test/path")); } + + @Test + public void registerFiltersAddsFiltersToMap2() { + List httpFilterParamsList = new ArrayList<>(); + httpFilterParamsList.add(HttpFilterParams.builder().pathSpec(pathSpec).filter(new AccessLogHttpFilter()).build()); + httpFilterParamsList.add(HttpFilterParams.builder().pathSpec(pathSpec).filter(new AccessLogHttpFilter()).build()); + interceptor.registerFilters(httpFilterParamsList); + assertEquals(2, interceptor.getMatchingFilters("/test/path").size()); + } + + }