Resolve all expressions using the variable value substitutions provided. Variable values will be pct-encoded, if they are not already. @param variables containing the variable values to use when resolving expressions. @return a new Request Template with all of the variables resolved.
(Map<String, ?> variables)
| 180 | * @return a new Request Template with all of the variables resolved. |
| 181 | */ |
| 182 | public RequestTemplate resolve(Map<String, ?> variables) { |
| 183 | |
| 184 | StringBuilder uri = new StringBuilder(); |
| 185 | |
| 186 | /* create a new template form this one, but explicitly */ |
| 187 | RequestTemplate resolved = RequestTemplate.from(this); |
| 188 | |
| 189 | if (this.uriTemplate == null) { |
| 190 | /* create a new uri template using the default root */ |
| 191 | this.uriTemplate = UriTemplate.create("", !this.decodeSlash, this.charset); |
| 192 | } |
| 193 | |
| 194 | String expanded = this.uriTemplate.expand(variables); |
| 195 | if (expanded != null) { |
| 196 | uri.append(expanded); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * for simplicity, combine the queries into the uri and use the resulting uri to seed the |
| 201 | * resolved template. |
| 202 | */ |
| 203 | if (!this.queries.isEmpty()) { |
| 204 | /* |
| 205 | * since we only want to keep resolved query values, reset any queries on the resolved copy |
| 206 | */ |
| 207 | resolved.queries(Collections.emptyMap()); |
| 208 | StringBuilder query = new StringBuilder(); |
| 209 | Iterator<QueryTemplate> queryTemplates = this.queries.values().iterator(); |
| 210 | |
| 211 | while (queryTemplates.hasNext()) { |
| 212 | QueryTemplate queryTemplate = queryTemplates.next(); |
| 213 | String queryExpanded = queryTemplate.expand(variables); |
| 214 | if (Util.isNotBlank(queryExpanded)) { |
| 215 | query.append(queryExpanded); |
| 216 | if (queryTemplates.hasNext()) { |
| 217 | query.append("&"); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | String queryString = query.toString(); |
| 223 | if (!queryString.isEmpty()) { |
| 224 | Matcher queryMatcher = QUERY_STRING_PATTERN.matcher(uri); |
| 225 | if (queryMatcher.find()) { |
| 226 | /* the uri already has a query, so any additional queries should be appended */ |
| 227 | uri.append("&"); |
| 228 | } else { |
| 229 | uri.append("?"); |
| 230 | } |
| 231 | uri.append(queryString); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /* add the uri to result */ |
| 236 | resolved.uri(uri.toString()); |
| 237 | |
| 238 | /* headers */ |
| 239 | if (!this.headers.isEmpty()) { |