11package com .onelogin .saml2 .http ;
22
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 ;
64
7- import java .util .ArrayList ;
8- import java .util .Collections ;
5+ import java .util .Arrays ;
96import java .util .HashMap ;
107import java .util .List ;
118import java .util .Map ;
12- import java .util .Objects ;
139import java .util .regex .Matcher ;
1410import java .util .regex .Pattern ;
1511
16- import org .apache .commons .lang3 .StringUtils ;
17-
18- import com .onelogin .saml2 .util .Util ;
19-
2012/**
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.
2215 *
23- * @since 2 .0.0
16+ * @since 3 .0.0
2417 */
25- public final class HttpRequest {
18+ public interface HttpRequest {
2619
27- public static final Map < String , List < String >> EMPTY_PARAMETERS = Collections .< String , List < String >> emptyMap ();
20+ int getServerPort ();
2821
29- private final String requestURL ;
30- private final Map <String , List <String >> parameters ;
31- private final String queryString ;
22+ String getScheme ();
3223
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- }
24+ String getServerName ();
4425
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- }
26+ String getRequestURL ();
5427
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- }
28+ String getRequestURI ();
6729
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- }
81-
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" );
30+ String getQueryString ();
9131
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 );
97-
98- return new HttpRequest (requestURL , params , queryString );
99- }
32+ void invalidateSession ();
10033
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" );
34+ Map <String , String []> getParameterMap ();
10835
109- final Map <String , List <String >> params = new HashMap <>(parameters );
110- params .remove (name );
36+ default List <String > getParameters (String name ) {
37+ final Map <String , String []> paramsAsArray = getParameterMap ();
38+ final Map <String , List <String >> paramsAsList = new HashMap <>();
39+ for (Map .Entry <String , String []> param : paramsAsArray .entrySet ()) {
40+ paramsAsList .put (param .getKey (), Arrays .asList (param .getValue ()));
41+ }
11142
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 ;
43+ return paramsAsList .get (name );
12344 }
12445
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 ) {
46+ default String getParameter (String name ) {
13047 List <String > values = getParameters (name );
13148 return values .isEmpty () ? null : values .get (0 );
13249 }
13350
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 ();
141- }
142-
143- /**
144- * @return a map of all query parameters
145- */
146- public Map <String , List <String >> getParameters () {
147- return parameters ;
51+ default String getEncodedParameter (String name , String defaultValue ) {
52+ String value = getEncodedParameter (name );
53+ return (value != null ) ? value : Util .urlEncoder (defaultValue );
14854 }
14955
15056 /**
@@ -155,7 +61,8 @@ public Map<String, List<String>> getParameters() {
15561 * @param name
15662 * @return the first value for the parameter, or null
15763 */
158- public String getEncodedParameter (String name ) {
64+ default String getEncodedParameter (String name ) {
65+ String queryString = getQueryString ();
15966 Matcher matcher = Pattern .compile (Pattern .quote (name ) + "=([^&#]+)" ).matcher (queryString );
16067 if (matcher .find ()) {
16168 return matcher .group (1 );
@@ -164,58 +71,4 @@ public String getEncodedParameter(String name) {
16471 }
16572 }
16673
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-
220-
22174}
0 commit comments