You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2025/01/05 10:33:57 UTC

(sling-org-apache-sling-api) branch jakarta-2 updated: Add wrappers for servlets and filters

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch jakarta-2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git


The following commit(s) were added to refs/heads/jakarta-2 by this push:
     new c6cf095  Add wrappers for servlets and filters
c6cf095 is described below

commit c6cf095fd0e674211ba1def3b40f3f02b397d03e
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sun Jan 5 11:33:53 2025 +0100

    Add wrappers for servlets and filters
---
 .../api/wrappers/JakartaToJavaxFilterWrapper.java  |  95 +++++++++++++++++++
 .../api/wrappers/JakartaToJavaxServletWrapper.java | 104 +++++++++++++++++++++
 .../api/wrappers/JavaxToJakartaFilterWrapper.java  |  95 +++++++++++++++++++
 .../api/wrappers/JavaxToJakartaServletWrapper.java | 104 +++++++++++++++++++++
 4 files changed, 398 insertions(+)

diff --git a/src/main/java/org/apache/sling/api/wrappers/JakartaToJavaxFilterWrapper.java b/src/main/java/org/apache/sling/api/wrappers/JakartaToJavaxFilterWrapper.java
new file mode 100644
index 0000000..2f4e57e
--- /dev/null
+++ b/src/main/java/org/apache/sling/api/wrappers/JakartaToJavaxFilterWrapper.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.wrappers;
+
+import java.io.IOException;
+
+import org.apache.felix.http.jakartawrappers.FilterConfigWrapper;
+import org.apache.felix.http.javaxwrappers.ServletExceptionUtil;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+public class JakartaToJavaxFilterWrapper implements Filter {
+
+    public static @Nullable Filter toJavaxFilter(final @Nullable jakarta.servlet.Filter filter) {
+        if (filter != null) {
+            return new JakartaToJavaxFilterWrapper(filter);
+        }
+        return null;
+    }
+
+    private final jakarta.servlet.Filter filter;
+
+    public JakartaToJavaxFilterWrapper(final jakarta.servlet.Filter filter) {
+         this.filter = filter;
+    }
+
+    @Override
+    public void init(final FilterConfig config) throws ServletException {
+        try {
+            this.filter.init(new FilterConfigWrapper(config));
+        } catch (final jakarta.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
+            throws IOException, ServletException {
+        try {
+            this.filter.doFilter(JavaxToJakartaRequestWrapper.toJakartaRequest(request),
+                JavaxToJakartaResponseWrapper.toJakartaResponse(response),
+                new FilterChainWrapper(chain));
+        } catch (final jakarta.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void destroy() {
+        this.filter.destroy();
+    }
+
+    public static class FilterChainWrapper implements jakarta.servlet.FilterChain{
+
+        private final FilterChain filterChain;
+
+        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 {
+                this.filterChain.doFilter(JakartaToJavaxRequestWrapper.toJavaxRequest(request), JakartaToJavaxResponseWrapper.toJavaxResponse(response));
+            } catch (final javax.servlet.ServletException e) {
+                throw org.apache.felix.http.jakartawrappers.ServletExceptionUtil.getServletException(e);
+            }
+        }
+    }
+}
+
diff --git a/src/main/java/org/apache/sling/api/wrappers/JakartaToJavaxServletWrapper.java b/src/main/java/org/apache/sling/api/wrappers/JakartaToJavaxServletWrapper.java
new file mode 100644
index 0000000..80b0437
--- /dev/null
+++ b/src/main/java/org/apache/sling/api/wrappers/JakartaToJavaxServletWrapper.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.wrappers;
+
+import java.io.IOException;
+
+import org.apache.felix.http.jakartawrappers.ServletConfigWrapper;
+import org.apache.felix.http.javaxwrappers.ServletExceptionUtil;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.servlets.JakartaOptingServlet;
+import org.apache.sling.api.servlets.OptingServlet;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+@SuppressWarnings("deprecation")
+public class JakartaToJavaxServletWrapper implements Servlet {
+
+    public static @Nullable Servlet toJavaxServlet(final @Nullable jakarta.servlet.Servlet servlet) {
+        if (servlet != null) {
+            if (servlet instanceof JakartaOptingServlet) {
+                return new JavaxToJakartaOptingServletWrapper((JakartaOptingServlet) servlet);
+            }
+            return new JakartaToJavaxServletWrapper(servlet);
+        }
+        return null;
+    }
+
+    private final jakarta.servlet.Servlet servlet;
+
+    public JakartaToJavaxServletWrapper(final jakarta.servlet.Servlet servlet) {
+         this.servlet = servlet;
+    }
+
+    @Override
+    public void init(final ServletConfig config) throws ServletException {
+        try {
+            this.servlet.init(new ServletConfigWrapper(config));
+        } catch (final jakarta.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void service(final ServletRequest req, final ServletResponse res) throws ServletException, IOException {
+        try {
+            this.servlet.service(JavaxToJakartaRequestWrapper.toJakartaRequest(req), JavaxToJakartaResponseWrapper.toJakartaResponse(res));
+        } catch (final jakarta.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void destroy() {
+        this.servlet.destroy();
+    }
+
+    @Override
+    public ServletConfig getServletConfig() {
+        return new org.apache.felix.http.javaxwrappers.ServletConfigWrapper(this.servlet.getServletConfig());
+    }
+
+    @Override
+    public String getServletInfo() {
+        return servlet.getServletInfo();
+    }
+
+    public static class JavaxToJakartaOptingServletWrapper extends JakartaToJavaxServletWrapper implements OptingServlet {
+
+        private final JakartaOptingServlet servlet;
+
+        public JavaxToJakartaOptingServletWrapper(final JakartaOptingServlet servlet) {
+            super(servlet);
+            this.servlet = servlet;
+        }
+
+        @Override
+        public boolean accepts(@NotNull SlingHttpServletRequest request) {
+            return this.servlet.accepts(JavaxToJakartaRequestWrapper.toJakartaRequest(request));
+        }
+    }
+}
+
diff --git a/src/main/java/org/apache/sling/api/wrappers/JavaxToJakartaFilterWrapper.java b/src/main/java/org/apache/sling/api/wrappers/JavaxToJakartaFilterWrapper.java
new file mode 100644
index 0000000..a84118b
--- /dev/null
+++ b/src/main/java/org/apache/sling/api/wrappers/JavaxToJakartaFilterWrapper.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.wrappers;
+
+import java.io.IOException;
+
+import org.apache.felix.http.javaxwrappers.FilterConfigWrapper;
+import org.apache.felix.http.jakartawrappers.ServletExceptionUtil;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import jakarta.servlet.Filter;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+
+public class JavaxToJakartaFilterWrapper implements Filter {
+
+    public static @Nullable Filter toJakartaFilter(final @Nullable javax.servlet.Filter filter) {
+        if (filter != null) {
+            return new JavaxToJakartaFilterWrapper(filter);
+        }
+        return null;
+    }
+
+    private final javax.servlet.Filter filter;
+
+    public JavaxToJakartaFilterWrapper(final javax.servlet.Filter filter) {
+         this.filter = filter;
+    }
+
+    @Override
+    public void init(final FilterConfig config) throws ServletException {
+        try {
+            this.filter.init(new FilterConfigWrapper(config));
+        } catch (final javax.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
+            throws IOException, ServletException {
+        try {
+            this.filter.doFilter(JakartaToJavaxRequestWrapper.toJavaxRequest(request),
+                JakartaToJavaxResponseWrapper.toJavaxResponse(response),
+                new FilterChainWrapper(chain));
+        } catch (final javax.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void destroy() {
+        this.filter.destroy();
+    }
+
+    public static class FilterChainWrapper implements javax.servlet.FilterChain{
+
+        private final FilterChain filterChain;
+
+        public FilterChainWrapper(@NotNull final FilterChain chain) {
+            this.filterChain = chain;
+        }
+
+        @Override
+        public void doFilter(final javax.servlet.ServletRequest request, final javax.servlet.ServletResponse response)
+                throws IOException, javax.servlet.ServletException {
+            try {
+                this.filterChain.doFilter(JavaxToJakartaRequestWrapper.toJakartaRequest(request), JavaxToJakartaResponseWrapper.toJakartaResponse(response));
+            } catch (final jakarta.servlet.ServletException e) {
+                throw org.apache.felix.http.javaxwrappers.ServletExceptionUtil.getServletException(e);
+            }
+        }
+    }
+}
+
diff --git a/src/main/java/org/apache/sling/api/wrappers/JavaxToJakartaServletWrapper.java b/src/main/java/org/apache/sling/api/wrappers/JavaxToJakartaServletWrapper.java
new file mode 100644
index 0000000..b8e6cf8
--- /dev/null
+++ b/src/main/java/org/apache/sling/api/wrappers/JavaxToJakartaServletWrapper.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.wrappers;
+
+import java.io.IOException;
+
+import org.apache.felix.http.javaxwrappers.ServletConfigWrapper;
+import org.apache.felix.http.jakartawrappers.ServletExceptionUtil;
+import org.apache.sling.api.SlingJakartaHttpServletRequest;
+import org.apache.sling.api.servlets.JakartaOptingServlet;
+import org.apache.sling.api.servlets.OptingServlet;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import jakarta.servlet.Servlet;
+import jakarta.servlet.ServletConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+
+@SuppressWarnings("deprecation")
+public class JavaxToJakartaServletWrapper implements Servlet {
+
+    public static @Nullable Servlet toJakartaServlet(final @Nullable javax.servlet.Servlet servlet) {
+        if (servlet != null) {
+            if (servlet instanceof OptingServlet) {
+                return new JavaxToJakartaOptingServletWrapper((OptingServlet) servlet);
+            }
+            return new JavaxToJakartaServletWrapper(servlet);
+        }
+        return null;
+    }
+
+    private final javax.servlet.Servlet servlet;
+
+    public JavaxToJakartaServletWrapper(final javax.servlet.Servlet servlet) {
+         this.servlet = servlet;
+    }
+
+    @Override
+    public void init(final ServletConfig config) throws ServletException {
+        try {
+            this.servlet.init(new ServletConfigWrapper(config));
+        } catch (final javax.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void service(final ServletRequest req, final ServletResponse res) throws ServletException, IOException {
+        try {
+            this.servlet.service(JakartaToJavaxRequestWrapper.toJavaxRequest(req), JakartaToJavaxResponseWrapper.toJavaxResponse(res));
+        } catch (final javax.servlet.ServletException e) {
+            throw ServletExceptionUtil.getServletException(e);
+        }
+    }
+
+    @Override
+    public void destroy() {
+        this.servlet.destroy();
+    }
+
+    @Override
+    public ServletConfig getServletConfig() {
+        return new org.apache.felix.http.jakartawrappers.ServletConfigWrapper(this.servlet.getServletConfig());
+    }
+
+    @Override
+    public String getServletInfo() {
+        return servlet.getServletInfo();
+    }
+
+    public static class JavaxToJakartaOptingServletWrapper extends JavaxToJakartaServletWrapper implements JakartaOptingServlet {
+
+        private final OptingServlet servlet;
+
+        public JavaxToJakartaOptingServletWrapper(final OptingServlet servlet) {
+            super(servlet);
+            this.servlet = servlet;
+        }
+
+        @Override
+        public boolean accepts(@NotNull SlingJakartaHttpServletRequest request) {
+            return this.servlet.accepts(JakartaToJavaxRequestWrapper.toJavaxRequest(request));
+        }
+    }
+}
+