Class UrlEncodedContent

  • All Implemented Interfaces:
    HttpContent, StreamingContent

    public class UrlEncodedContent
    extends AbstractHttpContent
    Implements support for HTTP form content encoding serialization of type application/x-www-form-urlencoded as specified in the HTML 4.0 Specification.

    Sample usage:

     static void setContent(HttpRequest request, Object item) {
     request.setContent(new UrlEncodedContent(item));
     }
     

    Implementation is not thread-safe.

    Since:
    1.0
    • Field Detail

      • data

        private java.lang.Object data
        Key name/value data.
      • uriPathEncodingFlag

        private boolean uriPathEncodingFlag
        Use URI Path encoder flag. False by default (use legacy and deprecated escapeUri)
    • Constructor Detail

      • UrlEncodedContent

        public UrlEncodedContent​(java.lang.Object data)
        Initialize the UrlEncodedContent with the legacy and deprecated escapeUri encoder
        Parameters:
        data - key name/value data
      • UrlEncodedContent

        public UrlEncodedContent​(java.lang.Object data,
                                 boolean useUriPathEncoding)
        Initialize the UrlEncodedContent with or without the legacy and deprecated escapeUri encoder
        Parameters:
        data - key name/value data
        useUriPathEncoding - escapes the string value so it can be safely included in URI path segments. For details on escaping URIs, see RFC 3986 - section 2.4
    • Method Detail

      • writeTo

        public void writeTo​(java.io.OutputStream out)
                     throws java.io.IOException
        Description copied from interface: StreamingContent
        Writes the byte content to the given output stream.

        Implementations must not close the output stream, and instead should flush the output stream. Some callers may assume that the output stream has not been closed, and will fail to work if it has been closed.

        Parameters:
        out - output stream
        Throws:
        java.io.IOException
      • setMediaType

        public UrlEncodedContent setMediaType​(HttpMediaType mediaType)
        Description copied from class: AbstractHttpContent
        Sets the media type to use for the Content-Type header, or null if unspecified.

        This will also overwrite any previously set parameter of the media type (for example "charset"), and therefore might change other properties as well.

        Overrides:
        setMediaType in class AbstractHttpContent
      • getData

        public final java.lang.Object getData()
        Returns the key name/value data or null for none.
        Since:
        1.5
      • setData

        public UrlEncodedContent setData​(java.lang.Object data)
        Sets the key name/value data.

        Overriding is only supported for the purpose of calling the super implementation and changing the return type, but nothing else.

        Since:
        1.5
      • getContent

        public static UrlEncodedContent getContent​(HttpRequest request)
        Returns the URL-encoded content of the given HTTP request, or if none return and set as content a new instance of UrlEncodedContent (whose getData() is an implementation of Map).
        Parameters:
        request - HTTP request
        Returns:
        URL-encoded content
        Throws:
        java.lang.ClassCastException - if the HTTP request has a content defined that is not UrlEncodedContent
        Since:
        1.7
      • appendParam

        private static boolean appendParam​(boolean first,
                                           java.io.Writer writer,
                                           java.lang.String name,
                                           java.lang.Object value,
                                           boolean uriPathEncodingFlag)
                                    throws java.io.IOException
        Throws:
        java.io.IOException