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

@AnyVerb annotation makes web methods explicitly accept any HTTP verb #205

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions core/src/main/java/org/kohsuke/stapler/verb/AnyVerb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.kohsuke.stapler.verb;

import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.interceptor.InterceptorAnnotation;
import org.kohsuke.stapler.interceptor.Stage;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Explicitly allows any HTTP verb for {@link WebMethod}.
* While conceptually similar to not have any annotation, it's friendlier for static analysis.
* This isn't a no-op only in case it would be combined with a more restrictive verb annotation, as it will continue to allow any verb.
*
*/
@Target(METHOD)
@Retention(RUNTIME)
@Documented
@InterceptorAnnotation(value = HttpVerbInterceptor.class, stage = Stage.SELECTION)
public @interface AnyVerb {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @see POST
* @see PUT
* @see DELETE
* @see AnyVerb
*/
public class HttpVerbInterceptor extends Interceptor {
@Override
Expand All @@ -57,7 +58,7 @@ private boolean matches(StaplerRequest request) {
Class<? extends Annotation> t = a.annotationType();
InterceptorAnnotation ia = t.getAnnotation(InterceptorAnnotation.class);
if (ia != null && ia.value() == HttpVerbInterceptor.class) {
if (t.getSimpleName().equals(method)) {
if (t.getSimpleName().equals(method) || t == AnyVerb.class) {
return true;
}
}
Expand Down
40 changes: 40 additions & 0 deletions core/src/test/java/org/kohsuke/stapler/DispatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.json.JsonBody;
import org.kohsuke.stapler.test.JettyTestCase;
import org.kohsuke.stapler.verb.AnyVerb;
import org.kohsuke.stapler.verb.DELETE;
import org.kohsuke.stapler.verb.GET;
import org.kohsuke.stapler.verb.POST;
import org.kohsuke.stapler.verb.PUT;
Expand Down Expand Up @@ -87,6 +89,44 @@ private void check(WebClient wc, HttpMethod m) throws java.io.IOException {
assertEquals("Got " + m.name(), p.getContent());
}

//===================================================================

public final AnyVerbMatch anyVerbMatch = new AnyVerbMatch();

public class AnyVerbMatch {
@WebMethod(name = "")
@GET
public HttpResponse get() {
return HttpResponses.text("get()");
}

@WebMethod(name = "")
@AnyVerb
@DELETE
public HttpResponse routeAny(StaplerRequest req) { // name sorts after 'get'
return HttpResponses.text("Got " + req.getMethod());
}

@WebMethod(name = "")
@POST
public HttpResponse zzzz(StaplerRequest req) { // name sorts last, unused
return HttpResponses.text("never");
}

}

public void testAnyVerbMatch() throws Exception {
WebClient wc = new WebClient();

checkText(wc, HttpMethod.GET, "get()");
checkText(wc, HttpMethod.POST, "Got POST");
checkText(wc, HttpMethod.DELETE, "Got DELETE");
}

private void checkText(WebClient wc, HttpMethod m, String text) throws java.io.IOException {
TextPage p = wc.getPage(new WebRequestSettings(new URL(url, "anyVerbMatch/"), m));
assertEquals(text, p.getContent());
}

//===================================================================

Expand Down