View Javadoc
1   /*
2    * Copyright 2002-2014 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.Collection;
20  import java.util.Collections;
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.springframework.web.bind.annotation.RequestMapping;
26  import org.springframework.web.util.WebUtils;
27  
28  /**
29   * A logical conjunction (' && ') request condition that matches a request against
30   * a set parameter expressions with syntax defined in {@link RequestMapping#params()}.
31   *
32   * @author Arjen Poutsma
33   * @author Rossen Stoyanchev
34   * @since 3.1
35   */
36  public final class ParamsRequestCondition extends AbstractRequestCondition<ParamsRequestCondition> {
37  
38  	private final Set<ParamExpression> expressions;
39  
40  
41  	/**
42  	 * Create a new instance from the given param expressions.
43  	 * @param params expressions with syntax defined in {@link RequestMapping#params()};
44  	 * 	if 0, the condition will match to every request.
45  	 */
46  	public ParamsRequestCondition(String... params) {
47  		this(parseExpressions(params));
48  	}
49  
50  	private ParamsRequestCondition(Collection<ParamExpression> conditions) {
51  		this.expressions = Collections.unmodifiableSet(new LinkedHashSet<ParamExpression>(conditions));
52  	}
53  
54  
55  	private static Collection<ParamExpression> parseExpressions(String... params) {
56  		Set<ParamExpression> expressions = new LinkedHashSet<ParamExpression>();
57  		if (params != null) {
58  			for (String param : params) {
59  				expressions.add(new ParamExpression(param));
60  			}
61  		}
62  		return expressions;
63  	}
64  
65  
66  	/**
67  	 * Return the contained request parameter expressions.
68  	 */
69  	public Set<NameValueExpression<String>> getExpressions() {
70  		return new LinkedHashSet<NameValueExpression<String>>(this.expressions);
71  	}
72  
73  	@Override
74  	protected Collection<ParamExpression> getContent() {
75  		return this.expressions;
76  	}
77  
78  	@Override
79  	protected String getToStringInfix() {
80  		return " && ";
81  	}
82  
83  	/**
84  	 * Returns a new instance with the union of the param expressions
85  	 * from "this" and the "other" instance.
86  	 */
87  	@Override
88  	public ParamsRequestCondition combine(ParamsRequestCondition other) {
89  		Set<ParamExpression> set = new LinkedHashSet<ParamExpression>(this.expressions);
90  		set.addAll(other.expressions);
91  		return new ParamsRequestCondition(set);
92  	}
93  
94  	/**
95  	 * Returns "this" instance if the request matches all param expressions;
96  	 * or {@code null} otherwise.
97  	 */
98  	@Override
99  	public ParamsRequestCondition getMatchingCondition(HttpServletRequest request) {
100 		for (ParamExpression expression : expressions) {
101 			if (!expression.match(request)) {
102 				return null;
103 			}
104 		}
105 		return this;
106 	}
107 
108 	/**
109 	 * Returns:
110 	 * <ul>
111 	 * <li>0 if the two conditions have the same number of parameter expressions
112 	 * <li>Less than 0 if "this" instance has more parameter expressions
113 	 * <li>Greater than 0 if the "other" instance has more parameter expressions
114 	 * </ul>
115 	 * <p>It is assumed that both instances have been obtained via
116 	 * {@link #getMatchingCondition(HttpServletRequest)} and each instance
117 	 * contains the matching parameter expressions only or is otherwise empty.
118 	 */
119 	@Override
120 	public int compareTo(ParamsRequestCondition other, HttpServletRequest request) {
121 		return (other.expressions.size() - this.expressions.size());
122 	}
123 
124 
125 	/**
126 	 * Parses and matches a single param expression to a request.
127 	 */
128 	static class ParamExpression extends AbstractNameValueExpression<String> {
129 
130 		ParamExpression(String expression) {
131 			super(expression);
132 		}
133 
134 		@Override
135 		protected boolean isCaseSensitiveName() {
136 			return true;
137 		}
138 
139 		@Override
140 		protected String parseValue(String valueExpression) {
141 			return valueExpression;
142 		}
143 
144 		@Override
145 		protected boolean matchName(HttpServletRequest request) {
146 			return WebUtils.hasSubmitParameter(request, name);
147 		}
148 
149 		@Override
150 		protected boolean matchValue(HttpServletRequest request) {
151 			return value.equals(request.getParameter(name));
152 		}
153 	}
154 
155 }