Search in sources :

Example 1 with InvalidMediaTypeException

use of cn.taketoday.http.InvalidMediaTypeException in project today-infrastructure by TAKETODAY.

the class ConsumesRequestCondition method getMatchingCondition.

/**
 * Checks if any of the contained media type expressions match the given
 * request 'Content-Type' header and returns an instance that is guaranteed
 * to contain matching expressions only. The match is performed via
 * {@link MediaType#includes(MediaType)}.
 *
 * @param request the current request
 * @return the same instance if the condition contains no expressions;
 * or a new condition with matching expressions only;
 * or {@code null} if no expressions match
 */
@Override
@Nullable
public ConsumesRequestCondition getMatchingCondition(RequestContext request) {
    if (CorsUtils.isPreFlightRequest(request)) {
        return EMPTY_CONDITION;
    }
    if (isEmpty()) {
        return this;
    }
    if (!hasBody(request) && !this.bodyRequired) {
        return EMPTY_CONDITION;
    }
    // Common media types are cached at the level of MimeTypeUtils
    MediaType contentType;
    try {
        contentType = StringUtils.isNotEmpty(request.getContentType()) ? MediaType.parseMediaType(request.getContentType()) : MediaType.APPLICATION_OCTET_STREAM;
    } catch (InvalidMediaTypeException ex) {
        return null;
    }
    List<ConsumeMediaTypeExpression> result = getMatchingExpressions(contentType);
    return CollectionUtils.isNotEmpty(result) ? new ConsumesRequestCondition(result) : null;
}
Also used : MediaType(cn.taketoday.http.MediaType) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException) Nullable(cn.taketoday.lang.Nullable)

Example 2 with InvalidMediaTypeException

use of cn.taketoday.http.InvalidMediaTypeException 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 InvalidMediaTypeException

use of cn.taketoday.http.InvalidMediaTypeException in project today-framework by TAKETODAY.

the class ConsumesRequestCondition method getMatchingCondition.

/**
 * Checks if any of the contained media type expressions match the given
 * request 'Content-Type' header and returns an instance that is guaranteed
 * to contain matching expressions only. The match is performed via
 * {@link MediaType#includes(MediaType)}.
 *
 * @param request the current request
 * @return the same instance if the condition contains no expressions;
 * or a new condition with matching expressions only;
 * or {@code null} if no expressions match
 */
@Override
@Nullable
public ConsumesRequestCondition getMatchingCondition(RequestContext request) {
    if (request.isPreFlightRequest()) {
        return EMPTY_CONDITION;
    }
    if (isEmpty()) {
        return this;
    }
    if (!hasBody(request) && !this.bodyRequired) {
        return EMPTY_CONDITION;
    }
    // Common media types are cached at the level of MimeTypeUtils
    MediaType contentType;
    try {
        contentType = StringUtils.isNotEmpty(request.getContentType()) ? MediaType.parseMediaType(request.getContentType()) : MediaType.APPLICATION_OCTET_STREAM;
    } catch (InvalidMediaTypeException ex) {
        return null;
    }
    List<MediaTypeExpression> result = getMatchingExpressions(contentType);
    return result != null ? new ConsumesRequestCondition(result) : null;
}
Also used : MediaType(cn.taketoday.http.MediaType) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException) Nullable(cn.taketoday.lang.Nullable)

Example 4 with InvalidMediaTypeException

use of cn.taketoday.http.InvalidMediaTypeException 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)

Example 5 with InvalidMediaTypeException

use of cn.taketoday.http.InvalidMediaTypeException in project today-framework by TAKETODAY.

the class HeaderContentNegotiationStrategy method resolveMediaTypes.

/**
 * {@inheritDoc}
 *
 * @return
 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed
 */
@Override
public List<MediaType> resolveMediaTypes(RequestContext request) throws HttpMediaTypeNotAcceptableException {
    List<String> headerValues = request.requestHeaders().get(HttpHeaders.ACCEPT);
    if (headerValues == null) {
        return MEDIA_TYPE_ALL_LIST;
    }
    try {
        List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues);
        MimeTypeUtils.sortBySpecificity(mediaTypes);
        return CollectionUtils.isNotEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST;
    } catch (InvalidMediaTypeException ex) {
        throw new HttpMediaTypeNotAcceptableException("Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage());
    }
}
Also used : HttpMediaTypeNotAcceptableException(cn.taketoday.web.HttpMediaTypeNotAcceptableException) MediaType(cn.taketoday.http.MediaType) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException)

Aggregations

InvalidMediaTypeException (cn.taketoday.http.InvalidMediaTypeException)7 MediaType (cn.taketoday.http.MediaType)7 HttpMediaTypeNotAcceptableException (cn.taketoday.web.HttpMediaTypeNotAcceptableException)4 Nullable (cn.taketoday.lang.Nullable)3 HttpMediaTypeNotSupportedException (cn.taketoday.web.HttpMediaTypeNotSupportedException)3 HttpRequestMethodNotSupportedException (cn.taketoday.web.HttpRequestMethodNotSupportedException)2 UnsatisfiedRequestParameterException (cn.taketoday.web.bind.UnsatisfiedRequestParameterException)2 ResolvableType (cn.taketoday.core.ResolvableType)1 HttpInputMessage (cn.taketoday.http.HttpInputMessage)1 HttpMethod (cn.taketoday.http.HttpMethod)1 GenericHttpMessageConverter (cn.taketoday.http.converter.GenericHttpMessageConverter)1 HttpMessageConverter (cn.taketoday.http.converter.HttpMessageConverter)1 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)1 IOException (java.io.IOException)1