/** * Returns the media types for this condition excluding negated expressions. */ public Set<MediaType> getConsumableMediaTypes() { Set<MediaType> result = new LinkedHashSet<>(); for (ConsumeMediaTypeExpression expression : this.expressions) { if (!expression.isNegated()) { result.add(expression.getMediaType()); } } return result; }
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes, String[] headers) { Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(); if (headers != null) { for (String header : headers) { HeadersRequestCondition.HeaderExpression expr = new HeadersRequestCondition.HeaderExpression(header); if ("Content-Type".equalsIgnoreCase(expr.name)) { for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) { result.add(new ConsumeMediaTypeExpression(mediaType, expr.isNegated)); } } } } if (consumes != null) { for (String consume : consumes) { result.add(new ConsumeMediaTypeExpression(consume)); } } return result; }
/** * Returns: * <ul> * <li>0 if the two conditions have the same number of expressions * <li>Less than 0 if "this" has more or more specific media type expressions * <li>Greater than 0 if "other" has more or more specific media type expressions * </ul> * <p>It is assumed that both instances have been obtained via * {@link #getMatchingCondition(ServerWebExchange)} and each instance contains * the matching consumable media type expression only or is otherwise empty. */ @Override public int compareTo(ConsumesRequestCondition other, ServerWebExchange exchange) { if (this.expressions.isEmpty() && other.expressions.isEmpty()) { return 0; } else if (this.expressions.isEmpty()) { return 1; } else if (other.expressions.isEmpty()) { return -1; } else { return this.expressions.get(0).compareTo(other.expressions.get(0)); } }
/** * Returns: * <ul> * <li>0 if the two conditions have the same number of expressions * <li>Less than 0 if "this" has more or more specific media type expressions * <li>Greater than 0 if "other" has more or more specific media type expressions * </ul> * <p>It is assumed that both instances have been obtained via * {@link #getMatchingCondition(ServerWebExchange)} and each instance contains * the matching consumable media type expression only or is otherwise empty. */ @Override public int compareTo(ConsumesRequestCondition other, ServerWebExchange exchange) { if (this.expressions.isEmpty() && other.expressions.isEmpty()) { return 0; } else if (this.expressions.isEmpty()) { return 1; } else if (other.expressions.isEmpty()) { return -1; } else { return this.expressions.get(0).compareTo(other.expressions.get(0)); } }
/** * 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 exchange the current exchange * @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(ServerWebExchange exchange) { if (CorsUtils.isPreFlightRequest(exchange.getRequest())) { return PRE_FLIGHT_MATCH; } if (isEmpty()) { return this; } Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(expressions); for (Iterator<ConsumeMediaTypeExpression> iterator = result.iterator(); iterator.hasNext();) { ConsumeMediaTypeExpression expression = iterator.next(); if (!expression.match(exchange)) { iterator.remove(); } } return (result.isEmpty()) ? null : new ConsumesRequestCondition(result); }
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes, String[] headers) { Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(); if (headers != null) { for (String header : headers) { HeadersRequestCondition.HeaderExpression expr = new HeadersRequestCondition.HeaderExpression(header); if ("Content-Type".equalsIgnoreCase(expr.name)) { for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) { result.add(new ConsumeMediaTypeExpression(mediaType, expr.isNegated)); } } } } if (consumes != null) { for (String consume : consumes) { result.add(new ConsumeMediaTypeExpression(consume)); } } return result; }
@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()); } } }
/** * Returns the media types for this condition excluding negated expressions. */ public Set<MediaType> getConsumableMediaTypes() { Set<MediaType> result = new LinkedHashSet<>(); for (ConsumeMediaTypeExpression expression : this.expressions) { if (!expression.isNegated()) { result.add(expression.getMediaType()); } } return result; }
/** * 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 exchange the current exchange * @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(ServerWebExchange exchange) { if (CorsUtils.isPreFlightRequest(exchange.getRequest())) { return PRE_FLIGHT_MATCH; } if (isEmpty()) { return this; } Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>(this.expressions); result.removeIf(expression -> !expression.match(exchange)); return (!result.isEmpty() ? new ConsumesRequestCondition(result) : null); }
private void assertConditions(ConsumesRequestCondition condition, String... expected) { Collection<ConsumeMediaTypeExpression> expressions = condition.getContent(); assertEquals("Invalid amount of conditions", expressions.size(), expected.length); for (String s : expected) { boolean found = false; for (ConsumeMediaTypeExpression expr : expressions) { String conditionMediaType = expr.getMediaType().toString(); if (conditionMediaType.equals(s)) { found = true; break; } } if (!found) { fail("Condition [" + s + "] not found"); } } }
@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()); } } }