Permalink
Browse files

Merge branch 'master' of github.com:caelum/vraptor

  • Loading branch information...
garcia-jj committed Aug 29, 2012
2 parents e5ac551 + 4d81efc commit efe6c0c829cf7ad6a5e7c11e3fe3797459827b50
View
15 pom.xml
@@ -218,15 +218,14 @@
<!-- [hibernate] -->
<dependency>
<groupId>org.hibernate</groupId>
- <artifactId>hibernate</artifactId>
- <version>3.2.0.ga</version>
+ <artifactId>hibernate-core</artifactId>
+ <version>3.6.10.Final</version>
<optional>true</optional>
- <exclusions>
- <exclusion>
- <groupId>javax.transaction</groupId>
- <artifactId>jta</artifactId>
- </exclusion>
- </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate.javax.persistence</groupId>
+ <artifactId>hibernate-jpa-2.0-api</artifactId>
+ <version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
@@ -35,6 +35,7 @@
import br.com.caelum.vraptor.core.Localization;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.http.MutableRequest;
+import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.resource.ResourceMethod;
import br.com.caelum.vraptor.validator.Message;
@@ -49,6 +50,7 @@
@Lazy
public class ParametersInstantiatorInterceptor implements Interceptor {
private final ParametersProvider provider;
+ private final ParameterNameProvider parameterNameProvider;
private final MethodInfo parameters;
private static final Logger logger = LoggerFactory.getLogger(ParametersInstantiatorInterceptor.class);
@@ -58,9 +60,10 @@
private final MutableRequest request;
private final FlashScope flash;
- public ParametersInstantiatorInterceptor(ParametersProvider provider, MethodInfo parameters,
+ public ParametersInstantiatorInterceptor(ParametersProvider provider, ParameterNameProvider parameterNameProvider, MethodInfo parameters,
Validator validator, Localization localization, MutableRequest request, FlashScope flash) {
this.provider = provider;
+ this.parameterNameProvider = parameterNameProvider;
this.parameters = parameters;
this.validator = validator;
this.localization = localization;
@@ -92,20 +95,23 @@ public void intercept(InterceptorStack stack, ResourceMethod method, Object reso
parameters.setParameters(values);
stack.next(method, resourceInstance);
}
-
+
private void addHeaderParametersToAttribute(ResourceMethod method) {
- Method trueMethod = method.getMethod();
- Annotation[][] parameterAnnotations = trueMethod.getParameterAnnotations();
-
- for (Annotation[] annotations : parameterAnnotations) {
- for (Annotation annotation : annotations) {
- if(annotation instanceof HeaderParam){
- HeaderParam headerParam = (HeaderParam) annotation;
- String value = request.getHeader(headerParam.value());
- request.setAttribute(headerParam.value(), value);
- }
- }
- }
+ Method trueMethod = method.getMethod();
+
+ String[] parameters = parameterNameProvider.parameterNamesFor(trueMethod);
+
+ Annotation[][] annotations = trueMethod.getParameterAnnotations();
+ for (int i = 0; i < annotations.length; i++) {
+ for (Annotation annotation : annotations[i]) {
+ if (annotation instanceof HeaderParam) {
+ HeaderParam headerParam = (HeaderParam) annotation;
+ String value = request.getHeader(headerParam.value());
+ request.setAttribute(parameters[i], value);
+ }
+ }
+ }
+
}
private void fixParameter(String name) {
@@ -22,8 +22,10 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.anyString;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -45,6 +47,7 @@
import br.com.caelum.vraptor.core.Localization;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.http.MutableRequest;
+import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.resource.DefaultResourceMethod;
import br.com.caelum.vraptor.resource.ResourceMethod;
@@ -56,6 +59,7 @@
private @Mock MethodInfo params;
private @Mock ParametersProvider parametersProvider;
+ private @Mock ParameterNameProvider parameterNameProvider;
private @Mock Validator validator;
private @Mock Localization localization;
private @Mock InterceptorStack stack;
@@ -76,7 +80,7 @@ public void setup() throws Exception {
when(localization.getBundle()).thenReturn(bundle);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.EMPTY_LIST));
- this.instantiator = new ParametersInstantiatorInterceptor(parametersProvider, params, validator, localization, request, flash);
+ this.instantiator = new ParametersInstantiatorInterceptor(parametersProvider, parameterNameProvider, params, validator, localization, request, flash);
this.errors = (List<Message>) new Mirror().on(instantiator).get().field("errors");
this.method = DefaultResourceMethod.instanceFor(Component.class, Component.class.getDeclaredMethod("method"));
@@ -91,8 +95,8 @@ void otherMethod(int oneParam){
}
class HeaderParamComponent{
- void method(@HeaderParam("password") String password) {}
- void otherMethod(@HeaderParam("user") String user,@HeaderParam("password") String password, @HeaderParam("token") String token) {}
+ void method(@HeaderParam("X-MyApp-Password") String password) {}
+ void otherMethod(@HeaderParam("X-MyApp-User") String user,@HeaderParam("X-MyApp-Password") String password, @HeaderParam("X-MyApp-Token") String token) {}
}
@Test
@@ -144,6 +148,7 @@ public void shouldThrowExceptionWhenThereIsAParameterContainingDotClass() throws
instantiator.intercept(stack, method, null);
}
+
@Test
public void shouldUseAndDiscardFlashParameters() throws InterceptionException, IOException, NoSuchMethodException {
Object[] values = new Object[] { new Object() };
@@ -182,49 +187,57 @@ public void shouldThrowException() throws Exception {
@Test
public void shouldAddHeaderInformationToRequestWhenHeaderParamAnnotationIsPresent() throws Exception {
Object[] values = new Object[] { new Object() };
- ResourceMethod method = DefaultResourceMethod.instanceFor(HeaderParamComponent.class, HeaderParamComponent.class.getDeclaredMethod("method", String.class));
- when(request.getHeader("password")).thenReturn("123");
- when(parametersProvider.getParametersFor(method, errors, bundle)).thenReturn(values);
+ Method method = HeaderParamComponent.class.getDeclaredMethod("method", String.class);
+ ResourceMethod resourceMethod = DefaultResourceMethod.instanceFor(HeaderParamComponent.class, method);
+
+ when(request.getHeader("X-MyApp-Password")).thenReturn("123");
+ when(parametersProvider.getParametersFor(resourceMethod, errors, bundle)).thenReturn(values);
+ when(parameterNameProvider.parameterNamesFor(method)).thenReturn(new String[]{"password"});
- instantiator.intercept(stack, method, null);
+ instantiator.intercept(stack, resourceMethod, null);
- verify(request).setAttribute("password", "123");
+ verify(request).setAttribute("password", "123");
verify(params).setParameters(values);
- verify(stack).next(method, null);
+ verify(stack).next(resourceMethod, null);
verify(validator).addAll(Collections.<Message>emptyList());
}
@Test
public void shouldAddHeaderInformationToRequestWhenHeaderParamAnnotationIsNotPresent() throws Exception {
Object[] values = new Object[] { new Object() };
- ResourceMethod method = DefaultResourceMethod.instanceFor(Component.class, Component.class.getDeclaredMethod("method"));
- when(parametersProvider.getParametersFor(method, errors, bundle)).thenReturn(values);
+ Method method = Component.class.getDeclaredMethod("method");
+ ResourceMethod resourceMethod = DefaultResourceMethod.instanceFor(Component.class, method);
+
+ when(parametersProvider.getParametersFor(resourceMethod, errors, bundle)).thenReturn(values);
+ when(parameterNameProvider.parameterNamesFor(method)).thenReturn(new String[]{"password"});
- instantiator.intercept(stack, method, null);
+ instantiator.intercept(stack, resourceMethod, null);
- verify(request, never()).setAttribute("password", "123");
+ verify(request, never()).setAttribute(anyString(), anyString());
verify(params).setParameters(values);
- verify(stack).next(method, null);
+ verify(stack).next(resourceMethod, null);
verify(validator).addAll(Collections.<Message>emptyList());
}
@Test
public void shouldAddVariousHeaderInformationsToRequestWhenHeaderParamAnnotationIsPresent() throws Exception {
Object[] values = new Object[] { new Object() };
- ResourceMethod method = DefaultResourceMethod.instanceFor(HeaderParamComponent.class, HeaderParamComponent.class.getDeclaredMethod("otherMethod", String.class, String.class, String.class));
+ Method method = HeaderParamComponent.class.getDeclaredMethod("otherMethod", String.class, String.class, String.class);
+ ResourceMethod resouceMethod = DefaultResourceMethod.instanceFor(HeaderParamComponent.class, method);
- when(request.getHeader("user")).thenReturn("user");
- when(request.getHeader("password")).thenReturn("123");
- when(request.getHeader("token")).thenReturn("daek2321");
- when(parametersProvider.getParametersFor(method, errors, bundle)).thenReturn(values);
+ when(request.getHeader("X-MyApp-User")).thenReturn("user");
+ when(request.getHeader("X-MyApp-Password")).thenReturn("123");
+ when(request.getHeader("X-MyApp-Token")).thenReturn("daek2321");
+ when(parametersProvider.getParametersFor(resouceMethod, errors, bundle)).thenReturn(values);
+ when(parameterNameProvider.parameterNamesFor(method)).thenReturn(new String[]{"user", "password", "token"});
- instantiator.intercept(stack, method, null);
+ instantiator.intercept(stack, resouceMethod, null);
verify(request).setAttribute("user", "user");
verify(request).setAttribute("password", "123");
verify(request).setAttribute("token", "daek2321");
verify(params).setParameters(values);
- verify(stack).next(method, null);
+ verify(stack).next(resouceMethod, null);
verify(validator).addAll(Collections.<Message>emptyList());
}

0 comments on commit efe6c0c

Please sign in to comment.