All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kohsuke.stapler.verb.HttpVerbInterceptor Maven / Gradle / Ivy

There is a newer version: 1.263
Show newest version
package org.kohsuke.stapler.verb;

import org.kohsuke.stapler.CancelRequestHandlingException;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.Interceptor;
import org.kohsuke.stapler.interceptor.InterceptorAnnotation;

import javax.servlet.ServletException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;

/**
 * Restricts the routing to matching HTTP verbs.
 *
 * 

Usage

*

* This package defines a number of HTTP verb (method) annotations that can be used to restrict * routing. For example, * *

 * @WebMethod(name="") @DELETE
 * public void delete() {
 *     // this method will be invoked only when the request is DELETE
 *     ...
 * }
 *
 * @WebMethod(name="") @POST
 * public void create(@JsonBody Order order) {
 *     // this method will be invoked only when the request is POST
 *     ...
 * }
 * 
* *

* This class is the actual logic that implements this semantics on top of {@link Interceptor}. * * @author Kohsuke Kawaguchi * @see GET * @see POST * @see PUT * @see DELETE */ public class HttpVerbInterceptor extends Interceptor { @Override public Object invoke(StaplerRequest request, StaplerResponse response, Object instance, Object[] arguments) throws IllegalAccessException, InvocationTargetException, ServletException { if (matches(request)) return target.invoke(request,response,instance,arguments); else throw new CancelRequestHandlingException(); } private boolean matches(StaplerRequest request) { String method = request.getMethod(); for (Annotation a : target.getAnnotations()) { Class t = a.annotationType(); InterceptorAnnotation ia = t.getAnnotation(InterceptorAnnotation.class); if (ia !=null && ia.value()==HttpVerbInterceptor.class) { if (t.getName().endsWith(method)) return true; } } return false; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy