Java Code Examples for org.springframework.web.context.request.NativeWebRequest#getHeaderValues()

The following examples show how to use org.springframework.web.context.request.NativeWebRequest#getHeaderValues() . 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: HeaderContentNegotiationStrategy.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed
 */
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request)
		throws HttpMediaTypeNotAcceptableException {

	String[] headerValueArray = request.getHeaderValues(HttpHeaders.ACCEPT);
	if (headerValueArray == null) {
		return MEDIA_TYPE_ALL_LIST;
	}

	List<String> headerValues = Arrays.asList(headerValueArray);
	try {
		List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues);
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST;
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotAcceptableException(
				"Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage());
	}
}
 
Example 2
Source File: HeaderContentNegotiationStrategy.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed
 */
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request)
		throws HttpMediaTypeNotAcceptableException {

	String[] headerValueArray = request.getHeaderValues(HttpHeaders.ACCEPT);
	if (headerValueArray == null) {
		return MEDIA_TYPE_ALL_LIST;
	}

	List<String> headerValues = Arrays.asList(headerValueArray);
	try {
		List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues);
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST;
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotAcceptableException(
				"Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage());
	}
}
 
Example 3
Source File: HeaderContentNegotiationStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed
 */
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request)
		throws HttpMediaTypeNotAcceptableException {

	String[] headerValueArray = request.getHeaderValues(HttpHeaders.ACCEPT);
	if (headerValueArray == null) {
		return Collections.<MediaType>emptyList();
	}

	List<String> headerValues = Arrays.asList(headerValueArray);
	try {
		List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues);
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return mediaTypes;
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotAcceptableException(
				"Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage());
	}
}
 
Example 4
Source File: RequestHeaderMethodArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	String[] headerValues = request.getHeaderValues(name);
	if (headerValues != null) {
		return (headerValues.length == 1 ? headerValues[0] : headerValues);
	}
	else {
		return null;
	}
}
 
Example 5
Source File: RequestHeaderMethodArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	String[] headerValues = request.getHeaderValues(name);
	if (headerValues != null) {
		return (headerValues.length == 1 ? headerValues[0] : headerValues);
	}
	else {
		return null;
	}
}
 
Example 6
Source File: RequestHeaderMethodArgumentResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	String[] headerValues = request.getHeaderValues(name);
	if (headerValues != null) {
		return (headerValues.length == 1 ? headerValues[0] : headerValues);
	}
	else {
		return null;
	}
}
 
Example 7
Source File: HandlerMethodInvoker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Object resolveRequestHeader(String headerName, boolean required, String defaultValue,
		MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
		throws Exception {

	Class<?> paramType = methodParam.getParameterType();
	if (Map.class.isAssignableFrom(paramType)) {
		return resolveRequestHeaderMap((Class<? extends Map<?, ?>>) paramType, webRequest);
	}
	if (headerName.length() == 0) {
		headerName = getRequiredParameterName(methodParam);
	}
	Object headerValue = null;
	String[] headerValues = webRequest.getHeaderValues(headerName);
	if (headerValues != null) {
		headerValue = (headerValues.length == 1 ? headerValues[0] : headerValues);
	}
	if (headerValue == null) {
		if (defaultValue != null) {
			headerValue = resolveDefaultValue(defaultValue);
		}
		else if (required) {
			raiseMissingHeaderException(headerName, paramType);
		}
		headerValue = checkValue(headerName, headerValue, paramType);
	}
	WebDataBinder binder = createBinder(webRequest, null, headerName);
	initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
	return binder.convertIfNecessary(headerValue, paramType, methodParam);
}
 
Example 8
Source File: RequestHeaderMethodArgumentResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
	String[] headerValues = request.getHeaderValues(name);
	if (headerValues != null) {
		return (headerValues.length == 1 ? headerValues[0] : headerValues);
	}
	else {
		return null;
	}
}
 
Example 9
Source File: HandlerMethodInvoker.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Object resolveRequestHeader(String headerName, boolean required, String defaultValue,
		MethodParameter methodParam, NativeWebRequest webRequest, Object handlerForInitBinderCall)
		throws Exception {

	Class<?> paramType = methodParam.getParameterType();
	if (Map.class.isAssignableFrom(paramType)) {
		return resolveRequestHeaderMap((Class<? extends Map<?, ?>>) paramType, webRequest);
	}
	if (headerName.length() == 0) {
		headerName = getRequiredParameterName(methodParam);
	}
	Object headerValue = null;
	String[] headerValues = webRequest.getHeaderValues(headerName);
	if (headerValues != null) {
		headerValue = (headerValues.length == 1 ? headerValues[0] : headerValues);
	}
	if (headerValue == null) {
		if (defaultValue != null) {
			headerValue = resolveDefaultValue(defaultValue);
		}
		else if (required) {
			raiseMissingHeaderException(headerName, paramType);
		}
		headerValue = checkValue(headerName, headerValue, paramType);
	}
	WebDataBinder binder = createBinder(webRequest, null, headerName);
	initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
	return binder.convertIfNecessary(headerValue, paramType, methodParam);
}