View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.client.entity;
28  
29  import java.io.UnsupportedEncodingException;
30  import java.nio.charset.Charset;
31  import java.util.List;
32  
33  import org.apache.http.NameValuePair;
34  import org.apache.http.annotation.NotThreadSafe;
35  import org.apache.http.client.utils.URLEncodedUtils;
36  import org.apache.http.entity.ContentType;
37  import org.apache.http.entity.StringEntity;
38  import org.apache.http.protocol.HTTP;
39  
40  /**
41   * An entity composed of a list of url-encoded pairs.
42   * This is typically useful while sending an HTTP POST request.
43   *
44   * @since 4.0
45   */
46  @NotThreadSafe // AbstractHttpEntity is not thread-safe
47  public class UrlEncodedFormEntity extends StringEntity {
48  
49      /**
50       * Constructs a new {@link UrlEncodedFormEntity} with the list
51       * of parameters in the specified encoding.
52       *
53       * @param parameters list of name/value pairs
54       * @param charset encoding the name/value pairs be encoded with
55       * @throws UnsupportedEncodingException if the encoding isn't supported
56       */
57      public UrlEncodedFormEntity (
58          final List <? extends NameValuePair> parameters,
59          final String charset) throws UnsupportedEncodingException {
60          super(URLEncodedUtils.format(parameters,
61                  charset != null ? charset : HTTP.DEF_CONTENT_CHARSET.name()),
62                  ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
63      }
64  
65      /**
66       * Constructs a new {@link UrlEncodedFormEntity} with the list
67       * of parameters in the specified encoding.
68       *
69       * @param parameters iterable collection of name/value pairs
70       * @param charset encoding the name/value pairs be encoded with
71       *
72       * @since 4.2
73       */
74      public UrlEncodedFormEntity (
75          final Iterable <? extends NameValuePair> parameters,
76          final Charset charset) {
77          super(URLEncodedUtils.format(parameters,
78                  charset != null ? charset : HTTP.DEF_CONTENT_CHARSET),
79                  ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
80      }
81  
82      /**
83       * Constructs a new {@link UrlEncodedFormEntity} with the list
84       * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
85       *
86       * @param parameters list of name/value pairs
87       * @throws UnsupportedEncodingException if the default encoding isn't supported
88       */
89      public UrlEncodedFormEntity (
90          final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
91          this(parameters, (Charset) null);
92      }
93  
94      /**
95       * Constructs a new {@link UrlEncodedFormEntity} with the list
96       * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
97       *
98       * @param parameters iterable collection of name/value pairs
99       *
100      * @since 4.2
101      */
102     public UrlEncodedFormEntity (
103         final Iterable <? extends NameValuePair> parameters) {
104         this(parameters, null);
105     }
106 
107 }