This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch feature/SLING-13029-servlet61 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-starter-test-services.git
commit a610e91bd4c0fba3938e76ebc68b66961f9ef127 Author: Stefan Seifert <[email protected]> AuthorDate: Thu Dec 11 09:58:19 2025 +0100 SLING-13029 Validate new Servlet API 6.1 Features --- .../servlets/Jakarta61RedirectServlet.java | 57 ++++++++++++++++++++++ .../testservices/servlets/Jakarta61Servlet.java | 51 +++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/src/main/java/org/apache/sling/starter/testservices/servlets/Jakarta61RedirectServlet.java b/src/main/java/org/apache/sling/starter/testservices/servlets/Jakarta61RedirectServlet.java new file mode 100644 index 0000000..9b4c971 --- /dev/null +++ b/src/main/java/org/apache/sling/starter/testservices/servlets/Jakarta61RedirectServlet.java @@ -0,0 +1,57 @@ +/* + * 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.starter.testservices.servlets; + +import java.io.IOException; + +import jakarta.servlet.Servlet; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.sling.api.SlingJakartaHttpServletRequest; +import org.apache.sling.api.SlingJakartaHttpServletResponse; +import org.apache.sling.api.servlets.SlingJakartaSafeMethodsServlet; +import org.apache.sling.servlets.annotations.SlingServletPathsStrict; +import org.jetbrains.annotations.NotNull; +import org.osgi.service.component.annotations.Component; + +/** + * Jakarta Servlet using new sendRedirect overload and new HTTP status constant added in Servlet API 6.1. + */ +@Component(service = Servlet.class) +@SlingServletPathsStrict(paths = "/bin/jakarta-servlet61-redirect", extensions = "txt") +public class Jakarta61RedirectServlet extends SlingJakartaSafeMethodsServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + @NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response) + throws IOException { + + // Prepare a custom body to check clearBuffer behavior + response.setContentType("text/plain"); + response.getWriter().write("Custom body before redirect"); + + // Use 6.1-only overload (custom status + keep buffer) + response.sendRedirect( + request.getContextPath() + "/jakarta-servlet61.txt", + HttpServletResponse.SC_PERMANENT_REDIRECT, // 308, new in 6.1 spec + false // do NOT clear buffer + ); + } +} diff --git a/src/main/java/org/apache/sling/starter/testservices/servlets/Jakarta61Servlet.java b/src/main/java/org/apache/sling/starter/testservices/servlets/Jakarta61Servlet.java new file mode 100644 index 0000000..01a9855 --- /dev/null +++ b/src/main/java/org/apache/sling/starter/testservices/servlets/Jakarta61Servlet.java @@ -0,0 +1,51 @@ +/* + * 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.starter.testservices.servlets; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import jakarta.servlet.Servlet; +import org.apache.sling.api.SlingJakartaHttpServletRequest; +import org.apache.sling.api.SlingJakartaHttpServletResponse; +import org.apache.sling.api.servlets.SlingJakartaSafeMethodsServlet; +import org.apache.sling.servlets.annotations.SlingServletPathsStrict; +import org.jetbrains.annotations.NotNull; +import org.osgi.service.component.annotations.Component; + +/** + * Jakarta Servlet using new setCharacterEncoding overload added in Servlet API 6.1. + */ +@Component(service = Servlet.class) +@SlingServletPathsStrict(paths = "/bin/jakarta-servlet61", extensions = "txt") +public class Jakarta61Servlet extends SlingJakartaSafeMethodsServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet( + @NotNull SlingJakartaHttpServletRequest request, @NotNull SlingJakartaHttpServletResponse response) + throws IOException { + + response.setContentType("text/plain"); + // setCharacterEncoding overload with Charset added in Servlet API 6.1 + response.setCharacterEncoding(StandardCharsets.UTF_8); + response.getWriter().write("Hello Servlet API 6.1"); + } +}
