|
| 1 | +/* |
| 2 | + * Copyright 2002-2012 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.servlet.mvc.condition; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | + |
| 25 | +import org.springframework.util.Assert; |
| 26 | +import org.springframework.util.ObjectUtils; |
| 27 | + |
| 28 | +import edu.emory.mathcs.backport.java.util.Collections; |
| 29 | + |
| 30 | +/** |
| 31 | + * Implements the {@link RequestCondition} contract by delegating to multiple |
| 32 | + * {@code RequestCondition} types and using a logical conjunction (' && ') to |
| 33 | + * ensure all conditions match a given request. |
| 34 | + * |
| 35 | + * <p>When {@code CompositeRequestCondition} instances are combined or compared |
| 36 | + * they are expected to (a) contain the same number of conditions and (b) that |
| 37 | + * conditions in the respective index are of the same type. It is acceptable to |
| 38 | + * provide {@code null} conditions or no conditions at all to the constructor. |
| 39 | + * |
| 40 | + * @author Rossen Stoyanchev |
| 41 | + * @since 3.2 |
| 42 | + */ |
| 43 | +public class CompositeRequestCondition extends AbstractRequestCondition<CompositeRequestCondition> { |
| 44 | + |
| 45 | + private final RequestConditionHolder[] requestConditions; |
| 46 | + |
| 47 | + /** |
| 48 | + * Create an instance with 0 or more {@code RequestCondition} types. It is |
| 49 | + * important to create {@code CompositeRequestCondition} instances with the |
| 50 | + * same number of conditions so they may be compared and combined. |
| 51 | + * It is acceptable to provide {@code null} conditions. |
| 52 | + */ |
| 53 | + public CompositeRequestCondition(RequestCondition<?>... requestConditions) { |
| 54 | + this.requestConditions = wrap(requestConditions); |
| 55 | + } |
| 56 | + |
| 57 | + private RequestConditionHolder[] wrap(RequestCondition<?>... rawConditions) { |
| 58 | + RequestConditionHolder[] wrappedConditions = new RequestConditionHolder[rawConditions.length]; |
| 59 | + for (int i = 0; i < rawConditions.length; i++) { |
| 60 | + wrappedConditions[i] = new RequestConditionHolder(rawConditions[i]); |
| 61 | + } |
| 62 | + return wrappedConditions; |
| 63 | + } |
| 64 | + |
| 65 | + private CompositeRequestCondition(RequestConditionHolder[] requestConditions) { |
| 66 | + this.requestConditions = requestConditions; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Whether this instance contains 0 conditions or not. |
| 71 | + */ |
| 72 | + public boolean isEmpty() { |
| 73 | + return ObjectUtils.isEmpty(this.requestConditions); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Return the underlying conditions, possibly empty but never {@code null}. |
| 78 | + */ |
| 79 | + public List<RequestCondition<?>> getConditions() { |
| 80 | + return unwrap(); |
| 81 | + } |
| 82 | + |
| 83 | + private List<RequestCondition<?>> unwrap() { |
| 84 | + List<RequestCondition<?>> result = new ArrayList<RequestCondition<?>>(); |
| 85 | + for (RequestConditionHolder holder : this.requestConditions) { |
| 86 | + result.add(holder.getCondition()); |
| 87 | + } |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected Collection<?> getContent() { |
| 93 | + return (isEmpty()) ? Collections.emptyList() : getConditions(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected String getToStringInfix() { |
| 98 | + return " && "; |
| 99 | + } |
| 100 | + |
| 101 | + private int getLength() { |
| 102 | + return this.requestConditions.length; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * If one instance is empty, return the other. |
| 107 | + * If both instances have conditions, combine the individual conditions |
| 108 | + * after ensuring they are of the same type and number. |
| 109 | + */ |
| 110 | + public CompositeRequestCondition combine(CompositeRequestCondition other) { |
| 111 | + if (isEmpty() && other.isEmpty()) { |
| 112 | + return this; |
| 113 | + } |
| 114 | + else if (other.isEmpty()) { |
| 115 | + return this; |
| 116 | + } |
| 117 | + else if (isEmpty()) { |
| 118 | + return other; |
| 119 | + } |
| 120 | + else { |
| 121 | + assertNumberOfConditions(other); |
| 122 | + RequestConditionHolder[] combinedConditions = new RequestConditionHolder[getLength()]; |
| 123 | + for (int i = 0; i < getLength(); i++) { |
| 124 | + combinedConditions[i] = this.requestConditions[i].combine(other.requestConditions[i]); |
| 125 | + } |
| 126 | + return new CompositeRequestCondition(combinedConditions); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void assertNumberOfConditions(CompositeRequestCondition other) { |
| 131 | + Assert.isTrue(getLength() == other.getLength(), |
| 132 | + "Cannot combine CompositeRequestConditions with a different number of conditions. " |
| 133 | + + this.requestConditions + " and " + other.requestConditions); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Delegate to <em>all</em> contained conditions to match the request and return the |
| 138 | + * resulting "matching" condition instances. |
| 139 | + * <p>An empty {@code CompositeRequestCondition} matches to all requests. |
| 140 | + */ |
| 141 | + public CompositeRequestCondition getMatchingCondition(HttpServletRequest request) { |
| 142 | + if (isEmpty()) { |
| 143 | + return this; |
| 144 | + } |
| 145 | + RequestConditionHolder[] matchingConditions = new RequestConditionHolder[getLength()]; |
| 146 | + for (int i = 0; i < getLength(); i++) { |
| 147 | + matchingConditions[i] = this.requestConditions[i].getMatchingCondition(request); |
| 148 | + if (matchingConditions[i] == null) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | + return new CompositeRequestCondition(matchingConditions); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * If one instance is empty, the other "wins". If both instances have |
| 157 | + * conditions, compare them in the order in which they were provided. |
| 158 | + */ |
| 159 | + public int compareTo(CompositeRequestCondition other, HttpServletRequest request) { |
| 160 | + if (isEmpty() && other.isEmpty()) { |
| 161 | + return 0; |
| 162 | + } |
| 163 | + else if (isEmpty()) { |
| 164 | + return 1; |
| 165 | + } |
| 166 | + else if (other.isEmpty()) { |
| 167 | + return -1; |
| 168 | + } |
| 169 | + else { |
| 170 | + assertNumberOfConditions(other); |
| 171 | + for (int i = 0; i < getLength(); i++) { |
| 172 | + int result = this.requestConditions[i].compareTo(other.requestConditions[i], request); |
| 173 | + if (result != 0) { |
| 174 | + return result; |
| 175 | + } |
| 176 | + } |
| 177 | + return 0; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments