Added convenience #create methods to UrlEncodedFormEntity
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1231075 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
76762ca4ed
commit
3b8726022b
|
@ -33,6 +33,7 @@ import org.apache.http.annotation.NotThreadSafe;
|
||||||
|
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.utils.URLEncodedUtils;
|
import org.apache.http.client.utils.URLEncodedUtils;
|
||||||
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.protocol.HTTP;
|
import org.apache.http.protocol.HTTP;
|
||||||
|
|
||||||
|
@ -45,20 +46,38 @@ import org.apache.http.protocol.HTTP;
|
||||||
@NotThreadSafe // AbstractHttpEntity is not thread-safe
|
@NotThreadSafe // AbstractHttpEntity is not thread-safe
|
||||||
public class UrlEncodedFormEntity extends StringEntity {
|
public class UrlEncodedFormEntity extends StringEntity {
|
||||||
|
|
||||||
|
public static UrlEncodedFormEntity create(
|
||||||
|
final Iterable <? extends NameValuePair> parameters, final String charset) {
|
||||||
|
try {
|
||||||
|
return new UrlEncodedFormEntity(parameters, charset);
|
||||||
|
} catch (UnsupportedEncodingException ex) {
|
||||||
|
throw new IllegalArgumentException(ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UrlEncodedFormEntity create(
|
||||||
|
final Iterable <? extends NameValuePair> parameters) {
|
||||||
|
try {
|
||||||
|
return new UrlEncodedFormEntity(parameters, null);
|
||||||
|
} catch (UnsupportedEncodingException ex) {
|
||||||
|
throw new IllegalArgumentException(ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new {@link UrlEncodedFormEntity} with the list
|
* Constructs a new {@link UrlEncodedFormEntity} with the list
|
||||||
* of parameters in the specified encoding.
|
* of parameters in the specified encoding.
|
||||||
*
|
*
|
||||||
* @param parameters list of name/value pairs
|
* @param parameters list of name/value pairs
|
||||||
* @param encoding encoding the name/value pairs be encoded with
|
* @param charset encoding the name/value pairs be encoded with
|
||||||
* @throws UnsupportedEncodingException if the encoding isn't supported
|
* @throws UnsupportedEncodingException if the encoding isn't supported
|
||||||
*/
|
*/
|
||||||
public UrlEncodedFormEntity (
|
public UrlEncodedFormEntity (
|
||||||
final List <? extends NameValuePair> parameters,
|
final List <? extends NameValuePair> parameters,
|
||||||
final String encoding) throws UnsupportedEncodingException {
|
final String charset) throws UnsupportedEncodingException {
|
||||||
super(URLEncodedUtils.format(parameters, encoding), encoding);
|
super(URLEncodedUtils.format(parameters,
|
||||||
setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM +
|
charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET),
|
||||||
(encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET));
|
ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,17 +85,17 @@ public class UrlEncodedFormEntity extends StringEntity {
|
||||||
* of parameters in the specified encoding.
|
* of parameters in the specified encoding.
|
||||||
*
|
*
|
||||||
* @param parameters iterable collection of name/value pairs
|
* @param parameters iterable collection of name/value pairs
|
||||||
* @param encoding encoding the name/value pairs be encoded with
|
* @param charset encoding the name/value pairs be encoded with
|
||||||
* @throws UnsupportedEncodingException if the encoding isn't supported
|
* @throws UnsupportedEncodingException if the encoding isn't supported
|
||||||
*
|
*
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public UrlEncodedFormEntity (
|
public UrlEncodedFormEntity (
|
||||||
final Iterable <? extends NameValuePair> parameters,
|
final Iterable <? extends NameValuePair> parameters,
|
||||||
final String encoding) throws UnsupportedEncodingException {
|
final String charset) throws UnsupportedEncodingException {
|
||||||
super(URLEncodedUtils.format(parameters, encoding), encoding);
|
super(URLEncodedUtils.format(parameters,
|
||||||
setContentType(URLEncodedUtils.CONTENT_TYPE + HTTP.CHARSET_PARAM +
|
charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET),
|
||||||
(encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET));
|
ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,7 +107,7 @@ public class UrlEncodedFormEntity extends StringEntity {
|
||||||
*/
|
*/
|
||||||
public UrlEncodedFormEntity (
|
public UrlEncodedFormEntity (
|
||||||
final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
|
final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
|
||||||
this(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
|
this(parameters, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -102,7 +121,7 @@ public class UrlEncodedFormEntity extends StringEntity {
|
||||||
*/
|
*/
|
||||||
public UrlEncodedFormEntity (
|
public UrlEncodedFormEntity (
|
||||||
final Iterable <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
|
final Iterable <? extends NameValuePair> parameters) throws UnsupportedEncodingException {
|
||||||
this(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
|
this(parameters, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,17 +244,17 @@ public class URLEncodedUtils {
|
||||||
* list of parameters in an HTTP PUT or HTTP POST.
|
* list of parameters in an HTTP PUT or HTTP POST.
|
||||||
*
|
*
|
||||||
* @param parameters The parameters to include.
|
* @param parameters The parameters to include.
|
||||||
* @param encoding The encoding to use.
|
* @param charset The encoding to use.
|
||||||
*
|
*
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public static String format (
|
public static String format (
|
||||||
final Iterable<? extends NameValuePair> parameters,
|
final Iterable<? extends NameValuePair> parameters,
|
||||||
final String encoding) {
|
final String charset) {
|
||||||
final StringBuilder result = new StringBuilder();
|
final StringBuilder result = new StringBuilder();
|
||||||
for (final NameValuePair parameter : parameters) {
|
for (final NameValuePair parameter : parameters) {
|
||||||
final String encodedName = encode(parameter.getName(), encoding);
|
final String encodedName = encode(parameter.getName(), charset);
|
||||||
final String encodedValue = encode(parameter.getValue(), encoding);
|
final String encodedValue = encode(parameter.getValue(), charset);
|
||||||
if (result.length() > 0) {
|
if (result.length() > 0) {
|
||||||
result.append(PARAMETER_SEPARATOR);
|
result.append(PARAMETER_SEPARATOR);
|
||||||
}
|
}
|
||||||
|
@ -267,25 +267,25 @@ public class URLEncodedUtils {
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String decode (final String content, final String encoding) {
|
private static String decode (final String content, final String charset) {
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return URLDecoder.decode(content,
|
return URLDecoder.decode(content,
|
||||||
encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
|
charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET);
|
||||||
} catch (UnsupportedEncodingException problem) {
|
} catch (UnsupportedEncodingException problem) {
|
||||||
throw new IllegalArgumentException(problem);
|
throw new IllegalArgumentException(problem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String encode (final String content, final String encoding) {
|
private static String encode (final String content, final String charset) {
|
||||||
if (content == null) {
|
if (content == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return URLEncoder.encode(content,
|
return URLEncoder.encode(content,
|
||||||
encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
|
charset != null ? charset : HTTP.DEFAULT_CONTENT_CHARSET);
|
||||||
} catch (UnsupportedEncodingException problem) {
|
} catch (UnsupportedEncodingException problem) {
|
||||||
throw new IllegalArgumentException(problem);
|
throw new IllegalArgumentException(problem);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue