Search in sources :

Example 1 with HttpMediaTypeNotSupportedException

use of cn.taketoday.web.HttpMediaTypeNotSupportedException 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 2 with HttpMediaTypeNotSupportedException

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

the class ResponseEntityExceptionHandlerTests method handleHttpMediaTypeNotSupported.

@Test
public void handleHttpMediaTypeNotSupported() {
    List<MediaType> acceptable = Arrays.asList(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML);
    Exception ex = new HttpMediaTypeNotSupportedException(MediaType.APPLICATION_JSON, acceptable);
    ResponseEntity<Object> responseEntity = testException(ex);
    assertThat(responseEntity.getHeaders().getAccept()).isEqualTo(acceptable);
    assertThat(responseEntity.getHeaders().getAcceptPatch()).isEmpty();
}
Also used : HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) MediaType(cn.taketoday.http.MediaType) 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 3 with HttpMediaTypeNotSupportedException

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

the class ResponseEntityExceptionHandlerTests method patchHttpMediaTypeNotSupported.

@Test
public void patchHttpMediaTypeNotSupported() {
    this.servletRequest = new MockHttpServletRequest("PATCH", "/");
    this.request = new ServletRequestContext(null, this.servletRequest, this.servletResponse);
    List<MediaType> acceptable = Arrays.asList(MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_XML);
    Exception ex = new HttpMediaTypeNotSupportedException(MediaType.APPLICATION_JSON, acceptable, HttpMethod.PATCH);
    ResponseEntity<Object> responseEntity = testException(ex);
    assertThat(responseEntity.getHeaders().getAccept()).isEqualTo(acceptable);
    assertThat(responseEntity.getHeaders().getAcceptPatch()).isEqualTo(acceptable);
}
Also used : HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MediaType(cn.taketoday.http.MediaType) 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 HttpMediaTypeNotSupportedException

use of cn.taketoday.web.HttpMediaTypeNotSupportedException 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 HttpMediaTypeNotSupportedException

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

the class AbstractMessageConverterParameterResolver method readWithMessageConverters.

/**
 * Create the method argument value of the expected parameter type by
 * reading from the given request.
 *
 * @param context the current request context
 * @param parameter the method parameter descriptor (may be {@code null})
 * @param targetType the target type, not necessarily the same as the method
 * parameter type, e.g. for {@code HttpEntity<String>}.
 * @return the created method argument value
 * @throws IOException if the reading from the request fails
 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
 */
@Nullable
@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(RequestContext context, MethodParameter parameter, Type targetType) throws // 
IOException, // 
HttpMediaTypeNotSupportedException, // 
HttpMessageNotReadableException {
    MediaType contentType;
    boolean noContentType = false;
    try {
        contentType = context.requestHeaders().getContentType();
    } catch (InvalidMediaTypeException ex) {
        throw new HttpMediaTypeNotSupportedException(ex.getMessage());
    }
    if (contentType == null) {
        noContentType = true;
        contentType = MediaType.APPLICATION_OCTET_STREAM;
    }
    Class<?> contextClass = parameter.getContainingClass();
    Class<T> targetClass = targetType instanceof Class ? (Class<T>) targetType : null;
    if (targetClass == null) {
        ResolvableType resolvableType = ResolvableType.forMethodParameter(parameter);
        targetClass = (Class<T>) resolvableType.resolve();
    }
    Object body = NO_VALUE;
    EmptyBodyCheckingHttpInputMessage message = null;
    try {
        message = new EmptyBodyCheckingHttpInputMessage(context);
        RequestResponseBodyAdviceChain adviceChain = getAdvice();
        for (HttpMessageConverter<?> converter : this.messageConverters) {
            Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
            GenericHttpMessageConverter<?> genericConverter = converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter<?>) converter : null;
            if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) : targetClass != null && converter.canRead(targetClass, contentType)) {
                if (message.hasBody()) {
                    HttpInputMessage msgToUse = adviceChain.beforeBodyRead(message, parameter, targetType, converterType);
                    body = genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse);
                    body = adviceChain.afterBodyRead(body, msgToUse, parameter, targetType, converterType);
                } else {
                    body = adviceChain.handleEmptyBody(null, message, parameter, targetType, converterType);
                }
                break;
            }
        }
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("I/O error while reading input message", ex, context);
    } finally {
        if (message != null && message.hasBody()) {
            closeStreamIfNecessary(message.getBody());
        }
    }
    if (body == NO_VALUE) {
        HttpMethod httpMethod = context.getMethod();
        if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) || (noContentType && !message.hasBody())) {
            return null;
        }
        throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(targetClass != null ? targetClass : Object.class));
    }
    MediaType selectedContentType = contentType;
    Object theBody = body;
    LogFormatUtils.traceDebug(log, traceOn -> {
        String formatted = LogFormatUtils.formatValue(theBody, !traceOn);
        return "Read \"" + selectedContentType + "\" to [" + formatted + "]";
    });
    return body;
}
Also used : HttpInputMessage(cn.taketoday.http.HttpInputMessage) IOException(java.io.IOException) HttpMediaTypeNotSupportedException(cn.taketoday.web.HttpMediaTypeNotSupportedException) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) HttpMessageConverter(cn.taketoday.http.converter.HttpMessageConverter) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) MediaType(cn.taketoday.http.MediaType) ResolvableType(cn.taketoday.core.ResolvableType) HttpMethod(cn.taketoday.http.HttpMethod) InvalidMediaTypeException(cn.taketoday.http.InvalidMediaTypeException) GenericHttpMessageConverter(cn.taketoday.http.converter.GenericHttpMessageConverter) Nullable(cn.taketoday.lang.Nullable)

Aggregations

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