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

Jonathan Hart jonathan.hart at gmail.com
Thu Sep 25 05:49:50 EDT 2014


Here is a code example of how to use a RouterHandler in a way that I think
you are looking for...

https://gist.github.com/jonathanhart/0aeef8f72b9ef3a4ea78

How you handle the HTTPHandler part is entirely up to you, but this is how
I chose to do it.. but it does illustrate how to use RouterHandler with
HTTP verbs.. You can obviously have the same URL template for different
verbs and route them to different behaviors, as I do by passing in a custom
constructor..

Jonathan


On Thu, Sep 25, 2014 at 1:28 AM, Luke Ambrogio <gryzlaw at hotmail.com> wrote:

> 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
>>
>>
>>
>
> _______________________________________________
> undertow-dev mailing list
> undertow-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/undertow-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/undertow-dev/attachments/20140925/f57f6953/attachment-0001.html 


More information about the undertow-dev mailing list