Search in sources :

Example 1 with HttpMediaTypeNotAcceptableException

use of cn.taketoday.web.HttpMediaTypeNotAcceptableException in project today-infrastructure by TAKETODAY.

the class ProducesRequestCondition method compareTo.

/**
 * Compares this and another "produces" condition as follows:
 * <ol>
 * <li>Sort 'Accept' header media types by quality value via
 * {@link cn.taketoday.util.MimeTypeUtils#sortBySpecificity(List)}
 * and iterate the list.
 * <li>Get the first index of matching media types in each "produces"
 * condition first matching with {@link MediaType#equals(Object)} and
 * then with {@link MediaType#includes(MediaType)}.
 * <li>If a lower index is found, the condition at that index wins.
 * <li>If both indexes are equal, the media types at the index are
 * compared further with {@link MediaType#isMoreSpecific(MimeType)}.
 * </ol>
 * <p>It is assumed that both instances have been obtained via
 * {@link #getMatchingCondition(RequestContext)} and each instance
 * contains the matching producible media type expression only or
 * is otherwise empty.
 */
@Override
public int compareTo(ProducesRequestCondition other, RequestContext request) {
    try {
        List<MediaType> acceptedMediaTypes = getAcceptedMediaTypes(request);
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            int thisIndex = this.indexOfEqualMediaType(acceptedMediaType);
            int otherIndex = other.indexOfEqualMediaType(acceptedMediaType);
            int result = compareMatchingMediaTypes(this, thisIndex, other, otherIndex);
            if (result != 0) {
                return result;
            }
            thisIndex = this.indexOfIncludedMediaType(acceptedMediaType);
            otherIndex = other.indexOfIncludedMediaType(acceptedMediaType);
            result = compareMatchingMediaTypes(this, thisIndex, other, otherIndex);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    } catch (HttpMediaTypeNotAcceptableException ex) {
        // should never happen
        throw new IllegalStateException("Cannot compare without having any requested media types", ex);
    }
}
Also used : HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) MediaType(cn.taketoday.http.MediaType)

Example 2 with HttpMediaTypeNotAcceptableException

use of cn.taketoday.web.HttpMediaTypeNotAcceptableException in project today-infrastructure by TAKETODAY.

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
 * Iterate all RequestMappingInfo's once again, look if any match by URL at
 * least and raise exceptions according to what doesn't match.
 *
 * @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> infos, String lookupPath, RequestContext request) {
    PartialMatchHelper helper = new PartialMatchHelper(infos, request);
    if (helper.isEmpty()) {
        return null;
    }
    if (helper.hasMethodsMismatch()) {
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS == request.getMethod()) {
            Set<MediaType> mediaTypes = helper.getConsumablePatchMediaTypes();
            HttpOptionsHandler handler = new HttpOptionsHandler(methods, mediaTypes);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new HttpRequestMethodNotSupportedException(request.getMethodValue(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        MediaType contentType = null;
        if (StringUtils.isNotEmpty(request.getContentType())) {
            try {
                contentType = MediaType.parseMediaType(request.getContentType());
            } catch (InvalidMediaTypeException ex) {
                throw new HttpMediaTypeNotSupportedException(ex.getMessage());
            }
        }
        throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes), request.getMethod());
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        List<String[]> conditions = helper.getParamConditions();
        throw new UnsatisfiedRequestParameterException(conditions, request.getParameters());
    }
    return null;
}
Also used : HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) HttpRequestMethodNotSupportedException(cn.taketoday.web.HttpRequestMethodNotSupportedException) UnsatisfiedRequestParameterException(cn.taketoday.web.bind.UnsatisfiedRequestParameterException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) MediaType(cn.taketoday.http.MediaType) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException)

Example 3 with HttpMediaTypeNotAcceptableException

use of cn.taketoday.web.HttpMediaTypeNotAcceptableException in project today-framework by TAKETODAY.

the class ResponseEntityExceptionHandlerTests method httpMediaTypeNotAcceptable.

@Test
public void httpMediaTypeNotAcceptable() {
    Exception ex = new HttpMediaTypeNotAcceptableException("");
    testException(ex);
}
Also used : HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) MissingRequestPartException(cn.taketoday.web.bind.resolver.MissingRequestPartException) MissingPathVariableException(cn.taketoday.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpRequestMethodNotSupportedException(cn.taketoday.web.HttpRequestMethodNotSupportedException) MissingRequestParameterException(cn.taketoday.web.bind.MissingRequestParameterException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) ConversionNotSupportedException(cn.taketoday.beans.ConversionNotSupportedException) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) BindException(cn.taketoday.validation.BindException) AsyncRequestTimeoutException(cn.taketoday.web.context.async.AsyncRequestTimeoutException) HttpMessageNotWritableException(cn.taketoday.http.converter.HttpMessageNotWritableException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMediaTypeNotAcceptableException

use of cn.taketoday.web.HttpMediaTypeNotAcceptableException in project today-framework by TAKETODAY.

the class ProducesRequestCondition method compareTo.

/**
 * Compares this and another "produces" condition as follows:
 * <ol>
 * <li>Sort 'Accept' header media types by quality value via
 * {@link cn.taketoday.util.MimeTypeUtils#sortBySpecificity(List)}
 * and iterate the list.
 * <li>Get the first index of matching media types in each "produces"
 * condition first matching with {@link MediaType#equals(Object)} and
 * then with {@link MediaType#includes(MediaType)}.
 * <li>If a lower index is found, the condition at that index wins.
 * <li>If both indexes are equal, the media types at the index are
 * compared further with {@link MediaType#isMoreSpecific(MimeType)}.
 * </ol>
 * <p>It is assumed that both instances have been obtained via
 * {@link #getMatchingCondition(RequestContext)} and each instance
 * contains the matching producible media type expression only or
 * is otherwise empty.
 */
@Override
public int compareTo(ProducesRequestCondition other, RequestContext request) {
    try {
        List<MediaType> acceptedMediaTypes = getAcceptedMediaTypes(request);
        for (MediaType acceptedMediaType : acceptedMediaTypes) {
            int thisIndex = this.indexOfEqualMediaType(acceptedMediaType);
            int otherIndex = other.indexOfEqualMediaType(acceptedMediaType);
            int result = compareMatchingMediaTypes(this, thisIndex, other, otherIndex);
            if (result != 0) {
                return result;
            }
            thisIndex = this.indexOfIncludedMediaType(acceptedMediaType);
            otherIndex = other.indexOfIncludedMediaType(acceptedMediaType);
            result = compareMatchingMediaTypes(this, thisIndex, other, otherIndex);
            if (result != 0) {
                return result;
            }
        }
        return 0;
    } catch (HttpMediaTypeNotAcceptableException ex) {
        // should never happen
        throw new IllegalStateException("Cannot compare without having any requested media types", ex);
    }
}
Also used : HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) MediaType(cn.taketoday.http.MediaType)

Example 5 with HttpMediaTypeNotAcceptableException

use of cn.taketoday.web.HttpMediaTypeNotAcceptableException in project today-framework by TAKETODAY.

the class RequestMappingInfoHandlerMapping method handleNoMatch.

/**
 * Iterate all RequestMappingInfo's once again, look if any match by URL at
 * least and raise exceptions according to what doesn't match.
 *
 * @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> infos, String lookupPath, RequestContext request) {
    PartialMatchHelper helper = new PartialMatchHelper(infos, request);
    if (helper.isEmpty()) {
        return null;
    }
    if (helper.hasMethodsMismatch()) {
        Set<String> methods = helper.getAllowedMethods();
        if (HttpMethod.OPTIONS == request.getMethod()) {
            Set<MediaType> mediaTypes = helper.getConsumablePatchMediaTypes();
            HttpOptionsHandler handler = new HttpOptionsHandler(methods, mediaTypes);
            return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
        }
        throw new HttpRequestMethodNotSupportedException(request.getMethodValue(), methods);
    }
    if (helper.hasConsumesMismatch()) {
        Set<MediaType> mediaTypes = helper.getConsumableMediaTypes();
        MediaType contentType = null;
        if (StringUtils.isNotEmpty(request.getContentType())) {
            try {
                contentType = MediaType.parseMediaType(request.getContentType());
            } catch (InvalidMediaTypeException ex) {
                throw new HttpMediaTypeNotSupportedException(ex.getMessage());
            }
        }
        throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<>(mediaTypes), request.getMethod());
    }
    if (helper.hasProducesMismatch()) {
        Set<MediaType> mediaTypes = helper.getProducibleMediaTypes();
        throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
    }
    if (helper.hasParamsMismatch()) {
        List<String[]> conditions = helper.getParamConditions();
        throw new UnsatisfiedRequestParameterException(conditions, request.getParameters());
    }
    return null;
}
Also used : HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) HttpRequestMethodNotSupportedException(cn.taketoday.web.HttpRequestMethodNotSupportedException) UnsatisfiedRequestParameterException(cn.taketoday.web.bind.UnsatisfiedRequestParameterException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) MediaType(cn.taketoday.http.MediaType) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException)

Aggregations

HttpMediaTypeNotAcceptableException (cn.taketoday.web.HttpMediaTypeNotAcceptableException)7 MediaType (cn.taketoday.http.MediaType)6 InvalidMediaTypeException (cn.taketoday.http.InvalidMediaTypeException)4 HttpMediaTypeNotSupportedException (cn.taketoday.web.HttpMediaTypeNotSupportedException)3 HttpRequestMethodNotSupportedException (cn.taketoday.web.HttpRequestMethodNotSupportedException)3 UnsatisfiedRequestParameterException (cn.taketoday.web.bind.UnsatisfiedRequestParameterException)2 ConversionNotSupportedException (cn.taketoday.beans.ConversionNotSupportedException)1 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)1 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)1 HttpMessageNotWritableException (cn.taketoday.http.converter.HttpMessageNotWritableException)1 BindException (cn.taketoday.validation.BindException)1 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)1 MissingPathVariableException (cn.taketoday.web.bind.MissingPathVariableException)1 MissingRequestParameterException (cn.taketoday.web.bind.MissingRequestParameterException)1 RequestBindingException (cn.taketoday.web.bind.RequestBindingException)1 MissingRequestPartException (cn.taketoday.web.bind.resolver.MissingRequestPartException)1 AsyncRequestTimeoutException (cn.taketoday.web.context.async.AsyncRequestTimeoutException)1 ServletException (jakarta.servlet.ServletException)1 Test (org.junit.jupiter.api.Test)1