Search in sources :

Example 1 with HttpMediaTypeNotAcceptableException

use of org.springframework.web.HttpMediaTypeNotAcceptableException in project spring-security-oauth by spring-projects.

the class DefaultOAuth2ExceptionRenderer method writeWithMessageConverters.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void writeWithMessageConverters(Object returnValue, HttpInputMessage inputMessage, HttpOutputMessage outputMessage) throws IOException, HttpMediaTypeNotAcceptableException {
    List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }
    MediaType.sortByQualityValue(acceptedMediaTypes);
    Class<?> returnValueType = returnValue.getClass();
    List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
    for (MediaType acceptedMediaType : acceptedMediaTypes) {
        for (HttpMessageConverter messageConverter : messageConverters) {
            if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                messageConverter.write(returnValue, acceptedMediaType, outputMessage);
                if (logger.isDebugEnabled()) {
                    MediaType contentType = outputMessage.getHeaders().getContentType();
                    if (contentType == null) {
                        contentType = acceptedMediaType;
                    }
                    logger.debug("Written [" + returnValue + "] as \"" + contentType + "\" using [" + messageConverter + "]");
                }
                return;
            }
        }
    }
    for (HttpMessageConverter messageConverter : messageConverters) {
        allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
    }
    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}
Also used : HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) ArrayList(java.util.ArrayList) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType(org.springframework.http.MediaType)

Example 2 with HttpMediaTypeNotAcceptableException

use of org.springframework.web.HttpMediaTypeNotAcceptableException in project com.revolsys.open by revolsys.

the class WebAnnotationMethodHandlerAdapter method handleResponseBody.

private View handleResponseBody(final Object returnValue, final ServletWebRequest webRequest) throws ServletException, IOException {
    final HttpServletRequest request = webRequest.getRequest();
    String jsonp = request.getParameter("jsonp");
    if (jsonp == null) {
        jsonp = request.getParameter("callback");
    }
    request.setAttribute(IoConstants.JSONP_PROPERTY, jsonp);
    List<MediaType> acceptedMediaTypes = MediaTypeUtil.getAcceptedMediaTypes(request, WebAnnotationMethodHandlerAdapter.this.mediaTypes, WebAnnotationMethodHandlerAdapter.this.mediaTypeOrder, WebAnnotationMethodHandlerAdapter.this.urlPathHelper, WebAnnotationMethodHandlerAdapter.this.parameterName, WebAnnotationMethodHandlerAdapter.this.defaultMediaType);
    if (acceptedMediaTypes.isEmpty()) {
        acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
    }
    final Class<?> returnValueType = returnValue.getClass();
    final Set<MediaType> allSupportedMediaTypes = new LinkedHashSet<>();
    if (WebAnnotationMethodHandlerAdapter.this.messageConverters != null) {
        for (final MediaType acceptedMediaType : acceptedMediaTypes) {
            for (final HttpMessageConverter<?> messageConverter : WebAnnotationMethodHandlerAdapter.this.messageConverters) {
                allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
                if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
                    final MediaType mediaType = getMediaType(messageConverter.getSupportedMediaTypes(), acceptedMediaType);
                    return new HttpMessageConverterView(messageConverter, mediaType, returnValue);
                }
            }
        }
    }
    throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(allSupportedMediaTypes));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LinkedHashSet(java.util.LinkedHashSet) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) MediaType(org.springframework.http.MediaType)

Example 3 with HttpMediaTypeNotAcceptableException

use of org.springframework.web.HttpMediaTypeNotAcceptableException in project profile by craftercms.

the class ReturnCurrentAuthenticationProcessor method sendAuthentication.

protected <T> void sendAuthentication(Authentication auth, RequestContext context) throws IOException {
    HttpServletRequest request = context.getRequest();
    HttpServletResponse response = context.getResponse();
    if (auth != null) {
        response.setStatus(HttpServletResponse.SC_OK);
        try {
            responseWriter.writeWithMessageConverters(auth, request, response);
        } catch (HttpMediaTypeNotAcceptableException e) {
            logger.error(e.getMessage(), e);
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    } else {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 4 with HttpMediaTypeNotAcceptableException

use of org.springframework.web.HttpMediaTypeNotAcceptableException in project profile by craftercms.

the class AbstractRestHandlerBase method sendObject.

protected <T> void sendObject(int status, T responseBody, RequestContext context) throws IOException {
    HttpServletRequest request = context.getRequest();
    HttpServletResponse response = context.getResponse();
    response.setStatus(status);
    try {
        responseWriter.writeWithMessageConverters(responseBody, request, response);
    } catch (HttpMediaTypeNotAcceptableException e) {
        logger.error(e.getMessage(), e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 5 with HttpMediaTypeNotAcceptableException

use of org.springframework.web.HttpMediaTypeNotAcceptableException in project commons by craftercms.

the class HttpMessageConvertingResponseWriter method writeWithMessageConverters.

@SuppressWarnings("unchecked")
public <T> void writeWithMessageConverters(T returnValue, HttpServletRequest request, HttpServletResponse response) throws IOException, HttpMediaTypeNotAcceptableException {
    Class<?> returnValueClass = returnValue.getClass();
    List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(request);
    List<MediaType> producibleMediaTypes = getProducibleMediaTypes(returnValueClass);
    Set<MediaType> compatibleMediaTypes = new LinkedHashSet<MediaType>();
    for (MediaType r : requestedMediaTypes) {
        for (MediaType p : producibleMediaTypes) {
            if (r.isCompatibleWith(p)) {
                compatibleMediaTypes.add(getMostSpecificMediaType(r, p));
            }
        }
    }
    if (compatibleMediaTypes.isEmpty()) {
        throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes);
    }
    List<MediaType> mediaTypes = new ArrayList<MediaType>(compatibleMediaTypes);
    MediaType.sortBySpecificityAndQuality(mediaTypes);
    MediaType selectedMediaType = null;
    for (MediaType mediaType : mediaTypes) {
        if (mediaType.isConcrete()) {
            selectedMediaType = mediaType;
            break;
        } else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) {
            selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
            break;
        }
    }
    if (selectedMediaType != null) {
        selectedMediaType = selectedMediaType.removeQualityValue();
        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter.canWrite(returnValueClass, selectedMediaType)) {
                ((HttpMessageConverter<T>) messageConverter).write(returnValue, selectedMediaType, new ServletServerHttpResponse(response));
                logger.debug(LOG_KEY_WRITTEN_WITH_MESSAGE_CONVERTER, returnValue, selectedMediaType, messageConverter);
                return;
            }
        }
    }
    throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) ArrayList(java.util.ArrayList) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType(org.springframework.http.MediaType) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Aggregations

HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)15 MediaType (org.springframework.http.MediaType)10 ArrayList (java.util.ArrayList)4 LinkedHashSet (java.util.LinkedHashSet)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.jupiter.api.Test)2 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)2 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)2 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)2 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 ServletException (jakarta.servlet.ServletException)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1