org.springframework.lang.UsesJava8 Java Examples

The following examples show how to use org.springframework.lang.UsesJava8. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RequestPartMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
@UsesJava8
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {

	HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
	assertIsMultipartRequest(servletRequest);

	MultipartHttpServletRequest multipartRequest =
			WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);

	Class<?> paramType = parameter.getParameterType();
	boolean optional = paramType.getName().equals("java.util.Optional");
	if (optional) {
		parameter.increaseNestingLevel();
		paramType = parameter.getNestedParameterType();
	}

	String partName = getPartName(parameter);
	Object arg;

	if (MultipartFile.class == paramType) {
		Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
		arg = multipartRequest.getFile(partName);
	}
	else if (isMultipartFileCollection(parameter)) {
		Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
		arg = multipartRequest.getFiles(partName);
	}
	else if (isMultipartFileArray(parameter)) {
		Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
		List<MultipartFile> files = multipartRequest.getFiles(partName);
		arg = files.toArray(new MultipartFile[files.size()]);
	}
	else if ("javax.servlet.http.Part".equals(paramType.getName())) {
		assertIsMultipartRequest(servletRequest);
		arg = servletRequest.getPart(partName);
	}
	else if (isPartCollection(parameter)) {
		assertIsMultipartRequest(servletRequest);
		arg = new ArrayList<Object>(servletRequest.getParts());
	}
	else if (isPartArray(parameter)) {
		assertIsMultipartRequest(servletRequest);
		arg = RequestPartResolver.resolvePart(servletRequest);
	}
	else {
		try {
			HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, partName);
			arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
			WebDataBinder binder = binderFactory.createBinder(request, arg, partName);
			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 + partName, binder.getBindingResult());
		}
		catch (MissingServletRequestPartException ex) {
			// handled below
			arg = null;
		}
	}

	RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
	boolean isRequired = ((requestPart == null || requestPart.required()) && !optional);

	if (arg == null && isRequired) {
		throw new MissingServletRequestPartException(partName);
	}
	if (optional) {
		arg = Optional.ofNullable(arg);
	}

	return arg;
}