1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/client/utils/URLEncodedUtils.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 655107 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-05-10 08:20:42 -0700 (Sat, 10 May 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * or more contributor license agreements.  See the NOTICE file
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * distributed with this work for additional information
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * regarding copyright ownership.  The ASF licenses this file
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * to you under the Apache License, Version 2.0 (the
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "License"); you may not use this file except in compliance
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * with the License.  You may obtain a copy of the License at
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *   http://www.apache.org/licenses/LICENSE-2.0
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * Unless required by applicable law or agreed to in writing,
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * software distributed under the License is distributed on an
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * KIND, either express or implied.  See the License for the
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * specific language governing permissions and limitations
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * under the License.
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.client.utils;
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException;
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.UnsupportedEncodingException;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.URI;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.URLDecoder;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.net.URLEncoder;
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.ArrayList;
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Collections;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.List;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Scanner;
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.Header;
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpEntity;
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.NameValuePair;
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.message.BasicNameValuePair;
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.protocol.HTTP;
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.util.EntityUtils;
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * A collection of utilities for encoding URLs.
51069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
52069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class URLEncodedUtils {
53069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
54069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final String PARAMETER_SEPARATOR = "&";
56069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static final String NAME_VALUE_SEPARATOR = "=";
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns a list of {@link NameValuePair NameValuePairs} as built from the
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * URI's query portion. For example, a URI of
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * NameValuePairs, one for a=1, one for b=2, and one for c=3.
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <p>
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This is typically useful while parsing an HTTP PUT.
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param uri
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *            uri to parse
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param encoding
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *            encoding to use while parsing the query
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static List <NameValuePair> parse (final URI uri, final String encoding) {
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        List <NameValuePair> result = Collections.emptyList();
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final String query = uri.getRawQuery();
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (query != null && query.length() > 0) {
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            result = new ArrayList <NameValuePair>();
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            parse(result, new Scanner(query), encoding);
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return result;
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * {@link HttpEntity}. The encoding is taken from the entity's
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Content-Encoding header.
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <p>
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This is typically used while parsing an HTTP POST.
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param entity
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *            The entity to parse
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @throws IOException
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *             If there was an exception getting the entity's data.
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static List <NameValuePair> parse (
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final HttpEntity entity) throws IOException {
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        List <NameValuePair> result = Collections.emptyList();
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (isEncoded(entity)) {
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String content = EntityUtils.toString(entity);
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final Header encoding = entity.getContentEncoding();
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (content != null && content.length() > 0) {
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                result = new ArrayList <NameValuePair>();
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                parse(result, new Scanner(content),
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                        encoding != null ? encoding.getValue() : null);
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return result;
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns true if the entity's Content-Type header is
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <code>application/x-www-form-urlencoded</code>.
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static boolean isEncoded (final HttpEntity entity) {
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final Header contentType = entity.getContentType();
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return (contentType != null && contentType.getValue().equalsIgnoreCase(CONTENT_TYPE));
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Adds all parameters within the Scanner to the list of
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * <code>parameters</code>, as encoded by <code>encoding</code>. For
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * example, a scanner containing the string <code>a=1&b=2&c=3</code> would
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * list of parameters.
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param parameters
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *            List to add parameters to.
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param scanner
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *            Input that contains the parameters to parse.
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param encoding
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *            Encoding to use when decoding the parameters.
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static void parse (
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final List <NameValuePair> parameters,
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final Scanner scanner,
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String encoding) {
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        scanner.useDelimiter(PARAMETER_SEPARATOR);
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        while (scanner.hasNext()) {
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (nameValue.length == 0 || nameValue.length > 2)
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                throw new IllegalArgumentException("bad parameter");
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String name = decode(nameValue[0], encoding);
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            String value = null;
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (nameValue.length == 2)
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                value = decode(nameValue[1], encoding);
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            parameters.add(new BasicNameValuePair(name, value));
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Returns a String that is suitable for use as an <code>application/x-www-form-urlencoded</code>
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * list of parameters in an HTTP PUT or HTTP POST.
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param parameters  The parameters to include.
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param encoding The encoding to use.
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public static String format (
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final List <? extends NameValuePair> parameters,
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String encoding) {
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        final StringBuilder result = new StringBuilder();
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        for (final NameValuePair parameter : parameters) {
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String encodedName = encode(parameter.getName(), encoding);
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String value = parameter.getValue();
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            final String encodedValue = value != null ? encode(value, encoding) : "";
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (result.length() > 0)
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                result.append(PARAMETER_SEPARATOR);
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            result.append(encodedName);
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            result.append(NAME_VALUE_SEPARATOR);
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            result.append(encodedValue);
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        return result.toString();
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static String decode (final String content, final String encoding) {
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return URLDecoder.decode(content,
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } catch (UnsupportedEncodingException problem) {
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException(problem);
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static String encode (final String content, final String encoding) {
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        try {
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return URLEncoder.encode(content,
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } catch (UnsupportedEncodingException problem) {
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            throw new IllegalArgumentException(problem);
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
192