diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index c7c9e67cff..50a2bd0cf5 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -45,7 +45,7 @@ public static final class DescriptorImpl extends Descriptor { /** 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"))); @@ -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); diff --git a/docs/REQUIREMENTS.md b/docs/REQUIREMENTS.md index 661a72a24e..475ef4a3a1 100644 --- a/docs/REQUIREMENTS.md +++ b/docs/REQUIREMENTS.md @@ -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. @@ -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"); ... @@ -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(); @@ -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")); diff --git a/plugin/src/main/java/io/jenkins/plugins/casc/CasCGlobalConfig.java b/plugin/src/main/java/io/jenkins/plugins/casc/CasCGlobalConfig.java index 7d1999aeac..61a20c4f89 100644 --- a/plugin/src/main/java/io/jenkins/plugins/casc/CasCGlobalConfig.java +++ b/plugin/src/main/java/io/jenkins/plugins/casc/CasCGlobalConfig.java @@ -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; /** * @@ -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); diff --git a/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCode.java b/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCode.java index 77fb17c0a4..751ff596a6 100644 --- a/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCode.java +++ b/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCode.java @@ -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; @@ -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; @@ -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; @@ -167,7 +167,7 @@ public List 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; @@ -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()); @@ -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; @@ -421,7 +421,7 @@ public List getBundledCasCURIs() { final String cascDirectory = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH + ".d/"; List res = new ArrayList<>(); - final ServletContext servletContext = Jenkins.get().servletContext; + final ServletContext servletContext = Jenkins.get().getServletContext(); try { URL bundled = servletContext.getResource(cascFile); if (bundled != null) { @@ -452,7 +452,7 @@ public List 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); @@ -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); @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCodeBootFailure.java b/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCodeBootFailure.java index 8c2d1a1a72..7e438a2d7d 100644 --- a/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCodeBootFailure.java +++ b/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCodeBootFailure.java @@ -1,10 +1,10 @@ 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 { @@ -12,7 +12,7 @@ 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()); } diff --git a/plugin/src/main/java/io/jenkins/plugins/casc/TokenReloadCrumbExclusion.java b/plugin/src/main/java/io/jenkins/plugins/casc/TokenReloadCrumbExclusion.java index 1480eb01ec..04c77343ac 100644 --- a/plugin/src/main/java/io/jenkins/plugins/casc/TokenReloadCrumbExclusion.java +++ b/plugin/src/main/java/io/jenkins/plugins/casc/TokenReloadCrumbExclusion.java @@ -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 { diff --git a/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlSource.java b/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlSource.java index fc178c1746..a0888c3771 100644 --- a/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlSource.java +++ b/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlSource.java @@ -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 Nicolas De Loof diff --git a/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlUtils.java b/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlUtils.java index a36b5bf129..cd64c5dada 100644 --- a/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlUtils.java +++ b/plugin/src/main/java/io/jenkins/plugins/casc/yaml/YamlUtils.java @@ -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; @@ -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; diff --git a/plugin/src/test/java/io/jenkins/plugins/casc/MockHttpServletRequest.java b/plugin/src/test/java/io/jenkins/plugins/casc/MockHttpServletRequest.java index 97b999b1df..4beb6ad562 100644 --- a/plugin/src/test/java/io/jenkins/plugins/casc/MockHttpServletRequest.java +++ b/plugin/src/test/java/io/jenkins/plugins/casc/MockHttpServletRequest.java @@ -1,5 +1,18 @@ 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; @@ -7,19 +20,6 @@ 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 { diff --git a/plugin/src/test/java/io/jenkins/plugins/casc/yaml/YamlSourceTest.java b/plugin/src/test/java/io/jenkins/plugins/casc/yaml/YamlSourceTest.java index 8ac3423e3b..423dcfeb18 100644 --- a/plugin/src/test/java/io/jenkins/plugins/casc/yaml/YamlSourceTest.java +++ b/plugin/src/test/java/io/jenkins/plugins/casc/yaml/YamlSourceTest.java @@ -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 { diff --git a/test-harness/src/test/java/io/jenkins/plugins/casc/JenkinsConfigTest.java b/test-harness/src/test/java/io/jenkins/plugins/casc/JenkinsConfigTest.java index b40a9d1304..cb0c656747 100644 --- a/test-harness/src/test/java/io/jenkins/plugins/casc/JenkinsConfigTest.java +++ b/test-harness/src/test/java/io/jenkins/plugins/casc/JenkinsConfigTest.java @@ -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;