Package org.apache.http.message
Class BasicHeaderValueFormatter
java.lang.Object
org.apache.http.message.BasicHeaderValueFormatter
- All Implemented Interfaces:
HeaderValueFormatter
public class BasicHeaderValueFormatter extends Object implements HeaderValueFormatter
Basic implementation for formatting header value elements.
Instances of this class are stateless and thread-safe.
Derived classes are expected to maintain these properties.
- Since:
- 4.0
- Version:
- $Revision: 574185 $
- Author:
- Oleg Kalnichevski, and others
-
Field Summary
Fields Modifier and Type Field Description static BasicHeaderValueFormatterDEFAULTA default instance of this class, for use as default or fallback.static StringSEPARATORSSpecial characters that can be used as separators in HTTP parameters.static StringUNSAFE_CHARSUnsafe special characters that must be escaped using the backslash character -
Constructor Summary
Constructors Constructor Description BasicHeaderValueFormatter() -
Method Summary
Modifier and Type Method Description protected voiddoFormatValue(CharArrayBuffer buffer, String value, boolean quote)Actually formats the value of a name-value pair.protected intestimateElementsLen(HeaderElement[] elems)Estimates the length of formatted header elements.protected intestimateHeaderElementLen(HeaderElement elem)Estimates the length of a formatted header element.protected intestimateNameValuePairLen(NameValuePair nvp)Estimates the length of a formatted name-value pair.protected intestimateParametersLen(NameValuePair[] nvps)Estimates the length of formatted parameters.static StringformatElements(HeaderElement[] elems, boolean quote, HeaderValueFormatter formatter)Formats an array of header elements.CharArrayBufferformatElements(CharArrayBuffer buffer, HeaderElement[] elems, boolean quote)Formats an array of header elements.static StringformatHeaderElement(HeaderElement elem, boolean quote, HeaderValueFormatter formatter)Formats a header element.CharArrayBufferformatHeaderElement(CharArrayBuffer buffer, HeaderElement elem, boolean quote)Formats one header element.static StringformatNameValuePair(NameValuePair nvp, boolean quote, HeaderValueFormatter formatter)Formats a name-value pair.CharArrayBufferformatNameValuePair(CharArrayBuffer buffer, NameValuePair nvp, boolean quote)Formats one name-value pair, where the value is optional.static StringformatParameters(NameValuePair[] nvps, boolean quote, HeaderValueFormatter formatter)Formats a set of parameters.CharArrayBufferformatParameters(CharArrayBuffer buffer, NameValuePair[] nvps, boolean quote)Formats the parameters of a header element.protected booleanisSeparator(char ch)Checks whether a character is aseparator.protected booleanisUnsafe(char ch)Checks whether a character isunsafe.
-
Field Details
-
DEFAULT
A default instance of this class, for use as default or fallback. Note thatBasicHeaderValueFormatteris not a singleton, there can be many instances of the class itself and of derived classes. The instance here provides non-customized, default behavior. -
SEPARATORS
Special characters that can be used as separators in HTTP parameters. These special characters MUST be in a quoted string to be used within a parameter value .- See Also:
- Constant Field Values
-
UNSAFE_CHARS
Unsafe special characters that must be escaped using the backslash character- See Also:
- Constant Field Values
-
-
Constructor Details
-
BasicHeaderValueFormatter
public BasicHeaderValueFormatter()
-
-
Method Details
-
formatElements
public static final String formatElements(HeaderElement[] elems, boolean quote, HeaderValueFormatter formatter)Formats an array of header elements.- Parameters:
elems- the header elements to formatquote-trueto always format with quoted values,falseto use quotes only when necessaryformatter- the formatter to use, ornullfor thedefault- Returns:
- the formatted header elements
-
formatElements
public CharArrayBuffer formatElements(CharArrayBuffer buffer, HeaderElement[] elems, boolean quote)Description copied from interface:HeaderValueFormatterFormats an array of header elements.- Specified by:
formatElementsin interfaceHeaderValueFormatter- Parameters:
buffer- the buffer to append to, ornullto create a new bufferelems- the header elements to formatquote-trueto always format with quoted values,falseto use quotes only when necessary- Returns:
- a buffer with the formatted header elements.
If the
bufferargument was notnull, that buffer will be used and returned.
-
estimateElementsLen
Estimates the length of formatted header elements.- Parameters:
elems- the header elements to format, ornull- Returns:
- a length estimate, in number of characters
-
formatHeaderElement
public static final String formatHeaderElement(HeaderElement elem, boolean quote, HeaderValueFormatter formatter)Formats a header element.- Parameters:
elem- the header element to formatquote-trueto always format with quoted values,falseto use quotes only when necessaryformatter- the formatter to use, ornullfor thedefault- Returns:
- the formatted header element
-
formatHeaderElement
public CharArrayBuffer formatHeaderElement(CharArrayBuffer buffer, HeaderElement elem, boolean quote)Description copied from interface:HeaderValueFormatterFormats one header element.- Specified by:
formatHeaderElementin interfaceHeaderValueFormatter- Parameters:
buffer- the buffer to append to, ornullto create a new bufferelem- the header element to formatquote-trueto always format with quoted values,falseto use quotes only when necessary- Returns:
- a buffer with the formatted header element.
If the
bufferargument was notnull, that buffer will be used and returned.
-
estimateHeaderElementLen
Estimates the length of a formatted header element.- Parameters:
elem- the header element to format, ornull- Returns:
- a length estimate, in number of characters
-
formatParameters
public static final String formatParameters(NameValuePair[] nvps, boolean quote, HeaderValueFormatter formatter)Formats a set of parameters.- Parameters:
nvps- the parameters to formatquote-trueto always format with quoted values,falseto use quotes only when necessaryformatter- the formatter to use, ornullfor thedefault- Returns:
- the formatted parameters
-
formatParameters
public CharArrayBuffer formatParameters(CharArrayBuffer buffer, NameValuePair[] nvps, boolean quote)Description copied from interface:HeaderValueFormatterFormats the parameters of a header element. That's a list of name-value pairs, to be separated by semicolons. This method will not generate a leading semicolon.- Specified by:
formatParametersin interfaceHeaderValueFormatter- Parameters:
buffer- the buffer to append to, ornullto create a new buffernvps- the parameters (name-value pairs) to formatquote-trueto always format with quoted values,falseto use quotes only when necessary- Returns:
- a buffer with the formatted parameters.
If the
bufferargument was notnull, that buffer will be used and returned.
-
estimateParametersLen
Estimates the length of formatted parameters.- Parameters:
nvps- the parameters to format, ornull- Returns:
- a length estimate, in number of characters
-
formatNameValuePair
public static final String formatNameValuePair(NameValuePair nvp, boolean quote, HeaderValueFormatter formatter)Formats a name-value pair.- Parameters:
nvp- the name-value pair to formatquote-trueto always format with a quoted value,falseto use quotes only when necessaryformatter- the formatter to use, ornullfor thedefault- Returns:
- the formatted name-value pair
-
formatNameValuePair
public CharArrayBuffer formatNameValuePair(CharArrayBuffer buffer, NameValuePair nvp, boolean quote)Description copied from interface:HeaderValueFormatterFormats one name-value pair, where the value is optional.- Specified by:
formatNameValuePairin interfaceHeaderValueFormatter- Parameters:
buffer- the buffer to append to, ornullto create a new buffernvp- the name-value pair to formatquote-trueto always format with a quoted value,falseto use quotes only when necessary- Returns:
- a buffer with the formatted name-value pair.
If the
bufferargument was notnull, that buffer will be used and returned.
-
estimateNameValuePairLen
Estimates the length of a formatted name-value pair.- Parameters:
nvp- the name-value pair to format, ornull- Returns:
- a length estimate, in number of characters
-
doFormatValue
Actually formats the value of a name-value pair. This does not include a leading = character. Called fromformatNameValuePair.- Parameters:
buffer- the buffer to append to, nevernullvalue- the value to append, nevernullquote-trueto always format with quotes,falseto use quotes only when necessary
-
isSeparator
protected boolean isSeparator(char ch)Checks whether a character is aseparator.- Parameters:
ch- the character to check- Returns:
trueif the character is a separator,falseotherwise
-
isUnsafe
protected boolean isUnsafe(char ch)Checks whether a character isunsafe.- Parameters:
ch- the character to check- Returns:
trueif the character is unsafe,falseotherwise
-