Skip to content

Commit

Permalink
Migrate from EE 8 to EE 9 (#2625)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jan 17, 2025
1 parent 4f0c1ad commit 44ef3da
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions docs/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static final class DescriptorImpl extends Descriptor<Foo> {
/** optional password */
private Secret password;

public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
charset = json.getString("charset");
if (json.has("usePassword")) {
password = Secret.fromString(nullify(auth.getString("password")));
Expand Down Expand Up @@ -170,7 +170,7 @@ Rewrite `Descriptor#configure()` implementation to rely on `request.bindJson(thi
default values as a `Descriptor` is a mutable object, i.e. data-binding won't reset values if they are not present in the JSON payload.
```java
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
// reset optional authentication to default before data-binding
this.authentication = null;
req.bindJSON(this, json);
Expand Down
8 changes: 4 additions & 4 deletions docs/REQUIREMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ is attached to a Descriptors.

### Rule 1: don't write code for data-binding

Check implementation of `Descriptor#configure(StaplerRequest,JSONObject)` in your descriptors.
Check implementation of `Descriptor#configure(StaplerRequest2,JSONObject)` in your descriptors.
This one should **not** use any of the `JSONObject.get*()` methods to set value for an internal
field. Prefer exposing JavaBean setter methods, and use `req.bindJSON(this,JSONObject)` to rely
on introspection-friendly data-binding.
Expand All @@ -35,7 +35,7 @@ to do anyway, as it makes their intent more clear.
sample:

```java
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
smtpHost = nullify(json.getString("smtpHost"));
replyToAddress = json.getString("replyToAddress");
...
Expand All @@ -47,7 +47,7 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
to be replaced by:

```java
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
try (BulkChange bc = new BulkChange(this)) {
req.bindJSON(this, json);
bc.commit();
Expand Down Expand Up @@ -102,7 +102,7 @@ sample:
private String username;
private Secret password;

public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
if(json.has("useAuth")) {
JSONObject auth = json.getJSONObject("useAuth");
username = nullify(auth.getString("username"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerRequest2;

/**
*
Expand Down Expand Up @@ -41,7 +41,7 @@ public void setConfigurationPath(String configurationPath) {
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
req.bindJSON(this, json);
save();
return super.configure(req, json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import io.jenkins.plugins.casc.yaml.YamlUtils;
import io.jenkins.plugins.prism.PrismConfiguration;
import jakarta.inject.Inject;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -68,10 +72,6 @@
import java.util.logging.Logger;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
Expand All @@ -81,8 +81,8 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.lang.Klass;
import org.kohsuke.stapler.verb.POST;
Expand Down Expand Up @@ -167,7 +167,7 @@ public List<String> getSources() {

@RequirePOST
@Restricted(NoExternalUse.class)
public void doReload(StaplerRequest request, StaplerResponse response) throws Exception {
public void doReload(StaplerRequest2 request, StaplerResponse2 response) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.MANAGE)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
Expand All @@ -193,7 +193,7 @@ public void doReload(StaplerRequest request, StaplerResponse response) throws Ex

@Restricted(NoExternalUse.class)
public static void handleExceptionOnReloading(
StaplerRequest request, StaplerResponse response, ConfiguratorException cause)
StaplerRequest2 request, StaplerResponse2 response, ConfiguratorException cause)
throws ServletException, IOException {
Configurator<?> configurator = cause.getConfigurator();
request.setAttribute("errorMessage", cause.getErrorMessage());
Expand All @@ -213,7 +213,7 @@ public static void handleExceptionOnReloading(

@RequirePOST
@Restricted(NoExternalUse.class)
public void doReplace(StaplerRequest request, StaplerResponse response) throws Exception {
public void doReplace(StaplerRequest2 request, StaplerResponse2 response) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
Expand Down Expand Up @@ -421,7 +421,7 @@ public List<String> getBundledCasCURIs() {
final String cascDirectory = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH + ".d/";
List<String> res = new ArrayList<>();

final ServletContext servletContext = Jenkins.get().servletContext;
final ServletContext servletContext = Jenkins.get().getServletContext();
try {
URL bundled = servletContext.getResource(cascFile);
if (bundled != null) {
Expand Down Expand Up @@ -452,7 +452,7 @@ public List<String> getBundledCasCURIs() {

@RequirePOST
@Restricted(NoExternalUse.class)
public void doCheck(StaplerRequest req, StaplerResponse res) throws Exception {
public void doCheck(StaplerRequest2 req, StaplerResponse2 res) throws Exception {

if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
Expand All @@ -470,7 +470,7 @@ public void doCheck(StaplerRequest req, StaplerResponse res) throws Exception {

@RequirePOST
@Restricted(NoExternalUse.class)
public void doApply(StaplerRequest req, StaplerResponse res) throws Exception {
public void doApply(StaplerRequest2 req, StaplerResponse2 res) throws Exception {

if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
Expand All @@ -485,7 +485,7 @@ public void doApply(StaplerRequest req, StaplerResponse res) throws Exception {
*/
@RequirePOST
@Restricted(NoExternalUse.class)
public void doExport(StaplerRequest req, StaplerResponse res) throws Exception {
public void doExport(StaplerRequest2 req, StaplerResponse2 res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.SYSTEM_READ)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
Expand All @@ -501,7 +501,7 @@ public void doExport(StaplerRequest req, StaplerResponse res) throws Exception {
* @throws Exception
*/
@Restricted(NoExternalUse.class)
public void doSchema(StaplerRequest req, StaplerResponse res) throws Exception {
public void doSchema(StaplerRequest2 req, StaplerResponse2 res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.SYSTEM_READ)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
Expand All @@ -513,7 +513,7 @@ public void doSchema(StaplerRequest req, StaplerResponse res) throws Exception {

@RequirePOST
@Restricted(NoExternalUse.class)
public void doViewExport(StaplerRequest req, StaplerResponse res) throws Exception {
public void doViewExport(StaplerRequest2 req, StaplerResponse2 res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.SYSTEM_READ)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
Expand All @@ -532,7 +532,7 @@ public PrismConfiguration getPrismConfiguration() {
}

@Restricted(NoExternalUse.class)
public void doReference(StaplerRequest req, StaplerResponse res) throws Exception {
public void doReference(StaplerRequest2 req, StaplerResponse2 res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.SYSTEM_READ)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package io.jenkins.plugins.casc;

import hudson.util.BootFailure;
import jakarta.servlet.ServletException;
import java.io.IOException;
import javax.servlet.ServletException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;

public class ConfigurationAsCodeBootFailure extends BootFailure {

public ConfigurationAsCodeBootFailure(ConfiguratorException cause) {
super(cause);
}

public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
public void doDynamic(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException {
rsp.setStatus(503);
ConfigurationAsCode.handleExceptionOnReloading(req, rsp, (ConfiguratorException) getCause());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import hudson.Extension;
import hudson.security.csrf.CrumbExclusion;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Extension
public class TokenReloadCrumbExclusion extends CrumbExclusion {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.jenkins.plugins.casc.yaml;

import jakarta.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.nio.file.Path;
import javax.servlet.http.HttpServletRequest;

/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorException;
import io.jenkins.plugins.casc.model.Mapping;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -16,7 +17,6 @@
import java.nio.file.Path;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.composer.Composer;
import org.yaml.snakeyaml.error.YAMLException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package io.jenkins.plugins.casc;

import jakarta.servlet.AsyncContext;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpUpgradeHandler;
import jakarta.servlet.http.Part;
import java.io.BufferedReader;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.Part;

public class MockHttpServletRequest implements HttpServletRequest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import static org.junit.Assert.assertEquals;

import io.jenkins.plugins.casc.MockHttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import javax.servlet.http.HttpServletRequest;
import org.junit.Test;

public class YamlSourceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import hudson.security.csrf.CrumbIssuer;
import hudson.security.csrf.CrumbIssuerDescriptor;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import jakarta.servlet.ServletRequest;
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletRequest;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.junit.Rule;
Expand Down

0 comments on commit 44ef3da

Please sign in to comment.