From c87bb6a762479fc2d190d7cf5a079ae09cea8235 Mon Sep 17 00:00:00 2001 From: Dommi Date: Wed, 4 Sep 2024 07:36:40 +0200 Subject: [PATCH] add missing filter wrappers (#331) Co-authored-by: d.klysch --- .../jakartawrappers/FilterChainWrapper.java | 46 +++++++++++ .../jakartawrappers/FilterConfigWrapper.java | 57 +++++++++++++ .../http/javaxwrappers/FilterWrapper.java | 82 +++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterChainWrapper.java create mode 100644 http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterConfigWrapper.java create mode 100644 http/wrappers/src/main/java/org/apache/felix/http/javaxwrappers/FilterWrapper.java diff --git a/http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterChainWrapper.java b/http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterChainWrapper.java new file mode 100644 index 0000000000..e5ef3bacfa --- /dev/null +++ b/http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterChainWrapper.java @@ -0,0 +1,46 @@ +package org.apache.felix.http.jakartawrappers; + + +import java.io.IOException; + +import javax.servlet.FilterChain; + +import org.apache.felix.http.javaxwrappers.ServletRequestWrapper; +import org.apache.felix.http.javaxwrappers.ServletResponseWrapper; +import org.jetbrains.annotations.NotNull; + +/** + * Jakarta filter chain based on a javax filter chain + */ +public class FilterChainWrapper implements jakarta.servlet.FilterChain +{ + private final FilterChain filterChain; + + + /** + * Create new chain + * + * @param chain Wrapped chain + */ + public FilterChainWrapper(@NotNull final FilterChain chain) + { + this.filterChain = chain; + } + + + @Override + public void doFilter(final jakarta.servlet.ServletRequest request, final jakarta.servlet.ServletResponse response) + throws IOException, jakarta.servlet.ServletException + { + try + { + filterChain.doFilter(ServletRequestWrapper.getWrapper(request), + ServletResponseWrapper.getWrapper(response)); + } + catch (final javax.servlet.ServletException e) + { + throw ServletExceptionUtil.getServletException(e); + } + } + +} diff --git a/http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterConfigWrapper.java b/http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterConfigWrapper.java new file mode 100644 index 0000000000..fc1d6828ba --- /dev/null +++ b/http/wrappers/src/main/java/org/apache/felix/http/jakartawrappers/FilterConfigWrapper.java @@ -0,0 +1,57 @@ +package org.apache.felix.http.jakartawrappers; + + +import java.util.Enumeration; + +import javax.servlet.FilterConfig; + +import org.jetbrains.annotations.NotNull; + + +/** + * Filter config wrapper + */ +public class FilterConfigWrapper implements jakarta.servlet.FilterConfig +{ + + private final FilterConfig filterConfig; + + + /** + * Create config + * + * @param filterConfig wrapped config + */ + public FilterConfigWrapper(@NotNull final FilterConfig filterConfig) + { + this.filterConfig = filterConfig; + } + + + @Override + public String getFilterName() + { + return this.filterConfig.getFilterName(); + } + + + @Override + public jakarta.servlet.ServletContext getServletContext() + { + return new ServletContextWrapper(this.filterConfig.getServletContext()); + } + + + @Override + public String getInitParameter(final String name) + { + return this.filterConfig.getInitParameter(name); + } + + + @Override + public Enumeration getInitParameterNames() + { + return this.filterConfig.getInitParameterNames(); + } +} diff --git a/http/wrappers/src/main/java/org/apache/felix/http/javaxwrappers/FilterWrapper.java b/http/wrappers/src/main/java/org/apache/felix/http/javaxwrappers/FilterWrapper.java new file mode 100644 index 0000000000..e805cd6b66 --- /dev/null +++ b/http/wrappers/src/main/java/org/apache/felix/http/javaxwrappers/FilterWrapper.java @@ -0,0 +1,82 @@ +package org.apache.felix.http.javaxwrappers; + + +import java.io.IOException; + +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 org.jetbrains.annotations.NotNull; + +/** + * Javax Filter based on a jakarta filter + */ +public class FilterWrapper implements Filter +{ + + private final jakarta.servlet.Filter filter; + + + /** + * Create new filter + * + * @param filter wrapped filter + */ + public FilterWrapper(@NotNull final jakarta.servlet.Filter filter) + { + this.filter = filter; + } + + + @Override + public void init(final FilterConfig filterConfig) throws ServletException + { + try + { + this.filter.init(new org.apache.felix.http.jakartawrappers.FilterConfigWrapper(filterConfig)); + } + catch (final jakarta.servlet.ServletException e) + { + throw ServletExceptionUtil.getServletException(e); + } + } + + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException + { + try + { + this.filter.doFilter(org.apache.felix.http.jakartawrappers.ServletRequestWrapper.getWrapper(request), + org.apache.felix.http.jakartawrappers.ServletResponseWrapper.getWrapper(response), + new org.apache.felix.http.jakartawrappers.FilterChainWrapper(chain)); + } + catch (final jakarta.servlet.ServletException e) + { + throw ServletExceptionUtil.getServletException(e); + } + } + + + @Override + public void destroy() + { + this.filter.destroy(); + } + + + /** + * Get the filter + * + * @return The filter + */ + public @NotNull jakarta.servlet.Filter getFilter() + { + return this.filter; + } +}