001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.client.entity;
028
029import java.io.UnsupportedEncodingException;
030import java.nio.charset.Charset;
031import java.util.List;
032
033import org.apache.http.NameValuePair;
034import org.apache.http.client.utils.URLEncodedUtils;
035import org.apache.http.entity.ContentType;
036import org.apache.http.entity.StringEntity;
037import org.apache.http.protocol.HTTP;
038
039/**
040 * An entity composed of a list of url-encoded pairs.
041 * This is typically useful while sending an HTTP POST request.
042 *
043 * @since 4.0
044 */
045public class UrlEncodedFormEntity extends StringEntity {
046
047    /**
048     * Constructs a new {@link UrlEncodedFormEntity} with the list
049     * of parameters in the specified encoding.
050     *
051     * @param parameters list of name/value pairs
052     * @param charset encoding the name/value pairs be encoded with
053     * @throws UnsupportedEncodingException if the encoding isn't supported
054     */
055    public UrlEncodedFormEntity (
056        final List <? extends NameValuePair> parameters,
057        final String charset) throws UnsupportedEncodingException {
058        super(URLEncodedUtils.format(parameters,
059                charset != null ? charset : HTTP.DEF_CONTENT_CHARSET.name()),
060                ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
061    }
062
063    /**
064     * Constructs a new {@link UrlEncodedFormEntity} with the list
065     * of parameters in the specified encoding.
066     *
067     * @param parameters iterable collection of name/value pairs
068     * @param charset encoding the name/value pairs be encoded with
069     *
070     * @since 4.2
071     */
072    public UrlEncodedFormEntity (
073        final Iterable <? extends NameValuePair> parameters,
074        final Charset charset) {
075        super(URLEncodedUtils.format(parameters,
076                charset != null ? charset : HTTP.DEF_CONTENT_CHARSET),
077                ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
078    }
079
080    /**
081     * Constructs a new {@link UrlEncodedFormEntity} with the list
082     * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
083     *
084     * @param parameters list of name/value pairs
085     * @throws UnsupportedEncodingException if the default encoding isn't supported
086     */
087    public UrlEncodedFormEntity (
088        final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
089        this(parameters, (Charset) null);
090    }
091
092    /**
093     * Constructs a new {@link UrlEncodedFormEntity} with the list
094     * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET}
095     *
096     * @param parameters iterable collection of name/value pairs
097     *
098     * @since 4.2
099     */
100    public UrlEncodedFormEntity (
101        final Iterable <? extends NameValuePair> parameters) {
102        this(parameters, null);
103    }
104
105}