package org.kohsuke.stapler.interceptor;

import java.io.IOException;
import java.io.PrintWriter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.verb.POST;

/**
 * Requires the request to be a POST.
 *
 * <p>
 * When the current request has a non-matching HTTP method (such as 'GET'), this annotation
 * will send a failure response instead of searching for other matching web methods.
 *
 * @author Kohsuke Kawaguchi
 * @since 1.180
 * @see POST
 */
@Retention(RUNTIME)
@Target({METHOD,FIELD})
@InterceptorAnnotation(RequirePOST.Processor.class)
public @interface RequirePOST {
    public static class Processor extends Interceptor {
        @Override
        public Object invoke(StaplerRequest request, StaplerResponse response, Object instance, Object[] arguments)
                throws IllegalAccessException, InvocationTargetException, ServletException {
            if (!request.getMethod().equals("POST")) {
                throw new InvocationTargetException(new HttpResponses.HttpResponseException() {
                    public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
                        rsp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
                        rsp.addHeader("Allow", "POST");
                        rsp.setContentType("text/html");
                        PrintWriter w = rsp.getWriter();
                        w.println("<html><head><title>POST required</title></head><body>");
                        w.println("POST is required for " + target.getQualifiedName() + "<br>");
                        w.println("<form method='POST'><input type='submit' value='Try POSTing'></form>");
                        w.println("</body></html>");
                    }
                });
            }
            return target.invoke(request, response, instance, arguments);
        }
    }
}

@@ -90,16 +90,59 @@
 tWriter;
+%0A%0Aimport org.kohsuke.stapler.ForwardToView;
 %0Aimport 
@@ -1047,345 +1047,1295 @@
 T %7B%0A
-    public static class Processor extends Interceptor %7B%0A        @Override%0A        public Object invoke(StaplerRequest request, StaplerResponse response, Object instance, Object%5B%5D arguments)%0A                throws IllegalAccessException, InvocationTargetException, ServletException %7B%0A            if (!request.getMethod().equals(%22POST%22)) %7B
+%0A    /**%0A     * Allows customizing the error page shown when an annotated method is called with the wrong HTTP method.%0A     */%0A    interface ErrorHandler %7B%0A        ForwardToView getForwardView();%0A    %7D%0A%0A    public static class Processor extends Interceptor %7B%0A        private static ErrorHandler handler;%0A%0A        /**%0A         * Register the custom error handler to be used when an annotated method is called with the wrong HTTP method.%0A         * @param errorHandler the error handler providing the view to show instead%0A         */%0A        public static void registerErrorHandler(ErrorHandler errorHandler) %7B%0A            handler = errorHandler;%0A        %7D%0A%0A        @Override%0A        public Object invoke(StaplerRequest request, StaplerResponse response, Object instance, Object%5B%5D arguments)%0A                throws IllegalAccessException, InvocationTargetException, ServletException %7B%0A            if (!request.getMethod().equals(%22POST%22)) %7B%0A                if (handler != null) %7B%0A                    ForwardToView forwardToView = handler.getForwardView();%0A                    if (forwardToView != null) %7B%0A                        throw new InvocationTargetException(forwardToView.with(%22requestURL%22, request.getRequestURLWithQueryString().toString()));%0A                    %7D%0A                %7D
 %0A   

Allow customizing RequirePOST error pages