List of usage examples for org.springframework.web HttpMediaTypeNotAcceptableException HttpMediaTypeNotAcceptableException
public HttpMediaTypeNotAcceptableException(List<MediaType> supportedMediaTypes)
From source file:org.craftercms.commons.rest.HttpMessageConvertingResponseWriter.java
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)); }/* w ww. java2 s .c o m*/ } } 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); }
From source file:org.cloudfoundry.identity.uaa.error.ConvertingExceptionView.java
@SuppressWarnings("unchecked") 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); }//from ww w.j av a2 s . c o m MediaType.sortByQualityValue(acceptedMediaTypes); Class<?> returnValueType = returnValue.getClass(); List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); if (messageConverters != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (@SuppressWarnings("rawtypes") 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 + "]"); } // this.responseArgumentUsed = true; return; } } } for (@SuppressWarnings("rawtypes") HttpMessageConverter messageConverter : messageConverters) { allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); } } throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes); }
From source file:com.monarchapis.driver.spring.rest.ApiErrorResponseEntityExceptionHandlerTest.java
@Test public void testHttpMediaTypeNotAcceptableException() { List<MediaType> supportedMediaTypes = Lists.newArrayList(// MediaType.APPLICATION_XML, // MediaType.APPLICATION_JSON); performTest(// new HttpMediaTypeNotAcceptableException(supportedMediaTypes), // 400, // "mediaTypeNotAcceptable"); }
From source file:com.revolsys.ui.web.rest.interceptor.WebAnnotationMethodHandlerAdapter.java
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"); }/* w ww . j a va 2 s.com*/ 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)); }
From source file:org.cloudfoundry.identity.uaa.login.LoginInfoEndpoint.java
@RequestMapping(value = { "/login" }, headers = "Accept=text/html, */*")
public String loginForHtml(Model model, Principal principal, HttpServletRequest request,
@RequestHeader(value = "Accept", required = false) List<MediaType> headers)
throws HttpMediaTypeNotAcceptableException {
boolean match = headers == null
|| headers.stream().anyMatch(mediaType -> mediaType.isCompatibleWith(MediaType.TEXT_HTML));
if (!match) {
throw new HttpMediaTypeNotAcceptableException(request.getHeader(HttpHeaders.ACCEPT));
}/*from w w w. ja va 2s . c om*/
Cookie[] cookies = request.getCookies();
List<SavedAccountOptionModel> savedAccounts = getSavedAccounts(cookies, SavedAccountOptionModel.class);
savedAccounts.forEach(account -> {
Color color = ColorHash.getColor(account.getUserId());
account.assignColors(color);
});
model.addAttribute("savedAccounts", savedAccounts);
return login(model, principal, Collections.singletonList(PASSCODE), false, request);
}
From source file:org.infoscoop.api.oauth2.provider.ISOAuth2ExceptionRenderer.java
@SuppressWarnings({ "unchecked", "rawtypes" })
private void writeWithMessageConverters(Object returnValue, HttpInputMessage inputMessage,
HttpOutputMessage outputMessage, HttpStatus status, HttpServletRequest request)
throws IOException, HttpMediaTypeNotAcceptableException {
log.info(request.getRemoteAddr() + " " + returnValue + " " + status.value());
Class<?> returnValueType = returnValue.getClass();
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
String fileType = request.getPathInfo();
MediaType acceptedMediaType = MediaType.APPLICATION_JSON;
if (fileType.substring(fileType.lastIndexOf(".") + 1).equals("xml"))
acceptedMediaType = MediaType.APPLICATION_XML;
for (HttpMessageConverter messageConverter : messageConverters) {
if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
messageConverter.write(returnValue, acceptedMediaType, outputMessage);
if (log.isDebugEnabled()) {
MediaType contentType = outputMessage.getHeaders().getContentType();
if (contentType == null) {
contentType = acceptedMediaType;
}// ww w .java2 s .c om
log.debug("Written [" + returnValue + "] as \"" + contentType + "\" using [" + messageConverter
+ "]");
}
return;
}
}
for (HttpMessageConverter messageConverter : messageConverters) {
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
}
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
}
From source file:org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerAdapter.java
@SuppressWarnings("unchecked") private void handle(HttpServletRequest request, HttpServletResponse response, Endpoint<?> endpoint) throws Exception { Object result = endpoint.invoke(); Class<?> resultClass = result.getClass(); List<MediaType> mediaTypes = getMediaTypes(request, endpoint, resultClass); MediaType selectedMediaType = selectMediaType(mediaTypes); ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response); try {/* w w w. j a v a 2 s .c om*/ if (selectedMediaType != null) { selectedMediaType = selectedMediaType.removeQualityValue(); for (HttpMessageConverter<?> messageConverter : this.messageConverters) { if (messageConverter.canWrite(resultClass, selectedMediaType)) { ((HttpMessageConverter<Object>) messageConverter).write(result, selectedMediaType, outputMessage); if (this.logger.isDebugEnabled()) { this.logger.debug("Written [" + result + "] as \"" + selectedMediaType + "\" using [" + messageConverter + "]"); } return; } } } throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes); } finally { outputMessage.close(); } }
From source file:org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerAdapter.java
private List<MediaType> getMediaTypes(HttpServletRequest request, Endpoint<?> endpoint, Class<?> resultClass) throws HttpMediaTypeNotAcceptableException { List<MediaType> requested = getAcceptableMediaTypes(request); List<MediaType> producible = getProducibleMediaTypes(endpoint, resultClass); Set<MediaType> compatible = new LinkedHashSet<MediaType>(); for (MediaType r : requested) { for (MediaType p : producible) { if (r.isCompatibleWith(p)) { compatible.add(getMostSpecificMediaType(r, p)); }//from w ww .ja v a2s. c o m } } if (compatible.isEmpty()) { throw new HttpMediaTypeNotAcceptableException(producible); } List<MediaType> mediaTypes = new ArrayList<MediaType>(compatible); MediaType.sortBySpecificityAndQuality(mediaTypes); return mediaTypes; }
From source file:org.springframework.security.oauth2.provider.error.DefaultOAuth2ExceptionRenderer.java
@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);
}//from ww w .ja v a2 s. c o m
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);
}
From source file:org.springframework.web.accept.AbstractMappingContentNegotiationStrategy.java
/** * Override to provide handling when a key is not resolved via. * {@link #lookupMediaType}. Sub-classes can take further steps to * determine the media type(s). If a MediaType is returned from * this method it will be added to the cache in the base class. *///w w w. j a v a2 s . c om @Nullable protected MediaType handleNoMatch(NativeWebRequest request, String key) throws HttpMediaTypeNotAcceptableException { if (!isUseRegisteredExtensionsOnly()) { Optional<MediaType> mediaType = MediaTypeFactory.getMediaType("file." + key); if (mediaType.isPresent()) { return mediaType.get(); } } if (isIgnoreUnknownExtensions()) { return null; } throw new HttpMediaTypeNotAcceptableException(getAllMediaTypes()); }