[NO ISSUE][HYR][UTL] Add methods to extract query params from a uri
Change-Id: Ifac17d25b72b5a550a626325fcd6f9d5fe91407e
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/14623
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
index 3bd4033..e433c02 100644
--- a/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
+++ b/hyracks-fullstack/hyracks/hyracks-http/src/main/java/org/apache/hyracks/http/server/utils/HttpUtil.java
@@ -38,7 +38,9 @@
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
+import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.hyracks.http.api.IServletRequest;
import org.apache.hyracks.http.api.IServletResponse;
import org.apache.hyracks.http.server.BaseRequest;
@@ -252,6 +254,23 @@
? HttpScheme.HTTPS : HttpScheme.HTTP;
}
+ /**
+ * @return the first parameter value of the supplied parameter name, or {@link Optional#empty()}
+ */
+ public static Optional<String> extractQueryParameter(String uri, String parameterName) {
+ return extractQueryParameters(uri, parameterName).map(values -> values[0]);
+ }
+
+ /**
+ * @return all parameter values of the supplied parameter name, or {@link Optional#empty()}
+ */
+ public static Optional<String[]> extractQueryParameters(String uri, String parameterName) {
+ String[] values = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8, '?', '&').stream()
+ .filter(pair -> pair.getName().equals(parameterName)).map(NameValuePair::getValue)
+ .toArray(String[]::new);
+ return values.length == 0 ? Optional.empty() : Optional.of(values);
+ }
+
public static class ContentType {
public static final String ADM = "adm";
public static final String JSON = "json";