@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class); MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class); ExcelRequestBody annotation = parameter.getParameterAnnotation(ExcelRequestBody.class); if (multipartRequest != null) { List<Object> result = new ArrayList<>(); List<MultipartFile> files = multipartRequest.getFiles(annotation.name()); for (MultipartFile file : files) { if (converters.supportsExcelType(annotation.type())) { List<?> part = converters.fromExcel(annotation, file.getInputStream()); result.addAll(part); } } return result; } return null; }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String nonceBase64 = getNonceOrThrow(webRequest); String nonceSignatureBase64 = getNonceSignatureOrThrow(webRequest); if (!isBase64(nonceBase64)) { throw new BadRequestException("Nonce must be base64"); } if (!isBase64(nonceSignatureBase64)) { throw new BadRequestException("Nonce signature must be base64"); } return NonceAuthenticationImpl.builder() .nonceBase64(nonceBase64) .nonceSignatureBase64(nonceSignatureBase64) .build(); }
@Override public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { ModelMap model = mavContainer.getModel(); if (model.size() > 0) { int lastIndex = model.size()-1; String lastKey = new ArrayList<String>(model.keySet()).get(lastIndex); if (lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return model.get(lastKey); } } throw new IllegalStateException( "An Errors/BindingResult argument is expected to be declared immediately after the model attribute, " + "the @RequestBody or the @RequestPart arguments to which they apply: " + parameter.getMethod()); }
/** * 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; }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class); Class<?> paramType = parameter.getParameterType(); if (paramType.equals(WebInput.class)) { return new WebInput(request); } else if (paramType.equals(WebOutput.class)) { return new WebOutput(response); } else if (paramType.equals(FileWebInput.class)) { return new FileWebInput(request); } return null; }
/** * Instantiate the model attribute from a URI template variable or from a * request parameter if the name matches to the model attribute name and * if there is an appropriate type conversion strategy. If none of these * are true delegate back to the base class. * @see #createAttributeFromRequestValue */ @Override protected final Object createAttribute(String attributeName, MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue( value, attributeName, methodParam, binderFactory, request); if (attribute != null) { return attribute; } } return super.createAttribute(attributeName, methodParam, binderFactory, request); }
/** * Create a model attribute from a String request value (e.g. URI template * variable, request parameter) using type conversion. * <p>The default implementation converts only if there a registered * {@link Converter} that can perform the conversion. * @param sourceValue the source value to create the model attribute from * @param attributeName the name of the attribute, never {@code null} * @param methodParam the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request * @return the created model attribute, or {@code null} * @throws Exception */ protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { DataBinder binder = binderFactory.createBinder(request, null, attributeName); ConversionService conversionService = binder.getConversionService(); if (conversionService != null) { TypeDescriptor source = TypeDescriptor.valueOf(String.class); TypeDescriptor target = new TypeDescriptor(methodParam); if (conversionService.canConvert(source, target)) { return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam); } } return null; }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws IOException, HttpMediaTypeNotSupportedException { ServletServerHttpRequest inputMessage = createInputMessage(webRequest); Type paramType = getHttpEntityType(parameter); Object body = readWithMessageConverters(webRequest, parameter, paramType); if (RequestEntity.class == parameter.getParameterType()) { return new RequestEntity<Object>(body, inputMessage.getHeaders(), inputMessage.getMethod(), inputMessage.getURI()); } else { return new HttpEntity<Object>(body, inputMessage.getHeaders()); } }
/** * Return a Map with all URI template variables or an empty map. */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { @SuppressWarnings("unchecked") Map<String, String> uriTemplateVars = (Map<String, String>) webRequest.getAttribute( HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); if (!CollectionUtils.isEmpty(uriTemplateVars)) { return new LinkedHashMap<String, String>(uriTemplateVars); } else { return Collections.emptyMap(); } }
/** * Throws MethodArgumentNotValidException if validation fails. * @throws HttpMessageNotReadableException if {@link RequestBody#required()} * is {@code true} and there is no body content or if there is no suitable * converter to read the content with. */ @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Object arg = readWithMessageConverters(webRequest, parameter, parameter.getGenericParameterType()); String name = Conventions.getVariableNameForParameter(parameter); WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name); if (arg != null) { validateIfApplicable(binder, parameter); if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) { throw new MethodArgumentNotValidException(parameter, binder.getBindingResult()); } } mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult()); return arg; }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { ModelMap model = mavContainer.getModel(); if (model.size() > 0) { int lastIndex = model.size()-1; String lastKey = new ArrayList<String>(model.keySet()).get(lastIndex); if (lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return model.get(lastKey); } } throw new IllegalStateException( "An Errors/BindingResult argument is expected to be declared immediately after the model attribute, " + "the @RequestBody or the @RequestPart arguments to which they apply: " + parameter.getMethod()); }
/** * Resolve the argument from the model or if not found instantiate it with * its default if it is available. The model attribute is then populated * with request values via data binding and optionally validated * if {@code @java.validation.Valid} is present on the argument. * @throws BindException if data binding and validation result in an error * and the next method parameter is not of type {@link Errors}. * @throws Exception if WebDataBinder initialization fails. */ @Override public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String name = ModelFactory.getNameForParameter(parameter); Object attribute = (mavContainer.containsAttribute(name) ? mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, webRequest)); WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name); if (binder.getTarget() != null) { bindRequestParameters(binder, webRequest); validateIfApplicable(binder, parameter); if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) { throw new BindException(binder.getBindingResult()); } } // Add resolved attribute and BindingResult at the end of the model Map<String, Object> bindingResultModel = binder.getBindingResult().getModel(); mavContainer.removeAttributes(bindingResultModel); mavContainer.addAllAttributes(bindingResultModel); return binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType(), parameter); }
@Test // SPR-9378 public void resolveArgumentOrdering() throws Exception { String name = "testBean"; Object testBean = new TestBean(name); mavContainer.addAttribute(name, testBean); mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, testBean); Object anotherTestBean = new TestBean(); mavContainer.addAttribute("anotherTestBean", anotherTestBean); StubRequestDataBinder dataBinder = new StubRequestDataBinder(testBean, name); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, testBean, name)).willReturn(dataBinder); processor.resolveArgument(paramModelAttr, mavContainer, webRequest, binderFactory); assertSame("Resolved attribute should be updated to be last in the order", testBean, mavContainer.getModel().values().toArray()[1]); assertSame("BindingResult of resolved attribute should be last in the order", dataBinder.getBindingResult(), mavContainer.getModel().values().toArray()[2]); }
@Test @SuppressWarnings("rawtypes") public void resolveOptional() throws Exception { ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultConversionService()); WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer); Object result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(Optional.empty(), result); this.request.addParameter("name", "123"); result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(123, ((Optional) result).get()); }
@Test public void updateModelBindingResult() throws Exception { String commandName = "attr1"; Object command = new Object(); ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(commandName, command); WebDataBinder dataBinder = new WebDataBinder(command, commandName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(command, container.getModel().get(commandName)); assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey(commandName))); assertEquals(2, container.getModel().size()); }
@Test public void updateModelSessionAttributesSaved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(attribute, container.getModel().get(attributeName)); assertEquals(attribute, this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Test public void updateModelSessionAttributesRemoved() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); // Store and resolve once (to be "remembered") this.sessionAttributeStore.storeAttribute(this.webRequest, attributeName, attribute); this.sessionAttrsHandler.isHandlerSessionAttribute(attributeName, null); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); container.getSessionStatus().setComplete(); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(attribute, container.getModel().get(attributeName)); assertNull(this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Test public void updateModelWhenRedirecting() throws Exception { String attributeName = "sessionAttr"; String attribute = "value"; ModelAndViewContainer container = new ModelAndViewContainer(); container.addAttribute(attributeName, attribute); String queryParam = "123"; String queryParamName = "q"; container.setRedirectModel(new ModelMap(queryParamName, queryParam)); container.setRedirectModelScenario(true); WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName); WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder); ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.sessionAttrsHandler); modelFactory.updateModel(this.webRequest, container); assertEquals(queryParam, container.getModel().get(queryParamName)); assertEquals(1, container.getModel().size()); assertEquals(attribute, this.sessionAttributeStore.retrieveAttribute(this.webRequest, attributeName)); }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { CommandMap commandMap = new CommandMap(); HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); Enumeration<?> enumeration = request.getParameterNames(); String key = null; String[] values = null; while(enumeration.hasMoreElements()){ key = (String) enumeration.nextElement(); values = request.getParameterValues(key); if(values != null){ commandMap.put(key, (values.length > 1) ? values:values[0] ); } } return commandMap; }
@Override protected final Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue(value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } return super.createAttribute(attributeName, parameter, binderFactory, request); }
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { DataBinder binder = binderFactory.createBinder(request, null, attributeName); ConversionService conversionService = binder.getConversionService(); if (conversionService != null) { TypeDescriptor source = TypeDescriptor.valueOf(String.class); TypeDescriptor target = new TypeDescriptor(parameter); if (conversionService.canConvert(source, target)) { return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter); } } return null; }
/** * Extension point to create the model attribute if not found in the model. * The default implementation uses the default constructor. * * @param attributeName the name of the attribute, never {@code null} * @param parameter the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request * @return the created model attribute, never {@code null} */ protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue(value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } Class<?> parameterType = parameter.getParameterType(); if (parameterType.isArray() || List.class.isAssignableFrom(parameterType)) { return ArrayList.class.newInstance(); } if (Set.class.isAssignableFrom(parameterType)) { return HashSet.class.newInstance(); } if (MapWapper.class.isAssignableFrom(parameterType)) { return MapWapper.class.newInstance(); } return BeanUtils.instantiateClass(parameter.getParameterType()); }
/** * Create a model attribute from a String request value (e.g. URI template * variable, request parameter) using type conversion. * <p>The default implementation converts only if there a registered * {@link org.springframework.core.convert.converter.Converter} that can perform the conversion. * * @param sourceValue the source value to create the model attribute from * @param attributeName the name of the attribute, never {@code null} * @param parameter the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request * @return the created model attribute, or {@code null} * @throws Exception */ protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { DataBinder binder = binderFactory.createBinder(request, null, attributeName); ConversionService conversionService = binder.getConversionService(); if (conversionService != null) { TypeDescriptor source = TypeDescriptor.valueOf(String.class); TypeDescriptor target = new TypeDescriptor(parameter); if (conversionService.canConvert(source, target)) { return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter); } } return null; }
/** * Extension point to create the model attribute if not found in the model. * The default implementation uses the default constructor. * @param attributeName the name of the attribute, never {@code null} * @param parameter the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request * @return the created model attribute, never {@code null} */ protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue(value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } Class<?> parameterType = parameter.getParameterType(); if(parameterType.isArray() || List.class.isAssignableFrom(parameterType)) { return ArrayList.class.newInstance(); } if(Set.class.isAssignableFrom(parameterType)) { return HashSet.class.newInstance(); } if(MapWapper.class.isAssignableFrom(parameterType)) { return MapWapper.class.newInstance(); } return BeanUtils.instantiateClass(parameter.getParameterType()); }
/** * Create a model attribute from a String request value (e.g. URI template * variable, request parameter) using type conversion. * <p>The default implementation converts only if there a registered * {@link Converter} that can perform the conversion. * @param sourceValue the source value to create the model attribute from * @param attributeName the name of the attribute, never {@code null} * @param parameter the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request * @return the created model attribute, or {@code null} * @throws Exception */ protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { DataBinder binder = binderFactory.createBinder(request, null, attributeName); ConversionService conversionService = binder.getConversionService(); if (conversionService != null) { TypeDescriptor source = TypeDescriptor.valueOf(String.class); TypeDescriptor target = new TypeDescriptor(parameter); if (conversionService.canConvert(source, target)) { return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter); } } return null; }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { CurrentUser currentUser = parameter.getParameterAnnotation(CurrentUser.class); boolean required = currentUser.required(); User user = userService.getCurrentUser(); if (user == null) { if (currentUser.defaultAdmin()) { user = userService.findSuperAdmin(); } } if (user == null) { Assert.isTrue(!required); return null; } return user; }
/** * Instantiate the model attribute from a URI template variable or from a * request parameter if the name matches to the model attribute name and if * there is an appropriate type conversion strategy. If none of these are * true delegate back to the base class. * * @see #createAttributeFromRequestValue */ @Override protected final Object createAttribute(String attributeName, MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue(value, attributeName, methodParam, binderFactory, request); if (attribute != null) { return attribute; } } else { Class<?> parameterType = methodParam.getParameterType(); if (ClassUtils.isAssignable(BaseEntity.class, parameterType)) { String id = request.getParameter("id"); if (!Strings.isNullOrEmpty(id)) { return conversionService.convert(id, methodParam.getParameterType()); } else { return entityService.getService(parameterType).newEntity(); } } } return super.createAttribute(attributeName, methodParam, binderFactory, request); }
@Override public GlobalSearch resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { String searchValue = webRequest.getParameter(getSearchValueParameter()); if (StringUtils.isEmpty(searchValue)) { return null; } String regexp = webRequest.getParameter(getRegexpParameter()); if ("true".equalsIgnoreCase(regexp)) { return new GlobalSearch(searchValue, true); } else if ("false".equalsIgnoreCase(regexp)) { return new GlobalSearch(searchValue, false); } return new GlobalSearch(searchValue); }
@Override public Object resolveArgument(final MethodParameter param, final ModelAndViewContainer mavContainer, final NativeWebRequest webRequest, final WebDataBinderFactory binderFactory) throws Exception { LOG.debug("Debugging PagedRequest Parsing:"); HttpServletRequest httpRequest = (HttpServletRequest) webRequest.getNativeRequest(); Map<String, Object> parameters = parametersRetriever.getParameters(webRequest); Object resource = resourceParameterResolver.getResource(param, parameters); Integer limit = limitParameterResolver.getLimit(param.getParameterAnnotation(Limit.class), param.getParameterType().getDeclaredField("limit"), httpRequest); Integer offset = offsetParameterResolver.getOffset(param.getParameterAnnotation(Offset.class), param.getParameterType().getDeclaredField("offset"), httpRequest); List<OrderBy> orders = orderByFieldResolver.getOrdersBy(httpRequest.getParameter(Constants.ORDER_BY), resource); PagedRequest<Object> pagedRequest = new PagedRequest<Object>(resource, offset, limit, orders); return pagedRequest; }
@SuppressWarnings("unchecked") @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { // JwtUserDetail jwtUserDetail = (JwtUserDetail)webRequest.getNativeRequest(HttpServletRequest.class).getAttribute(JwtUtils.AUTH_ATTR_KEY); Optional<JwtUserDetail> userOpt = JwtUtils.getOrSetJwtUserDetail(webRequest.getNativeRequest(HttpServletRequest.class), jwtTokenService, authHeaderName); // JwtUserDetail jwtUserDetail = userOpt.orElse(null); Class<? extends UserDetail> parameterType = (Class<? extends UserDetail>)parameter.getParameterType(); if(JwtUserDetail.class.isAssignableFrom(parameterType)){ return userOpt.orElse(null); }else if (parameterType.isInterface()){ throw new BaseException("the type of login user objet can not be defined as a interface!"); }else { if(!userOpt.isPresent()){ return null; } UserDetail userDetail = JwtUtils.createUserDetail(userOpt.get(), parameterType); return userDetail; } }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { SPARQLRequestParameters sparqlParams = parameter .getParameterAnnotation(SPARQLRequestParameters.class); if (sparqlParams != null) { HttpServletRequest request = (HttpServletRequest) webRequest .getNativeRequest(); @SuppressWarnings("unchecked") Map<String, String[]> paramMap = request.getParameterMap(); SPARQLParameters params = new SPARQLParameters(); params.setQuery(getStringValue(paramMap, "query")); params.setJSONPCallback(getStringValue(paramMap, "callback")); params.setPlainText(getBooleanValue(paramMap, "plaintext")); return params; } return null; }
@Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { PathVariablesMap ret = new PathVariablesMap(); Class<?> containingClass = parameter.getContainingClass(); HasCommonPathVariables annPlural = containingClass.getAnnotation(HasCommonPathVariables.class); HasCommonPathVariable annSingle = containingClass.getAnnotation(HasCommonPathVariable.class); if (annPlural == null && annSingle == null) { return ret; } if (annSingle != null) { processCommonPathVariable(parameter, annSingle, webRequest, binderFactory, ret); } if (annPlural != null && annPlural.value() != null) { for (HasCommonPathVariable ann : annPlural.value()) { processCommonPathVariable(parameter, ann, webRequest, binderFactory, ret); } } return ret; }
/** * Extension point to create the model attribute if not found in the model. The default implementation uses the * default constructor. * * @param attributeName the name of the attribute, never {@code null} * @param parameter the method parameter * @param binderFactory for creating WebDataBinder instance * @param request the current request * @return the created model attribute, never {@code null} */ protected Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue(value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } Class<?> parameterType = parameter.getParameterType(); if (parameterType.isArray() || List.class.isAssignableFrom(parameterType)) { return ArrayList.class.newInstance(); } if (Set.class.isAssignableFrom(parameterType)) { return HashSet.class.newInstance(); } if (MapWapper.class.isAssignableFrom(parameterType)) { return MapWapper.class.newInstance(); } return BeanUtils.instantiateClass(parameter.getParameterType()); }
@Override public ClientAppUserAccessToken resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { String tokenStr = Strings.trimToEmpty(webRequest.getHeader(HttpHeaders.AUTHORIZATION)); if (!tokenStr.startsWith("Bearer ")) { throw new MissingBearerTokenException(); } String token = tokenStr.substring("Bearer ".length(), tokenStr.length()).trim(); Optional<ClientAppUserAccessToken> accessToken = clientAppService.verifyAccessToken(token); if (!accessToken.isPresent()) { throw new InvalidTokenException(); } RequiredScope requiredScope = parameter.getMethodAnnotation(RequiredScope.class); if (!accessToken.get().containsScope(requiredScope.value())) { throw new InsufficientScopeException(requiredScope.value()); } return accessToken.get(); }
@Override public Object resolveArgument( MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory ) throws Exception { LOGGER.debug( "resolving argument of methodParameter: {}", methodParameter ); Integer page = null; if ( nativeWebRequest.getParameter( "page" ) != null ) { page = Integer.valueOf( nativeWebRequest.getParameter( "page" ) ); } Integer size = null; if ( nativeWebRequest.getParameter( "size" ) != null ) { size = Integer.valueOf( nativeWebRequest.getParameter( "size" ) ); } SimplePaginator pageRequest = null; if (size != null && page != null) { pageRequest = new SimplePaginator( page, size ); } LOGGER.debug( "argument resolved as {}", pageRequest ); return pageRequest; }
/** * Instantiate the model attribute from a URI template variable or from a * request parameter if the name matches to the model attribute name and * if there is an appropriate type conversion strategy. If none of these * are true delegate back to the base class. * @see #createAttributeFromRequestValue(String, String, MethodParameter, WebDataBinderFactory, NativeWebRequest) */ @Override protected final Object createAttribute(String attributeName, MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception { String value = getRequestValueForAttribute(attributeName, request); if (value != null) { Object attribute = createAttributeFromRequestValue(value, attributeName, parameter, binderFactory, request); if (attribute != null) { return attribute; } } return super.createAttribute(attributeName, parameter, binderFactory, request); }
/** * Return a Map with all URI template variables or an empty map. */ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { @SuppressWarnings("unchecked") Map<String, String> uriTemplateVars = (Map<String, String>) webRequest.getAttribute( HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); if (!CollectionUtils.isEmpty(uriTemplateVars)) { return new LinkedHashMap<String, String>(uriTemplateVars); } else { return Collections.emptyMap(); } }
/** * {@inheritDoc} * @throws MethodArgumentNotValidException if validation fails * @throws HttpMessageNotReadableException if {@link RequestBody#required()} * is {@code true} and there is no body content or if there is no suitable * converter to read the content with. */ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Object argument = readWithMessageConverters(webRequest, parameter, parameter.getGenericParameterType()); String name = Conventions.getVariableNameForParameter(parameter); WebDataBinder binder = binderFactory.createBinder(webRequest, argument, name); if (argument != null) { validate(binder, parameter); } mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult()); return argument; }
public Object resolveArgument( MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { ModelMap model = mavContainer.getModel(); if (model.size() > 0) { int lastIndex = model.size()-1; String lastKey = new ArrayList<String>(model.keySet()).get(lastIndex); if (lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) { return model.get(lastKey); } } throw new IllegalStateException( "An Errors/BindingResult argument is expected to be declared immediately after the model attribute, " + "the @RequestBody or the @RequestPart arguments to which they apply: " + parameter.getMethod()); }