Пример #1
0
  private static StringBuilder createURIComponent(
      final UriComponent.Type t,
      String template,
      final Map<String, ? extends Object> values,
      final boolean encode,
      final StringBuilder b) {
    if (template.indexOf('{') == -1) {
      b.append(template);
      return b;
    }

    // Find all template variables
    template = new UriTemplateParser(template).getNormalizedTemplate();
    final Matcher m = TEMPLATE_NAMES_PATTERN.matcher(template);

    int i = 0;
    while (m.find()) {
      b.append(template, i, m.start());
      Object tValue = values.get(m.group(1));
      if (tValue != null) {
        if (encode) tValue = UriComponent.encode(tValue.toString(), t);
        else tValue = UriComponent.contextualEncode(tValue.toString(), t);
        b.append(tValue);
      } else {
        throw templateVariableHasNoValue(m.group(1));
      }
      i = m.end();
    }
    b.append(template, i, template.length());
    return b;
  }
  @Override
  public UriBuilder replaceQueryParam(String name, Object... values) {
    checkSsp();

    if (queryParams == null) {
      queryParams = UriComponent.decodeQuery(query.toString(), false);
      query.setLength(0);
    }

    name = encode(name, UriComponent.Type.QUERY_PARAM);
    queryParams.remove(name);

    if (values == null) {
      return this;
    }

    for (Object value : values) {
      if (value == null) {
        throw new IllegalArgumentException("One or more of query value parameters are null");
      }

      queryParams.add(name, encode(value.toString(), UriComponent.Type.QUERY_PARAM));
    }
    return this;
  }
  @Override
  public UriBuilder replaceMatrixParam(String name, Object... values) {
    checkSsp();

    if (name == null) {
      throw new IllegalArgumentException("Name parameter is null");
    }

    if (matrixParams == null) {
      int i = path.lastIndexOf("/");
      if (i != -1) {
        i = 0;
      }
      matrixParams = UriComponent.decodeMatrix((i != -1) ? path.substring(i) : "", false);
      i = path.indexOf(";", i);
      if (i != -1) {
        path.setLength(i);
      }
    }

    name = encode(name, UriComponent.Type.MATRIX_PARAM);
    matrixParams.remove(name);
    if (values != null) {
      for (Object value : values) {
        if (value == null) {
          throw new IllegalArgumentException("One or more of matrix value parameters are null");
        }

        matrixParams.add(name, encode(value.toString(), UriComponent.Type.MATRIX_PARAM));
      }
    }
    return this;
  }
Пример #4
0
  private static int createURIComponent(
      final UriComponent.Type t,
      String template,
      final String[] values,
      final int offset,
      final boolean encode,
      final Map<String, String> mapValues,
      final StringBuilder b) {
    if (template.indexOf('{') == -1) {
      b.append(template);
      return offset;
    }

    // Find all template variables
    template = new UriTemplateParser(template).getNormalizedTemplate();
    final Matcher m = TEMPLATE_NAMES_PATTERN.matcher(template);
    int v = offset;
    int i = 0;
    while (m.find()) {
      b.append(template, i, m.start());
      final String tVariable = m.group(1);
      // Check if a template variable has already occurred
      // If so use the value to ensure that two or more declarations of
      // a template variable have the same value
      String tValue = mapValues.get(tVariable);
      if (tValue != null) {
        b.append(tValue);
      } else if (v < values.length) {
        tValue = values[v++];
        if (tValue != null) {
          if (encode) tValue = UriComponent.encode(tValue, t);
          else tValue = UriComponent.contextualEncode(tValue, t);
          mapValues.put(tVariable, tValue);
          b.append(tValue);
        } else {
          throw templateVariableHasNoValue(tVariable);
        }
      } else {
        throw templateVariableHasNoValue(tVariable);
      }
      i = m.end();
    }
    b.append(template, i, template.length());
    return v;
  }
Пример #5
0
 @Override
 public UriBuilder scheme(String scheme) {
   if (scheme != null) {
     this.scheme = scheme;
     UriComponent.validate(scheme, UriComponent.Type.SCHEME, true);
   } else {
     this.scheme = null;
   }
   return this;
 }
  private String create() {
    encodeMatrix();
    encodeQuery();

    StringBuilder sb = new StringBuilder();

    if (scheme != null) {
      sb.append(scheme).append(':');
    }

    if (ssp != null) {
      sb.append(ssp);
    } else {
      if (userInfo != null || host != null || port != -1) {
        sb.append("//");

        if (userInfo != null && userInfo.length() > 0) {
          sb.append(userInfo).append('@');
        }

        if (host != null) {
          // TODO check IPv6 address
          sb.append(host);
        }

        if (port != -1) {
          sb.append(':').append(port);
        }
      } else if (authority != null) {
        sb.append("//").append(authority);
      }

      if (path.length() > 0) {
        if (sb.length() > 0 && path.charAt(0) != '/') {
          sb.append("/");
        }
        sb.append(path);
      }

      if (query.length() > 0) {
        sb.append('?').append(query);
      }
    }

    if (fragment != null && fragment.length() > 0) {
      sb.append('#').append(fragment);
    }

    return UriComponent.encodeTemplateNames(sb.toString());
  }
Пример #7
0
 private String encode(String s, UriComponent.Type type) {
   return UriComponent.contextualEncode(s, type, true);
 }