/** * If one instance is empty, return the other. * If both instances have conditions, combine the individual conditions * after ensuring they are of the same type and number. */ @Override public CompositeRequestCondition combine(CompositeRequestCondition other) { if (isEmpty() && other.isEmpty()) { return this; } else if (other.isEmpty()) { return this; } else if (isEmpty()) { return other; } else { assertNumberOfConditions(other); RequestConditionHolder[] combinedConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { combinedConditions[i] = this.requestConditions[i].combine(other.requestConditions[i]); } return new CompositeRequestCondition(combinedConditions); } }
@Override protected Collection<?> getContent() { return (!isEmpty() ? getConditions() : Collections.emptyList()); }
/** * Return the underlying conditions, possibly empty but never {@code null}. */ public List<RequestCondition<?>> getConditions() { return unwrap(); }
/** * Delegate to <em>all</em> contained conditions to match the request and return the * resulting "matching" condition instances. * <p>An empty {@code CompositeRequestCondition} matches to all requests. */ @Override public CompositeRequestCondition getMatchingCondition(ServerWebExchange exchange) { if (isEmpty()) { return this; } RequestConditionHolder[] matchingConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { matchingConditions[i] = this.requestConditions[i].getMatchingCondition(exchange); if (matchingConditions[i] == null) { return null; } } return new CompositeRequestCondition(matchingConditions); }
/** * If one instance is empty, the other "wins". If both instances have * conditions, compare them in the order in which they were provided. */ @Override public int compareTo(CompositeRequestCondition other, ServerWebExchange exchange) { if (isEmpty() && other.isEmpty()) { return 0; } else if (isEmpty()) { return 1; } else if (other.isEmpty()) { return -1; } else { assertNumberOfConditions(other); for (int i = 0; i < getLength(); i++) { int result = this.requestConditions[i].compareTo(other.requestConditions[i], exchange); if (result != 0) { return result; } } return 0; } }
@Test(expected = IllegalArgumentException.class) public void compareDifferentLength() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1); cond1.compareTo(cond2, MockServerWebExchange.from(MockServerHttpRequest.get("/"))); }
@Test public void matchEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); assertSame(empty, empty.getMatchingCondition(MockServerWebExchange.from(MockServerHttpRequest.get("/")))); }
@Test(expected = IllegalArgumentException.class) public void combineDifferentLength() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param1, this.header1); cond1.combine(cond2); }
/** * Create an instance with 0 or more {@code RequestCondition} types. It is * important to create {@code CompositeRequestCondition} instances with the * same number of conditions so they may be compared and combined. * It is acceptable to provide {@code null} conditions. */ public CompositeRequestCondition(RequestCondition<?>... requestConditions) { this.requestConditions = wrap(requestConditions); }
private void assertNumberOfConditions(CompositeRequestCondition other) { Assert.isTrue(getLength() == other.getLength(), "Cannot combine CompositeRequestConditions with a different number of conditions. " + ObjectUtils.nullSafeToString(this.requestConditions) + " and " + ObjectUtils.nullSafeToString(other.requestConditions)); }
/** * Delegate to <em>all</em> contained conditions to match the request and return the * resulting "matching" condition instances. * <p>An empty {@code CompositeRequestCondition} matches to all requests. */ @Override public CompositeRequestCondition getMatchingCondition(ServerWebExchange exchange) { if (isEmpty()) { return this; } RequestConditionHolder[] matchingConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { matchingConditions[i] = this.requestConditions[i].getMatchingCondition(exchange); if (matchingConditions[i] == null) { return null; } } return new CompositeRequestCondition(matchingConditions); }
/** * If one instance is empty, the other "wins". If both instances have * conditions, compare them in the order in which they were provided. */ @Override public int compareTo(CompositeRequestCondition other, ServerWebExchange exchange) { if (isEmpty() && other.isEmpty()) { return 0; } else if (isEmpty()) { return 1; } else if (other.isEmpty()) { return -1; } else { assertNumberOfConditions(other); for (int i = 0; i < getLength(); i++) { int result = this.requestConditions[i].compareTo(other.requestConditions[i], exchange); if (result != 0) { return result; } } return 0; } }
@Test public void compareEmpty() { CompositeRequestCondition empty = new CompositeRequestCondition(); CompositeRequestCondition notEmpty = new CompositeRequestCondition(this.param1); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); assertEquals(0, empty.compareTo(empty, exchange)); assertEquals(-1, notEmpty.compareTo(empty, exchange)); assertEquals(1, empty.compareTo(notEmpty, exchange)); }
@Test public void noMatch() { CompositeRequestCondition cond = new CompositeRequestCondition(this.param1); assertNull(cond.getMatchingCondition(MockServerWebExchange.from(MockServerHttpRequest.get("/")))); }
@Test public void combine() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1, this.header1); CompositeRequestCondition cond2 = new CompositeRequestCondition(this.param2, this.header2); CompositeRequestCondition cond3 = new CompositeRequestCondition(this.param3, this.header3); assertEquals(cond3, cond1.combine(cond2)); }
/** * Create an instance with 0 or more {@code RequestCondition} types. It is * important to create {@code CompositeRequestCondition} instances with the * same number of conditions so they may be compared and combined. * It is acceptable to provide {@code null} conditions. */ public CompositeRequestCondition(RequestCondition<?>... requestConditions) { this.requestConditions = wrap(requestConditions); }
private void assertNumberOfConditions(CompositeRequestCondition other) { Assert.isTrue(getLength() == other.getLength(), "Cannot combine CompositeRequestConditions with a different number of conditions. " + ObjectUtils.nullSafeToString(this.requestConditions) + " and " + ObjectUtils.nullSafeToString(other.requestConditions)); }
/** * If one instance is empty, return the other. * If both instances have conditions, combine the individual conditions * after ensuring they are of the same type and number. */ @Override public CompositeRequestCondition combine(CompositeRequestCondition other) { if (isEmpty() && other.isEmpty()) { return this; } else if (other.isEmpty()) { return this; } else if (isEmpty()) { return other; } else { assertNumberOfConditions(other); RequestConditionHolder[] combinedConditions = new RequestConditionHolder[getLength()]; for (int i = 0; i < getLength(); i++) { combinedConditions[i] = this.requestConditions[i].combine(other.requestConditions[i]); } return new CompositeRequestCondition(combinedConditions); } }
@Test public void compare() { CompositeRequestCondition cond1 = new CompositeRequestCondition(this.param1); CompositeRequestCondition cond3 = new CompositeRequestCondition(this.param3); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); assertEquals(1, cond1.compareTo(cond3, exchange)); assertEquals(-1, cond3.compareTo(cond1, exchange)); }
@Test public void match() { MockServerHttpRequest request = MockServerHttpRequest.get("/path?param1=paramValue1").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); RequestCondition<?> condition1 = new RequestMethodsRequestCondition(RequestMethod.GET, RequestMethod.POST); RequestCondition<?> condition2 = new RequestMethodsRequestCondition(RequestMethod.GET); CompositeRequestCondition composite1 = new CompositeRequestCondition(this.param1, condition1); CompositeRequestCondition composite2 = new CompositeRequestCondition(this.param1, condition2); assertEquals(composite2, composite1.getMatchingCondition(exchange)); }