From 72a9b3bd97460577779e23ee3c50fdfec164eda3 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Mon, 18 Nov 2024 14:31:00 -0800 Subject: [PATCH] Migrate from EE 8 to EE 9 --- .../hudson/plugins/folder/AbstractFolder.java | 117 +++++++++++++++++- .../folder/AbstractFolderDescriptor.java | 4 +- .../folder/AbstractFolderProperty.java | 19 +++ .../AbstractFolderPropertyDescriptor.java | 19 +++ .../hudson/plugins/folder/Folder.java | 60 ++++++++- .../plugins/folder/FolderAddFilter.java | 4 +- .../hudson/plugins/folder/FolderIcon.java | 2 +- .../folder/FolderPropertyDescriptor.java | 15 +++ .../folder/computed/ComputedFolder.java | 71 ++++++++--- .../folder/computed/FolderComputation.java | 24 +++- .../plugins/folder/computed/PseudoRun.java | 10 +- .../config/AbstractFolderConfiguration.java | 4 +- .../plugins/folder/icons/StockFolderIcon.java | 2 +- .../folder/relocate/DefaultRelocationUI.java | 4 +- .../folder/ChildNameGeneratorAltTest.java | 4 +- .../folder/ChildNameGeneratorRecTest.java | 4 +- .../folder/ChildNameGeneratorTest.java | 4 +- .../folder/computed/ComputedFolderTest.java | 8 +- 18 files changed, 326 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java index a2ddd4fd..55c81d22 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java @@ -70,6 +70,9 @@ import hudson.util.HttpResponses; import hudson.views.DefaultViewsTabBar; import hudson.views.ViewsTabBar; +import io.jenkins.servlet.ServletExceptionWrapper; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -94,12 +97,12 @@ import java.util.logging.Logger; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; -import javax.servlet.ServletException; import jenkins.model.DirectlyModifiableTopLevelItemGroup; import jenkins.model.Jenkins; import jenkins.model.ModelObjectWithChildren; import jenkins.model.ProjectNamingStrategy; import jenkins.model.TransientActionFactory; +import jenkins.security.stapler.StaplerNotDispatchable; import net.sf.json.JSONObject; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.Beta; @@ -110,7 +113,9 @@ import org.kohsuke.stapler.StaplerFallback; import org.kohsuke.stapler.StaplerOverridable; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.StaplerResponse2; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.interceptor.RequirePOST; import org.kohsuke.stapler.verb.POST; @@ -723,7 +728,25 @@ private ItemGroup grp() { * {@inheritDoc} */ @Override + public ContextMenu doChildrenContextMenu(StaplerRequest2 request, StaplerResponse2 response) { + if (Util.isOverridden(AbstractFolder.class, getClass(), "doChildrenContextMenu", StaplerRequest.class, StaplerResponse.class)) { + return doChildrenContextMenu(request != null ? StaplerRequest.fromStaplerRequest2(request) : null, response != null ? StaplerResponse.fromStaplerResponse2(response) : null); + } else { + return doChildrenContextMenuImpl(request, response); + } + } + + /** + * @deprecated use {@link #doChildrenContextMenu(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @Override + @StaplerNotDispatchable public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse response) { + return doChildrenContextMenuImpl(request != null ? StaplerRequest.toStaplerRequest2(request) : null, response != null ? StaplerResponse.toStaplerResponse2(response) : null); + } + + private ContextMenu doChildrenContextMenuImpl(StaplerRequest2 request, StaplerResponse2 response) { ContextMenu menu = new ContextMenu(); for (View view : getViews()) { menu.add(view.getAbsoluteUrl(),view.getDisplayName()); @@ -732,7 +755,34 @@ public ContextMenu doChildrenContextMenu(StaplerRequest request, StaplerResponse } @POST + public synchronized void doCreateView(StaplerRequest2 req, StaplerResponse2 rsp) + throws IOException, ServletException, ParseException, Descriptor.FormException { + if (Util.isOverridden(AbstractFolder.class, getClass(), "doCreateView", StaplerRequest.class, StaplerResponse.class)) { + try { + doCreateView(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, rsp != null ? StaplerResponse.fromStaplerResponse2(rsp) : null); + } catch (javax.servlet.ServletException e) { + throw ServletExceptionWrapper.toJakartaServletException(e); + } + } else { + doCreateViewImpl(req, rsp); + } + } + + /** + * @deprecated use {@link #doCreateView(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @StaplerNotDispatchable public synchronized void doCreateView(StaplerRequest req, StaplerResponse rsp) + throws IOException, javax.servlet.ServletException, ParseException, Descriptor.FormException { + try { + doCreateViewImpl(req != null ? StaplerRequest.toStaplerRequest2(req) : null, rsp != null ? StaplerResponse.toStaplerResponse2(rsp) : null); + } catch (ServletException e) { + throw ServletExceptionWrapper.fromJakartaServletException(e); + } + } + + private void doCreateViewImpl(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, ParseException, Descriptor.FormException { checkPermission(View.CREATE); addView(View.create(req, rsp, this)); @@ -860,7 +910,24 @@ public DescribableList getHeal return healthMetrics; } + public HttpResponse doLastBuild(StaplerRequest2 req) { + if (Util.isOverridden(AbstractFolder.class, getClass(), "doLastBuild", StaplerRequest.class)) { + return doLastBuild(req != null ? StaplerRequest.fromStaplerRequest2(req) : null); + } else { + return doLastBuildImpl(req); + } + } + + /** + * @deprecated use {@link #doLastBuild(StaplerRequest2)} + */ + @Deprecated + @StaplerNotDispatchable public HttpResponse doLastBuild(StaplerRequest req) { + return doLastBuildImpl(req != null ? StaplerRequest.toStaplerRequest2(req) : null); + } + + private HttpResponse doLastBuildImpl(StaplerRequest2 req) { return HttpResponses.redirectToDot(); } @@ -1092,13 +1159,39 @@ public void renameTo(String newName) throws IOException { * {@inheritDoc} */ @Override - public synchronized void doSubmitDescription(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { + public synchronized void doSubmitDescription(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException { + if (Util.isOverridden(AbstractFolder.class, getClass(), "doSubmitDescription", StaplerRequest.class, StaplerResponse.class)) { + try { + doSubmitDescription(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, rsp != null ? StaplerResponse.fromStaplerResponse2(rsp) : null); + } catch (javax.servlet.ServletException e) { + throw ServletExceptionWrapper.toJakartaServletException(e); + } + } else { + doSubmitDescriptionImpl(req, rsp); + } + } + + /** + * @deprecated use {@link #doSubmitDescription(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @Override + @StaplerNotDispatchable + public synchronized void doSubmitDescription(StaplerRequest req, StaplerResponse rsp) throws IOException, javax.servlet.ServletException { + try { + doSubmitDescriptionImpl(req != null ? StaplerRequest.toStaplerRequest2(req) : null, rsp != null ? StaplerResponse.toStaplerResponse2(rsp) : null); + } catch (ServletException e) { + throw ServletExceptionWrapper.fromJakartaServletException(e); + } + } + + private void doSubmitDescriptionImpl(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException { getPrimaryView().doSubmitDescription(req, rsp); } @Restricted(NoExternalUse.class) @RequirePOST - public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { + public void doConfigSubmit(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, Descriptor.FormException { checkPermission(CONFIGURE); req.setCharacterEncoding("UTF-8"); @@ -1147,7 +1240,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOExc * * @return A string that represents the redirect location URL. * - * @see javax.servlet.http.HttpServletResponse#sendRedirect(String) + * @see HttpServletResponse#sendRedirect(String) */ @Restricted(NoExternalUse.class) @NonNull @@ -1155,7 +1248,21 @@ protected String getSuccessfulDestination() { return "."; } - protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException {} + protected void submit(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, Descriptor.FormException { + if (Util.isOverridden(AbstractFolder.class, getClass(), "submit", StaplerRequest.class, StaplerResponse.class)) { + try { + submit(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, rsp != null ? StaplerResponse.fromStaplerResponse2(rsp) : null); + } catch (javax.servlet.ServletException e) { + throw ServletExceptionWrapper.toJakartaServletException(e); + } + } + } + + /** + * @deprecated use {@link #submit(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, javax.servlet.ServletException, Descriptor.FormException {} /** * {@inheritDoc} diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderDescriptor.java b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderDescriptor.java index 28e82fde..6ee1f5d1 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderDescriptor.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderDescriptor.java @@ -44,7 +44,7 @@ import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.Stapler; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; /** * Category of {@link AbstractFolder}. @@ -124,7 +124,7 @@ public List getIconDescriptors() { r.add(p); } } - StaplerRequest request = Stapler.getCurrentRequest(); + StaplerRequest2 request = Stapler.getCurrentRequest2(); if (request != null) { AbstractFolder folder = request.findAncestorObject(AbstractFolder.class); if (folder != null) { diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderProperty.java b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderProperty.java index c06cced1..5464f62e 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderProperty.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderProperty.java @@ -27,6 +27,7 @@ import com.cloudbees.hudson.plugins.folder.health.FolderHealthMetric; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.ExtensionPoint; +import hudson.Util; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.model.HealthReport; @@ -39,6 +40,7 @@ import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.StaplerOverridable; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; /** * Property potentially applicable to any {@link AbstractFolder}. @@ -88,8 +90,25 @@ public Collection getItemContainerOverrides() { return Collections.emptyList(); } + @Override + public AbstractFolderProperty reconfigure(StaplerRequest2 req, JSONObject form) throws Descriptor.FormException { + if (Util.isOverridden(AbstractFolderProperty.class, getClass(), "reconfigure", StaplerRequest.class, JSONObject.class)) { + return reconfigure(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, form); + } else { + return reconfigureImpl(req, form); + } + } + + /** + * @deprecated use {@link #reconfigure(StaplerRequest2, JSONObject)} + */ + @Deprecated @Override public AbstractFolderProperty reconfigure(StaplerRequest req, JSONObject form) throws Descriptor.FormException { + return reconfigureImpl(req != null ? StaplerRequest.toStaplerRequest2(req) : null, form); + } + + private AbstractFolderProperty reconfigureImpl(StaplerRequest2 req, JSONObject form) throws Descriptor.FormException { return form == null ? null : getDescriptor().newInstance(req, form); } diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderPropertyDescriptor.java b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderPropertyDescriptor.java index 9158dfa5..aef3e9ef 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderPropertyDescriptor.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolderPropertyDescriptor.java @@ -25,6 +25,7 @@ package com.cloudbees.hudson.plugins.folder; import hudson.ExtensionList; +import hudson.Util; import hudson.model.Descriptor; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -33,6 +34,7 @@ import net.sf.json.JSONObject; import org.jvnet.tiger_types.Types; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; /** * Category of {@link AbstractFolderProperty}. @@ -47,6 +49,23 @@ public abstract class AbstractFolderPropertyDescriptor extends Descriptor newInstance(StaplerRequest2 req, JSONObject formData) throws FormException { + if (Util.isOverridden(AbstractFolderPropertyDescriptor.class, getClass(), "newInstance", StaplerRequest.class, JSONObject.class)) { + return newInstance(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, formData); + } else { + // Analogous to hack in JobPropertyDescriptor. + if (formData.isNullObject()) { + formData = new JSONObject(); + } + return super.newInstance(req, formData); + } + } + + /** + * @deprecated use {@link #newInstance(StaplerRequest2, JSONObject)} + */ + @Deprecated + @Override public AbstractFolderProperty newInstance(StaplerRequest req, JSONObject formData) throws FormException { // Analogous to hack in JobPropertyDescriptor. if (formData.isNullObject()) { diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java b/src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java index e2c343d6..fb4b84b6 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java @@ -43,6 +43,8 @@ import hudson.util.DescribableList; import hudson.views.ListViewColumn; import hudson.views.ViewJobFilter; +import io.jenkins.servlet.ServletExceptionWrapper; +import jakarta.servlet.ServletException; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -52,16 +54,18 @@ import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; -import javax.servlet.ServletException; import jenkins.model.DirectlyModifiableTopLevelItemGroup; import jenkins.model.Jenkins; import jenkins.model.TransientActionFactory; +import jenkins.security.stapler.StaplerNotDispatchable; import org.jenkins.ui.icon.Icon; import org.jenkins.ui.icon.IconSet; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.StaplerResponse2; import org.kohsuke.stapler.verb.POST; /** @@ -234,7 +238,32 @@ public void onCopiedFrom(Item _src) { } @POST - public TopLevelItem doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { + public TopLevelItem doCreateItem(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException { + if (Util.isOverridden(Folder.class, getClass(), "doCreateItem", StaplerRequest.class, StaplerResponse.class)) { + try { + return doCreateItem(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, rsp != null ? StaplerResponse.fromStaplerResponse2(rsp) : null); + } catch (javax.servlet.ServletException e) { + throw ServletExceptionWrapper.toJakartaServletException(e); + } + } else { + return doCreateItemImpl(req, rsp); + } + } + + /** + * @deprecated use {@link #doCreateItem(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @StaplerNotDispatchable + public TopLevelItem doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, javax.servlet.ServletException { + try { + return doCreateItemImpl(req != null ? StaplerRequest.toStaplerRequest2(req) : null, rsp != null ? StaplerResponse.toStaplerResponse2(rsp) : null); + } catch (ServletException e) { + throw ServletExceptionWrapper.fromJakartaServletException(e); + } + } + + private TopLevelItem doCreateItemImpl(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException { TopLevelItem nue = mixin.createTopLevelItem(req, rsp); if (!isAllowedChild(nue)) { // TODO would be better to intercept it before creation, if mode is set @@ -288,7 +317,32 @@ public TopLevelItem createProject(@NonNull TopLevelItemDescriptor type, @NonNull } @Override - protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, FormException { + protected void submit(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, FormException { + if (Util.isOverridden(Folder.class, getClass(), "submit", StaplerRequest.class, StaplerResponse.class)) { + try { + submit(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, rsp != null ? StaplerResponse.fromStaplerResponse2(rsp) : null); + } catch (javax.servlet.ServletException e) { + throw ServletExceptionWrapper.toJakartaServletException(e); + } + } else { + submitImpl(req, rsp); + } + } + + /** + * @deprecated use {@link #submit(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @Override + protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, javax.servlet.ServletException, FormException { + try { + submitImpl(req != null ? StaplerRequest.toStaplerRequest2(req) : null, rsp != null ? StaplerResponse.toStaplerResponse2(rsp) : null); + } catch (ServletException e) { + throw ServletExceptionWrapper.fromJakartaServletException(e); + } + } + + private void submitImpl(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, FormException { updateTransientActions(); } diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/FolderAddFilter.java b/src/main/java/com/cloudbees/hudson/plugins/folder/FolderAddFilter.java index 57ace031..2c05df91 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/FolderAddFilter.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/FolderAddFilter.java @@ -30,7 +30,7 @@ import hudson.model.TopLevelItemDescriptor; import hudson.model.View; import org.kohsuke.stapler.Stapler; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; /** * Restricts additions to a folder via {@code View/newJob.jelly}. @@ -39,7 +39,7 @@ @Extension public class FolderAddFilter extends DescriptorVisibilityFilter { @Override public boolean filter(Object context, Descriptor descriptor) { - StaplerRequest req = Stapler.getCurrentRequest(); + StaplerRequest2 req = Stapler.getCurrentRequest2(); // View/newJob.jelly for 1.x, View.doItemCategories for 2.x if (req == null || !req.getRequestURI().matches(".*/(newJob|itemCategories)")) { return true; diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/FolderIcon.java b/src/main/java/com/cloudbees/hudson/plugins/folder/FolderIcon.java index cacf9378..03957c3f 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/FolderIcon.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/FolderIcon.java @@ -79,7 +79,7 @@ protected String iconClassNameImageOf(String size) { Icon icon = IconSet.icons.getIconByClassSpec(iconClassName + " " + spec); if (icon != null) { JellyContext ctx = new JellyContext(); - ctx.setVariable("resURL", Stapler.getCurrentRequest().getContextPath() + Jenkins.RESOURCE_PATH); + ctx.setVariable("resURL", Stapler.getCurrentRequest2().getContextPath() + Jenkins.RESOURCE_PATH); return icon.getQualifiedUrl(ctx); } } diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/FolderPropertyDescriptor.java b/src/main/java/com/cloudbees/hudson/plugins/folder/FolderPropertyDescriptor.java index 9c815eb5..5472e8af 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/FolderPropertyDescriptor.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/FolderPropertyDescriptor.java @@ -26,8 +26,10 @@ import hudson.DescriptorExtensionList; import hudson.ExtensionList; +import hudson.Util; import net.sf.json.JSONObject; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import java.util.ArrayList; import java.util.List; @@ -35,6 +37,19 @@ public abstract class FolderPropertyDescriptor extends AbstractFolderPropertyDescriptor { + @Override + public FolderProperty newInstance(StaplerRequest2 req, JSONObject formData) throws FormException { + if (Util.isOverridden(FolderPropertyDescriptor.class, getClass(), "newInstance", StaplerRequest.class, JSONObject.class)) { + return newInstance(req != null ? StaplerRequest.fromStaplerRequest2(req) : null, formData); + } else { + return (FolderProperty) super.newInstance(req, formData); + } + } + + /** + * @deprecated use {@link #newInstance(StaplerRequest2, JSONObject)} + */ + @Deprecated @Override public FolderProperty newInstance(StaplerRequest req, JSONObject formData) throws FormException { return (FolderProperty) super.newInstance(req, formData); diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java index e5d15e85..d6fced4f 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java @@ -51,6 +51,9 @@ import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import hudson.util.DescribableList; +import io.jenkins.servlet.ServletExceptionWrapper; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -68,7 +71,6 @@ import java.util.logging.Logger; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; -import javax.servlet.ServletException; import jenkins.model.Jenkins; import jenkins.model.ParameterizedJobMixIn; import jenkins.triggers.TriggeredItem; @@ -83,12 +85,12 @@ import org.kohsuke.stapler.HttpResponses; import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.StaplerResponse2; import org.kohsuke.stapler.export.Exported; import org.kohsuke.stapler.interceptor.RequirePOST; -import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; - /** * A folder-like item whose children are computed. * Users cannot directly add or remove (or rename) children. @@ -158,11 +160,11 @@ public abstract class ComputedFolder extends AbstractFol private transient boolean currentObservationsLockDisabled; /** - * Tracks recalculation requirements in {@link #doConfigSubmit(StaplerRequest, StaplerResponse)}. + * Tracks recalculation requirements in {@link #doConfigSubmit(StaplerRequest2, StaplerResponse2)}. * * @see #recalculateAfterSubmitted(boolean) - * @see #submit(StaplerRequest, StaplerResponse) - * @see #doConfigSubmit(StaplerRequest, StaplerResponse) + * @see #submit(StaplerRequest2, StaplerResponse2) + * @see #doConfigSubmit(StaplerRequest2, StaplerResponse2) */ private transient Recalculation recalculate; @@ -386,7 +388,7 @@ public boolean supportsMakeDisabled() { */ @RequirePOST @Override - public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { + public void doConfigSubmit(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, Descriptor.FormException { try { recalculate = Recalculation.UNKNOWN; super.doConfigSubmit(req, rsp); @@ -400,13 +402,13 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOExc /** * Method for child classes to use if they want to suppress/confirm the automatic recalculation provided in - * {@link #doConfigSubmit(StaplerRequest, StaplerResponse)}. This method should only be called from - * {@link #submit(StaplerRequest, StaplerResponse)}. If called multiple times from - * {@link #submit(StaplerRequest, StaplerResponse)} then all calls must be with the {@code false} parameter + * {@link #doConfigSubmit(StaplerRequest2, StaplerResponse2)}. This method should only be called from + * {@link #submit(StaplerRequest2, StaplerResponse2)}. If called multiple times from + * {@link #submit(StaplerRequest2, StaplerResponse2)} then all calls must be with the {@code false} parameter * to suppress recalculation. * * @param recalculate {@code true} to require recalculation, {@code false} to suppress recalculation. - * @see #submit(StaplerRequest, StaplerResponse) + * @see #submit(StaplerRequest2, StaplerResponse2) */ /* * Note: it would have been much nicer to have submit(req,rsp) return a boolean... but that would have required @@ -429,7 +431,48 @@ protected final void recalculateAfterSubmitted(boolean recalculate) { * @see #recalculateAfterSubmitted(boolean) */ @Override - protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, Descriptor.FormException { + protected void submit(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, Descriptor.FormException { + if (Util.isOverridden(ComputedFolder.class, getClass(), "submit", StaplerRequest.class, StaplerResponse.class)) { + try { + submit(StaplerRequest.fromStaplerRequest2(req), StaplerResponse.fromStaplerResponse2(rsp)); + } catch (javax.servlet.ServletException e) { + throw ServletExceptionWrapper.toJakartaServletException(e); + } + } else { + String oisDigest = null; + try { + oisDigest = Util.getDigestOf(Items.XSTREAM2.toXML(orphanedItemStrategy)); + } catch (XStreamException e) { + // ignore + } + super.submit(req, rsp); + JSONObject json = req.getSubmittedForm(); + orphanedItemStrategy = req.bindJSON(OrphanedItemStrategy.class, json.getJSONObject("orphanedItemStrategy")); + for (Trigger t : triggers) { + t.stop(); + } + triggers.rebuild(req, json, Trigger.for_(this)); + for (Trigger t : triggers) { + t.start(this, true); + } + try { + if (oisDigest == null || !oisDigest.equals(Util.getDigestOf(Items.XSTREAM2.toXML(orphanedItemStrategy)))) { + // force a recalculation if orphanedItemStrategy has changed as recalculation is when we find orphans + recalculateAfterSubmitted(true); + } + } catch (XStreamException e) { + // force a recalculation anyway in this case + recalculateAfterSubmitted(true); + } + } + } + + /** + * @deprecated use {@link #submit(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @Override + protected void submit(StaplerRequest req, StaplerResponse rsp) throws IOException, javax.servlet.ServletException, Descriptor.FormException { String oisDigest = null; try { oisDigest = Util.getDigestOf(Items.XSTREAM2.toXML(orphanedItemStrategy)); @@ -548,7 +591,7 @@ public boolean isBuildable() { public HttpResponse doBuild(@QueryParameter TimeDuration delay) { checkPermission(BUILD); if (!isBuildable()) { - throw HttpResponses.error(SC_INTERNAL_SERVER_ERROR, new IOException(getFullName() + " cannot be recomputed")); + throw HttpResponses.error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, new IOException(getFullName() + " cannot be recomputed")); } scheduleBuild2(delay == null ? 0 : delay.getTimeInSeconds(), new CauseAction(new Cause.UserIdCause())); return HttpResponses.forwardToPreviousPage(); @@ -993,7 +1036,7 @@ public Map orphaned() { } /** - * Records the recalculation requirements of a call to {@link #submit(StaplerRequest, StaplerResponse)}. + * Records the recalculation requirements of a call to {@link #submit(StaplerRequest2, StaplerResponse2)}. */ private enum Recalculation { /** diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/FolderComputation.java b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/FolderComputation.java index 8933696a..36346c12 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/FolderComputation.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/FolderComputation.java @@ -53,6 +53,7 @@ import hudson.util.HttpResponses; import hudson.util.StreamTaskListener; import hudson.util.io.RewindableRotatingFileOutputStream; +import jakarta.servlet.ServletException; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; @@ -72,7 +73,7 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import javax.servlet.ServletException; +import jenkins.security.stapler.StaplerNotDispatchable; import net.jcip.annotations.GuardedBy; import java.nio.charset.StandardCharsets; import jenkins.model.Loadable; @@ -81,7 +82,9 @@ import org.apache.commons.jelly.XMLOutput; import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.StaplerResponse2; import org.kohsuke.stapler.framework.io.ByteBuffer; import org.kohsuke.stapler.interceptor.RequirePOST; @@ -377,9 +380,26 @@ public void writeWholeLogTo(@NonNull OutputStream out) throws IOException, Inter * @param rsp the response. * @throws IOException if things go wrong. */ + public void doConsoleText(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException { + if (Util.isOverridden(FolderComputation.class, getClass(), "doConsoleText", StaplerRequest.class, StaplerResponse.class)) { + doConsoleText(StaplerRequest.fromStaplerRequest2(req), StaplerResponse.fromStaplerResponse2(rsp)); + } else { + doConsoleTextImpl(req, rsp); + } + } + + /** + * @deprecated use {@link #doConsoleText(StaplerRequest2, StaplerResponse2)} + */ + @Deprecated + @StaplerNotDispatchable public void doConsoleText(StaplerRequest req, StaplerResponse rsp) throws IOException { + doConsoleTextImpl(StaplerRequest.toStaplerRequest2(req), StaplerResponse.toStaplerResponse2(rsp)); + } + + private void doConsoleTextImpl(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException { rsp.setContentType("text/plain;charset=UTF-8"); - try (PlainTextConsoleOutputStream out = new PlainTextConsoleOutputStream(rsp.getCompressedOutputStream(req)); + try (PlainTextConsoleOutputStream out = new PlainTextConsoleOutputStream(rsp.getOutputStream()); InputStream input = getLogInputStream()) { IOUtils.copy(input, out); out.flush(); diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/PseudoRun.java b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/PseudoRun.java index a061ae9b..ebcce0b3 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/computed/PseudoRun.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/computed/PseudoRun.java @@ -17,7 +17,7 @@ import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.HttpResponses; import org.kohsuke.stapler.StaplerFallback; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; /** * A fake {@link Run} used to render last build information via Stapler and Jelly @@ -34,7 +34,7 @@ public String getDisplayName() { return "log"; } - public RunUrl decompose(StaplerRequest req) { + public RunUrl decompose(StaplerRequest2 req) { List ancestors = req.getAncestors(); // find the first and last Run instances @@ -104,15 +104,15 @@ public Object getStaplerFallback() { return computation; } - public HttpResponse doIndex(StaplerRequest request) { + public HttpResponse doIndex(StaplerRequest2 request) { return HttpResponses.redirectViaContextPath(computation.getUrl()); } - public HttpResponse doConsole(StaplerRequest request) { + public HttpResponse doConsole(StaplerRequest2 request) { return HttpResponses.redirectViaContextPath(computation.getUrl() + "console"); } - public HttpResponse doConsoleText(StaplerRequest request) { + public HttpResponse doConsoleText(StaplerRequest2 request) { return HttpResponses.redirectViaContextPath(computation.getUrl() + "consoleText"); } diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfiguration.java b/src/main/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfiguration.java index 6a1658aa..39d081b6 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfiguration.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/config/AbstractFolderConfiguration.java @@ -13,7 +13,7 @@ import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import edu.umd.cs.findbugs.annotations.NonNull; import java.util.ArrayList; @@ -84,7 +84,7 @@ public void setHealthMetrics(List healthMetrics) { } @Override - public boolean configure(StaplerRequest req, JSONObject json) { + public boolean configure(StaplerRequest2 req, JSONObject json) { if(json.containsKey("healthMetrics")) { req.bindJSON(this, json); this.save(); diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/icons/StockFolderIcon.java b/src/main/java/com/cloudbees/hudson/plugins/folder/icons/StockFolderIcon.java index 85fd468f..59c21afc 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/icons/StockFolderIcon.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/icons/StockFolderIcon.java @@ -48,7 +48,7 @@ public String getImageOf(String size) { String image = iconClassNameImageOf(size); return image != null ? image - : (Stapler.getCurrentRequest().getContextPath() + Hudson.RESOURCE_PATH + : (Stapler.getCurrentRequest2().getContextPath() + Hudson.RESOURCE_PATH + "/plugin/cloudbees-folder/images/svgs/folder.svg"); } diff --git a/src/main/java/com/cloudbees/hudson/plugins/folder/relocate/DefaultRelocationUI.java b/src/main/java/com/cloudbees/hudson/plugins/folder/relocate/DefaultRelocationUI.java index bcabe224..51945ef5 100644 --- a/src/main/java/com/cloudbees/hudson/plugins/folder/relocate/DefaultRelocationUI.java +++ b/src/main/java/com/cloudbees/hudson/plugins/folder/relocate/DefaultRelocationUI.java @@ -39,7 +39,7 @@ import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.HttpResponse; import org.kohsuke.stapler.QueryParameter; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.interceptor.RequirePOST; /** @@ -98,7 +98,7 @@ public Collection> listDestinations(Item item) { * @throws InterruptedException if interrupted. */ @RequirePOST - public HttpResponse doMove(StaplerRequest req, @AncestorInPath Item item, @QueryParameter String destination) + public HttpResponse doMove(StaplerRequest2 req, @AncestorInPath Item item, @QueryParameter String destination) throws IOException, InterruptedException { item.checkPermission(RelocationAction.RELOCATE); ItemGroup dest = null; diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java index af4a2780..3119f265 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java @@ -61,7 +61,7 @@ import org.jvnet.hudson.test.RestartableJenkinsRule; import org.jvnet.hudson.test.TestExtension; import org.jvnet.hudson.test.recipes.LocalData; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorTest.asJavaStrings; import static org.hamcrest.MatcherAssert.assertThat; @@ -574,7 +574,7 @@ public String getName() { } @Override - public JobProperty reconfigure(StaplerRequest req, JSONObject form) { + public JobProperty reconfigure(StaplerRequest2 req, JSONObject form) { return this; } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java index 0d09ee1d..ceee3262 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java @@ -60,7 +60,7 @@ import org.jvnet.hudson.test.RestartableJenkinsRule; import org.jvnet.hudson.test.TestExtension; import org.jvnet.hudson.test.recipes.LocalData; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorAltTest.windowsFFS; import static org.hamcrest.MatcherAssert.assertThat; @@ -484,7 +484,7 @@ public String getName() { } @Override - public JobProperty reconfigure(StaplerRequest req, JSONObject form) { + public JobProperty reconfigure(StaplerRequest2 req, JSONObject form) { return this; } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java index 34731fa6..666f9901 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java @@ -61,7 +61,7 @@ import org.jvnet.hudson.test.RestartableJenkinsRule; import org.jvnet.hudson.test.TestExtension; import org.jvnet.hudson.test.recipes.LocalData; -import org.kohsuke.stapler.StaplerRequest; +import org.kohsuke.stapler.StaplerRequest2; import static com.cloudbees.hudson.plugins.folder.ChildNameGeneratorAltTest.windowsFFS; import static org.hamcrest.MatcherAssert.assertThat; @@ -667,7 +667,7 @@ public String getName() { } @Override - public JobProperty reconfigure(StaplerRequest req, JSONObject form) { + public JobProperty reconfigure(StaplerRequest2 req, JSONObject form) { return this; } diff --git a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java index 19cf5afe..360b32ae 100644 --- a/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java +++ b/src/test/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolderTest.java @@ -70,6 +70,7 @@ import hudson.util.StreamTaskListener; import hudson.views.DefaultViewsTabBar; import hudson.views.ViewsTabBar; +import jakarta.servlet.ServletException; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.management.ThreadInfo; @@ -86,7 +87,6 @@ import java.util.concurrent.CancellationException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import javax.servlet.ServletException; import jenkins.model.CauseOfInterruption; import jenkins.model.InterruptedBuildAction; import jenkins.model.Jenkins; @@ -101,8 +101,8 @@ import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.SleepBuilder; import org.jvnet.hudson.test.TestExtension; -import org.kohsuke.stapler.StaplerRequest; -import org.kohsuke.stapler.StaplerResponse; +import org.kohsuke.stapler.StaplerRequest2; +import org.kohsuke.stapler.StaplerResponse2; public class ComputedFolderTest { @@ -983,7 +983,7 @@ private VariableRecomputationComputedFolder(ItemGroup parent, String name) { } @Override - protected void submit(StaplerRequest req, StaplerResponse rsp) + protected void submit(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException, Descriptor.FormException { super.submit(req, rsp); if (submit != null) {