1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.servlet.mvc.condition;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Set;
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.springframework.web.bind.annotation.RequestMethod;
28
29
30
31
32
33
34
35
36
37 public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
38
39 private final Set<RequestMethod> methods;
40
41
42
43
44
45
46
47 public RequestMethodsRequestCondition(RequestMethod... requestMethods) {
48 this(asList(requestMethods));
49 }
50
51 private RequestMethodsRequestCondition(Collection<RequestMethod> requestMethods) {
52 this.methods = Collections.unmodifiableSet(new LinkedHashSet<RequestMethod>(requestMethods));
53 }
54
55
56 private static List<RequestMethod> asList(RequestMethod... requestMethods) {
57 return (requestMethods != null ? Arrays.asList(requestMethods) : Collections.<RequestMethod>emptyList());
58 }
59
60
61
62
63
64 public Set<RequestMethod> getMethods() {
65 return this.methods;
66 }
67
68 @Override
69 protected Collection<RequestMethod> getContent() {
70 return this.methods;
71 }
72
73 @Override
74 protected String getToStringInfix() {
75 return " || ";
76 }
77
78
79
80
81
82 @Override
83 public RequestMethodsRequestCondition combine(RequestMethodsRequestCondition other) {
84 Set<RequestMethod> set = new LinkedHashSet<RequestMethod>(this.methods);
85 set.addAll(other.methods);
86 return new RequestMethodsRequestCondition(set);
87 }
88
89
90
91
92
93
94
95
96 @Override
97 public RequestMethodsRequestCondition getMatchingCondition(HttpServletRequest request) {
98 if (this.methods.isEmpty()) {
99 return this;
100 }
101 RequestMethod incomingRequestMethod = getRequestMethod(request);
102 if (incomingRequestMethod != null) {
103 for (RequestMethod method : this.methods) {
104 if (method.equals(incomingRequestMethod)) {
105 return new RequestMethodsRequestCondition(method);
106 }
107 }
108 }
109 return null;
110 }
111
112 private RequestMethod getRequestMethod(HttpServletRequest request) {
113 try {
114 return RequestMethod.valueOf(request.getMethod());
115 }
116 catch (IllegalArgumentException ex) {
117 return null;
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 @Override
133 public int compareTo(RequestMethodsRequestCondition other, HttpServletRequest request) {
134 return (other.methods.size() - this.methods.size());
135 }
136
137 }