org.springframework.http.InvalidMediaTypeException Java Examples
The following examples show how to use
org.springframework.http.InvalidMediaTypeException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultServerRequestBuilder.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, Part>> initMultipartData(ServerHttpRequest request, List<HttpMessageReader<?>> readers) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, Part>>) readers.stream() .filter(reader -> reader.canRead(MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA)) .findFirst() .orElseThrow(() -> new IllegalStateException("No multipart HttpMessageReader."))) .readMono(MULTIPART_DATA_TYPE, request, Hints.none()) .switchIfEmpty(EMPTY_MULTIPART_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_MULTIPART_DATA; }
Example #2
Source File: HeaderContentNegotiationStrategy.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header * cannot be parsed. */ @Override public List<MediaType> resolveMediaTypes(NativeWebRequest request) throws HttpMediaTypeNotAcceptableException { String header = request.getHeader(HttpHeaders.ACCEPT); if (!StringUtils.hasText(header)) { return Collections.emptyList(); } try { List<MediaType> mediaTypes = MediaType.parseMediaTypes(header); MediaType.sortBySpecificityAndQuality(mediaTypes); return mediaTypes; } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotAcceptableException( "Could not parse 'Accept' header [" + header + "]: " + ex.getMessage()); } }
Example #3
Source File: ServerInterceptor.java From careconnect-reference-implementation with Apache License 2.0 | 6 votes |
public void checkContentType(String contentType) { try { MediaType media = MediaType.parseMediaType(contentType); // TODO improve the logic here if (media.getSubtype() != null && !media.getSubtype().contains("xml") && !media.getSubtype().contains("fhir") && !media.getSubtype().contains("json") && !media.getSubtype().contains("plain")) { log.debug("Unsupported media type: " + contentType); throw new InvalidRequestException("Unsupported media type: sub " + contentType); } else { if (!contentType.contains("xml") && !contentType.contains("json")) { log.debug("Unsupported media type: " + contentType); throw new InvalidRequestException("Unsupported media type: content " + contentType); } } } catch (InvalidMediaTypeException e) { log.debug("Unsupported media type: " + contentType); // KGM 26/02/2018 Don't throw error for xml and json if (!contentType.contains("xml") && !contentType.contains("json")) { log.debug("Unsupported media type: " + contentType); throw new InvalidRequestException("Unsupported media type: content " + contentType); } } }
Example #4
Source File: HeaderContentNegotiationStrategy.java From lams with GNU General Public License v2.0 | 6 votes |
/** * {@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.<MediaType>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()); } }
Example #5
Source File: HeaderContentNegotiationStrategy.java From java-technology-stack with MIT License | 6 votes |
/** * {@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()); } }
Example #6
Source File: DefaultServerWebExchange.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, Part>> initMultipartData(ServerHttpRequest request, ServerCodecConfigurer configurer, String logPrefix) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, Part>>) configurer.getReaders().stream() .filter(reader -> reader.canRead(MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA)) .findFirst() .orElseThrow(() -> new IllegalStateException("No multipart HttpMessageReader."))) .readMono(MULTIPART_DATA_TYPE, request, Hints.from(Hints.LOG_PREFIX_HINT, logPrefix)) .switchIfEmpty(EMPTY_MULTIPART_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_MULTIPART_DATA; }
Example #7
Source File: DefaultServerWebExchange.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request, ServerCodecConfigurer configurer, String logPrefix) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, String>>) configurer.getReaders().stream() .filter(reader -> reader.canRead(FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED)) .findFirst() .orElseThrow(() -> new IllegalStateException("No form data HttpMessageReader."))) .readMono(FORM_DATA_TYPE, request, Hints.from(Hints.LOG_PREFIX_HINT, logPrefix)) .switchIfEmpty(EMPTY_FORM_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_FORM_DATA; }
Example #8
Source File: ConsumesRequestCondition.java From java-technology-stack with MIT License | 6 votes |
/** * Checks if any of the contained media type expressions match the given * request 'Content-Type' header and returns an instance that is guaranteed * to contain matching expressions only. The match is performed via * {@link MediaType#includes(MediaType)}. * @param request the current request * @return the same instance if the condition contains no expressions; * or a new condition with matching expressions only; * or {@code null} if no expressions match */ @Override @Nullable public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request) { if (CorsUtils.isPreFlightRequest(request)) { return PRE_FLIGHT_MATCH; } if (isEmpty()) { return this; } MediaType contentType; try { contentType = (StringUtils.hasLength(request.getContentType()) ? MediaType.parseMediaType(request.getContentType()) : MediaType.APPLICATION_OCTET_STREAM); } catch (InvalidMediaTypeException ex) { return null; } Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(this.expressions); result.removeIf(expression -> !expression.match(contentType)); return (!result.isEmpty() ? new ConsumesRequestCondition(result) : null); }
Example #9
Source File: DefaultServerRequestBuilder.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, Part>> initMultipartData(ServerHttpRequest request, List<HttpMessageReader<?>> readers) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, Part>>) readers.stream() .filter(reader -> reader.canRead(MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA)) .findFirst() .orElseThrow(() -> new IllegalStateException("No multipart HttpMessageReader."))) .readMono(MULTIPART_DATA_TYPE, request, Hints.none()) .switchIfEmpty(EMPTY_MULTIPART_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_MULTIPART_DATA; }
Example #10
Source File: DefaultServerRequestBuilder.java From java-technology-stack with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request, List<HttpMessageReader<?>> readers) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, String>>) readers.stream() .filter(reader -> reader.canRead(FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED)) .findFirst() .orElseThrow(() -> new IllegalStateException("No form data HttpMessageReader."))) .readMono(FORM_DATA_TYPE, request, Hints.none()) .switchIfEmpty(EMPTY_FORM_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_FORM_DATA; }
Example #11
Source File: HeaderContentNegotiationStrategy.java From spring-analysis-note with MIT License | 6 votes |
/** * {@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()); } }
Example #12
Source File: DefaultServerWebExchange.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, Part>> initMultipartData(ServerHttpRequest request, ServerCodecConfigurer configurer, String logPrefix) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, Part>>) configurer.getReaders().stream() .filter(reader -> reader.canRead(MULTIPART_DATA_TYPE, MediaType.MULTIPART_FORM_DATA)) .findFirst() .orElseThrow(() -> new IllegalStateException("No multipart HttpMessageReader."))) .readMono(MULTIPART_DATA_TYPE, request, Hints.from(Hints.LOG_PREFIX_HINT, logPrefix)) .switchIfEmpty(EMPTY_MULTIPART_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_MULTIPART_DATA; }
Example #13
Source File: DefaultServerWebExchange.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request, ServerCodecConfigurer configurer, String logPrefix) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, String>>) configurer.getReaders().stream() .filter(reader -> reader.canRead(FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED)) .findFirst() .orElseThrow(() -> new IllegalStateException("No form data HttpMessageReader."))) .readMono(FORM_DATA_TYPE, request, Hints.from(Hints.LOG_PREFIX_HINT, logPrefix)) .switchIfEmpty(EMPTY_FORM_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_FORM_DATA; }
Example #14
Source File: DefaultServerRequestBuilder.java From spring-analysis-note with MIT License | 6 votes |
@SuppressWarnings("unchecked") private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpRequest request, List<HttpMessageReader<?>> readers) { try { MediaType contentType = request.getHeaders().getContentType(); if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(contentType)) { return ((HttpMessageReader<MultiValueMap<String, String>>) readers.stream() .filter(reader -> reader.canRead(FORM_DATA_TYPE, MediaType.APPLICATION_FORM_URLENCODED)) .findFirst() .orElseThrow(() -> new IllegalStateException("No form data HttpMessageReader."))) .readMono(FORM_DATA_TYPE, request, Hints.none()) .switchIfEmpty(EMPTY_FORM_DATA) .cache(); } } catch (InvalidMediaTypeException ex) { // Ignore } return EMPTY_FORM_DATA; }
Example #15
Source File: DefaultBlockRequestHandler.java From Sentinel with Apache License 2.0 | 5 votes |
/** * Reference from {@code DefaultErrorWebExceptionHandler} of Spring Boot. */ private boolean acceptsHtml(ServerWebExchange exchange) { try { List<MediaType> acceptedMediaTypes = exchange.getRequest().getHeaders().getAccept(); acceptedMediaTypes.remove(MediaType.ALL); MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); return acceptedMediaTypes.stream() .anyMatch(MediaType.TEXT_HTML::isCompatibleWith); } catch (InvalidMediaTypeException ex) { return false; } }
Example #16
Source File: ApiClient.java From tutorials with MIT License | 5 votes |
/** * Check if the given {@code String} is a JSON MIME. * @param mediaType the input MediaType * @return boolean true if the MediaType represents JSON, false otherwise */ public boolean isJsonMime(String mediaType) { // "* / *" is default to JSON if ("*/*".equals(mediaType)) { return true; } try { return isJsonMime(MediaType.parseMediaType(mediaType)); } catch (InvalidMediaTypeException e) { } return false; }
Example #17
Source File: ApiClient.java From tutorials with MIT License | 5 votes |
/** * Check if the given {@code String} is a JSON MIME. * @param mediaType the input MediaType * @return boolean true if the MediaType represents JSON, false otherwise */ public boolean isJsonMime(String mediaType) { try { return isJsonMime(MediaType.parseMediaType(mediaType)); } catch (InvalidMediaTypeException e) { } return false; }
Example #18
Source File: SyncopeSRAWebExceptionHandler.java From syncope with Apache License 2.0 | 5 votes |
private boolean acceptsTextHtml(final ServerHttpRequest request) { try { List<MediaType> acceptedMediaTypes = request.getHeaders().getAccept(); acceptedMediaTypes.remove(MediaType.ALL); MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); return acceptedMediaTypes.stream().anyMatch(MediaType.TEXT_HTML::isCompatibleWith); } catch (InvalidMediaTypeException e) { LOG.debug("Unexpected exception", e); return false; } }
Example #19
Source File: DefaultClientDetailFilterInterceptor.java From onetwo with Apache License 2.0 | 5 votes |
protected MediaType getMediaType(ServletServerHttpRequest inputMessage){ MediaType contentType; try { contentType = inputMessage.getHeaders().getContentType(); } catch (InvalidMediaTypeException ex) { throw new BaseException(ex.getMessage()); } if (contentType == null) { contentType = MediaType.APPLICATION_OCTET_STREAM; } return contentType; }
Example #20
Source File: CustomClientCredentialsTokenEndpointFilter.java From onetwo with Apache License 2.0 | 5 votes |
protected MediaType getMediaType(ServletServerHttpRequest inputMessage){ MediaType contentType; try { contentType = inputMessage.getHeaders().getContentType(); } catch (InvalidMediaTypeException ex) { throw new BaseException(ex.getMessage()); } if (contentType == null) { contentType = MediaType.APPLICATION_OCTET_STREAM; } return contentType; }
Example #21
Source File: ContextUtils.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Indicates whether the media type (content type) of the * given HTTP request is compatible with the given media type. * * @param response the HTTP response. * @param mediaType the media type. */ public static boolean isCompatibleWith( HttpServletResponse response, MediaType mediaType ) { try { String contentType = response.getContentType(); return contentType != null && MediaType.parseMediaType( contentType ).isCompatibleWith( mediaType ); } catch ( InvalidMediaTypeException ex ) { return false; } }
Example #22
Source File: ConsumesRequestCondition.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override protected boolean matchMediaType(HttpServletRequest request) throws HttpMediaTypeNotSupportedException { try { MediaType contentType = StringUtils.hasLength(request.getContentType()) ? MediaType.parseMediaType(request.getContentType()) : MediaType.APPLICATION_OCTET_STREAM; return getMediaType().includes(contentType); } catch (InvalidMediaTypeException ex) { throw new HttpMediaTypeNotSupportedException( "Can't parse Content-Type [" + request.getContentType() + "]: " + ex.getMessage()); } }
Example #23
Source File: HeaderContentTypeResolver.java From spring-analysis-note with MIT License | 5 votes |
@Override public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) throws NotAcceptableStatusException { try { List<MediaType> mediaTypes = exchange.getRequest().getHeaders().getAccept(); MediaType.sortBySpecificityAndQuality(mediaTypes); return (!CollectionUtils.isEmpty(mediaTypes) ? mediaTypes : MEDIA_TYPE_ALL_LIST); } catch (InvalidMediaTypeException ex) { String value = exchange.getRequest().getHeaders().getFirst("Accept"); throw new NotAcceptableStatusException( "Could not parse 'Accept' header [" + value + "]: " + ex.getMessage()); } }
Example #24
Source File: ConsumesRequestCondition.java From spring-analysis-note with MIT License | 5 votes |
@Override protected boolean matchMediaType(ServerWebExchange exchange) throws UnsupportedMediaTypeStatusException { try { MediaType contentType = exchange.getRequest().getHeaders().getContentType(); contentType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM); return getMediaType().includes(contentType); } catch (InvalidMediaTypeException ex) { throw new UnsupportedMediaTypeStatusException("Can't parse Content-Type [" + exchange.getRequest().getHeaders().getFirst("Content-Type") + "]: " + ex.getMessage()); } }
Example #25
Source File: ConsumesRequestCondition.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Checks if any of the contained media type expressions match the given * request 'Content-Type' header and returns an instance that is guaranteed * to contain matching expressions only. The match is performed via * {@link MediaType#includes(MediaType)}. * @param request the current request * @return the same instance if the condition contains no expressions; * or a new condition with matching expressions only; * or {@code null} if no expressions match. */ @Override public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request) { if (CorsUtils.isPreFlightRequest(request)) { return PRE_FLIGHT_MATCH; } if (isEmpty()) { return this; } MediaType contentType; try { contentType = StringUtils.hasLength(request.getContentType()) ? MediaType.parseMediaType(request.getContentType()) : MediaType.APPLICATION_OCTET_STREAM; } catch (InvalidMediaTypeException ex) { return null; } Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<ConsumeMediaTypeExpression>(this.expressions); for (Iterator<ConsumeMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) { ConsumeMediaTypeExpression expression = iterator.next(); if (!expression.match(contentType)) { iterator.remove(); } } return (result.isEmpty()) ? null : new ConsumesRequestCondition(result); }
Example #26
Source File: DefaultBlockRequestHandler.java From Sentinel with Apache License 2.0 | 5 votes |
/** * Reference from {@code DefaultErrorWebExceptionHandler} of Spring Boot. */ private boolean acceptsHtml(ServerWebExchange exchange) { try { List<MediaType> acceptedMediaTypes = exchange.getRequest().getHeaders().getAccept(); acceptedMediaTypes.remove(MediaType.ALL); MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); return acceptedMediaTypes.stream() .anyMatch(MediaType.TEXT_HTML::isCompatibleWith); } catch (InvalidMediaTypeException ex) { return false; } }
Example #27
Source File: DefaultBlockRequestHandler.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
/** * Reference from {@code DefaultErrorWebExceptionHandler} of Spring Boot. */ private boolean acceptsHtml(ServerWebExchange exchange) { try { List<MediaType> acceptedMediaTypes = exchange.getRequest().getHeaders().getAccept(); acceptedMediaTypes.remove(MediaType.ALL); MediaType.sortBySpecificityAndQuality(acceptedMediaTypes); return acceptedMediaTypes.stream() .anyMatch(MediaType.TEXT_HTML::isCompatibleWith); } catch (InvalidMediaTypeException ex) { return false; } }
Example #28
Source File: ApiClient.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * Check if the given {@code String} is a JSON MIME. * @param mediaType the input MediaType * @return boolean true if the MediaType represents JSON, false otherwise */ public boolean isJsonMime(String mediaType) { // "* / *" is default to JSON if ("*/*".equals(mediaType)) { return true; } try { return isJsonMime(MediaType.parseMediaType(mediaType)); } catch (InvalidMediaTypeException e) { } return false; }
Example #29
Source File: ApiClient.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * Check if the given {@code String} is a JSON MIME. * @param mediaType the input MediaType * @return boolean true if the MediaType represents JSON, false otherwise */ public boolean isJsonMime(String mediaType) { // "* / *" is default to JSON if ("*/*".equals(mediaType)) { return true; } try { return isJsonMime(MediaType.parseMediaType(mediaType)); } catch (InvalidMediaTypeException e) { } return false; }
Example #30
Source File: ApiClient.java From openapi-generator with Apache License 2.0 | 5 votes |
/** * Check if the given {@code String} is a JSON MIME. * @param mediaType the input MediaType * @return boolean true if the MediaType represents JSON, false otherwise */ public boolean isJsonMime(String mediaType) { // "* / *" is default to JSON if ("*/*".equals(mediaType)) { return true; } try { return isJsonMime(MediaType.parseMediaType(mediaType)); } catch (InvalidMediaTypeException e) { } return false; }