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());
}
}
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;
}
}
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);
}
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;
}
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());
}
}
Aggregations