PathDetails.java

  1. package com.renomad.minum.web;


  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Objects;

  5. /**
  6.  * Some essential characteristics of the path portion of the start line
  7.  */
  8. public final class PathDetails {

  9.     public static final PathDetails empty = new PathDetails("", "", Map.of());

  10.     private final String isolatedPath;
  11.     private final String rawQueryString;
  12.     private final Map<String, String> queryString;

  13.     /**
  14.      * Basic constructor
  15.      * @param isolatedPath the isolated path is found after removing the query string
  16.      * @param rawQueryString the raw query is the string after a question mark (if it exists - it's optional)
  17.      *                       if there is no query string, then we leave rawQuery as a null value
  18.      * @param queryString the query is a map of the keys -> values found in the query string
  19.      */
  20.     public PathDetails (
  21.             String isolatedPath,
  22.             String rawQueryString,
  23.             Map<String, String> queryString
  24.     ) {
  25.         this.isolatedPath = isolatedPath;
  26.         this.rawQueryString = rawQueryString;
  27.         this.queryString = new HashMap<>(queryString == null ? Map.of() : queryString);
  28.     }

  29.     /**
  30.      * Provides the path by itself, without the query string.  For examples,
  31.      * here are some request lines with their isolated paths:
  32.      * <pre>
  33.      * {@code
  34.      * request line                                isolated path
  35.      * -------------------                         -------------
  36.      * POST / HTTP/1.1                             ""
  37.      * GET /background.png HTTP/1.0                "background.png"
  38.      * HEAD /test.html?query=alibaba HTTP/1.1      "test.html"
  39.      * OPTIONS /anypage.html HTTP/1.0              "anypage.html"
  40.      * }
  41.      * </pre>
  42.      */
  43.     public String getIsolatedPath() {
  44.         return isolatedPath;
  45.     }

  46.     /**
  47.      * Returns the raw query string.  For example, in "HEAD /test.html?query=alibaba HTTP/1.1",
  48.      * the raw query string is "query=alibaba"
  49.      */
  50.     public String getRawQueryString() {
  51.         return rawQueryString;
  52.     }

  53.     /**
  54.      * This returns the query string portion of the request line as a map, with
  55.      * case-sensitive keys.
  56.      */
  57.     public Map<String, String> getQueryString() {
  58.         return new HashMap<>(queryString);
  59.     }

  60.     @Override
  61.     public boolean equals(Object o) {
  62.         if (this == o) return true;
  63.         if (!(o instanceof PathDetails that)) return false;
  64.         return Objects.equals(isolatedPath, that.isolatedPath) && Objects.equals(rawQueryString, that.rawQueryString) && Objects.equals(queryString, that.queryString);
  65.     }

  66.     @Override
  67.     public int hashCode() {
  68.         return Objects.hash(isolatedPath, rawQueryString, queryString);
  69.     }

  70.     @Override
  71.     public String toString() {
  72.         return "PathDetails{" +
  73.                 "isolatedPath='" + isolatedPath + '\'' +
  74.                 ", rawQueryString='" + rawQueryString + '\'' +
  75.                 ", queryString=" + queryString +
  76.                 '}';
  77.     }
  78. }