Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support to options http verb in the controller resources through of O…

…ptions annotation
  • Loading branch information...
commit 8b0abb3757c4a28c64c0e36dd4830b00e1d53dc4 1 parent 1612b8f
@douglasrodrigo douglasrodrigo authored
View
36 vraptor-core/src/main/java/br/com/caelum/vraptor/Options.java
@@ -0,0 +1,36 @@
+/***
+ * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource
+ * All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package br.com.caelum.vraptor;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Supports the OPTIONS http method.
+ *
+ * @author Douglas Rodrigo Ferreira
+ */
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Options {
+}
@lucascs
lucascs added a note

Following the other annotations, this one should also accept a path attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
View
4 ...tor-core/src/main/java/br/com/caelum/vraptor/resource/DefaultMethodNotAllowedHandler.java
@@ -38,9 +38,7 @@ public void deny(RequestInfo request, Set<HttpMethod> allowedMethods) {
request.getResponse().addHeader(
"Allow", allowedMethods.toString().replaceAll("\\[|\\]", ""));
try {
- if (!"OPTIONS".equalsIgnoreCase(request.getRequest().getMethod())) {
- request.getResponse().sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
- }
+ request.getResponse().sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@lucascs
lucascs added a note

I don't think we should remove this if here... We should support OPTIONS the same way it is today... @Options is an extension for overriding default behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
} catch (IOException e) {
throw new InterceptionException(e);
}
View
4 vraptor-core/src/main/java/br/com/caelum/vraptor/resource/HttpMethod.java
@@ -24,13 +24,13 @@
import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Head;
+import br.com.caelum.vraptor.Options;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Trace;
public enum HttpMethod {
- // TODO: options?
- GET(Get.class), POST(Post.class), PUT(Put.class), DELETE(Delete.class), TRACE(Trace.class), HEAD(Head.class);
+ GET(Get.class), POST(Post.class), PUT(Put.class), DELETE(Delete.class), TRACE(Trace.class), HEAD(Head.class), OPTIONS(Options.class);
private static final String METHOD_PARAMETER = "_method";
private final Class<? extends Annotation> type;
View
15 ...r-core/src/test/java/br/com/caelum/vraptor/http/route/PathAnnotationRoutesParserTest.java
@@ -44,6 +44,7 @@
import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Head;
+import br.com.caelum.vraptor.Options;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.core.Converters;
@@ -329,6 +330,11 @@ public void remove() {
public void head() {
}
+ @Path("/clients/options")
+ @Options
+ public void options() {
+ }
+
public void add() {
}
@@ -411,6 +417,15 @@ public void shouldAcceptAResultWithASpecificWebMethod() throws SecurityException
}
+
+ @Test
+ public void shouldAcceptAResultWithOptionsWebMethod() throws SecurityException, NoSuchMethodException {
+ List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class));
+ Route route = getRouteMatching(routes, "/clients/options");
+
+ assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.OPTIONS)));
+ }
+
static class NiceClients extends ClientsController {
@Override
View
7 ...core/src/test/java/br/com/caelum/vraptor/resource/DefaultMethodNotAllowedHandlerTest.java
@@ -3,7 +3,6 @@
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -51,12 +50,12 @@ public void shouldSendErrorMethodNotAllowed() throws Exception {
}
@Test
- public void shouldNotSendMethodNotAllowedIfTheRequestMethodIsOptions() throws Exception {
+ public void shouldSendMethodNotAllowedIfTheRequestMethodIsOptions() throws Exception {
when(mockRequest.getMethod()).thenReturn("OPTIONS");
- this.handler.deny(request, EnumSet.of(HttpMethod.GET, HttpMethod.POST));
+ this.handler.deny(request, EnumSet.of(HttpMethod.OPTIONS));
- verify(response, never()).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+ verify(response).sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
@lucascs
lucascs added a note

this test doesn't apply if you remove that if.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
@Test(expected=InterceptionException.class)

1 comment on commit 8b0abb3

@douglasrodrigo
Collaborator

Hi @lucascs thanks for the tips, I'm already changing the code following your observations.

Please sign in to comment.
Something went wrong with that request. Please try again.