Exemplo n.º 1
0
  /**
   * given a canonical uri (/db/coll) returns the mapped uri (/db/coll) relative to this context
   * URLs are mapped to mongodb resources by using the mongo-mounts configuration properties
   *
   * @param unmappedUri
   * @return
   */
  public final String mapUri(String unmappedUri) {
    String ret = URLUtils.removeTrailingSlashes(unmappedUri);

    if (whatUri.equals("*")) {
      if (!this.whereUri.equals(SLASH)) {
        return this.whereUri + unmappedUri;
      }
    } else {
      ret = URLUtils.removeTrailingSlashes(ret.replaceFirst("^" + this.whatUri, this.whereUri));
    }

    if (ret.isEmpty()) {
      ret = SLASH;
    }

    return ret;
  }
Exemplo n.º 2
0
  /**
   * @param exchange the url rewriting feature is implemented by the whatUri and whereUri parameters
   *     <p>the exchange request path is rewritten replacing the whereUri string with the whatUri
   *     string the special whatUri value * means any resource: the whereUri is replaced with /
   *     <p>example 1
   *     <p>whatUri = /mydb/mycollection whereUri = /
   *     <p>then the requestPath / is rewritten to /mydb/mycollection
   *     <p>example 2
   *     <p>whatUri = * whereUri = /data
   *     <p>then the requestPath /data is rewritten to /
   * @param whereUri the uri to map to
   * @param whatUri the uri to map
   */
  public RequestContext(HttpServerExchange exchange, String whereUri, String whatUri) {
    this.whereUri =
        URLUtils.removeTrailingSlashes(
            whereUri == null ? null : whereUri.startsWith("/") ? whereUri : "/" + whereUri);

    this.whatUri =
        URLUtils.removeTrailingSlashes(
            whatUri == null
                ? null
                : whatUri.startsWith("/") || "*".equals(whatUri) ? whatUri : "/" + whatUri);

    this.unmappedRequestUri = exchange.getRequestPath();
    this.mappedRequestUri = unmapUri(exchange.getRequestPath());

    // "/db/collection/document" --> { "", "mappedDbName", "collection", "document" }
    this.pathTokens = this.mappedRequestUri.split(SLASH);
    this.type = selectRequestType(pathTokens);

    this.method = selectRequestMethod(exchange.getRequestMethod());
  }