Search in sources :

Example 6 with HttpMediaTypeNotAcceptableException

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

the class RequestMappingInfoHandlerMappingTests method testHttpMediaTypeNotAcceptableException.

private void testHttpMediaTypeNotAcceptableException(String url) throws Exception {
    try {
        MockHttpServletRequest request = new MockHttpServletRequest("GET", url);
        request.addHeader("Accept", "application/json");
        this.handlerMapping.getHandler(request);
        fail("HttpMediaTypeNotAcceptableException expected");
    } catch (HttpMediaTypeNotAcceptableException ex) {
        assertEquals("Invalid supported producible media types", Collections.singletonList(new MediaType("application", "xml")), ex.getSupportedMediaTypes());
    }
}
Also used : HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MediaType(org.springframework.http.MediaType)

Example 7 with HttpMediaTypeNotAcceptableException

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

the class ContentNegotiatingViewResolver method getMediaTypes.

/**
	 * Determines the list of {@link MediaType} for the given {@link HttpServletRequest}.
	 * @param request the current servlet request
	 * @return the list of media types requested, if any
	 */
protected List<MediaType> getMediaTypes(HttpServletRequest request) {
    try {
        ServletWebRequest webRequest = new ServletWebRequest(request);
        List<MediaType> acceptableMediaTypes = this.contentNegotiationManager.resolveMediaTypes(webRequest);
        acceptableMediaTypes = (!acceptableMediaTypes.isEmpty() ? acceptableMediaTypes : Collections.singletonList(MediaType.ALL));
        List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request);
        Set<MediaType> compatibleMediaTypes = new LinkedHashSet<>();
        for (MediaType acceptable : acceptableMediaTypes) {
            for (MediaType producible : producibleMediaTypes) {
                if (acceptable.isCompatibleWith(producible)) {
                    compatibleMediaTypes.add(getMostSpecificMediaType(acceptable, producible));
                }
            }
        }
        List<MediaType> selectedMediaTypes = new ArrayList<>(compatibleMediaTypes);
        MediaType.sortBySpecificityAndQuality(selectedMediaTypes);
        if (logger.isDebugEnabled()) {
            logger.debug("Requested media types are " + selectedMediaTypes + " based on Accept header types " + "and producible media types " + producibleMediaTypes + ")");
        }
        return selectedMediaTypes;
    } catch (HttpMediaTypeNotAcceptableException ex) {
        return null;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 8 with HttpMediaTypeNotAcceptableException

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

the class ResponseEntityExceptionHandlerTests method httpMediaTypeNotAcceptable.

@Test
public void httpMediaTypeNotAcceptable() {
    Exception ex = new HttpMediaTypeNotAcceptableException("");
    testException(ex);
}
Also used : HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) Test(org.junit.Test)

Example 9 with HttpMediaTypeNotAcceptableException

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

the class MediaTypeRequestMatcher method matches.

public boolean matches(HttpServletRequest request) {
    List<MediaType> httpRequestMediaTypes;
    try {
        httpRequestMediaTypes = this.contentNegotiationStrategy.resolveMediaTypes(new ServletWebRequest(request));
    } catch (HttpMediaTypeNotAcceptableException e) {
        this.logger.debug("Failed to parse MediaTypes, returning false", e);
        return false;
    }
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("httpRequestMediaTypes=" + httpRequestMediaTypes);
    }
    for (MediaType httpRequestMediaType : httpRequestMediaTypes) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Processing " + httpRequestMediaType);
        }
        if (shouldIgnore(httpRequestMediaType)) {
            this.logger.debug("Ignoring");
            continue;
        }
        if (this.useEquals) {
            boolean isEqualTo = this.matchingMediaTypes.contains(httpRequestMediaType);
            this.logger.debug("isEqualTo " + isEqualTo);
            return isEqualTo;
        }
        for (MediaType matchingMediaType : this.matchingMediaTypes) {
            boolean isCompatibleWith = matchingMediaType.isCompatibleWith(httpRequestMediaType);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug(matchingMediaType + " .isCompatibleWith " + httpRequestMediaType + " = " + isCompatibleWith);
            }
            if (isCompatibleWith) {
                return true;
            }
        }
    }
    this.logger.debug("Did not match any media types");
    return false;
}
Also used : HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) MediaType(org.springframework.http.MediaType) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 10 with HttpMediaTypeNotAcceptableException

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

the class HeaderContentNegotiationStrategy method resolveMediaTypes.

/**
	 * {@inheritDoc}
	 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed
	 */
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException {
    String[] headerValueArray = request.getHeaderValues(HttpHeaders.ACCEPT);
    if (headerValueArray == null) {
        return Collections.emptyList();
    }
    List<String> headerValues = Arrays.asList(headerValueArray);
    try {
        List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues);
        MediaType.sortBySpecificityAndQuality(mediaTypes);
        return mediaTypes;
    } catch (InvalidMediaTypeException ex) {
        throw new HttpMediaTypeNotAcceptableException("Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage());
    }
}
Also used : HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) MediaType(org.springframework.http.MediaType) InvalidMediaTypeException(org.springframework.http.InvalidMediaTypeException)

Aggregations

HttpMediaTypeNotAcceptableException (org.springframework.web.HttpMediaTypeNotAcceptableException)10 MediaType (org.springframework.http.MediaType)8 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 InvalidMediaTypeException (org.springframework.http.InvalidMediaTypeException)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 HttpMediaTypeNotSupportedException (org.springframework.web.HttpMediaTypeNotSupportedException)2 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 Type (java.lang.reflect.Type)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 ResolvableType (org.springframework.core.ResolvableType)1 GenericHttpMessageConverter (org.springframework.http.converter.GenericHttpMessageConverter)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)1 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)1 MediaTypeRequestMatcher (org.springframework.security.web.util.matcher.MediaTypeRequestMatcher)1