Java Code Examples for org.springframework.http.InvalidMediaTypeException#getMessage()

The following examples show how to use org.springframework.http.InvalidMediaTypeException#getMessage() . 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: HeaderContentNegotiationStrategy.java    From spring4-understanding with Apache License 2.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 header = request.getHeader(HttpHeaders.ACCEPT);
	if (!StringUtils.hasText(header)) {
		return Collections.emptyList();
	}
	try {
		List<MediaType> mediaTypes = MediaType.parseMediaTypes(header);
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return mediaTypes;
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotAcceptableException(
				"Could not parse 'Accept' header [" + header + "]: " + ex.getMessage());
	}
}
 
Example 5
Source File: HeaderContentTypeResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
	try {
		List<MediaType> mediaTypes = exchange.getRequest().getHeaders().getAccept();
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return (!CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST);
	}
	catch (InvalidMediaTypeException ex) {
		String value = exchange.getRequest().getHeaders().getFirst("Accept");
		throw new NotAcceptableStatusException(
				"Could not parse 'Accept' header [" + value + "]: " + ex.getMessage());
	}
}
 
Example 6
Source File: ConsumesRequestCondition.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected boolean matchMediaType(ServerWebExchange exchange) throws UnsupportedMediaTypeStatusException {
	try {
		MediaType contentType = exchange.getRequest().getHeaders().getContentType();
		contentType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
		return getMediaType().includes(contentType);
	}
	catch (InvalidMediaTypeException ex) {
		throw new UnsupportedMediaTypeStatusException("Can't parse Content-Type [" +
				exchange.getRequest().getHeaders().getFirst("Content-Type") +
				"]: " + ex.getMessage());
	}
}
 
Example 7
Source File: HeaderContentTypeResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException {
	try {
		List<MediaType> mediaTypes = exchange.getRequest().getHeaders().getAccept();
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return (!CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST);
	}
	catch (InvalidMediaTypeException ex) {
		String value = exchange.getRequest().getHeaders().getFirst("Accept");
		throw new NotAcceptableStatusException(
				"Could not parse 'Accept' header [" + value + "]: " + ex.getMessage());
	}
}
 
Example 8
Source File: ConsumesRequestCondition.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected boolean matchMediaType(ServerWebExchange exchange) throws UnsupportedMediaTypeStatusException {
	try {
		MediaType contentType = exchange.getRequest().getHeaders().getContentType();
		contentType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
		return getMediaType().includes(contentType);
	}
	catch (InvalidMediaTypeException ex) {
		throw new UnsupportedMediaTypeStatusException("Can't parse Content-Type [" +
				exchange.getRequest().getHeaders().getFirst("Content-Type") +
				"]: " + ex.getMessage());
	}
}
 
Example 9
Source File: ConsumesRequestCondition.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean matchMediaType(HttpServletRequest request) throws HttpMediaTypeNotSupportedException {
	try {
		MediaType contentType = StringUtils.hasLength(request.getContentType()) ?
				MediaType.parseMediaType(request.getContentType()) :
				MediaType.APPLICATION_OCTET_STREAM;
				return getMediaType().includes(contentType);
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotSupportedException(
				"Can't parse Content-Type [" + request.getContentType() + "]: " + ex.getMessage());
	}
}
 
Example 10
Source File: CustomClientCredentialsTokenEndpointFilter.java    From onetwo with Apache License 2.0 5 votes vote down vote up
protected MediaType getMediaType(ServletServerHttpRequest inputMessage){
	MediaType contentType;
	try {
		contentType = inputMessage.getHeaders().getContentType();
	}
	catch (InvalidMediaTypeException ex) {
		throw new BaseException(ex.getMessage());
	}
	if (contentType == null) {
		contentType = MediaType.APPLICATION_OCTET_STREAM;
	}
	return contentType;
}
 
Example 11
Source File: DefaultClientDetailFilterInterceptor.java    From onetwo with Apache License 2.0 5 votes vote down vote up
protected MediaType getMediaType(ServletServerHttpRequest inputMessage){
	MediaType contentType;
	try {
		contentType = inputMessage.getHeaders().getContentType();
	}
	catch (InvalidMediaTypeException ex) {
		throw new BaseException(ex.getMessage());
	}
	if (contentType == null) {
		contentType = MediaType.APPLICATION_OCTET_STREAM;
	}
	return contentType;
}
 
Example 12
Source File: RequestMappingInfoHandlerMapping.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Iterate all RequestMappingInfos once again, look if any match by URL at
 * least and raise exceptions accordingly.
 * @throws HttpRequestMethodNotSupportedException if there are matches by URL
 * but not by HTTP method
 * @throws HttpMediaTypeNotAcceptableException if there are matches by URL
 * but not by consumable/producible media types
 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> requestMappingInfos,
		String lookupPath, HttpServletRequest request) throws ServletException {

	Set<String> allowedMethods = new LinkedHashSet<String>(4);

	Set<RequestMappingInfo> patternMatches = new HashSet<RequestMappingInfo>();
	Set<RequestMappingInfo> patternAndMethodMatches = new HashSet<RequestMappingInfo>();

	for (RequestMappingInfo info : requestMappingInfos) {
		if (info.getPatternsCondition().getMatchingCondition(request) != null) {
			patternMatches.add(info);
			if (info.getMethodsCondition().getMatchingCondition(request) != null) {
				patternAndMethodMatches.add(info);
			}
			else {
				for (RequestMethod method : info.getMethodsCondition().getMethods()) {
					allowedMethods.add(method.name());
				}
			}
		}
	}

	if (patternMatches.isEmpty()) {
		return null;
	}
	else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), allowedMethods);
	}

	Set<MediaType> consumableMediaTypes;
	Set<MediaType> producibleMediaTypes;
	List<String[]> paramConditions;

	if (patternAndMethodMatches.isEmpty()) {
		consumableMediaTypes = getConsumableMediaTypes(request, patternMatches);
		producibleMediaTypes = getProducibleMediaTypes(request, patternMatches);
		paramConditions = getRequestParams(request, patternMatches);
	}
	else {
		consumableMediaTypes = getConsumableMediaTypes(request, patternAndMethodMatches);
		producibleMediaTypes = getProducibleMediaTypes(request, patternAndMethodMatches);
		paramConditions = getRequestParams(request, patternAndMethodMatches);
	}

	if (!consumableMediaTypes.isEmpty()) {
		MediaType contentType = null;
		if (StringUtils.hasLength(request.getContentType())) {
			try {
				contentType = MediaType.parseMediaType(request.getContentType());
			}
			catch (InvalidMediaTypeException ex) {
				throw new HttpMediaTypeNotSupportedException(ex.getMessage());
			}
		}
		throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<MediaType>(consumableMediaTypes));
	}
	else if (!producibleMediaTypes.isEmpty()) {
		throw new HttpMediaTypeNotAcceptableException(new ArrayList<MediaType>(producibleMediaTypes));
	}
	else if (!CollectionUtils.isEmpty(paramConditions)) {
		throw new UnsatisfiedServletRequestParameterException(paramConditions, request.getParameterMap());
	}
	else {
		return null;
	}
}