Package feign

Class RequestTemplate

  • All Implemented Interfaces:
    java.io.Serializable

    public final class RequestTemplate
    extends java.lang.Object
    implements java.io.Serializable
    Request Builder for an HTTP Target.

    This class is a variation on a UriTemplate, where, in addition to the uri, Headers and Query information also support template expressions.

    See Also:
    Serialized Form
    • Constructor Detail

      • RequestTemplate

        public RequestTemplate()
        Create a new Request Template.
      • RequestTemplate

        @Deprecated
        public RequestTemplate​(RequestTemplate toCopy)
        Deprecated.
        Create a Request Template from an existing Request Template.
        Parameters:
        toCopy - template.
    • Method Detail

      • from

        public static RequestTemplate from​(RequestTemplate requestTemplate)
        Create a Request Template from an existing Request Template.
        Parameters:
        requestTemplate - to copy from.
        Returns:
        a new Request Template.
      • resolve

        public RequestTemplate resolve​(java.util.Map<java.lang.String,​?> variables)
        Resolve all expressions using the variable value substitutions provided. Variable values will be pct-encoded, if they are not already.
        Parameters:
        variables - containing the variable values to use when resolving expressions.
        Returns:
        a new Request Template with all of the variables resolved.
      • request

        public Request request()
        Creates a Request from this template. The template must be resolved before calling this method, or an IllegalStateException will be thrown.
        Returns:
        a new Request instance.
        Throws:
        java.lang.IllegalStateException - if this template has not been resolved.
      • method

        @Deprecated
        public RequestTemplate method​(java.lang.String method)
        Deprecated.
        Set the Http Method.
        Parameters:
        method - to use.
        Returns:
        a RequestTemplate for chaining.
      • method

        public java.lang.String method()
        The Request Http Method.
        Returns:
        Http Method.
      • decodeSlash

        public RequestTemplate decodeSlash​(boolean decodeSlash)
        Set whether do encode slash / characters when resolving this template.
        Parameters:
        decodeSlash - if slash literals should not be encoded.
        Returns:
        a RequestTemplate for chaining.
      • decodeSlash

        public boolean decodeSlash()
        If slash / characters are not encoded when resolving.
        Returns:
        true if slash literals are not encoded, false otherwise.
      • collectionFormat

        public RequestTemplate collectionFormat​(CollectionFormat collectionFormat)
        The Collection Format to use when resolving variables that represent Iterables or Collections
        Parameters:
        collectionFormat - to use.
        Returns:
        a RequestTemplate for chaining.
      • collectionFormat

        public CollectionFormat collectionFormat()
        The Collection Format that will be used when resolving Iterable and Collection variables.
        Returns:
        the collection format set
      • append

        @Deprecated
        public RequestTemplate append​(java.lang.CharSequence value)
        Deprecated.
        Append the value to the template.

        This method is poorly named and is used primarily to store the relative uri for the request. It has been replaced by uri(String) and will be removed in a future release.

        Parameters:
        value - to append.
        Returns:
        a RequestTemplate for chaining.
      • insert

        @Deprecated
        public RequestTemplate insert​(int pos,
                                      java.lang.CharSequence value)
        Deprecated.
        Insert the value at the specified point in the template uri.

        This method is poorly named has undocumented behavior. When the value contains a fully qualified http request url, the value is always inserted at the beginning of the uri.

        Due to this, use of this method is not recommended and remains for backward compatibility. It has been replaced by target(String) and will be removed in a future release.

        Parameters:
        pos - in the uri to place the value.
        value - to insert.
        Returns:
        a RequestTemplate for chaining.
      • uri

        public RequestTemplate uri​(java.lang.String uri)
        Set the Uri for the request, replacing the existing uri if set.
        Parameters:
        uri - to use, must be a relative uri.
        Returns:
        a RequestTemplate for chaining.
      • uri

        public RequestTemplate uri​(java.lang.String uri,
                                   boolean append)
        Set the uri for the request.
        Parameters:
        uri - to use, must be a relative uri.
        append - if the uri should be appended, if the uri is already set.
        Returns:
        a RequestTemplate for chaining.
      • target

        public RequestTemplate target​(java.lang.String target)
        Set the target host for this request.
        Parameters:
        target - host for this request. Must be an absolute target.
        Returns:
        a RequestTemplate for chaining.
      • url

        public java.lang.String url()
        The URL for the request. If the template has not been resolved, the url will represent a uri template.
        Returns:
        the url
      • path

        public java.lang.String path()
        The Uri Path.
        Returns:
        the uri path.
      • variables

        public java.util.List<java.lang.String> variables()
        List all of the template variable expressions for this template.
        Returns:
        a list of template variable names
      • query

        public RequestTemplate query​(java.lang.String name,
                                     java.lang.Iterable<java.lang.String> values)
        Specify a Query String parameter, with the specified values. Values can be literals or template expressions.
        Parameters:
        name - of the parameter.
        values - for this parameter.
        Returns:
        a RequestTemplate for chaining.
      • queries

        public RequestTemplate queries​(java.util.Map<java.lang.String,​java.util.Collection<java.lang.String>> queries)
        Sets the Query Parameters.
        Parameters:
        queries - to use for this request.
        Returns:
        a RequestTemplate for chaining.
      • queries

        public java.util.Map<java.lang.String,​java.util.Collection<java.lang.String>> queries()
        Return an immutable Map of all Query Parameters and their values.
        Returns:
        registered Query Parameters.
      • header

        public RequestTemplate header​(java.lang.String name,
                                      java.lang.Iterable<java.lang.String> values)
        Specify a Header, with the specified values. Values can be literals or template expressions.
        Parameters:
        name - of the header.
        values - for this header.
        Returns:
        a RequestTemplate for chaining.
      • headers

        public RequestTemplate headers​(java.util.Map<java.lang.String,​java.util.Collection<java.lang.String>> headers)
        Headers for this Request.
        Parameters:
        headers - to use.
        Returns:
        a RequestTemplate for chaining.
      • headers

        public java.util.Map<java.lang.String,​java.util.Collection<java.lang.String>> headers()
        Returns an immutable copy of the Headers for this request.
        Returns:
        the currently applied headers.
      • body

        @Deprecated
        public RequestTemplate body​(byte[] bodyData,
                                    java.nio.charset.Charset charset)
        Deprecated.
        Sets the Body and Charset for this request.
        Parameters:
        bodyData - to send, can be null.
        charset - of the encoded data.
        Returns:
        a RequestTemplate for chaining.
      • body

        @Deprecated
        public RequestTemplate body​(java.lang.String bodyText)
        Deprecated.
        Set the Body for this request. Charset is assumed to be UTF_8. Data must be encoded.
        Parameters:
        bodyText - to send.
        Returns:
        a RequestTemplate for chaining.
      • body

        public RequestTemplate body​(Request.Body body)
        Set the Body for this request.
        Parameters:
        body - to send.
        Returns:
        a RequestTemplate for chaining.
      • requestCharset

        public java.nio.charset.Charset requestCharset()
        Charset of the Request Body, if known.
        Returns:
        the currently applied Charset.
      • body

        @Deprecated
        public byte[] body()
        Deprecated.
        replaced by requestBody()
        The Request Body.
        Returns:
        the request body.
      • bodyTemplate

        @Deprecated
        public RequestTemplate bodyTemplate​(java.lang.String bodyTemplate)
        Deprecated.
        Specify the Body Template to use. Can contain literals and expressions.
        Parameters:
        bodyTemplate - to use.
        Returns:
        a RequestTemplate for chaining.
      • bodyTemplate

        public java.lang.String bodyTemplate()
        Body Template to resolve.
        Returns:
        the unresolved body template.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hasRequestVariable

        public boolean hasRequestVariable​(java.lang.String variable)
        Return if the variable exists on the uri, query, or headers, in this template.
        Parameters:
        variable - to look for.
        Returns:
        true if the variable exists, false otherwise.
      • getRequestVariables

        public java.util.Collection<java.lang.String> getRequestVariables()
        Retrieve all uri, header, and query template variables.
        Returns:
        a List of all the variable names.
      • resolved

        public boolean resolved()
        If this template has been resolved.
        Returns:
        true if the template has been resolved, false otherwise.
      • queryLine

        public java.lang.String queryLine()
        The Query String for the template. Expressions are not resolved.
        Returns:
        the Query String.