<div dir="ltr">Hi Jason,<div><br></div><div>thanks for your feedback, there seems to be several solutions better than mine :)</div><div><br></div><div>Cheers</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 25, 2014 at 2:02 AM, Jason Greene <span dir="ltr">&lt;<a href="mailto:jason.greene@redhat.com" target="_blank">jason.greene@redhat.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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.<br>
<div><div class="h5"><br>
On Sep 24, 2014, at 3:04 AM, Luke Ambrogio &lt;<a href="mailto:gryzlaw@hotmail.com">gryzlaw@hotmail.com</a>&gt; wrote:<br>
<br>
&gt; So I&#39;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&#39;s fantastic there&#39;s a feature which is either missing or I can&#39;t find.<br>
&gt;<br>
&gt; I want to develop a RESTful web service so it&#39;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.<br>
&gt;<br>
&gt; My solution, which works but feels wrong, is this:<br>
&gt;<br>
&gt; Created an annotation interface called HTTPMethod:<br>
&gt;<br>
&gt; @Retention(RetentionPolicy.RUNTIME)<br>
&gt; @Target(ElementType.METHOD)<br>
&gt;<br>
&gt;<br>
&gt; public @interface HTTPMethod {<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; public enum Method {<br>
&gt;<br>
&gt;<br>
&gt;     OTHER<br>
&gt; , GET, PUT, POST,<br>
&gt;  DELETE<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Method method() default Method.OTHER;<br>
&gt; an &quot;abstract&quot; class (which is not abstract):<br>
&gt;<br>
&gt; public abstract class RESTfulHandler implements HttpHandler {<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; @Override<br>
&gt; public void handleRequest(HttpServerExchange hse) throws Exception {<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; for (Method method : this.getClass().getDeclaredMethods()) {<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; // if method is annotated with @Test<br>
&gt;<br>
&gt;<br>
&gt; if (method.isAnnotationPresent(HTTPMethod.class)) {<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Annotation annotation = method.getAnnotation(HTTPMethod.class);<br>
&gt;<br>
&gt;<br>
&gt; HTTPMethod test = (HTTPMethod) annotation;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; switch (test.method()) {<br>
&gt;<br>
&gt;<br>
&gt; case PUT:<br>
&gt;<br>
&gt;<br>
&gt; if (hse.getRequestMethod().toString().equals(&quot;PUT&quot;)) {<br>
&gt;<br>
&gt;                         method<br>
&gt; .invoke(this);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; break;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; case POST:<br>
&gt;<br>
&gt;<br>
&gt; if (hse.getRequestMethod().toString().equals(&quot;POST&quot;)) {<br>
&gt;<br>
&gt;                         method<br>
&gt; .invoke(this);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; break;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; case GET:<br>
&gt;<br>
&gt;<br>
&gt; if (hse.getRequestMethod().toString().equals(&quot;GET&quot;)) {<br>
&gt;<br>
&gt;                         method<br>
&gt; .invoke(this);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; break;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; case DELETE:<br>
&gt;<br>
&gt;<br>
&gt; if (hse.getRequestMethod().toString().equals(&quot;DELETE&quot;)) {<br>
&gt;<br>
&gt;                         method<br>
&gt; .invoke(this);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; break;<br>
&gt;<br>
&gt;<br>
&gt; case OTHER:<br>
&gt;<br>
&gt;<br>
&gt; if (hse.getRequestMethod().toString().equals(&quot;OTHER&quot;)) {<br>
&gt;<br>
&gt;                         method<br>
&gt; .invoke(this);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; break;<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; if (test.method() == HTTPMethod.Method.PUT) {<br>
&gt;<br>
&gt;                 method<br>
&gt; .invoke(this);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt; }<br>
&gt; }<br>
&gt;<br>
&gt; and an implementation of both the above:<br>
&gt;<br>
&gt; public class ItemHandler extends RESTfulHandler{<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; @HTTPMethod(method=GET)<br>
&gt; public void getAllItems()<br>
&gt; {<br>
&gt;<br>
&gt;<br>
&gt; System.out.println(&quot;GET&quot;);<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; @HTTPMethod(method=POST)<br>
&gt; public void addItem()<br>
&gt; {<br>
&gt;<br>
&gt;<br>
&gt; System.out.println(&quot;POST&quot;);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; @HTTPMethod<br>
&gt; public void doNothing()<br>
&gt; {<br>
&gt;<br>
&gt;<br>
&gt; System.out.println(&quot;OTHERS&quot;);<br>
&gt;<br>
&gt;<br>
&gt; }<br>
&gt; }<br>
&gt;<br>
&gt; Now as I said, it works, but I&#39;m sure that the abstract class and it&#39;s implementation have something missing so that they glue correctly. So my question is two fold:<br>
&gt;<br>
&gt; 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?<br>
&gt;<br>
&gt; Thanks<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div><span class="">&gt; _______________________________________________<br>
&gt; undertow-dev mailing list<br>
&gt; <a href="mailto:undertow-dev@lists.jboss.org">undertow-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/undertow-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/undertow-dev</a><br>
<br>
</span>--<br>
Jason T. Greene<br>
WildFly Lead / JBoss EAP Platform Architect<br>
JBoss, a division of Red Hat<br>
<br>
<br>
</blockquote></div><br></div>