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: * 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
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 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 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 extends NameValuePair> 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: }