Skip to content

Commit

Permalink
support to options http verb in the controller resources through of O…
Browse files Browse the repository at this point in the history
…ptions annotation
  • Loading branch information
douglasrodrigo committed Oct 29, 2012
1 parent 1612b8f commit 8b0abb3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
36 changes: 36 additions & 0 deletions 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 {
}

This comment has been minimized.

Copy link
@lucascs

lucascs Oct 30, 2012

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


Expand Up @@ -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);

This comment has been minimized.

Copy link
@lucascs

lucascs Oct 30, 2012

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.

} catch (IOException e) {
throw new InterceptionException(e);
}
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -329,6 +330,11 @@ public void remove() {
public void head() {
}

@Path("/clients/options")
@Options
public void options() {
}

public void add() {
}

Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

This comment has been minimized.

Copy link
@lucascs

lucascs Oct 30, 2012

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

}

@Test(expected=InterceptionException.class)
Expand Down

1 comment on commit 8b0abb3

@douglasrodrigo
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.