/** * 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. */ @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()); }
/** * {@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 MEDIA_TYPE_ALL_LIST; } List<String> headerValues = Arrays.asList(headerValueArray); try { List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues); MediaType.sortBySpecificityAndQuality(mediaTypes); return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST; } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotAcceptableException( "Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage()); } }
@Test public void httpMediaTypeNotAcceptable() { Exception ex = new HttpMediaTypeNotAcceptableException(""); testException(ex); }
/** * 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. */ @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()); }
@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)); } 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, Arrays.asList(PASSCODE, MFA_CODE), false, request); }
/** * {@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 MEDIA_TYPE_ALL_LIST; } List<String> headerValues = Arrays.asList(headerValueArray); try { List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues); MediaType.sortBySpecificityAndQuality(mediaTypes); return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST; } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotAcceptableException( "Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage()); } }
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
throw new HttpMediaTypeNotAcceptableException(producibleTypes); throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(mediaTypes));
throw new HttpMediaTypeNotAcceptableException(producibleTypes); throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
private List<MediaType> resolveMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException { Enumeration<String> headerValueArray = request.getHeaders(HttpHeaders.ACCEPT); if (headerValueArray == null) { return Collections.<MediaType>emptyList(); } List<String> headerValues = Collections.list(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()); } }
/** * 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. */ @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()); }
/** * 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. */ @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()); }
/** * {@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 MEDIA_TYPE_ALL_LIST; } List<String> headerValues = Arrays.asList(headerValueArray); try { List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues); MediaType.sortBySpecificityAndQuality(mediaTypes); return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST; } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotAcceptableException( "Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage()); } }
/** * {@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 MEDIA_TYPE_ALL_LIST; } List<String> headerValues = Arrays.asList(headerValueArray); try { List<MediaType> mediaTypes = MediaType.parseMediaTypes(headerValues); MediaType.sortBySpecificityAndQuality(mediaTypes); return !CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST; } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotAcceptableException( "Could not parse 'Accept' header " + headerValues + ": " + ex.getMessage()); } }
throw new HttpMediaTypeNotAcceptableException( new ArrayList<MediaType>(producibleMediaTypes)); } else if (!consumableMediaTypes.isEmpty()) {
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);