1
1
package com .onelogin .saml2 .http ;
2
2
3
- import static com .onelogin .saml2 .util .Preconditions .checkNotNull ;
4
- import static java .util .Collections .unmodifiableList ;
5
- import static java .util .Collections .unmodifiableMap ;
3
+ import com .onelogin .saml2 .util .Util ;
6
4
7
- import java .util .ArrayList ;
8
- import java .util .Collections ;
5
+ import java .util .Arrays ;
9
6
import java .util .HashMap ;
10
7
import java .util .List ;
11
8
import java .util .Map ;
12
- import java .util .Objects ;
13
9
import java .util .regex .Matcher ;
14
10
import java .util .regex .Pattern ;
15
11
16
- import org .apache .commons .lang3 .StringUtils ;
17
-
18
- import com .onelogin .saml2 .util .Util ;
19
-
20
12
/**
21
- * Framework-agnostic representation of an HTTP request.
13
+ * Framework-agnostic definition of an HTTP request with a very minimal set of
14
+ * methods needed to support the SAML handshake.
22
15
*
23
- * @since 2 .0.0
16
+ * @since 3 .0.0
24
17
*/
25
- public final class HttpRequest {
26
-
27
- public static final Map <String , List <String >> EMPTY_PARAMETERS = Collections .<String , List <String >>emptyMap ();
18
+ public interface HttpRequest {
28
19
29
- private final String requestURL ;
30
- private final Map <String , List <String >> parameters ;
31
- private final String queryString ;
32
-
33
- /**
34
- * Creates a new HttpRequest.
35
- *
36
- * @param requestURL the request URL (up to but not including query parameters)
37
- * @throws NullPointerException if requestURL is null
38
- * @deprecated Not providing a queryString can cause HTTP Redirect binding to fail.
39
- */
40
- @ Deprecated
41
- public HttpRequest (String requestURL ) {
42
- this (requestURL , EMPTY_PARAMETERS );
43
- }
44
-
45
- /**
46
- * Creates a new HttpRequest.
47
- *
48
- * @param requestURL the request URL (up to but not including query parameters)
49
- * @param queryString string that is contained in the request URL after the path
50
- */
51
- public HttpRequest (String requestURL , String queryString ) {
52
- this (requestURL , EMPTY_PARAMETERS , queryString );
53
- }
54
-
55
- /**
56
- * Creates a new HttpRequest.
57
- *
58
- * @param requestURL the request URL (up to but not including query parameters)
59
- * @param parameters the request query parameters
60
- * @throws NullPointerException if any of the parameters is null
61
- * @deprecated Not providing a queryString can cause HTTP Redirect binding to fail.
62
- */
63
- @ Deprecated
64
- public HttpRequest (String requestURL , Map <String , List <String >> parameters ) {
65
- this (requestURL , parameters , null );
66
- }
20
+ int getServerPort ();
67
21
68
- /**
69
- * Creates a new HttpRequest.
70
- *
71
- * @param requestURL the request URL (up to but not including query parameters)
72
- * @param parameters the request query parameters
73
- * @param queryString string that is contained in the request URL after the path
74
- * @throws NullPointerException if any of the parameters is null
75
- */
76
- public HttpRequest (String requestURL , Map <String , List <String >> parameters , String queryString ) {
77
- this .requestURL = checkNotNull (requestURL , "requestURL" );
78
- this .parameters = unmodifiableCopyOf (checkNotNull (parameters , "queryParams" ));
79
- this .queryString = StringUtils .trimToEmpty (queryString );
80
- }
22
+ String getScheme ();
81
23
82
- /**
83
- * @param name the query parameter name
84
- * @param value the query parameter value
85
- * @return a new HttpRequest with the given query parameter added
86
- * @throws NullPointerException if any of the parameters is null
87
- */
88
- public HttpRequest addParameter (String name , String value ) {
89
- checkNotNull (name , "name" );
90
- checkNotNull (value , "value" );
24
+ String getServerName ();
91
25
92
- final List <String > oldValues = parameters .containsKey (name ) ? parameters .get (name ) : new ArrayList <String >();
93
- final List <String > newValues = new ArrayList <>(oldValues );
94
- newValues .add (value );
95
- final Map <String , List <String >> params = new HashMap <>(parameters );
96
- params .put (name , newValues );
26
+ String getRequestURL ();
97
27
98
- return new HttpRequest (requestURL , params , queryString );
99
- }
28
+ String getRequestURI ();
100
29
101
- /**
102
- * @param name the query parameter name
103
- * @return a new HttpRequest with the given query parameter removed
104
- * @throws NullPointerException if any of the parameters is null
105
- */
106
- public HttpRequest removeParameter (String name ) {
107
- checkNotNull (name , "name" );
30
+ String getQueryString ();
108
31
109
- final Map <String , List <String >> params = new HashMap <>(parameters );
110
- params .remove (name );
32
+ Map <String , String []> getParameterMap ();
111
33
112
- return new HttpRequest (requestURL , params , queryString );
113
- }
114
-
115
- /**
116
- * The URL the client used to make the request. Includes a protocol, server name, port number, and server path, but
117
- * not the query string parameters.
118
- *
119
- * @return the request URL
120
- */
121
- public String getRequestURL () {
122
- return requestURL ;
123
- }
124
-
125
- /**
126
- * @param name the query parameter name
127
- * @return the first value for the parameter, or null
128
- */
129
- public String getParameter (String name ) {
34
+ default String getParameter (String name ) {
130
35
List <String > values = getParameters (name );
131
36
return values .isEmpty () ? null : values .get (0 );
132
37
}
133
38
134
- /**
135
- * @param name the query parameter name
136
- * @return a List containing all values for the parameter
137
- */
138
- public List <String > getParameters (String name ) {
139
- List <String > values = parameters .get (name );
140
- return values != null ? values : Collections .<String >emptyList ();
39
+ default List <String > getParameters (String name ) {
40
+ final Map <String , String []> paramsAsArray = getParameterMap ();
41
+ final Map <String , List <String >> paramsAsList = new HashMap <>();
42
+ for (Map .Entry <String , String []> param : paramsAsArray .entrySet ()) {
43
+ paramsAsList .put (param .getKey (), Arrays .asList (param .getValue ()));
44
+ }
45
+
46
+ return paramsAsList .get (name );
141
47
}
142
48
143
- /**
144
- * @return a map of all query parameters
145
- */
146
- public Map <String , List <String >> getParameters () {
147
- return parameters ;
49
+ default String getEncodedParameter (String name , String defaultValue ) {
50
+ String value = getEncodedParameter (name );
51
+ return (value != null ? value : Util .urlEncoder (defaultValue ));
148
52
}
149
53
150
54
/**
@@ -155,7 +59,8 @@ public Map<String, List<String>> getParameters() {
155
59
* @param name
156
60
* @return the first value for the parameter, or null
157
61
*/
158
- public String getEncodedParameter (String name ) {
62
+ default String getEncodedParameter (String name ) {
63
+ String queryString = getQueryString ();
159
64
Matcher matcher = Pattern .compile (Pattern .quote (name ) + "=([^&#]+)" ).matcher (queryString );
160
65
if (matcher .find ()) {
161
66
return matcher .group (1 );
@@ -164,58 +69,6 @@ public String getEncodedParameter(String name) {
164
69
}
165
70
}
166
71
167
- /**
168
- * Return an url encoded get parameter value
169
- * Prefer to extract the original encoded value directly from queryString since url
170
- * encoding is not canonical.
171
- *
172
- * @param name
173
- * @param defaultValue
174
- * @return the first value for the parameter, or url encoded default value
175
- */
176
- public String getEncodedParameter (String name , String defaultValue ) {
177
- String value = getEncodedParameter (name );
178
- return (value != null ? value : Util .urlEncoder (defaultValue ));
179
- }
180
-
181
- @ Override
182
- public boolean equals (Object o ) {
183
- if (this == o ) {
184
- return true ;
185
- }
186
-
187
- if (o == null || getClass () != o .getClass ()) {
188
- return false ;
189
- }
190
-
191
- HttpRequest that = (HttpRequest ) o ;
192
- return Objects .equals (requestURL , that .requestURL ) &&
193
- Objects .equals (parameters , that .parameters ) &&
194
- Objects .equals (queryString , that .queryString );
195
- }
196
-
197
- @ Override
198
- public int hashCode () {
199
- return Objects .hash (requestURL , parameters , queryString );
200
- }
201
-
202
- @ Override
203
- public String toString () {
204
- return "HttpRequest{" +
205
- "requestURL='" + requestURL + '\'' +
206
- ", parameters=" + parameters +
207
- ", queryString=" + queryString +
208
- '}' ;
209
- }
210
-
211
- private static Map <String , List <String >> unmodifiableCopyOf (Map <String , List <String >> orig ) {
212
- Map <String , List <String >> copy = new HashMap <>();
213
- for (Map .Entry <String , List <String >> entry : orig .entrySet ()) {
214
- copy .put (entry .getKey (), unmodifiableList (new ArrayList <>(entry .getValue ())));
215
- }
216
-
217
- return unmodifiableMap (copy );
218
- }
219
-
72
+ void invalidateSession ();
220
73
221
74
}
0 commit comments