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.urlencoding.*;
024
025/**
026 * Convenience class for constructing instances of <code>List&lt;NameValuePair&gt;</code> for the
027 * {@link UrlEncodedFormEntity} class.
028 * 
029 * <p>
030 * Instances of this method can be passed directly to the {@link RestClient#doPost(Object, Object)} method or
031 * {@link RestCall#input(Object)} methods to perform URL-encoded form posts.
032 * 
033 * <h5 class='section'>Example:</h5>
034 * <p class='bcode'>
035 *    NameValuePairs params = <jk>new</jk> NameValuePairs()
036 *       .append(<js>"j_username"</js>, user)
037 *       .append(<js>"j_password"</js>, pw);
038 *    restClient.doPost(url, params).run();
039 * </p>
040 */
041public final class NameValuePairs extends LinkedList<NameValuePair> {
042
043   private static final long serialVersionUID = 1L;
044
045   /**
046    * Appends the specified pair to the end of this list.
047    * 
048    * @param pair The pair to append to this list.
049    * @return This object (for method chaining).
050    */
051   public NameValuePairs append(NameValuePair pair) {
052      super.add(pair);
053      return this;
054   }
055
056   /**
057    * Appends the specified name/value pair to the end of this list.
058    * 
059    * <p>
060    * The value is simply converted to a string using <code>toString()</code>, or <js>"null"</js> if <jk>null</jk>.
061    * 
062    * @param name The pair name.
063    * @param value The pair value.
064    * @return This object (for method chaining).
065    */
066   public NameValuePairs append(String name, Object value) {
067      super.add(new BasicNameValuePair(name, asString(value)));
068      return this;
069   }
070
071   /**
072    * Appends the specified name/value pair to the end of this list.
073    * 
074    * <p>
075    * The value is converted to UON notation using the {@link UrlEncodingSerializer} defined on the client.
076    * 
077    * @param name The pair name.
078    * @param value The pair value.
079    * @param partSerializer The serializer to use for converting values to simple strings.
080    * @return This object (for method chaining).
081    */
082   public NameValuePairs append(String name, Object value, HttpPartSerializer partSerializer) {
083      super.add(new SerializedNameValuePair(name, value, partSerializer));
084      return this;
085   }
086}