[undertow-dev] How do I handle HTTP Methods in Undertow?

Luke Ambrogio gryzlaw at hotmail.com
Thu Sep 25 04:28:44 EDT 2014


Hi Jason,

thanks for your feedback, there seems to be several solutions better than
mine :)

Cheers

On Thu, Sep 25, 2014 at 2:02 AM, Jason Greene <jason.greene at redhat.com>
wrote:

> This looks nice to me. Although for efficiency you would want to
> initialize the methods and cache them. If you prefer you could use the
> servlet API which allows you to filter methods with annotations as well.
>
> On Sep 24, 2014, at 3:04 AM, Luke Ambrogio <gryzlaw at hotmail.com> wrote:
>
> > So I've decided to start using Undertow, both as an experiment and due
> to the great results it achieved in benchmark tests. And while I think it's
> fantastic there's a feature which is either missing or I can't find.
> >
> > I want to develop a RESTful web service so it's important for me to
> identify which HTTP method is being called. Now I can get this from
> RequestMethod in the HttpServerExchange parameter but if had to that for
> every handler that would become tedious.
> >
> > My solution, which works but feels wrong, is this:
> >
> > Created an annotation interface called HTTPMethod:
> >
> > @Retention(RetentionPolicy.RUNTIME)
> > @Target(ElementType.METHOD)
> >
> >
> > public @interface HTTPMethod {
> >
> >
> >
> > public enum Method {
> >
> >
> >     OTHER
> > , GET, PUT, POST,
> >  DELETE
> >
> > }
> >
> >
> >
> > Method method() default Method.OTHER;
> > an "abstract" class (which is not abstract):
> >
> > public abstract class RESTfulHandler implements HttpHandler {
> >
> >
> >
> > @Override
> > public void handleRequest(HttpServerExchange hse) throws Exception {
> >
> >
> >
> > for (Method method : this.getClass().getDeclaredMethods()) {
> >
> >
> >
> > // if method is annotated with @Test
> >
> >
> > if (method.isAnnotationPresent(HTTPMethod.class)) {
> >
> >
> >
> > Annotation annotation = method.getAnnotation(HTTPMethod.class);
> >
> >
> > HTTPMethod test = (HTTPMethod) annotation;
> >
> >
> >
> > switch (test.method()) {
> >
> >
> > case PUT:
> >
> >
> > if (hse.getRequestMethod().toString().equals("PUT")) {
> >
> >                         method
> > .invoke(this);
> >
> >
> > }
> >
> >
> > break;
> >
> >
> >
> > case POST:
> >
> >
> > if (hse.getRequestMethod().toString().equals("POST")) {
> >
> >                         method
> > .invoke(this);
> >
> >
> > }
> >
> >
> > break;
> >
> >
> >
> > case GET:
> >
> >
> > if (hse.getRequestMethod().toString().equals("GET")) {
> >
> >                         method
> > .invoke(this);
> >
> >
> > }
> >
> >
> > break;
> >
> >
> >
> > case DELETE:
> >
> >
> > if (hse.getRequestMethod().toString().equals("DELETE")) {
> >
> >                         method
> > .invoke(this);
> >
> >
> > }
> >
> >
> > break;
> >
> >
> > case OTHER:
> >
> >
> > if (hse.getRequestMethod().toString().equals("OTHER")) {
> >
> >                         method
> > .invoke(this);
> >
> >
> > }
> >
> >
> > break;
> >
> >
> > }
> >
> >
> > if (test.method() == HTTPMethod.Method.PUT) {
> >
> >                 method
> > .invoke(this);
> >
> >
> > }
> >
> >
> > }
> >
> >
> > }
> > }
> > }
> >
> > and an implementation of both the above:
> >
> > public class ItemHandler extends RESTfulHandler{
> >
> >
> >
> > @HTTPMethod(method=GET)
> > public void getAllItems()
> > {
> >
> >
> > System.out.println("GET");
> > }
> >
> >
> >
> > @HTTPMethod(method=POST)
> > public void addItem()
> > {
> >
> >
> > System.out.println("POST");
> >
> >
> > }
> >
> >
> >
> > @HTTPMethod
> > public void doNothing()
> > {
> >
> >
> > System.out.println("OTHERS");
> >
> >
> > }
> > }
> >
> > Now as I said, it works, but I'm sure that the abstract class and it's
> implementation have something missing so that they glue correctly. So my
> question is two fold:
> >
> > 1) Is there a better / proper way to filter HTTP requests in Undertow?
> 2) What is the correct way of using annotations correctly correctly in the
> above case?
> >
> > Thanks
> >
> >
> >
> > _______________________________________________
> > undertow-dev mailing list
> > undertow-dev at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/undertow-dev
>
> --
> Jason T. Greene
> WildFly Lead / JBoss EAP Platform Architect
> JBoss, a division of Red Hat
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20140925/8da958ca/attachment.html 


More information about the undertow-dev mailing list