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