Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.client.utils; |
michael@0 | 29 | |
michael@0 | 30 | import java.io.IOException; |
michael@0 | 31 | import java.io.UnsupportedEncodingException; |
michael@0 | 32 | import java.net.URI; |
michael@0 | 33 | import java.net.URLDecoder; |
michael@0 | 34 | import java.net.URLEncoder; |
michael@0 | 35 | import java.util.ArrayList; |
michael@0 | 36 | import java.util.Collections; |
michael@0 | 37 | import java.util.List; |
michael@0 | 38 | import java.util.Scanner; |
michael@0 | 39 | |
michael@0 | 40 | import ch.boye.httpclientandroidlib.annotation.Immutable; |
michael@0 | 41 | |
michael@0 | 42 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 43 | import ch.boye.httpclientandroidlib.HeaderElement; |
michael@0 | 44 | import ch.boye.httpclientandroidlib.HttpEntity; |
michael@0 | 45 | import ch.boye.httpclientandroidlib.NameValuePair; |
michael@0 | 46 | import ch.boye.httpclientandroidlib.message.BasicNameValuePair; |
michael@0 | 47 | import ch.boye.httpclientandroidlib.protocol.HTTP; |
michael@0 | 48 | import ch.boye.httpclientandroidlib.util.EntityUtils; |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * A collection of utilities for encoding URLs. |
michael@0 | 52 | * |
michael@0 | 53 | * @since 4.0 |
michael@0 | 54 | */ |
michael@0 | 55 | @Immutable |
michael@0 | 56 | public class URLEncodedUtils { |
michael@0 | 57 | |
michael@0 | 58 | public static final String CONTENT_TYPE = "application/x-www-form-urlencoded"; |
michael@0 | 59 | private static final String PARAMETER_SEPARATOR = "&"; |
michael@0 | 60 | private static final String NAME_VALUE_SEPARATOR = "="; |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Returns a list of {@link NameValuePair NameValuePairs} as built from the |
michael@0 | 64 | * URI's query portion. For example, a URI of |
michael@0 | 65 | * http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three |
michael@0 | 66 | * NameValuePairs, one for a=1, one for b=2, and one for c=3. |
michael@0 | 67 | * <p> |
michael@0 | 68 | * This is typically useful while parsing an HTTP PUT. |
michael@0 | 69 | * |
michael@0 | 70 | * @param uri |
michael@0 | 71 | * uri to parse |
michael@0 | 72 | * @param encoding |
michael@0 | 73 | * encoding to use while parsing the query |
michael@0 | 74 | */ |
michael@0 | 75 | public static List <NameValuePair> parse (final URI uri, final String encoding) { |
michael@0 | 76 | List <NameValuePair> result = Collections.emptyList(); |
michael@0 | 77 | final String query = uri.getRawQuery(); |
michael@0 | 78 | if (query != null && query.length() > 0) { |
michael@0 | 79 | result = new ArrayList <NameValuePair>(); |
michael@0 | 80 | parse(result, new Scanner(query), encoding); |
michael@0 | 81 | } |
michael@0 | 82 | return result; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an |
michael@0 | 87 | * {@link HttpEntity}. The encoding is taken from the entity's |
michael@0 | 88 | * Content-Encoding header. |
michael@0 | 89 | * <p> |
michael@0 | 90 | * This is typically used while parsing an HTTP POST. |
michael@0 | 91 | * |
michael@0 | 92 | * @param entity |
michael@0 | 93 | * The entity to parse |
michael@0 | 94 | * @throws IOException |
michael@0 | 95 | * If there was an exception getting the entity's data. |
michael@0 | 96 | */ |
michael@0 | 97 | public static List <NameValuePair> parse ( |
michael@0 | 98 | final HttpEntity entity) throws IOException { |
michael@0 | 99 | List <NameValuePair> result = Collections.emptyList(); |
michael@0 | 100 | |
michael@0 | 101 | String contentType = null; |
michael@0 | 102 | String charset = null; |
michael@0 | 103 | |
michael@0 | 104 | Header h = entity.getContentType(); |
michael@0 | 105 | if (h != null) { |
michael@0 | 106 | HeaderElement[] elems = h.getElements(); |
michael@0 | 107 | if (elems.length > 0) { |
michael@0 | 108 | HeaderElement elem = elems[0]; |
michael@0 | 109 | contentType = elem.getName(); |
michael@0 | 110 | NameValuePair param = elem.getParameterByName("charset"); |
michael@0 | 111 | if (param != null) { |
michael@0 | 112 | charset = param.getValue(); |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) { |
michael@0 | 118 | final String content = EntityUtils.toString(entity, HTTP.ASCII); |
michael@0 | 119 | if (content != null && content.length() > 0) { |
michael@0 | 120 | result = new ArrayList <NameValuePair>(); |
michael@0 | 121 | parse(result, new Scanner(content), charset); |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | return result; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Returns true if the entity's Content-Type header is |
michael@0 | 129 | * <code>application/x-www-form-urlencoded</code>. |
michael@0 | 130 | */ |
michael@0 | 131 | public static boolean isEncoded (final HttpEntity entity) { |
michael@0 | 132 | Header h = entity.getContentType(); |
michael@0 | 133 | if (h != null) { |
michael@0 | 134 | HeaderElement[] elems = h.getElements(); |
michael@0 | 135 | if (elems.length > 0) { |
michael@0 | 136 | String contentType = elems[0].getName(); |
michael@0 | 137 | return contentType.equalsIgnoreCase(CONTENT_TYPE); |
michael@0 | 138 | } else { |
michael@0 | 139 | return false; |
michael@0 | 140 | } |
michael@0 | 141 | } else { |
michael@0 | 142 | return false; |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | /** |
michael@0 | 147 | * Adds all parameters within the Scanner to the list of |
michael@0 | 148 | * <code>parameters</code>, as encoded by <code>encoding</code>. For |
michael@0 | 149 | * example, a scanner containing the string <code>a=1&b=2&c=3</code> would |
michael@0 | 150 | * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the |
michael@0 | 151 | * list of parameters. |
michael@0 | 152 | * |
michael@0 | 153 | * @param parameters |
michael@0 | 154 | * List to add parameters to. |
michael@0 | 155 | * @param scanner |
michael@0 | 156 | * Input that contains the parameters to parse. |
michael@0 | 157 | * @param encoding |
michael@0 | 158 | * Encoding to use when decoding the parameters. |
michael@0 | 159 | */ |
michael@0 | 160 | public static void parse ( |
michael@0 | 161 | final List <NameValuePair> parameters, |
michael@0 | 162 | final Scanner scanner, |
michael@0 | 163 | final String encoding) { |
michael@0 | 164 | scanner.useDelimiter(PARAMETER_SEPARATOR); |
michael@0 | 165 | while (scanner.hasNext()) { |
michael@0 | 166 | final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); |
michael@0 | 167 | if (nameValue.length == 0 || nameValue.length > 2) |
michael@0 | 168 | throw new IllegalArgumentException("bad parameter"); |
michael@0 | 169 | |
michael@0 | 170 | final String name = decode(nameValue[0], encoding); |
michael@0 | 171 | String value = null; |
michael@0 | 172 | if (nameValue.length == 2) |
michael@0 | 173 | value = decode(nameValue[1], encoding); |
michael@0 | 174 | parameters.add(new BasicNameValuePair(name, value)); |
michael@0 | 175 | } |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Returns a String that is suitable for use as an <code>application/x-www-form-urlencoded</code> |
michael@0 | 180 | * list of parameters in an HTTP PUT or HTTP POST. |
michael@0 | 181 | * |
michael@0 | 182 | * @param parameters The parameters to include. |
michael@0 | 183 | * @param encoding The encoding to use. |
michael@0 | 184 | */ |
michael@0 | 185 | public static String format ( |
michael@0 | 186 | final List <? extends NameValuePair> parameters, |
michael@0 | 187 | final String encoding) { |
michael@0 | 188 | final StringBuilder result = new StringBuilder(); |
michael@0 | 189 | for (final NameValuePair parameter : parameters) { |
michael@0 | 190 | final String encodedName = encode(parameter.getName(), encoding); |
michael@0 | 191 | final String value = parameter.getValue(); |
michael@0 | 192 | final String encodedValue = value != null ? encode(value, encoding) : ""; |
michael@0 | 193 | if (result.length() > 0) |
michael@0 | 194 | result.append(PARAMETER_SEPARATOR); |
michael@0 | 195 | result.append(encodedName); |
michael@0 | 196 | result.append(NAME_VALUE_SEPARATOR); |
michael@0 | 197 | result.append(encodedValue); |
michael@0 | 198 | } |
michael@0 | 199 | return result.toString(); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | private static String decode (final String content, final String encoding) { |
michael@0 | 203 | try { |
michael@0 | 204 | return URLDecoder.decode(content, |
michael@0 | 205 | encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); |
michael@0 | 206 | } catch (UnsupportedEncodingException problem) { |
michael@0 | 207 | throw new IllegalArgumentException(problem); |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | private static String encode (final String content, final String encoding) { |
michael@0 | 212 | try { |
michael@0 | 213 | return URLEncoder.encode(content, |
michael@0 | 214 | encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); |
michael@0 | 215 | } catch (UnsupportedEncodingException problem) { |
michael@0 | 216 | throw new IllegalArgumentException(problem); |
michael@0 | 217 | } |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | } |