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 ;
9- import java .util .HashMap ;
10- import java .util .List ;
11- import java .util .Map ;
12- import java .util .Objects ;
135import java .util .regex .Matcher ;
146import java .util .regex .Pattern ;
157
16- import org .apache .commons .lang3 .StringUtils ;
17-
18- import com .onelogin .saml2 .util .Util ;
19-
208/**
219 * Framework-agnostic representation of an HTTP request.
2210 *
2311 * @since 2.0.0
2412 */
25- public final class HttpRequest {
26-
27- public static final Map <String , List <String >> EMPTY_PARAMETERS = Collections .<String , List <String >>emptyMap ();
28-
29- private final String requestURL ;
30- private final Map <String , List <String >> parameters ;
31- private final String queryString ;
13+ public abstract class HttpRequest {
3214
3315 /**
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.
16+ * @return true if the request is using a secure scheme (HTTPS)
3917 */
40- @ Deprecated
41- public HttpRequest (String requestURL ) {
42- this (requestURL , EMPTY_PARAMETERS );
43- }
18+ public abstract boolean isSecure ();
4419
4520 /**
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
21+ * @return the name of the request protocol (HTTP / HTTPS)
5022 */
51- public HttpRequest (String requestURL , String queryString ) {
52- this (requestURL , EMPTY_PARAMETERS , queryString );
53- }
23+ public abstract String getScheme ();
5424
5525 /**
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.
26+ * @return the server name in the request e.g. www.example.com
6227 */
63- @ Deprecated
64- public HttpRequest (String requestURL , Map <String , List <String >> parameters ) {
65- this (requestURL , parameters , null );
66- }
28+ public abstract String getServerName ();
6729
6830 /**
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
31+ * @return the port over which the request is made e.g. 80 or 443
7532 */
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- }
33+ public abstract int getServerPort ();
8134
8235 /**
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
36+ * @return the query string part of the URL
8737 */
88- public HttpRequest addParameter (String name , String value ) {
89- checkNotNull (name , "name" );
90- checkNotNull (value , "value" );
91-
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- }
38+ public abstract String getQueryString ();
10039
10140 /**
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
41+ * @return the URI the client used to make the request - only includes
42+ * the server path, but not the query string parameters.
10543 */
106- public HttpRequest removeParameter (String name ) {
107- checkNotNull (name , "name" );
108-
109- final Map <String , List <String >> params = new HashMap <>(parameters );
110- params .remove (name );
44+ public abstract String getRequestURI ();
11145
112- return new HttpRequest (requestURL , params , queryString );
113- }
114-
11546 /**
11647 * The URL the client used to make the request. Includes a protocol, server name, port number, and server path, but
11748 * not the query string parameters.
11849 *
11950 * @return the request URL
12051 */
121- public String getRequestURL () {
122- return requestURL ;
123- }
52+ public abstract String getRequestURL ();
12453
12554 /**
12655 * @param name the query parameter name
12756 * @return the first value for the parameter, or null
12857 */
129- public String getParameter (String name ) {
130- List <String > values = getParameters (name );
131- return values .isEmpty () ? null : values .get (0 );
132- }
133-
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 ;
148- }
58+ public abstract String getParameter (String name );
14959
15060 /**
15161 * Return an url encoded get parameter value
@@ -155,8 +65,8 @@ public Map<String, List<String>> getParameters() {
15565 * @param name
15666 * @return the first value for the parameter, or null
15767 */
158- public String getEncodedParameter (String name ) {
159- Matcher matcher = Pattern .compile (Pattern .quote (name ) + "=([^&#]+)" ).matcher (queryString );
68+ public final String getEncodedParameter (String name ) {
69+ Matcher matcher = Pattern .compile (Pattern .quote (name ) + "=([^&#]+)" ).matcher (getQueryString () );
16070 if (matcher .find ()) {
16171 return matcher .group (1 );
16272 } else {
@@ -173,49 +83,13 @@ public String getEncodedParameter(String name) {
17383 * @param defaultValue
17484 * @return the first value for the parameter, or url encoded default value
17585 */
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 );
86+ public final String getEncodedParameter (String name , String defaultValue ) {
87+ String value = getEncodedParameter (name );
88+ return (value != null ? value : Util .urlEncoder (defaultValue ));
19589 }
19690
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-
91+ /**
92+ * Invalidate the current session
93+ */
94+ public abstract void invalidateSession ();
22195}
0 commit comments