Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73258] Migrate Stapler from EE 8 to EE 9 #482

Merged
merged 11 commits into from
Sep 3, 2024
2 changes: 1 addition & 1 deletion core/example/src/example/Book.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package example;

import jakarta.servlet.ServletException;
import java.io.IOException;
import javax.servlet.ServletException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

Expand Down
2 changes: 1 addition & 1 deletion core/example/src/example/BookStore.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package example;

import jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.ServletException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

Expand Down
4 changes: 2 additions & 2 deletions core/example/src/example/WebAppMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package example;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import org.kohsuke.stapler.Stapler;

/**
Expand Down
18 changes: 9 additions & 9 deletions core/maven-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
<version>1.253</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>2.0.0</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.0</version>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion core/maven-example/src/main/java/example/Book.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package example;

import jakarta.servlet.ServletException;
import java.io.IOException;
import javax.servlet.ServletException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

Expand Down
2 changes: 1 addition & 1 deletion core/maven-example/src/main/java/example/BookStore.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package example;

import jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.ServletException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

Expand Down
4 changes: 2 additions & 2 deletions core/maven-example/src/main/java/example/WebAppMain.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package example;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import org.kohsuke.stapler.Stapler;

/**
Expand Down
17 changes: 11 additions & 6 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-fileupload2-javax</artifactId>
<artifactId>commons-fileupload2-jakarta-servlet5</artifactId>
<version>${commons-fileupload2.version}</version>
</dependency>
<dependency>
Expand All @@ -79,6 +79,11 @@
<version>2.4.21</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-servlet-api</artifactId>
<version>4.0.6</version>
</dependency>
Comment on lines +82 to +86
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A dependency of the compatibility layer. Not part of the servlet container (Jetty) classpath, but part of the web application classpath in order to handle calls made from Jenkins plugins that were compiled against EE 8.

<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>annotation-indexer</artifactId>
Expand Down Expand Up @@ -108,7 +113,7 @@
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.4</version>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -140,13 +145,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-servlet</artifactId>
<groupId>org.eclipse.jetty.ee9</groupId>
<artifactId>jetty-ee9-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee8</groupId>
<artifactId>jetty-ee8-webapp</artifactId>
<groupId>org.eclipse.jetty.ee9</groupId>
<artifactId>jetty-ee9-webapp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
192 changes: 192 additions & 0 deletions core/src/main/java/io/jenkins/servlet/AsyncContextWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package io.jenkins.servlet;
basil marked this conversation as resolved.
Show resolved Hide resolved

import io.jenkins.servlet.http.HttpServletRequestWrapper;
import io.jenkins.servlet.http.HttpServletResponseWrapper;
import java.util.Objects;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AsyncContextWrapper {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@basil Just curious why don't we have the documentation for any of the class here. At least 1-2 liner description would have given the clarity that the purpose of that class.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not hurt, but the most important aspect of Javadoc in this PR is linking to non-deprecated versions of newly-deprecated functionality, which I have done consistently.

public static jakarta.servlet.AsyncContext toJakartaAsyncContext(AsyncContext from) {
Objects.requireNonNull(from);
return new jakarta.servlet.AsyncContext() {
@Override
public jakarta.servlet.ServletRequest getRequest() {
ServletRequest request = from.getRequest();
return request instanceof HttpServletRequest
? HttpServletRequestWrapper.toJakartaHttpServletRequest(((HttpServletRequest) request))
: io.jenkins.servlet.ServletRequestWrapper.toJakartaServletRequest(request);
}

@Override
public jakarta.servlet.ServletResponse getResponse() {
ServletResponse response = from.getResponse();
return response instanceof HttpServletResponse
? HttpServletResponseWrapper.toJakartaHttpServletResponse(((HttpServletResponse) response))
: ServletResponseWrapper.toJakartaServletResponse(response);
}

@Override
public boolean hasOriginalRequestAndResponse() {
return from.hasOriginalRequestAndResponse();
}

@Override
public void dispatch() {
from.dispatch();
}

@Override
public void dispatch(String path) {
from.dispatch(path);
}

@Override
public void dispatch(jakarta.servlet.ServletContext context, String path) {
from.dispatch(ServletContextWrapper.fromJakartServletContext(context), path);
}

@Override
public void complete() {
from.complete();
}

@Override
public void start(Runnable run) {
from.start(run);
}

@Override
public void addListener(jakarta.servlet.AsyncListener listener) {
from.addListener(AsyncListenerWrapper.fromJakartaAsyncListener(listener));
}

@Override
public void addListener(
jakarta.servlet.AsyncListener listener,
jakarta.servlet.ServletRequest servletRequest,
jakarta.servlet.ServletResponse servletResponse) {
from.addListener(
AsyncListenerWrapper.fromJakartaAsyncListener(listener),
servletRequest instanceof jakarta.servlet.http.HttpServletRequest
? HttpServletRequestWrapper.fromJakartaHttpServletRequest(
(jakarta.servlet.http.HttpServletRequest) servletRequest)
: io.jenkins.servlet.ServletRequestWrapper.fromJakartaServletRequest(servletRequest),
servletResponse instanceof jakarta.servlet.http.HttpServletResponse
? HttpServletResponseWrapper.fromJakartaHttpServletResponse(
(jakarta.servlet.http.HttpServletResponse) servletResponse)
: ServletResponseWrapper.fromJakartaServletResponse(servletResponse));
}

@Override
public <T extends jakarta.servlet.AsyncListener> T createListener(Class<T> clazz) {
// TODO implement this

Check warning on line 88 in core/src/main/java/io/jenkins/servlet/AsyncContextWrapper.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: implement this
throw new UnsupportedOperationException();
}

@Override
public void setTimeout(long timeout) {
from.setTimeout(timeout);
}

@Override
public long getTimeout() {
return from.getTimeout();
}
};
}

public static AsyncContext fromJakartaAsyncContext(jakarta.servlet.AsyncContext from) {
Objects.requireNonNull(from);
return new AsyncContext() {
@Override
public ServletRequest getRequest() {
jakarta.servlet.ServletRequest request = from.getRequest();
return request instanceof jakarta.servlet.http.HttpServletRequest
? HttpServletRequestWrapper.fromJakartaHttpServletRequest(
(jakarta.servlet.http.HttpServletRequest) request)
: ServletRequestWrapper.fromJakartaServletRequest(request);
}

@Override
public ServletResponse getResponse() {
jakarta.servlet.ServletResponse response = from.getResponse();
return response instanceof jakarta.servlet.http.HttpServletResponse
? HttpServletResponseWrapper.fromJakartaHttpServletResponse(
(jakarta.servlet.http.HttpServletResponse) response)
: ServletResponseWrapper.fromJakartaServletResponse(response);
}

@Override
public boolean hasOriginalRequestAndResponse() {
return from.hasOriginalRequestAndResponse();
}

@Override
public void dispatch() {
from.dispatch();
}

@Override
public void dispatch(String path) {
from.dispatch(path);
}

@Override
public void dispatch(ServletContext context, String path) {
from.dispatch(ServletContextWrapper.toJakartaServletContext(context), path);
}

@Override
public void complete() {
from.complete();
}

@Override
public void start(Runnable run) {
from.start(run);
}

@Override
public void addListener(AsyncListener listener) {
from.addListener(AsyncListenerWrapper.toJakartaAsyncListener(listener));
}

@Override
public void addListener(
AsyncListener listener, ServletRequest servletRequest, ServletResponse servletResponse) {
from.addListener(
AsyncListenerWrapper.toJakartaAsyncListener(listener),
servletRequest instanceof HttpServletRequest
? HttpServletRequestWrapper.toJakartaHttpServletRequest(
((HttpServletRequest) servletRequest))
: io.jenkins.servlet.ServletRequestWrapper.toJakartaServletRequest(servletRequest),
servletResponse instanceof HttpServletResponse
? HttpServletResponseWrapper.toJakartaHttpServletResponse(
((HttpServletResponse) servletResponse))
: ServletResponseWrapper.toJakartaServletResponse(servletResponse));
}

@Override
public <T extends AsyncListener> T createListener(Class<T> clazz) {
// TODO implement this

Check warning on line 177 in core/src/main/java/io/jenkins/servlet/AsyncContextWrapper.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: implement this
throw new UnsupportedOperationException();
}

@Override
public void setTimeout(long timeout) {
from.setTimeout(timeout);
}

@Override
public long getTimeout() {
return from.getTimeout();
}
};
}
}
24 changes: 24 additions & 0 deletions core/src/main/java/io/jenkins/servlet/AsyncEventWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.jenkins.servlet;

import java.util.Objects;
import javax.servlet.AsyncEvent;

public class AsyncEventWrapper {
public static jakarta.servlet.AsyncEvent toJakartaServletHttpAsyncEvent(AsyncEvent from) {
Objects.requireNonNull(from);
return new jakarta.servlet.AsyncEvent(
AsyncContextWrapper.toJakartaAsyncContext(from.getAsyncContext()),
io.jenkins.servlet.ServletRequestWrapper.toJakartaServletRequest(from.getSuppliedRequest()),
ServletResponseWrapper.toJakartaServletResponse(from.getSuppliedResponse()),
from.getThrowable());
}

public static AsyncEvent fromJakartaServletHttpAsyncEvent(jakarta.servlet.AsyncEvent from) {
Objects.requireNonNull(from);
return new AsyncEvent(
AsyncContextWrapper.fromJakartaAsyncContext(from.getAsyncContext()),
ServletRequestWrapper.fromJakartaServletRequest(from.getSuppliedRequest()),
ServletResponseWrapper.fromJakartaServletResponse(from.getSuppliedResponse()),
from.getThrowable());
}
}
Loading
Loading