Class UriTemplate


  • public class UriTemplate
    extends java.lang.Object
    Expands URI Templates. This Class supports Level 1 templates and all Level 4 composite templates as described in: RFC 6570. Specifically, for the variables: var := "value" list := ["red", "green", "blue"] keys := [("semi", ";"),("dot", "."),("comma", ",")] The following templates results in the following expansions: {var} -> value {list} -> red,green,blue {list*} -> red,green,blue {keys} -> semi,%3B,dot,.,comma,%2C {keys*} -> semi=%3B,dot=.,comma=%2C {+list} -> red,green,blue {+list*} -> red,green,blue {+keys} -> semi,;,dot,.,comma,, {+keys*} -> semi=;,dot=.,comma=, {#list} -> #red,green,blue {#list*} -> #red,green,blue {#keys} -> #semi,;,dot,.,comma,, {#keys*} -> #semi=;,dot=.,comma=, X{.list} -> X.red,green,blue X{.list*} -> X.red.green.blue X{.keys} -> X.semi,%3B,dot,.,comma,%2C X{.keys*} -> X.semi=%3B.dot=..comma=%2C {/list} -> /red,green,blue {/list*} -> /red/green/blue {/keys} -> /semi,%3B,dot,.,comma,%2C {/keys*} -> /semi=%3B/dot=./comma=%2C {;list} -> ;list=red,green,blue {;list*} -> ;list=red;list=green;list=blue {;keys} -> ;keys=semi,%3B,dot,.,comma,%2C {;keys*} -> ;semi=%3B;dot=.;comma=%2C {?list} -> ?list=red,green,blue {?list*} -> ?list=red&list=green&list=blue {?keys} -> ?keys=semi,%3B,dot,.,comma,%2C {?keys*} -> ?semi=%3B&dot=.&comma=%2C {&list} -> &list=red,green,blue {&list*} -> &list=red&list=green&list=blue {&keys} -> &keys=semi,%3B,dot,.,comma,%2C {&keys*} -> &semi=%3B&dot=.&comma=%2C {?var,list} -> ?var=value&list=red,green,blue
    Since:
    1.6
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  UriTemplate.CompositeOutput
      Contains information on how to output a composite value.
    • Constructor Summary

      Constructors 
      Constructor Description
      UriTemplate()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String expand​(java.lang.String pathUri, java.lang.Object parameters, boolean addUnusedParamsAsQueryParams)
      Expands templates in a URI.
      static java.lang.String expand​(java.lang.String baseUrl, java.lang.String uriTemplate, java.lang.Object parameters, boolean addUnusedParamsAsQueryParams)
      Expands templates in a URI template that is relative to a base URL.
      (package private) static UriTemplate.CompositeOutput getCompositeOutput​(java.lang.String propertyName)  
      private static java.lang.String getListPropertyValue​(java.lang.String varName, java.util.Iterator<?> iterator, boolean containsExplodeModifier, UriTemplate.CompositeOutput compositeOutput)
      Expand the template of a composite list property.
      private static java.util.Map<java.lang.String,​java.lang.Object> getMap​(java.lang.Object obj)
      Constructs a new Map<String, Object> from an Object.
      private static java.lang.String getMapPropertyValue​(java.lang.String varName, java.util.Map<java.lang.String,​java.lang.Object> map, boolean containsExplodeModifier, UriTemplate.CompositeOutput compositeOutput)
      Expand the template of a composite map property.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • COMPOSITE_NON_EXPLODE_JOINER

        private static final java.lang.String COMPOSITE_NON_EXPLODE_JOINER
        See Also:
        Constant Field Values
    • Constructor Detail

      • UriTemplate

        public UriTemplate()
    • Method Detail

      • getMap

        private static java.util.Map<java.lang.String,​java.lang.Object> getMap​(java.lang.Object obj)
        Constructs a new Map<String, Object> from an Object.

        There are no null values in the returned map.

      • expand

        public static java.lang.String expand​(java.lang.String baseUrl,
                                              java.lang.String uriTemplate,
                                              java.lang.Object parameters,
                                              boolean addUnusedParamsAsQueryParams)
        Expands templates in a URI template that is relative to a base URL.

        If the URI template starts with a "/" the raw path from the base URL is stripped out. If the URI template is a full URL then it is used instead of the base URL.

        Supports Level 1 templates and all Level 4 composite templates as described in: RFC 6570.

        Parameters:
        baseUrl - The base URL which the URI component is relative to.
        uriTemplate - URI component. It may contain one or more sequences of the form "{name}", where "name" must be a key in variableMap.
        parameters - an object with parameters designated by Key annotations. If the template has no variable references, parameters may be null.
        addUnusedParamsAsQueryParams - If true then parameters that do not match the template are appended to the expanded template as query parameters.
        Returns:
        The expanded template
        Since:
        1.7
      • expand

        public static java.lang.String expand​(java.lang.String pathUri,
                                              java.lang.Object parameters,
                                              boolean addUnusedParamsAsQueryParams)
        Expands templates in a URI.

        Supports Level 1 templates and all Level 4 composite templates as described in: RFC 6570.

        Parameters:
        pathUri - URI component. It may contain one or more sequences of the form "{name}", where "name" must be a key in variableMap
        parameters - an object with parameters designated by Key annotations. If the template has no variable references, parameters may be null.
        addUnusedParamsAsQueryParams - If true then parameters that do not match the template are appended to the expanded template as query parameters.
        Returns:
        The expanded template
        Since:
        1.6
      • getListPropertyValue

        private static java.lang.String getListPropertyValue​(java.lang.String varName,
                                                             java.util.Iterator<?> iterator,
                                                             boolean containsExplodeModifier,
                                                             UriTemplate.CompositeOutput compositeOutput)
        Expand the template of a composite list property. Eg: If d := ["red", "green", "blue"] then {/d*} is expanded to "/red/green/blue"
        Parameters:
        varName - The name of the variable the value corresponds to. Eg: "d"
        iterator - The iterator over list values. Eg: ["red", "green", "blue"]
        containsExplodeModifier - Set to true if the template contains the explode modifier "*"
        compositeOutput - An instance of CompositeOutput. Contains information on how the expansion should be done
        Returns:
        The expanded list template
        Throws:
        java.lang.IllegalArgumentException - if the required list path parameter is empty
      • getMapPropertyValue

        private static java.lang.String getMapPropertyValue​(java.lang.String varName,
                                                            java.util.Map<java.lang.String,​java.lang.Object> map,
                                                            boolean containsExplodeModifier,
                                                            UriTemplate.CompositeOutput compositeOutput)
        Expand the template of a composite map property. Eg: If d := [("semi", ";"),("dot", "."),("comma", ",")] then {/d*} is expanded to "/semi=%3B/dot=./comma=%2C"
        Parameters:
        varName - The name of the variable the value corresponds to. Eg: "d"
        map - The map property value. Eg: [("semi", ";"),("dot", "."),("comma", ",")]
        containsExplodeModifier - Set to true if the template contains the explode modifier "*"
        compositeOutput - An instance of CompositeOutput. Contains information on how the expansion should be done
        Returns:
        The expanded map template
        Throws:
        java.lang.IllegalArgumentException - if the required list path parameter is map