/** * Actually resolve the value and check the resolved value is not * {@link WebArgumentResolver#UNRESOLVED} absorbing _any_ exceptions. */ @Override public boolean supportsParameter(MethodParameter parameter) { try { NativeWebRequest webRequest = getWebRequest(); Object result = this.adaptee.resolveArgument(parameter, webRequest); if (result == WebArgumentResolver.UNRESOLVED) { return false; } else { return ClassUtils.isAssignableValue(parameter.getParameterType(), result); } } catch (Exception ex) { // ignore (see class-level doc) logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage()); return false; } }
/** * Delegate to the {@link WebArgumentResolver} instance. * @exception IllegalStateException if the resolved value is not assignable * to the method parameter. */ @Override public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); Object result = this.adaptee.resolveArgument(parameter, webRequest); if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) { throw new IllegalStateException( "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() + "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) + "]. Consider declaring the argument type in a less specific fashion."); } return result; }
/** * Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}. */ private Object[] resolveHandlerArguments(Method handlerMethod, Object handler, NativeWebRequest webRequest, Exception thrownException) throws Exception { Class<?>[] paramTypes = handlerMethod.getParameterTypes(); Object[] args = new Object[paramTypes.length]; Class<?> handlerType = handler.getClass(); for (int i = 0; i < args.length; i++) { MethodParameter methodParam = new SynthesizingMethodParameter(handlerMethod, i); GenericTypeResolver.resolveParameterType(methodParam, handlerType); Class<?> paramType = methodParam.getParameterType(); Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException); if (argValue != WebArgumentResolver.UNRESOLVED) { args[i] = argValue; } else { throw new IllegalStateException("Unsupported argument [" + paramType.getName() + "] for @ExceptionHandler method: " + handlerMethod); } } return args; }
private ManagedList<Object> wrapLegacyResolvers(List<Object> list, ParserContext context) { ManagedList<Object> result = new ManagedList<Object>(); for (Object object : list) { if (object instanceof BeanDefinitionHolder) { BeanDefinitionHolder beanDef = (BeanDefinitionHolder) object; String className = beanDef.getBeanDefinition().getBeanClassName(); Class<?> clazz = ClassUtils.resolveClassName(className, context.getReaderContext().getBeanClassLoader()); if (WebArgumentResolver.class.isAssignableFrom(clazz)) { RootBeanDefinition adapter = new RootBeanDefinition(ServletWebArgumentResolverAdapter.class); adapter.getConstructorArgumentValues().addIndexedArgumentValue(0, beanDef); result.add(new BeanDefinitionHolder(adapter, beanDef.getBeanName() + "Adapter")); continue; } } result.add(object); } return result; }
/** * Resolves the arguments for the given method. Delegates to {@link #resolveCommonArgument}. */ private Object[] resolveHandlerArguments(Method handlerMethod, Object handler, NativeWebRequest webRequest, Exception thrownException) throws Exception { Class<?>[] paramTypes = handlerMethod.getParameterTypes(); Object[] args = new Object[paramTypes.length]; Class<?> handlerType = handler.getClass(); for (int i = 0; i < args.length; i++) { MethodParameter methodParam = new MethodParameter(handlerMethod, i); GenericTypeResolver.resolveParameterType(methodParam, handlerType); Class<?> paramType = methodParam.getParameterType(); Object argValue = resolveCommonArgument(methodParam, webRequest, thrownException); if (argValue != WebArgumentResolver.UNRESOLVED) { args[i] = argValue; } else { throw new IllegalStateException("Unsupported argument [" + paramType.getName() + "] for @ExceptionHandler method: " + handlerMethod); } } return args; }
private ManagedList<BeanDefinitionHolder> wrapWebArgumentResolverBeanDefs( List<BeanDefinitionHolder> beanDefs, ParserContext parserContext) { ManagedList<BeanDefinitionHolder> result = new ManagedList<BeanDefinitionHolder>(); for (BeanDefinitionHolder beanDef : beanDefs) { String className = beanDef.getBeanDefinition().getBeanClassName(); Class<?> clazz = ClassUtils.resolveClassName(className, parserContext.getReaderContext().getBeanClassLoader()); if (WebArgumentResolver.class.isAssignableFrom(clazz)) { RootBeanDefinition adapter = new RootBeanDefinition(ServletWebArgumentResolverAdapter.class); adapter.getConstructorArgumentValues().addIndexedArgumentValue(0, beanDef); result.add(new BeanDefinitionHolder(adapter, beanDef.getBeanName() + "Adapter")); } else { result.add(beanDef); } } return result; }
/** * Actually resolve the value and check the resolved value is not * {@link WebArgumentResolver#UNRESOLVED} absorbing _any_ exceptions. */ public boolean supportsParameter(MethodParameter parameter) { try { NativeWebRequest webRequest = getWebRequest(); Object result = this.adaptee.resolveArgument(parameter, webRequest); if (result == WebArgumentResolver.UNRESOLVED) { return false; } else { return ClassUtils.isAssignableValue(parameter.getParameterType(), result); } } catch (Exception ex) { // ignore (see class-level doc) logger.debug("Error in checking support for parameter [" + parameter + "], message: " + ex.getMessage()); return false; } }
/** * Delegate to the {@link WebArgumentResolver} instance. * @exception IllegalStateException if the resolved value is not assignable * to the method parameter. */ public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); Object result = this.adaptee.resolveArgument(parameter, webRequest); if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) { throw new IllegalStateException( "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() + "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) + "]. Consider declaring the argument type in a less specific fashion."); } return result; }
@Test public void testResolveArgument() throws Exception { // given ModelAndViewContainer mavContainer = mock(ModelAndViewContainer.class); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); NativeWebRequest webRequest = mock(NativeWebRequest.class); User stubUser = new User(USER_NAME, "", Collections.emptyList()); Principal stubPrincipal = new UsernamePasswordAuthenticationToken(stubUser, null); when(webRequest.getUserPrincipal()).thenReturn(stubPrincipal); // when/then assertEquals(stubUser, resolver.resolveArgument(validParam, mavContainer, webRequest,binderFactory)); assertEquals(WebArgumentResolver.UNRESOLVED, resolver.resolveArgument(notAnnotatedParam, mavContainer, webRequest,binderFactory)); assertEquals(WebArgumentResolver.UNRESOLVED, resolver.resolveArgument(wrongTypeParam, mavContainer, webRequest,binderFactory)); }
public HandlerMethodInvoker(HandlerMethodResolver methodResolver, WebBindingInitializer bindingInitializer, SessionAttributeStore sessionAttributeStore, ParameterNameDiscoverer parameterNameDiscoverer, WebArgumentResolver[] customArgumentResolvers, HttpMessageConverter<?>[] messageConverters) { this.methodResolver = methodResolver; this.bindingInitializer = bindingInitializer; this.sessionAttributeStore = sessionAttributeStore; this.parameterNameDiscoverer = parameterNameDiscoverer; this.customArgumentResolvers = customArgumentResolvers; this.messageConverters = messageConverters; }
/** * Delegate to the {@link WebArgumentResolver} instance. * @exception IllegalStateException if the resolved value is not assignable * to the method parameter. */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); Object result = this.adaptee.resolveArgument(parameter, webRequest); if (result == WebArgumentResolver.UNRESOLVED || !ClassUtils.isAssignableValue(paramType, result)) { throw new IllegalStateException( "Standard argument type [" + paramType.getName() + "] in method " + parameter.getMethod() + "resolved to incompatible value of type [" + (result != null ? result.getClass() : null) + "]. Consider declaring the argument type in a less specific fashion."); } return result; }
@Before public void setUp() throws Exception { adaptee = mock(WebArgumentResolver.class); adapter = new TestWebArgumentResolverAdapter(adaptee); parameter = new MethodParameter(getClass().getMethod("handle", Integer.TYPE), 0); webRequest = new ServletWebRequest(new MockHttpServletRequest()); // Expose request to the current thread (for SpEL expressions) RequestContextHolder.setRequestAttributes(webRequest); }
@Test public void supportsParameterUnresolved() throws Exception { given(adaptee.resolveArgument(parameter, webRequest)).willReturn(WebArgumentResolver.UNRESOLVED); assertFalse("Parameter supported", adapter.supportsParameter(parameter)); verify(adaptee).resolveArgument(parameter, webRequest); }
@Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (supportsParameter(methodParameter)) { return createUserDetails(webRequest); } else { return WebArgumentResolver.UNRESOLVED; } }
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (this.supportsParameter(methodParameter)) { return securityUtils.getCurrentUser(); } else { return WebArgumentResolver.UNRESOLVED; } }
/** * spring 3.1之前 */ @Override public Object resolveArgument(MethodParameter parameter, NativeWebRequest request) throws Exception { if(!supportsParameter(parameter)) { return WebArgumentResolver.UNRESOLVED; } return resolveArgument(parameter, null, request, null); }
@Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavc, NativeWebRequest webRequest, WebDataBinderFactory wdbf) throws Exception { if (this.supportsParameter(methodParameter)) { Principal principal = webRequest.getUserPrincipal(); return (User) ((Authentication) principal).getPrincipal(); } else { return WebArgumentResolver.UNRESOLVED; } }
@Override public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (this.supportsParameter(methodParameter)) { return getCurrentUser(); } else { return WebArgumentResolver.UNRESOLVED; } }
public HandlerMethodInvoker(HandlerMethodResolver methodResolver, WebBindingInitializer bindingInitializer, SessionAttributeStore sessionAttributeStore, ParameterNameDiscoverer parameterNameDiscoverer, WebArgumentResolver[] customArgumentResolvers, HttpMessageConverter[] messageConverters) { this.methodResolver = methodResolver; this.bindingInitializer = bindingInitializer; this.sessionAttributeStore = sessionAttributeStore; this.parameterNameDiscoverer = parameterNameDiscoverer; this.customArgumentResolvers = customArgumentResolvers; this.messageConverters = messageConverters; }
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { if (this.supportsParameter(methodParameter)) { Principal principal = (Principal) webRequest.getUserPrincipal(); return (User) ((Authentication) principal).getPrincipal(); } else { return WebArgumentResolver.UNRESOLVED; } }
@Test public void it_should_not_resolved_arguments_with_unknown_class() throws Exception { Class klass = Integer.class; when(methodParameter.getParameterType()).thenReturn(klass); Object argument = readableUserAgentResolver.resolveArgument(methodParameter, nativeWebRequest); assertThat(argument) .isNotNull() .isSameAs(WebArgumentResolver.UNRESOLVED); verify(methodParameter).getParameterType(); verifyZeroInteractions(nativeWebRequest); }
@Test public void it_should_not_resolved_arguments_with_unknown_class() throws Exception { Class klass = Integer.class; when(methodParameter.getParameterType()).thenReturn(klass); Object argument = browserResolver.resolveArgument(methodParameter, nativeWebRequest); assertThat(argument) .isNotNull() .isSameAs(WebArgumentResolver.UNRESOLVED); verify(methodParameter).getParameterType(); verifyZeroInteractions(nativeWebRequest); }
public Object resolveArgument(MethodParameter methodParam, NativeWebRequest nativeWebRequest) throws Exception { DatatablesParams parameterAnnotation = methodParam.getParameterAnnotation(DatatablesParams.class); if (parameterAnnotation != null) { HttpServletRequest request = (HttpServletRequest) nativeWebRequest.getNativeRequest(); return DatatablesCriterias.getFromRequest(request); } return WebArgumentResolver.UNRESOLVED; }
/** * Create a new instance. */ public AbstractWebArgumentResolverAdapter(WebArgumentResolver adaptee) { Assert.notNull(adaptee, "'adaptee' must not be null"); this.adaptee = adaptee; }
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod, WebDataBinder binder, NativeWebRequest webRequest) throws Exception { Class<?>[] initBinderParams = initBinderMethod.getParameterTypes(); Object[] initBinderArgs = new Object[initBinderParams.length]; for (int i = 0; i < initBinderArgs.length; i++) { MethodParameter methodParam = new MethodParameter(initBinderMethod, i); methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer); GenericTypeResolver.resolveParameterType(methodParam, handler.getClass()); String paramName = null; boolean paramRequired = false; String paramDefaultValue = null; String pathVarName = null; Annotation[] paramAnns = methodParam.getParameterAnnotations(); for (Annotation paramAnn : paramAnns) { if (RequestParam.class.isInstance(paramAnn)) { RequestParam requestParam = (RequestParam) paramAnn; paramName = requestParam.value(); paramRequired = requestParam.required(); paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue()); break; } else if (ModelAttribute.class.isInstance(paramAnn)) { throw new IllegalStateException( "@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod); } else if (PathVariable.class.isInstance(paramAnn)) { PathVariable pathVar = (PathVariable) paramAnn; pathVarName = pathVar.value(); } } if (paramName == null && pathVarName == null) { Object argValue = resolveCommonArgument(methodParam, webRequest); if (argValue != WebArgumentResolver.UNRESOLVED) { initBinderArgs[i] = argValue; } else { Class<?> paramType = initBinderParams[i]; if (paramType.isInstance(binder)) { initBinderArgs[i] = binder; } else if (BeanUtils.isSimpleProperty(paramType)) { paramName = ""; } else { throw new IllegalStateException("Unsupported argument [" + paramType.getName() + "] for @InitBinder method: " + initBinderMethod); } } } if (paramName != null) { initBinderArgs[i] = resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null); } else if (pathVarName != null) { initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null); } } return initBinderArgs; }
protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest) throws Exception { if (WebRequest.class.isAssignableFrom(parameterType)) { return webRequest; } return WebArgumentResolver.UNRESOLVED; }
/** * Resolves standard method arguments. The default implementation handles {@link NativeWebRequest}, * {@link ServletRequest}, {@link ServletResponse}, {@link HttpSession}, {@link Principal}, * {@link Locale}, request {@link InputStream}, request {@link Reader}, response {@link OutputStream}, * response {@link Writer}, and the given {@code thrownException}. * @param parameterType the method parameter type * @param webRequest the request * @param thrownException the exception thrown * @return the argument value, or {@link WebArgumentResolver#UNRESOLVED} */ protected Object resolveStandardArgument(Class<?> parameterType, NativeWebRequest webRequest, Exception thrownException) throws Exception { if (parameterType.isInstance(thrownException)) { return thrownException; } else if (WebRequest.class.isAssignableFrom(parameterType)) { return webRequest; } HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); if (ServletRequest.class.isAssignableFrom(parameterType)) { return request; } else if (ServletResponse.class.isAssignableFrom(parameterType)) { return response; } else if (HttpSession.class.isAssignableFrom(parameterType)) { return request.getSession(); } else if (Principal.class.isAssignableFrom(parameterType)) { return request.getUserPrincipal(); } else if (Locale.class == parameterType) { return RequestContextUtils.getLocale(request); } else if (InputStream.class.isAssignableFrom(parameterType)) { return request.getInputStream(); } else if (Reader.class.isAssignableFrom(parameterType)) { return request.getReader(); } else if (OutputStream.class.isAssignableFrom(parameterType)) { return response.getOutputStream(); } else if (Writer.class.isAssignableFrom(parameterType)) { return response.getWriter(); } else { return WebArgumentResolver.UNRESOLVED; } }
public ServletWebArgumentResolverAdapter(WebArgumentResolver adaptee) { super(adaptee); }
private Object[] resolveInitBinderArguments(Object handler, Method initBinderMethod, WebDataBinder binder, NativeWebRequest webRequest) throws Exception { Class<?>[] initBinderParams = initBinderMethod.getParameterTypes(); Object[] initBinderArgs = new Object[initBinderParams.length]; for (int i = 0; i < initBinderArgs.length; i++) { MethodParameter methodParam = new SynthesizingMethodParameter(initBinderMethod, i); methodParam.initParameterNameDiscovery(this.parameterNameDiscoverer); GenericTypeResolver.resolveParameterType(methodParam, handler.getClass()); String paramName = null; boolean paramRequired = false; String paramDefaultValue = null; String pathVarName = null; Annotation[] paramAnns = methodParam.getParameterAnnotations(); for (Annotation paramAnn : paramAnns) { if (RequestParam.class.isInstance(paramAnn)) { RequestParam requestParam = (RequestParam) paramAnn; paramName = requestParam.name(); paramRequired = requestParam.required(); paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue()); break; } else if (ModelAttribute.class.isInstance(paramAnn)) { throw new IllegalStateException( "@ModelAttribute is not supported on @InitBinder methods: " + initBinderMethod); } else if (PathVariable.class.isInstance(paramAnn)) { PathVariable pathVar = (PathVariable) paramAnn; pathVarName = pathVar.value(); } } if (paramName == null && pathVarName == null) { Object argValue = resolveCommonArgument(methodParam, webRequest); if (argValue != WebArgumentResolver.UNRESOLVED) { initBinderArgs[i] = argValue; } else { Class<?> paramType = initBinderParams[i]; if (paramType.isInstance(binder)) { initBinderArgs[i] = binder; } else if (BeanUtils.isSimpleProperty(paramType)) { paramName = ""; } else { throw new IllegalStateException("Unsupported argument [" + paramType.getName() + "] for @InitBinder method: " + initBinderMethod); } } } if (paramName != null) { initBinderArgs[i] = resolveRequestParam(paramName, paramRequired, paramDefaultValue, methodParam, webRequest, null); } else if (pathVarName != null) { initBinderArgs[i] = resolvePathVariable(pathVarName, methodParam, webRequest, null); } } return initBinderArgs; }
@Test(expected = IllegalStateException.class) public void resolveArgumentUnresolved() throws Exception { given(adaptee.resolveArgument(parameter, webRequest)).willReturn(WebArgumentResolver.UNRESOLVED); adapter.resolveArgument(parameter, null, webRequest, null); }
public TestWebArgumentResolverAdapter(WebArgumentResolver adaptee) { super(adaptee); }