001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.server; 018 019import java.util.Collections; 020import java.util.List; 021 022import org.springframework.core.ResolvableType; 023import org.springframework.http.HttpStatus; 024import org.springframework.http.MediaType; 025import org.springframework.lang.Nullable; 026 027/** 028 * Exception for errors that fit response status 415 (unsupported media type). 029 * 030 * @author Rossen Stoyanchev 031 * @since 5.0 032 */ 033@SuppressWarnings("serial") 034public class UnsupportedMediaTypeStatusException extends ResponseStatusException { 035 036 @Nullable 037 private final MediaType contentType; 038 039 private final List<MediaType> supportedMediaTypes; 040 041 @Nullable 042 private final ResolvableType bodyType; 043 044 045 /** 046 * Constructor for when the specified Content-Type is invalid. 047 */ 048 public UnsupportedMediaTypeStatusException(@Nullable String reason) { 049 super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, reason); 050 this.contentType = null; 051 this.supportedMediaTypes = Collections.emptyList(); 052 this.bodyType = null; 053 } 054 055 /** 056 * Constructor for when the Content-Type can be parsed but is not supported. 057 */ 058 public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes) { 059 this(contentType, supportedTypes, null); 060 } 061 062 /** 063 * Constructor for when trying to encode from or decode to a specific Java type. 064 * @since 5.1 065 */ 066 public UnsupportedMediaTypeStatusException(@Nullable MediaType contentType, List<MediaType> supportedTypes, 067 @Nullable ResolvableType bodyType) { 068 069 super(HttpStatus.UNSUPPORTED_MEDIA_TYPE, initReason(contentType, bodyType)); 070 this.contentType = contentType; 071 this.supportedMediaTypes = Collections.unmodifiableList(supportedTypes); 072 this.bodyType = bodyType; 073 } 074 075 private static String initReason(@Nullable MediaType contentType, @Nullable ResolvableType bodyType) { 076 return "Content type '" + (contentType != null ? contentType : "") + "' not supported" + 077 (bodyType != null ? " for bodyType=" + bodyType.toString() : ""); 078 } 079 080 081 /** 082 * Return the request Content-Type header if it was parsed successfully, 083 * or {@code null} otherwise. 084 */ 085 @Nullable 086 public MediaType getContentType() { 087 return this.contentType; 088 } 089 090 /** 091 * Return the list of supported content types in cases when the Content-Type 092 * header is parsed but not supported, or an empty list otherwise. 093 */ 094 public List<MediaType> getSupportedMediaTypes() { 095 return this.supportedMediaTypes; 096 } 097 098 /** 099 * Return the body type in the context of which this exception was generated. 100 * <p>This is applicable when the exception was raised as a result trying to 101 * encode from or decode to a specific Java type. 102 * @return the body type, or {@code null} if not available 103 * @since 5.1 104 */ 105 @Nullable 106 public ResolvableType getBodyType() { 107 return this.bodyType; 108 } 109 110}