michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.client.utils; michael@0: michael@0: import java.io.IOException; michael@0: import java.io.UnsupportedEncodingException; michael@0: import java.net.URI; michael@0: import java.net.URLDecoder; michael@0: import java.net.URLEncoder; michael@0: import java.util.ArrayList; michael@0: import java.util.Collections; michael@0: import java.util.List; michael@0: import java.util.Scanner; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.Header; michael@0: import ch.boye.httpclientandroidlib.HeaderElement; michael@0: import ch.boye.httpclientandroidlib.HttpEntity; michael@0: import ch.boye.httpclientandroidlib.NameValuePair; michael@0: import ch.boye.httpclientandroidlib.message.BasicNameValuePair; michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP; michael@0: import ch.boye.httpclientandroidlib.util.EntityUtils; michael@0: michael@0: /** michael@0: * A collection of utilities for encoding URLs. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class URLEncodedUtils { michael@0: michael@0: public static final String CONTENT_TYPE = "application/x-www-form-urlencoded"; michael@0: private static final String PARAMETER_SEPARATOR = "&"; michael@0: private static final String NAME_VALUE_SEPARATOR = "="; michael@0: michael@0: /** michael@0: * Returns a list of {@link NameValuePair NameValuePairs} as built from the michael@0: * URI's query portion. For example, a URI of michael@0: * http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three michael@0: * NameValuePairs, one for a=1, one for b=2, and one for c=3. michael@0: *

michael@0: * This is typically useful while parsing an HTTP PUT. michael@0: * michael@0: * @param uri michael@0: * uri to parse michael@0: * @param encoding michael@0: * encoding to use while parsing the query michael@0: */ michael@0: public static List parse (final URI uri, final String encoding) { michael@0: List result = Collections.emptyList(); michael@0: final String query = uri.getRawQuery(); michael@0: if (query != null && query.length() > 0) { michael@0: result = new ArrayList (); michael@0: parse(result, new Scanner(query), encoding); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an michael@0: * {@link HttpEntity}. The encoding is taken from the entity's michael@0: * Content-Encoding header. michael@0: *

michael@0: * This is typically used while parsing an HTTP POST. michael@0: * michael@0: * @param entity michael@0: * The entity to parse michael@0: * @throws IOException michael@0: * If there was an exception getting the entity's data. michael@0: */ michael@0: public static List parse ( michael@0: final HttpEntity entity) throws IOException { michael@0: List result = Collections.emptyList(); michael@0: michael@0: String contentType = null; michael@0: String charset = null; michael@0: michael@0: Header h = entity.getContentType(); michael@0: if (h != null) { michael@0: HeaderElement[] elems = h.getElements(); michael@0: if (elems.length > 0) { michael@0: HeaderElement elem = elems[0]; michael@0: contentType = elem.getName(); michael@0: NameValuePair param = elem.getParameterByName("charset"); michael@0: if (param != null) { michael@0: charset = param.getValue(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (contentType != null && contentType.equalsIgnoreCase(CONTENT_TYPE)) { michael@0: final String content = EntityUtils.toString(entity, HTTP.ASCII); michael@0: if (content != null && content.length() > 0) { michael@0: result = new ArrayList (); michael@0: parse(result, new Scanner(content), charset); michael@0: } michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Returns true if the entity's Content-Type header is michael@0: * application/x-www-form-urlencoded. michael@0: */ michael@0: public static boolean isEncoded (final HttpEntity entity) { michael@0: Header h = entity.getContentType(); michael@0: if (h != null) { michael@0: HeaderElement[] elems = h.getElements(); michael@0: if (elems.length > 0) { michael@0: String contentType = elems[0].getName(); michael@0: return contentType.equalsIgnoreCase(CONTENT_TYPE); michael@0: } else { michael@0: return false; michael@0: } michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Adds all parameters within the Scanner to the list of michael@0: * parameters, as encoded by encoding. For michael@0: * example, a scanner containing the string a=1&b=2&c=3 would michael@0: * add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the michael@0: * list of parameters. michael@0: * michael@0: * @param parameters michael@0: * List to add parameters to. michael@0: * @param scanner michael@0: * Input that contains the parameters to parse. michael@0: * @param encoding michael@0: * Encoding to use when decoding the parameters. michael@0: */ michael@0: public static void parse ( michael@0: final List parameters, michael@0: final Scanner scanner, michael@0: final String encoding) { michael@0: scanner.useDelimiter(PARAMETER_SEPARATOR); michael@0: while (scanner.hasNext()) { michael@0: final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); michael@0: if (nameValue.length == 0 || nameValue.length > 2) michael@0: throw new IllegalArgumentException("bad parameter"); michael@0: michael@0: final String name = decode(nameValue[0], encoding); michael@0: String value = null; michael@0: if (nameValue.length == 2) michael@0: value = decode(nameValue[1], encoding); michael@0: parameters.add(new BasicNameValuePair(name, value)); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Returns a String that is suitable for use as an application/x-www-form-urlencoded michael@0: * list of parameters in an HTTP PUT or HTTP POST. michael@0: * michael@0: * @param parameters The parameters to include. michael@0: * @param encoding The encoding to use. michael@0: */ michael@0: public static String format ( michael@0: final List parameters, michael@0: final String encoding) { michael@0: final StringBuilder result = new StringBuilder(); michael@0: for (final NameValuePair parameter : parameters) { michael@0: final String encodedName = encode(parameter.getName(), encoding); michael@0: final String value = parameter.getValue(); michael@0: final String encodedValue = value != null ? encode(value, encoding) : ""; michael@0: if (result.length() > 0) michael@0: result.append(PARAMETER_SEPARATOR); michael@0: result.append(encodedName); michael@0: result.append(NAME_VALUE_SEPARATOR); michael@0: result.append(encodedValue); michael@0: } michael@0: return result.toString(); michael@0: } michael@0: michael@0: private static String decode (final String content, final String encoding) { michael@0: try { michael@0: return URLDecoder.decode(content, michael@0: encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); michael@0: } catch (UnsupportedEncodingException problem) { michael@0: throw new IllegalArgumentException(problem); michael@0: } michael@0: } michael@0: michael@0: private static String encode (final String content, final String encoding) { michael@0: try { michael@0: return URLEncoder.encode(content, michael@0: encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET); michael@0: } catch (UnsupportedEncodingException problem) { michael@0: throw new IllegalArgumentException(problem); michael@0: } michael@0: } michael@0: michael@0: }