diff --git a/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateGetRealPath.java b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateGetRealPath.java new file mode 100644 index 0000000000..e321ba209c --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/jakarta/UpdateGetRealPath.java @@ -0,0 +1,59 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * Licensed 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 + *
+ * https://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.openrewrite.java.migrate.jakarta;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.J;
+
+public class UpdateGetRealPath extends Recipe {
+ @Override
+ public String getDisplayName() {
+ return "Updates `getRealPath()` to call `getContext()` followed by `getRealPath()`";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Updates `getRealPath()` for `jakarta.servlet.ServletRequest` and `jakarta.servlet.ServletRequestWrapper` to use `ServletContext.getRealPath(String)`.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new JavaVisitor
+ * Licensed 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
+ *
+ * https://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.openrewrite.java.migrate.jakarta;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class RemovalsServletJakarta10Test implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(),
+ "javax.servlet-api-4.0.0",
+ "jakarta.servlet-api-5.0.0",
+ "jakarta.servlet-api-6.0.0"))
+ .recipeFromResource("/META-INF/rewrite/jakarta-ee-10.yml", "org.openrewrite.java.migrate.jakarta.RemovalsServletJakarta10");
+ }
+
+ @Test
+ void servletReplacements() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import java.io.IOException;
+
+ import jakarta.servlet.ServletContext;
+ import jakarta.servlet.ServletException;
+ import jakarta.servlet.SingleThreadModel;
+ import jakarta.servlet.UnavailableException;
+ import jakarta.servlet.http.HttpServlet;
+ import jakarta.servlet.http.HttpServletRequest;
+ import jakarta.servlet.http.HttpServletRequestWrapper;
+ import jakarta.servlet.http.HttpServletResponse;
+ import jakarta.servlet.http.HttpServletResponseWrapper;
+ import jakarta.servlet.http.HttpSession;
+ import jakarta.servlet.http.HttpSessionContext;
+ import jakarta.servlet.http.HttpUtils;
+
+ class TestJakarta extends HttpServlet implements SingleThreadModel {
+ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
+ req.isRequestedSessionIdFromUrl();
+
+ res.encodeUrl("");
+ res.encodeRedirectUrl("");
+
+ res.setStatus(0, "");
+
+ res.setStatus(0);
+
+ HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
+ reqWrapper.isRequestedSessionIdFromUrl();
+
+ HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);
+
+ resWrapper.encodeUrl("");
+ resWrapper.encodeRedirectUrl("");
+
+ resWrapper.setStatus(0, "");
+
+ HttpSession httpSession = req.getSession();
+ httpSession.getSessionContext();
+ httpSession.getValue("");
+ httpSession.getValueNames();
+ httpSession.putValue("", null);
+ httpSession.removeValue("");
+
+ ServletContext servletContext = getServletContext();
+
+ servletContext.getServlet("");
+ servletContext.getServlets();
+ servletContext.getServletNames();
+
+ servletContext.log(null, "");
+ }
+ }
+ """,
+ """
+ import java.io.IOException;
+
+ import jakarta.servlet.ServletContext;
+ import jakarta.servlet.ServletException;
+ import jakarta.servlet.SingleThreadModel;
+ import jakarta.servlet.UnavailableException;
+ import jakarta.servlet.http.HttpServlet;
+ import jakarta.servlet.http.HttpServletRequest;
+ import jakarta.servlet.http.HttpServletRequestWrapper;
+ import jakarta.servlet.http.HttpServletResponse;
+ import jakarta.servlet.http.HttpServletResponseWrapper;
+ import jakarta.servlet.http.HttpSession;
+ import jakarta.servlet.http.HttpSessionContext;
+ import jakarta.servlet.http.HttpUtils;
+
+ class TestJakarta extends HttpServlet implements SingleThreadModel {
+ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
+ req.isRequestedSessionIdFromURL();
+
+ res.encodeURL("");
+ res.encodeRedirectURL("");
+
+ res.setStatus(0);
+
+ res.setStatus(0);
+
+ HttpServletRequestWrapper reqWrapper = new HttpServletRequestWrapper(req);
+ reqWrapper.isRequestedSessionIdFromURL();
+
+ HttpServletResponseWrapper resWrapper = new HttpServletResponseWrapper(res);
+
+ resWrapper.encodeURL("");
+ resWrapper.encodeRedirectURL("");
+
+ resWrapper.setStatus(0);
+
+ HttpSession httpSession = req.getSession();
+ httpSession.getSessionContext();
+ httpSession.getAttribute("");
+ httpSession.getAttributeNames();
+ httpSession.setAttribute("", null);
+ httpSession.removeAttribute("");
+
+ ServletContext servletContext = getServletContext();
+
+ servletContext.getServlet("");
+ servletContext.getServlets();
+ servletContext.getServletNames();
+
+ servletContext.log("", null);
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void unavailableException() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import jakarta.servlet.UnavailableException;
+ import jakarta.servlet.http.HttpServletRequest;
+ import jakarta.servlet.http.HttpServletResponse;
+
+ class Test {
+ void doGet(HttpServletRequest req, HttpServletResponse res) throws Exception {
+ jakarta.servlet.Servlet servlet ;
+ UnavailableException unavailableEx1 = new UnavailableException(0, null, "x");
+ UnavailableException unavailableEx2 = new UnavailableException(0, servlet, "x");
+ UnavailableException unavailableEx3 = new UnavailableException(servlet, "x");
+ }
+ }
+ """,
+ """
+ import jakarta.servlet.UnavailableException;
+ import jakarta.servlet.http.HttpServletRequest;
+ import jakarta.servlet.http.HttpServletResponse;
+
+ class Test {
+ void doGet(HttpServletRequest req, HttpServletResponse res) throws Exception {
+ jakarta.servlet.Servlet servlet ;
+ UnavailableException unavailableEx1 = new UnavailableException("x", 0);
+ UnavailableException unavailableEx2 = new UnavailableException("x", 0);
+ UnavailableException unavailableEx3 = new UnavailableException( "x");
+ }
+ }
+ """
+ )
+ );
+ }
+}
diff --git a/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateGetRealPathTest.java b/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateGetRealPathTest.java
new file mode 100644
index 0000000000..ae8e7533ee
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/migrate/jakarta/UpdateGetRealPathTest.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2024 the original author or authors.
+ *
+ * Licensed 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
+ *
+ * https://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.openrewrite.java.migrate.jakarta;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.InMemoryExecutionContext;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class UpdateGetRealPathTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.parser(JavaParser.fromJavaVersion()
+ .classpathFromResources(new InMemoryExecutionContext(),
+ "jakarta.servlet-api-5.0.0"))
+ .recipe(new UpdateGetRealPath());
+ }
+
+ @Test
+ void servletReplacements() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import jakarta.servlet.http.HttpServletRequest;
+ import jakarta.servlet.http.HttpServletRequestWrapper;
+ class Foo {
+ void bar(HttpServletRequest request, HttpServletRequestWrapper wrapper) throws Exception {
+ request.getRealPath("");
+ wrapper.getRealPath("");
+ }
+ }
+ """,
+ """
+ import jakarta.servlet.http.HttpServletRequest;
+ import jakarta.servlet.http.HttpServletRequestWrapper;
+ class Foo {
+ void bar(HttpServletRequest request, HttpServletRequestWrapper wrapper) throws Exception {
+ request.getServletContext().getRealPath("");
+ wrapper.getServletContext().getRealPath("");
+ }
+ }
+ """
+ )
+ );
+ }
+}
\ No newline at end of file
diff --git a/src/test/resources/META-INF/rewrite/classpath/jakarta.servlet-api-5.0.0.jar b/src/test/resources/META-INF/rewrite/classpath/jakarta.servlet-api-5.0.0.jar
new file mode 100644
index 0000000000..88b788a0bf
Binary files /dev/null and b/src/test/resources/META-INF/rewrite/classpath/jakarta.servlet-api-5.0.0.jar differ
diff --git a/src/test/resources/META-INF/rewrite/classpath/javax.servlet-api-4.0.0.jar b/src/test/resources/META-INF/rewrite/classpath/javax.servlet-api-4.0.0.jar
new file mode 100644
index 0000000000..9c9f4b7f6a
Binary files /dev/null and b/src/test/resources/META-INF/rewrite/classpath/javax.servlet-api-4.0.0.jar differ