001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import java.util.*;
018
019import org.apache.http.*;
020import org.apache.http.client.entity.*;
021import org.apache.http.message.*;
022import org.apache.juneau.httppart.*;
023import org.apache.juneau.oapi.*;
024import org.apache.juneau.urlencoding.*;
025
026/**
027 * Convenience class for constructing instances of <code>List&lt;NameValuePair&gt;</code> for the
028 * {@link UrlEncodedFormEntity} class.
029 *
030 * <p>
031 * Instances of this method can be passed directly to the {@link RestClient#doPost(Object, Object)} method or
032 * {@link RestCall#body(Object)} methods to perform URL-encoded form posts.
033 *
034 * <h5 class='section'>Example:</h5>
035 * <p class='bcode w800'>
036 *    NameValuePairs params = <jk>new</jk> NameValuePairs()
037 *       .append(<js>"j_username"</js>, user)
038 *       .append(<js>"j_password"</js>, pw);
039 *    restClient.doPost(url, params).run();
040 * </p>
041 */
042public final class NameValuePairs extends LinkedList<NameValuePair> {
043
044   private static final long serialVersionUID = 1L;
045
046   /**
047    * Appends the specified pair to the end of this list.
048    *
049    * @param pair The pair to append to this list.
050    * @return This object (for method chaining).
051    */
052   public NameValuePairs append(NameValuePair pair) {
053      super.add(pair);
054      return this;
055   }
056
057   /**
058    * Appends the specified name/value pair to the end of this list.
059    *
060    * <p>
061    * The value is simply converted to a string using <code>toString()</code>, or <js>"null"</js> if <jk>null</jk>.
062    *
063    * @param name The pair name.
064    * @param value The pair value.
065    * @return This object (for method chaining).
066    */
067   public NameValuePairs append(String name, Object value) {
068      super.add(new BasicNameValuePair(name, asString(value)));
069      return this;
070   }
071
072   /**
073    * Appends the specified name/value pair to the end of this list.
074    *
075    * <p>
076    * The value is converted to UON notation using the {@link UrlEncodingSerializer} defined on the client.
077    *
078    * @param name The pair name.
079    * @param value The pair value.
080    * @param serializer
081    *    The serializer to use for serializing the value to a string value.
082    * @param schema
083    *    The schema object that defines the format of the output.
084    *    <br>If <jk>null</jk>, defaults to the schema defined on the parser.
085    *    <br>If that's also <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}.
086    *    <br>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}).
087    * @return This object (for method chaining).
088    */
089   public NameValuePairs append(String name, Object value, HttpPartSerializer serializer, HttpPartSchema schema) {
090      super.add(new SerializedNameValuePair(name, value, serializer, schema));
091      return this;
092   }
093
094   /**
095    * @deprecated Use {@link #append(String, Object, HttpPartSerializer, HttpPartSchema)}
096    */
097   @SuppressWarnings("javadoc")
098   @Deprecated
099   public NameValuePairs append(String name, Object value, HttpPartSerializer partSerializer) {
100      return append(name, value, partSerializer, null);
101   }
102}