|
| 1 | +package com.onelogin.saml2.http; |
| 2 | + |
| 3 | +import static com.onelogin.saml2.util.Preconditions.checkNotNull; |
| 4 | +import static java.util.Collections.unmodifiableList; |
| 5 | +import static java.util.Collections.unmodifiableMap; |
| 6 | + |
| 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; |
| 13 | + |
| 14 | +/** |
| 15 | + * Framework-agnostic representation of an HTTP request. |
| 16 | + * |
| 17 | + * @since 2.0.0 |
| 18 | + */ |
| 19 | +public final class HttpRequest { |
| 20 | + private final String requestURL; |
| 21 | + private final Map<String, List<String>> parameters; |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates a new HttpRequest. |
| 25 | + * |
| 26 | + * @param requestURL the request URL (up to but not including query parameters) |
| 27 | + * @throws NullPointerException if requestURL is null |
| 28 | + */ |
| 29 | + public HttpRequest(String requestURL) { |
| 30 | + this(requestURL, Collections.<String, List<String>>emptyMap()); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new HttpRequest. |
| 35 | + * |
| 36 | + * @param requestURL the request URL (up to but not including query parameters) |
| 37 | + * @param parameters the request query parameters |
| 38 | + * @throws NullPointerException if any of the parameters is null |
| 39 | + */ |
| 40 | + public HttpRequest(String requestURL, Map<String, List<String>> parameters) { |
| 41 | + this.requestURL = checkNotNull(requestURL, "requestURL"); |
| 42 | + this.parameters = unmodifiableCopyOf(checkNotNull(parameters, "queryParams")); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param name the query parameter name |
| 47 | + * @param value the query parameter value |
| 48 | + * @return a new HttpRequest with the given query parameter added |
| 49 | + * @throws NullPointerException if any of the parameters is null |
| 50 | + */ |
| 51 | + public HttpRequest addParameter(String name, String value) { |
| 52 | + checkNotNull(name, "name"); |
| 53 | + checkNotNull(value, "value"); |
| 54 | + |
| 55 | + final List<String> oldValues = parameters.containsKey(name) ? parameters.get(name) : new ArrayList<String>(); |
| 56 | + final List<String> newValues = new ArrayList<>(oldValues); |
| 57 | + newValues.add(value); |
| 58 | + final Map<String, List<String>> params = new HashMap<>(parameters); |
| 59 | + params.put(name, newValues); |
| 60 | + |
| 61 | + return new HttpRequest(requestURL, params); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param name the query parameter name |
| 66 | + * @return a new HttpRequest with the given query parameter removed |
| 67 | + * @throws NullPointerException if any of the parameters is null |
| 68 | + */ |
| 69 | + public HttpRequest removeParameter(String name) { |
| 70 | + checkNotNull(name, "name"); |
| 71 | + |
| 72 | + final Map<String, List<String>> params = new HashMap<>(parameters); |
| 73 | + params.remove(name); |
| 74 | + |
| 75 | + return new HttpRequest(requestURL, params); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * The URL the client used to make the request. Includes a protocol, server name, port number, and server path, but |
| 80 | + * not the query string parameters. |
| 81 | + * |
| 82 | + * @return the request URL |
| 83 | + */ |
| 84 | + public String getRequestURL() { |
| 85 | + return requestURL; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param name the query parameter name |
| 90 | + * @return the first value for the parameter, or null |
| 91 | + */ |
| 92 | + public String getParameter(String name) { |
| 93 | + List<String> values = getParameters(name); |
| 94 | + return values.isEmpty() ? null : values.get(0); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param name the query parameter name |
| 99 | + * @return a List containing all values for the parameter |
| 100 | + */ |
| 101 | + public List<String> getParameters(String name) { |
| 102 | + List<String> values = parameters.get(name); |
| 103 | + return values != null ? values : Collections.<String>emptyList(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return a map of all query parameters |
| 108 | + */ |
| 109 | + public Map<String, List<String>> getParameters() { |
| 110 | + return parameters; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean equals(Object o) { |
| 115 | + if (this == o) return true; |
| 116 | + if (o == null || getClass() != o.getClass()) return false; |
| 117 | + HttpRequest that = (HttpRequest) o; |
| 118 | + return Objects.equals(requestURL, that.requestURL) && |
| 119 | + Objects.equals(parameters, that.parameters); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int hashCode() { |
| 124 | + return Objects.hash(requestURL, parameters); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String toString() { |
| 129 | + return "HttpRequest{" + |
| 130 | + "requestURL='" + requestURL + '\'' + |
| 131 | + ", parameters=" + parameters + |
| 132 | + '}'; |
| 133 | + } |
| 134 | + |
| 135 | + private static Map<String, List<String>> unmodifiableCopyOf(Map<String, List<String>> orig) { |
| 136 | + Map<String, List<String>> copy = new HashMap<>(); |
| 137 | + for (Map.Entry<String, List<String>> entry : orig.entrySet()) { |
| 138 | + copy.put(entry.getKey(), unmodifiableList(new ArrayList<>(entry.getValue()))); |
| 139 | + } |
| 140 | + |
| 141 | + return unmodifiableMap(copy); |
| 142 | + } |
| 143 | +} |
0 commit comments