示例#1
0
  /**
   * Adds many query parameters at once. Although it accepts objects, each value will be converted
   * to string.
   */
  public HttpRequest query(String name1, Object value1, Object... parameters) {
    query(name1, value1 == null ? null : value1.toString());

    for (int i = 0; i < parameters.length; i += 2) {
      String name = parameters[i].toString();

      String value = parameters[i + 1].toString();
      query.add(name, value == null ? null : value);
    }
    return this;
  }
示例#2
0
  /** Prepares the request buffer. */
  @Override
  protected Buffer buffer(boolean fullRequest) {
    // INITIALIZATION

    // host port

    if (header(HEADER_HOST) == null) {
      setHostHeader();
    }

    // form

    Buffer formBuffer = formBuffer();

    // query string

    String queryString = queryString();

    // user-agent

    if (header("User-Agent") == null) {
      header("User-Agent", "Jodd HTTP");
    }

    // POST method requires Content-Type to be set

    if (method.equals("POST") && (contentLength() == null)) {
      contentLength(0);
    }

    // BUILD OUT

    Buffer request = new Buffer();

    request.append(method).append(SPACE).append(path);

    if (query != null && !query.isEmpty()) {
      request.append('?');
      request.append(queryString);
    }

    request.append(SPACE).append(httpVersion).append(CRLF);

    populateHeaderAndBody(request, formBuffer, fullRequest);

    return request;
  }
示例#3
0
  /**
   * Sets request path. Query string is allowed. Adds a slash if path doesn't start with one. Query
   * will be stripped out from the path. Previous query is discarded.
   *
   * @see #query()
   */
  public HttpRequest path(String path) {
    // this must be the only place that sets the path

    if (!path.startsWith(StringPool.SLASH)) {
      path = StringPool.SLASH + path;
    }

    int ndx = path.indexOf('?');

    if (ndx != -1) {
      String queryString = path.substring(ndx + 1);

      path = path.substring(0, ndx);

      query = HttpUtil.parseQuery(queryString, true);
    } else {
      query = HttpMultiMap.newCaseInsensitveMap();
    }

    this.path = path;

    return this;
  }
示例#4
0
 /** Removes query parameters for given name. */
 public HttpRequest removeQuery(String name) {
   query.remove(name);
   return this;
 }
示例#5
0
 /** Clears all query parameters. */
 public HttpRequest clearQueries() {
   query.clear();
   return this;
 }
示例#6
0
 /** Adds all parameters from the provided map. */
 public HttpRequest query(Map<String, String> queryMap) {
   for (Map.Entry<String, String> entry : queryMap.entrySet()) {
     query.add(entry.getKey(), entry.getValue());
   }
   return this;
 }
示例#7
0
 /** Adds query parameter. */
 public HttpRequest query(String name, String value) {
   query.add(name, value);
   return this;
 }