001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.client2; 014 015import static org.apache.juneau.parser.InputStreamParser.*; 016import static org.apache.juneau.rest.client2.RestClient.*; 017import static org.apache.juneau.BeanTraverseContext.*; 018import static org.apache.juneau.httppart.HttpPartType.*; 019import static org.apache.juneau.internal.StringUtils.*; 020import static org.apache.juneau.internal.ClassUtils.*; 021import static org.apache.juneau.serializer.OutputStreamSerializer.*; 022import static org.apache.juneau.serializer.WriterSerializer.*; 023import static org.apache.juneau.oapi.OpenApiCommon.*; 024import static org.apache.juneau.uon.UonSerializer.*; 025 026import java.io.*; 027import java.lang.annotation.*; 028import java.lang.reflect.*; 029import java.net.*; 030import java.net.URI; 031import java.util.*; 032import java.util.concurrent.*; 033import java.util.function.*; 034import java.util.logging.*; 035 036import javax.net.ssl.*; 037 038import org.apache.http.*; 039import org.apache.http.auth.*; 040import org.apache.http.client.*; 041import org.apache.http.client.CookieStore; 042import org.apache.http.client.config.*; 043import org.apache.http.client.entity.*; 044import org.apache.http.config.*; 045import org.apache.http.conn.*; 046import org.apache.http.conn.routing.*; 047import org.apache.http.conn.socket.*; 048import org.apache.http.conn.util.*; 049import org.apache.http.cookie.*; 050import org.apache.http.impl.client.*; 051import org.apache.http.impl.conn.*; 052import org.apache.http.protocol.*; 053import org.apache.juneau.*; 054import org.apache.juneau.annotation.*; 055import org.apache.juneau.html.*; 056import org.apache.juneau.http.*; 057import org.apache.juneau.http.header.*; 058import org.apache.juneau.httppart.*; 059import org.apache.juneau.internal.*; 060import org.apache.juneau.json.*; 061import org.apache.juneau.marshall.*; 062import org.apache.juneau.msgpack.*; 063import org.apache.juneau.oapi.*; 064import org.apache.juneau.parser.*; 065import org.apache.juneau.parser.ParseException; 066import org.apache.juneau.plaintext.*; 067import org.apache.juneau.reflect.*; 068import org.apache.juneau.serializer.*; 069import org.apache.juneau.svl.*; 070import org.apache.juneau.uon.*; 071import org.apache.juneau.urlencoding.*; 072import org.apache.juneau.xml.*; 073 074/** 075 * Builder class for the {@link RestClient} class. 076 * 077 * <p> 078 * Instances of this class are created by the following methods: 079 * <ul class='javatree'> 080 * <li class='jc'>{@link RestClient} 081 * <ul> 082 * <li class='jm'>{@link RestClient#create() create()} - Create from scratch. 083 * <li class='jm'>{@link RestClient#builder() builder()} - Copy settings from an existing client. 084 * </ul> 085 * </ul> 086 * 087 * <p> 088 * Refer to the {@link RestClient} javadocs for information on using this class. 089 * 090 * <ul class='seealso'> 091 * <li class='link'>{@doc juneau-rest-client} 092 * </ul> 093 */ 094@FluentSetters(ignore={"beanMapPutReturnsOldValue","example","exampleJson"}) 095public class RestClientBuilder extends BeanContextBuilder { 096 097 private HttpClientBuilder httpClientBuilder; 098 private CloseableHttpClient httpClient; 099 private HttpClientConnectionManager httpClientConnectionManager; 100 private boolean pooled; 101 102 /** 103 * Constructor. 104 * @param ps 105 * Initial configuration properties for this builder. 106 */ 107 protected RestClientBuilder(PropertyStore ps) { 108 super(ps); 109 HttpClientBuilder httpClientBuilder = peek(HttpClientBuilder.class, RESTCLIENT_httpClientBuilder); 110 this.httpClientBuilder = httpClientBuilder != null ? httpClientBuilder : getHttpClientBuilder(); 111 } 112 113 /** 114 * No-arg constructor. 115 * 116 * <p> 117 * Provided so that this class can be easily subclassed. 118 */ 119 protected RestClientBuilder() { 120 this(null); 121 } 122 123 @Override /* ContextBuilder */ 124 public RestClient build() { 125 set(RESTCLIENT_httpClient, getHttpClient()); 126 set(RESTCLIENT_httpClientBuilder, getHttpClientBuilder()); 127 return new RestClient(getPropertyStore()); 128 } 129 130 @Override /* ContextBuilder */ 131 public <T extends Context> T build(Class<T> c) { 132 set(RESTCLIENT_httpClient, getHttpClient()); 133 set(RESTCLIENT_httpClientBuilder, getHttpClientBuilder()); 134 return super.build(c); 135 } 136 137 //------------------------------------------------------------------------------------------------------------------ 138 // Convenience marshalling support methods. 139 //------------------------------------------------------------------------------------------------------------------ 140 141 /** 142 * Convenience method for specifying JSON as the marshalling transmission media type. 143 * 144 * <p> 145 * {@link JsonSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 146 * <ul> 147 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 148 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 149 * </ul> 150 * <p> 151 * {@link JsonParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 152 * <ul> 153 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 154 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 155 * </ul> 156 * <p> 157 * <c>Accept</c> request header will be set to <js>"application/json"</js> unless overridden 158 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 159 * <p> 160 * <c>Content-Type</c> request header will be set to <js>"application/json"</js> unless overridden 161 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 162 * <p> 163 * Can be combined with other marshaller setters such as {@link #xml()} to provide support for multiple languages. 164 * <ul> 165 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 166 * last-enabled language if the headers are not set. 167 * </ul> 168 * <p> 169 * Identical to calling <c>serializer(JsonSerializer.<jk>class</jk>).parser(JsonParser.<jk>class</jk>)</c>. 170 * 171 * <h5 class='section'>Example:</h5> 172 * <p class='bcode w800'> 173 * <jc>// Construct a client that uses JSON marshalling.</jc> 174 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().json().build(); 175 * </p> 176 * 177 * @return This object (for method chaining). 178 */ 179 @FluentSetter 180 public RestClientBuilder json() { 181 return serializer(JsonSerializer.class).parser(JsonParser.class); 182 } 183 184 /** 185 * Convenience method for specifying Simplified JSON as the marshalling transmission media type. 186 * 187 * <p> 188 * Simplified JSON is typically useful for automated tests because you can do simple string comparison of results 189 * without having to escape lots of quotes. 190 * 191 * <p> 192 * {@link SimpleJsonSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 193 * <ul> 194 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 195 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 196 * </ul> 197 * <p> 198 * {@link JsonParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 199 * <ul> 200 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 201 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 202 * </ul> 203 * <p> 204 * <c>Accept</c> request header will be set to <js>"application/json"</js> unless overridden 205 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 206 * <p> 207 * <c>Content-Type</c> request header will be set to <js>"application/json+simple"</js> unless overridden 208 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 209 * <p> 210 * Can be combined with other marshaller setters such as {@link #xml()} to provide support for multiple languages. 211 * <ul> 212 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 213 * last-enabled language if the headers are not set. 214 * </ul> 215 * <p> 216 * Identical to calling <c>serializer(SimpleJsonSerializer.<jk>class</jk>).parser(JsonParser.<jk>class</jk>)</c>. 217 * 218 * <h5 class='section'>Example:</h5> 219 * <p class='bcode w800'> 220 * <jc>// Construct a client that uses Simplified JSON marshalling.</jc> 221 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().simpleJson().build(); 222 * </p> 223 * 224 * @return This object (for method chaining). 225 */ 226 @FluentSetter 227 public RestClientBuilder simpleJson() { 228 return serializer(SimpleJsonSerializer.class).parser(SimpleJsonParser.class); 229 } 230 231 /** 232 * Convenience method for specifying XML as the marshalling transmission media type. 233 * 234 * <p> 235 * {@link XmlSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 236 * <ul> 237 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 238 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 239 * </ul> 240 * <p> 241 * {@link XmlParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 242 * <ul> 243 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 244 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 245 * </ul> 246 * <p> 247 * <c>Accept</c> request header will be set to <js>"text/xml"</js> unless overridden 248 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 249 * <p> 250 * <c>Content-Type</c> request header will be set to <js>"text/xml"</js> unless overridden 251 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 252 * <p> 253 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 254 * <ul> 255 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 256 * last-enabled language if the headers are not set. 257 * </ul> 258 * <p> 259 * Identical to calling <c>serializer(XmlSerializer.<jk>class</jk>).parser(XmlParser.<jk>class</jk>)</c>. 260 * 261 * <h5 class='section'>Example:</h5> 262 * <p class='bcode w800'> 263 * <jc>// Construct a client that uses XML marshalling.</jc> 264 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().xml().build(); 265 * </p> 266 * 267 * @return This object (for method chaining). 268 */ 269 @FluentSetter 270 public RestClientBuilder xml() { 271 return serializer(XmlSerializer.class).parser(XmlParser.class); 272 } 273 274 /** 275 * Convenience method for specifying HTML as the marshalling transmission media type. 276 * 277 * <p> 278 * POJOs are converted to HTML without any sort of doc wrappers. 279 * 280 * <p> 281 * {@link HtmlSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 282 * <ul> 283 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 284 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 285 * </ul> 286 * <p> 287 * {@link HtmlParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 288 * <ul> 289 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 290 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 291 * </ul> 292 * <p> 293 * <c>Accept</c> request header will be set to <js>"text/html"</js> unless overridden 294 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 295 * <p> 296 * <c>Content-Type</c> request header will be set to <js>"text/html"</js> unless overridden 297 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 298 * <p> 299 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 300 * <ul> 301 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 302 * last-enabled language if the headers are not set. 303 * </ul> 304 * <p> 305 * Identical to calling <c>serializer(HtmlSerializer.<jk>class</jk>).parser(HtmlParser.<jk>class</jk>)</c>. 306 * 307 * <h5 class='section'>Example:</h5> 308 * <p class='bcode w800'> 309 * <jc>// Construct a client that uses HTML marshalling.</jc> 310 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().html().build(); 311 * </p> 312 * 313 * @return This object (for method chaining). 314 */ 315 @FluentSetter 316 public RestClientBuilder html() { 317 return serializer(HtmlSerializer.class).parser(HtmlParser.class); 318 } 319 320 /** 321 * Convenience method for specifying HTML DOC as the marshalling transmission media type. 322 * 323 * <p> 324 * POJOs are converted to fully renderable HTML pages. 325 * 326 * <p> 327 * {@link HtmlDocSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 328 * <ul> 329 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 330 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 331 * </ul> 332 * <p> 333 * {@link HtmlParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 334 * <ul> 335 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 336 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 337 * </ul> 338 * <p> 339 * <c>Accept</c> request header will be set to <js>"text/html"</js> unless overridden 340 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 341 * <p> 342 * <c>Content-Type</c> request header will be set to <js>"text/html"</js> unless overridden 343 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 344 * <p> 345 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 346 * <ul> 347 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 348 * last-enabled language if the headers are not set. 349 * </ul> 350 * <p> 351 * Identical to calling <c>serializer(HtmlDocSerializer.<jk>class</jk>).parser(HtmlParser.<jk>class</jk>)</c>. 352 * 353 * <h5 class='section'>Example:</h5> 354 * <p class='bcode w800'> 355 * <jc>// Construct a client that uses HTML Doc marshalling.</jc> 356 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().htmlDoc().build(); 357 * </p> 358 * 359 * @return This object (for method chaining). 360 */ 361 @FluentSetter 362 public RestClientBuilder htmlDoc() { 363 return serializer(HtmlDocSerializer.class).parser(HtmlParser.class); 364 } 365 366 /** 367 * Convenience method for specifying Stripped HTML DOC as the marshalling transmission media type. 368 * 369 * <p> 370 * Same as {@link #htmlDoc()} but without the header and body tags and page title and description. 371 * 372 * <p> 373 * {@link HtmlStrippedDocSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 374 * <ul> 375 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 376 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 377 * </ul> 378 * <p> 379 * {@link HtmlParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 380 * <ul> 381 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 382 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 383 * </ul> 384 * <p> 385 * <c>Accept</c> request header will be set to <js>"text/html+stripped"</js> unless overridden 386 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 387 * <p> 388 * <c>Content-Type</c> request header will be set to <js>"text/html+stripped"</js> unless overridden 389 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 390 * <p> 391 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 392 * <ul> 393 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 394 * last-enabled language if the headers are not set. 395 * </ul> 396 * <p> 397 * Identical to calling <c>serializer(HtmlStrippedDocSerializer.<jk>class</jk>).parser(HtmlParser.<jk>class</jk>)</c>. 398 * 399 * <h5 class='section'>Example:</h5> 400 * <p class='bcode w800'> 401 * <jc>// Construct a client that uses HTML Stripped Doc marshalling.</jc> 402 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().htmlStrippedDoc().build(); 403 * </p> 404 * 405 * @return This object (for method chaining). 406 */ 407 @FluentSetter 408 public RestClientBuilder htmlStrippedDoc() { 409 return serializer(HtmlStrippedDocSerializer.class).parser(HtmlParser.class); 410 } 411 412 /** 413 * Convenience method for specifying Plain Text as the marshalling transmission media type. 414 * 415 * <p> 416 * Plain text marshalling typically only works on simple POJOs that can be converted to and from strings using 417 * swaps, swap methods, etc... 418 * 419 * <p> 420 * {@link PlainTextSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 421 * <ul> 422 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 423 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 424 * </ul> 425 * <p> 426 * {@link PlainTextParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 427 * <ul> 428 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 429 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 430 * </ul> 431 * <p> 432 * <c>Accept</c> request header will be set to <js>"text/plain"</js> unless overridden 433 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 434 * <p> 435 * <c>Content-Type</c> request header will be set to <js>"text/plain"</js> unless overridden 436 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 437 * <p> 438 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 439 * <ul> 440 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 441 * last-enabled language if the headers are not set. 442 * </ul> 443 * <p> 444 * Identical to calling <c>serializer(PlainTextSerializer.<jk>class</jk>).parser(PlainTextParser.<jk>class</jk>)</c>. 445 * 446 * <h5 class='section'>Example:</h5> 447 * <p class='bcode w800'> 448 * <jc>// Construct a client that uses Plain Text marshalling.</jc> 449 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().plainText().build(); 450 * </p> 451 * 452 * @return This object (for method chaining). 453 */ 454 @FluentSetter 455 public RestClientBuilder plainText() { 456 return serializer(PlainTextSerializer.class).parser(PlainTextParser.class); 457 } 458 459 /** 460 * Convenience method for specifying MessagePack as the marshalling transmission media type. 461 * 462 * <p> 463 * MessagePack is a binary equivalent to JSON that takes up considerably less space than JSON. 464 * 465 * <p> 466 * {@link MsgPackSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 467 * <ul> 468 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 469 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 470 * </ul> 471 * <p> 472 * {@link MsgPackParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 473 * <ul> 474 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 475 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 476 * </ul> 477 * <p> 478 * <c>Accept</c> request header will be set to <js>"octal/msgpack"</js> unless overridden 479 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 480 * <p> 481 * <c>Content-Type</c> request header will be set to <js>"octal/msgpack"</js> unless overridden 482 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 483 * <p> 484 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 485 * <ul> 486 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 487 * last-enabled language if the headers are not set. 488 * </ul> 489 * <p> 490 * Identical to calling <c>serializer(MsgPackSerializer.<jk>class</jk>).parser(MsgPackParser.<jk>class</jk>)</c>. 491 * 492 * <h5 class='section'>Example:</h5> 493 * <p class='bcode w800'> 494 * <jc>// Construct a client that uses MessagePack marshalling.</jc> 495 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().msgPack().build(); 496 * </p> 497 * 498 * @return This object (for method chaining). 499 */ 500 @FluentSetter 501 public RestClientBuilder msgPack() { 502 return serializer(MsgPackSerializer.class).parser(MsgPackParser.class); 503 } 504 505 /** 506 * Convenience method for specifying UON as the marshalling transmission media type. 507 * 508 * <p> 509 * UON is Url-Encoding Object notation that is equivalent to JSON but suitable for transmission as URL-encoded 510 * query and form post values. 511 * 512 * <p> 513 * {@link UonSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 514 * <ul> 515 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 516 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 517 * </ul> 518 * <p> 519 * {@link UonParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 520 * <ul> 521 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 522 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 523 * </ul> 524 * <p> 525 * <c>Accept</c> request header will be set to <js>"text/uon"</js> unless overridden 526 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 527 * <p> 528 * <c>Content-Type</c> request header will be set to <js>"text/uon"</js> unless overridden 529 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 530 * <p> 531 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 532 * <ul> 533 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 534 * last-enabled language if the headers are not set. 535 * </ul> 536 * <p> 537 * Identical to calling <c>serializer(UonSerializer.<jk>class</jk>).parser(UonParser.<jk>class</jk>)</c>. 538 * 539 * <h5 class='section'>Example:</h5> 540 * <p class='bcode w800'> 541 * <jc>// Construct a client that uses UON marshalling.</jc> 542 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().uon().build(); 543 * </p> 544 * 545 * @return This object (for method chaining). 546 */ 547 @FluentSetter 548 public RestClientBuilder uon() { 549 return serializer(UonSerializer.class).parser(UonParser.class); 550 } 551 552 /** 553 * Convenience method for specifying URL-Encoding as the marshalling transmission media type. 554 * 555 * <p> 556 * {@link UrlEncodingSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 557 * <ul> 558 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 559 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 560 * <li>This serializer is NOT used when using the {@link RestRequest#formData(String, Object)} (and related) methods for constructing 561 * the request body. Instead, the part serializer specified via {@link #partSerializer(Class)} is used. 562 * </ul> 563 * <p> 564 * {@link UrlEncodingParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 565 * <ul> 566 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 567 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 568 * </ul> 569 * <p> 570 * <c>Accept</c> request header will be set to <js>"application/x-www-form-urlencoded"</js> unless overridden 571 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 572 * <p> 573 * <c>Content-Type</c> request header will be set to <js>"application/x-www-form-urlencoded"</js> unless overridden 574 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 575 * <p> 576 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 577 * <ul> 578 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 579 * last-enabled language if the headers are not set. 580 * </ul> 581 * <p> 582 * Identical to calling <c>serializer(UrlEncodingSerializer.<jk>class</jk>).parser(UrlEncodingParser.<jk>class</jk>)</c>. 583 * 584 * <h5 class='section'>Example:</h5> 585 * <p class='bcode w800'> 586 * <jc>// Construct a client that uses URL-Encoded marshalling.</jc> 587 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().urlEnc().build(); 588 * </p> 589 * 590 * @return This object (for method chaining). 591 */ 592 @FluentSetter 593 public RestClientBuilder urlEnc() { 594 return serializer(UrlEncodingSerializer.class).parser(UrlEncodingParser.class); 595 } 596 597 /** 598 * Convenience method for specifying OpenAPI as the marshalling transmission media type. 599 * 600 * <p> 601 * OpenAPI is a language that allows serialization to formats that use {@link HttpPartSchema} objects to describe their structure. 602 * 603 * <p> 604 * {@link OpenApiSerializer} will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 605 * <ul> 606 * <li>The serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 607 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 608 * <li>Typically the {@link RestRequest#body(Object, HttpPartSchema)} method will be used to specify the body of the request with the 609 * schema describing it's structure. 610 * </ul> 611 * <p> 612 * {@link OpenApiParser} will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 613 * <ul> 614 * <li>The parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 615 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 616 * <li>Typically the {@link RestResponseBody#schema(HttpPartSchema)} method will be used to specify the structure of the response body. 617 * </ul> 618 * <p> 619 * <c>Accept</c> request header will be set to <js>"text/openapi"</js> unless overridden 620 * by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)}. 621 * <p> 622 * <c>Content-Type</c> request header will be set to <js>"text/openapi"</js> unless overridden 623 * by {@link #header(String,Object)} or {@link #contentType(Object)}, or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)}. 624 * <p> 625 * Can be combined with other marshaller setters such as {@link #json()} to provide support for multiple languages. 626 * <ul> 627 * <li>When multiple languages are supported, the <c>Accept</c> and <c>Content-Type</c> headers control which marshallers are used, or uses the 628 * last-enabled language if the headers are not set. 629 * </ul> 630 * <p> 631 * Identical to calling <c>serializer(OpenApiSerializer.<jk>class</jk>).parser(OpenApiParser.<jk>class</jk>)</c>. 632 * 633 * <h5 class='section'>Example:</h5> 634 * <p class='bcode w800'> 635 * <jc>// Construct a client that uses OpenAPI marshalling.</jc> 636 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().openApi().build(); 637 * </p> 638 * 639 * @return This object (for method chaining). 640 */ 641 @FluentSetter 642 public RestClientBuilder openApi() { 643 return serializer(OpenApiSerializer.class).parser(OpenApiParser.class); 644 } 645 646 /** 647 * Convenience method for specifying all available transmission types. 648 * 649 * <p> 650 * All basic Juneau serializers will be used to serialize POJOs to request bodies unless overridden per request via {@link RestRequest#serializer(Serializer)}. 651 * <ul> 652 * <li>The serializers can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 653 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 654 * </ul> 655 * <p> 656 * All basic Juneau parsers will be used to parse POJOs from response bodies unless overridden per request via {@link RestRequest#parser(Parser)}. 657 * <ul> 658 * <li>The parsers can be configured using any of the parser property setters (e.g. {@link #strict()}), 659 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 660 * </ul> 661 * <p> 662 * <c>Accept</c> request header must be set by {@link #header(String,Object)} or {@link #accept(Object)}, or per-request 663 * via {@link RestRequest#header(String,Object)} or {@link RestRequest#accept(Object)} in order for the correct parser to be selected. 664 * <p> 665 * <c>Content-Type</c> request header must be set by {@link #header(String,Object)} or {@link #contentType(Object)}, 666 * or per-request via {@link RestRequest#header(String,Object)} or {@link RestRequest#contentType(Object)} in order for the correct serializer to be selected. 667 * <p> 668 * Similar to calling <c>json().simpleJson().html().xml().uon().urlEnc().openApi().msgPack().plainText()</c>. 669 * 670 * <h5 class='section'>Example:</h5> 671 * <p class='bcode w800'> 672 * <jc>// Construct a client that uses universal marshalling.</jc> 673 * RestClient <jv>client</jv> = RestClient.<jsm>create</jsm>().universal().build(); 674 * </p> 675 * 676 * @return This object (for method chaining). 677 */ 678 @SuppressWarnings("unchecked") 679 public RestClientBuilder universal() { 680 return 681 serializers( 682 JsonSerializer.class, 683 SimpleJsonSerializer.class, 684 HtmlSerializer.class, 685 XmlSerializer.class, 686 UonSerializer.class, 687 UrlEncodingSerializer.class, 688 OpenApiSerializer.class, 689 MsgPackSerializer.class, 690 PlainTextSerializer.class 691 ) 692 .parsers( 693 JsonParser.class, 694 SimpleJsonParser.class, 695 XmlParser.class, 696 HtmlParser.class, 697 UonParser.class, 698 UrlEncodingParser.class, 699 OpenApiParser.class, 700 MsgPackParser.class, 701 PlainTextParser.class 702 ); 703 } 704 705 //------------------------------------------------------------------------------------------------------------------ 706 // HttpClientBuilder 707 //------------------------------------------------------------------------------------------------------------------ 708 709 /** 710 * Creates an instance of an {@link HttpClientBuilder} to be used to create the {@link HttpClient}. 711 * 712 * <p> 713 * Subclasses can override this method to provide their own client builder. 714 * The builder can also be specified using the {@link #httpClientBuilder(HttpClientBuilder)} method. 715 * 716 * <h5 class='section'>Example:</h5> 717 * <p class='bcode w800'> 718 * <jc>// A RestClientBuilder that provides it's own customized HttpClientBuilder.</jc> 719 * <jk>public class</jk> MyRestClientBuilder <jk>extends</jk> RestClientBuilder { 720 * <ja>@Override</ja> 721 * <jk>protected</jk> HttpClientBuilder createHttpClientBuilder() { 722 * <jk>return</jk> HttpClientBuilder.<jsm>create</jsm>(); 723 * } 724 * } 725 * 726 * <jc>// Instantiate.</jc> 727 * RestClient <jv>client</jv> = <jk>new</jk> MyRestClientBuilder().build(); 728 * </p> 729 * 730 * @return The HTTP client builder to use to create the HTTP client. 731 */ 732 protected HttpClientBuilder createHttpClientBuilder() { 733 return HttpClientBuilder.create(); 734 } 735 736 /** 737 * Sets the {@link HttpClientBuilder} that will be used to create the {@link HttpClient} used by {@link RestClient}. 738 * 739 * <p> 740 * This can be used to bypass the builder created by {@link #createHttpClientBuilder()} method. 741 * 742 * <h5 class='section'>Example:</h5> 743 * <p class='bcode w800'> 744 * <jc>// Construct a client that uses a customized HttpClientBuilder.</jc> 745 * RestClient <jv>client</jv> = RestClient 746 * .<jsm>create</jsm>() 747 * .httpClientBuilder(HttpClientBuilder.<jsm>create</jsm>()) 748 * .build(); 749 * </p> 750 * 751 * @param value The {@link HttpClientBuilder} that will be used to create the {@link HttpClient} used by {@link RestClient}. 752 * @return This object (for method chaining). 753 */ 754 @FluentSetter 755 public RestClientBuilder httpClientBuilder(HttpClientBuilder value) { 756 this.httpClientBuilder = value; 757 return this; 758 } 759 760 final HttpClientBuilder getHttpClientBuilder() { 761 if (httpClientBuilder == null) 762 httpClientBuilder = createHttpClientBuilder(); 763 return httpClientBuilder; 764 } 765 766 //------------------------------------------------------------------------------------------------------------------ 767 // HttpClient 768 //------------------------------------------------------------------------------------------------------------------ 769 770 /** 771 * Creates an instance of an {@link HttpClient} to be used to handle all HTTP communications with the target server. 772 * 773 * <p> 774 * This HTTP client is used when the HTTP client is not specified through one of the constructors or the 775 * {@link #httpClient(CloseableHttpClient)} method. 776 * 777 * <p> 778 * Subclasses can override this method to provide specially-configured HTTP clients to handle stuff such as 779 * SSL/TLS certificate handling, authentication, etc. 780 * 781 * <p> 782 * The default implementation returns an instance of {@link HttpClient} using the client builder returned by 783 * {@link #createHttpClientBuilder()}. 784 * 785 * <h5 class='section'>Example:</h5> 786 * <p class='bcode w800'> 787 * <jc>// A RestClientBuilder that provides it's own customized HttpClient.</jc> 788 * <jk>public class</jk> MyRestClientBuilder <jk>extends</jk> RestClientBuilder { 789 * <ja>@Override</ja> 790 * <jk>protected</jk> HttpClientBuilder createHttpClient() { 791 * <jk>return</jk> HttpClientBuilder.<jsm>create</jsm>().build(); 792 * } 793 * } 794 * 795 * <jc>// Instantiate.</jc> 796 * RestClient <jv>client</jv> = <jk>new</jk> MyRestClientBuilder().build(); 797 * </p> 798 * 799 * @return The HTTP client to use. 800 */ 801 protected CloseableHttpClient createHttpClient() { 802 // Don't call createConnectionManager() if RestClient.setConnectionManager() was called. 803 if (httpClientConnectionManager == null) 804 httpClientBuilder.setConnectionManager(createConnectionManager()); 805 else 806 httpClientBuilder.setConnectionManager(httpClientConnectionManager); 807 return httpClientBuilder.build(); 808 } 809 810 /** 811 * Sets the {@link HttpClient} to be used to handle all HTTP communications with the target server. 812 * 813 * <p> 814 * This can be used to bypass the client created by {@link #createHttpClient()} method. 815 * 816 * <h5 class='section'>Example:</h5> 817 * <p class='bcode w800'> 818 * <jc>// Construct a client that uses a customized HttpClient.</jc> 819 * RestClient <jv>client</jv> = RestClient 820 * .<jsm>create</jsm>() 821 * .httpClient(HttpClientBuilder.<jsm>create</jsm>().build()) 822 * .build(); 823 * </p> 824 * 825 * @param value The {@link HttpClient} to be used to handle all HTTP communications with the target server. 826 * @return This object (for method chaining). 827 */ 828 @FluentSetter 829 public RestClientBuilder httpClient(CloseableHttpClient value) { 830 this.httpClient = value; 831 return this; 832 } 833 834 final CloseableHttpClient getHttpClient() { 835 return httpClient != null ? httpClient : createHttpClient(); 836 } 837 838 //------------------------------------------------------------------------------------------------------------------ 839 // Logging. 840 //------------------------------------------------------------------------------------------------------------------ 841 842 /** 843 * <i><l>RestClient</l> configuration property: </i> Logger. 844 * 845 * <p> 846 * Specifies the logger to use for logging. 847 * 848 * <p> 849 * If not specified, uses the following logger: 850 * <p class='bcode w800'> 851 * Logger.<jsm>getLogger</jsm>(RestClient.<jk>class</jk>.getName()); 852 * </p> 853 * 854 * <h5 class='section'>Example:</h5> 855 * <p class='bcode w800'> 856 * <jc>// Construct a client that logs messages to a special logger.</jc> 857 * RestClient <jv>client</jv> = RestClient 858 * .<jsm>create</jsm>() 859 * .logger(Logger.<jsm>getLogger</jsm>(<js>"MyLogger"</js>)) <jc>// Log to MyLogger logger.</jc> 860 * .logToConsole() <jc>// Also log to console.</jc> 861 * .logRequests(<jsf>FULL</jsf>, <jsf>WARNING</jsf>) <jc>// Log requests with full detail at WARNING level.</jc> 862 * .build(); 863 * </p> 864 * 865 * <ul class='seealso'> 866 * <li class='jf'>{@link RestClient#RESTCLIENT_logger} 867 * </ul> 868 * 869 * @param value The logger to use for logging. 870 * @return This object (for method chaining). 871 */ 872 @FluentSetter 873 public RestClientBuilder logger(Logger value) { 874 return set(RESTCLIENT_logger, value); 875 } 876 877 /** 878 * <i><l>RestClient</l> configuration property: </i> Log to console. 879 * 880 * <p> 881 * Specifies to log messages to the console. 882 * 883 * <h5 class='section'>Example:</h5> 884 * <p class='bcode w800'> 885 * <jc>// Construct a client that logs messages to a special logger.</jc> 886 * RestClient <jv>client</jv> = RestClient 887 * .<jsm>create</jsm>() 888 * .logToConsole() 889 * .logRequests(<jsf>FULL</jsf>, <jsf>INFO</jsf>) <jc>// Level is ignored when logging to console.</jc> 890 * .build(); 891 * </p> 892 * 893 * <ul class='seealso'> 894 * <li class='jf'>{@link RestClient#RESTCLIENT_logToConsole} 895 * </ul> 896 * 897 * @return This object (for method chaining). 898 */ 899 @FluentSetter 900 public RestClientBuilder logToConsole() { 901 return set(RESTCLIENT_logToConsole, true); 902 } 903 904 /** 905 * <i><i><l>RestClient</l> configuration property: </i></i> Log requests. 906 * 907 * <p> 908 * Causes requests/responses to be logged at the specified log level at the end of the request. 909 * 910 * <p> 911 * <jsf>SIMPLE</jsf> detail produces a log message like the following: 912 * <p class='bcode w800 console'> 913 * POST http://localhost:10000/testUrl, HTTP/1.1 200 OK 914 * </p> 915 * 916 * <p> 917 * <jsf>FULL</jsf> detail produces a log message like the following: 918 * <p class='bcode w800 console'> 919 * === HTTP Call (outgoing) ======================================================= 920 * === REQUEST === 921 * POST http://localhost:10000/testUrl 922 * ---request headers--- 923 * Debug: true 924 * No-Trace: true 925 * Accept: application/json 926 * ---request entity--- 927 * Content-Type: application/json 928 * ---request content--- 929 * {"foo":"bar","baz":123} 930 * === RESPONSE === 931 * HTTP/1.1 200 OK 932 * ---response headers--- 933 * Content-Type: application/json;charset=utf-8 934 * Content-Length: 21 935 * Server: Jetty(8.1.0.v20120127) 936 * ---response content--- 937 * {"message":"OK then"} 938 * === END ======================================================================== 939 * </p> 940 * 941 * <p> 942 * By default, the message is logged to the default logger. It can be logged to a different logger via the 943 * {@link #logger(Logger)} method or logged to the console using the 944 * {@link #logToConsole()} method. 945 * 946 * <ul class='seealso'> 947 * <li class='jf'>{@link RestClient#RESTCLIENT_logRequests} 948 * <li class='jf'>{@link RestClient#RESTCLIENT_logRequestsLevel} 949 * <li class='jf'>{@link RestClient#RESTCLIENT_logRequestsPredicate} 950 * </ul> 951 * 952 * @param detail The detail level of logging. 953 * @param level The log level. 954 * @param test A predicate to use per-request to see if the request should be logged. If <jk>null</jk>, always logs. 955 * @return This object (for method chaining). 956 */ 957 @FluentSetter 958 public RestClientBuilder logRequests(DetailLevel detail, Level level, BiPredicate<RestRequest,RestResponse> test) { 959 set(RESTCLIENT_logRequests, detail); 960 set(RESTCLIENT_logRequestsLevel, level); 961 set(RESTCLIENT_logRequestsPredicate, test); 962 return this; 963 } 964 965 //------------------------------------------------------------------------------------------------------------------ 966 // HttpClientConnectionManager methods. 967 //------------------------------------------------------------------------------------------------------------------ 968 969 /** 970 * Creates the {@link HttpClientConnectionManager} returned by {@link #createConnectionManager()}. 971 * 972 * <p> 973 * Subclasses can override this method to provide their own connection manager. 974 * 975 * <p> 976 * The default implementation returns an instance of a {@link PoolingHttpClientConnectionManager} if {@link #pooled()} 977 * was called or {@link BasicHttpClientConnectionManager} if not.. 978 * 979 * <h5 class='section'>Example:</h5> 980 * <p class='bcode w800'> 981 * <jc>// A RestClientBuilder that provides it's own customized HttpClientConnectionManager.</jc> 982 * <jk>public class</jk> MyRestClientBuilder <jk>extends</jk> RestClientBuilder { 983 * <ja>@Override</ja> 984 * <jk>protected</jk> HttpClientConnectionManager createConnectionManager() { 985 * <jk>return new</jk> PoolingHttpClientConnectionManager(); 986 * } 987 * } 988 * 989 * <jc>// Instantiate.</jc> 990 * RestClient <jv>client</jv> = <jk>new</jk> MyRestClientBuilder().build(); 991 * </p> 992 * 993 * @return The HTTP client builder to use to create the HTTP client. 994 */ 995 @SuppressWarnings("resource") 996 protected HttpClientConnectionManager createConnectionManager() { 997 return (pooled ? new PoolingHttpClientConnectionManager() : new BasicHttpClientConnectionManager()); 998 } 999 1000 /** 1001 * When called, the {@link #createConnectionManager()} method will return a {@link PoolingHttpClientConnectionManager} 1002 * instead of a {@link BasicHttpClientConnectionManager}. 1003 * 1004 * <h5 class='section'>Example:</h5> 1005 * <p class='bcode w800'> 1006 * <jc>// Construct a client that uses pooled connections.</jc> 1007 * RestClient <jv>client</jv> = RestClient 1008 * .<jsm>create</jsm>() 1009 * .pooled() 1010 * .build(); 1011 * </p> 1012 * 1013 * @return This object (for method chaining). 1014 */ 1015 @FluentSetter 1016 public RestClientBuilder pooled() { 1017 this.pooled = true; 1018 return this; 1019 } 1020 1021 /** 1022 * Set up this client to use BASIC auth. 1023 * 1024 * <h5 class='section'>Example:</h5> 1025 * <p class='bcode w800'> 1026 * <jc>// Construct a client that uses BASIC authentication.</jc> 1027 * RestClient <jv>client</jv> = RestClient 1028 * .<jsm>create</jsm>() 1029 * .basicAuth(<js>"http://localhost"</js>, 80, <js>"me"</js>, <js>"mypassword"</js>) 1030 * .build(); 1031 * </p> 1032 * 1033 * @param host The auth scope hostname. 1034 * @param port The auth scope port. 1035 * @param user The username. 1036 * @param pw The password. 1037 * @return This object (for method chaining). 1038 */ 1039 @FluentSetter 1040 public RestClientBuilder basicAuth(String host, int port, String user, String pw) { 1041 AuthScope scope = new AuthScope(host, port); 1042 Credentials up = new UsernamePasswordCredentials(user, pw); 1043 CredentialsProvider p = new BasicCredentialsProvider(); 1044 p.setCredentials(scope, up); 1045 defaultCredentialsProvider(p); 1046 return this; 1047 } 1048 1049 //----------------------------------------------------------------------------------------------------------------- 1050 // Headers 1051 //----------------------------------------------------------------------------------------------------------------- 1052 1053 /** 1054 * Sets a header on all requests. 1055 * 1056 * <h5 class='section'>Example:</h5> 1057 * <p class='bcode w800'> 1058 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1059 * 1060 * <jc>// Adds header "Foo: foo|bar" to all requests.</jc> 1061 * RestClient <jv>client</jv> = RestClient 1062 * .<jsm>create</jsm>() 1063 * .header(<js>"Foo"</js>, <jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>, <jv>myPartSerializer</jv>); 1064 * .build(); 1065 * </p> 1066 * 1067 * @param name The header name. 1068 * @param value The header value. 1069 * <ul> 1070 * <li>Can be any POJO. 1071 * <li>Converted to a string using the specified part serializer. 1072 * </ul> 1073 * @param schema The schema object that defines the format of the output. 1074 * <ul> 1075 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1076 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1077 * </ul> 1078 * @param serializer The serializer to use for serializing the value to a string. 1079 * <ul> 1080 * <li>If <jk>null</jk>, then the {@link HttpPartSerializer} defined on the client is used ({@link OpenApiSerializer} by default). 1081 * </ul> 1082 * @return This object (for method chaining). 1083 */ 1084 @FluentSetter 1085 public RestClientBuilder header(String name, Object value, HttpPartSchema schema, HttpPartSerializer serializer) { 1086 return headers(serializedHeader(name, value, serializer, schema)); 1087 } 1088 1089 /** 1090 * Sets a header with a dynamic value on all requests. 1091 * 1092 * <h5 class='section'>Example:</h5> 1093 * <p class='bcode w800'> 1094 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1095 * 1096 * <jc>// Adds header "Foo: foo|bar" to all requests.</jc> 1097 * RestClient <jv>client</jv> = RestClient 1098 * .<jsm>create</jsm>() 1099 * .header(<js>"Foo"</js>, ()-><jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>, <jv>myPartSerializer</jv>); 1100 * .build(); 1101 * </p> 1102 * 1103 * @param name The header name. 1104 * @param value The header value supplier. 1105 * <ul> 1106 * <li>Can be any POJO. 1107 * <li>Converted to a string using the specified part serializer. 1108 * </ul> 1109 * @param schema The schema object that defines the format of the output. 1110 * <ul> 1111 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1112 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1113 * </ul> 1114 * @param serializer The serializer to use for serializing the value to a string. 1115 * <ul> 1116 * <li>If <jk>null</jk>, then the {@link HttpPartSerializer} defined on the client is used ({@link OpenApiSerializer} by default). 1117 * </ul> 1118 * @return This object (for method chaining). 1119 */ 1120 @FluentSetter 1121 public RestClientBuilder header(String name, Supplier<?> value, HttpPartSchema schema, HttpPartSerializer serializer) { 1122 return headers(serializedHeader(name, value, serializer, schema)); 1123 } 1124 1125 /** 1126 * Sets a header on all requests. 1127 * 1128 * <h5 class='section'>Example:</h5> 1129 * <p class='bcode w800'> 1130 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1131 * 1132 * <jc>// Adds header "Foo: foo|bar" to all requests.</jc> 1133 * RestClient <jv>client</jv> = RestClient 1134 * .<jsm>create</jsm>() 1135 * .header(<js>"Foo"</js>, <jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>); 1136 * .build(); 1137 * </p> 1138 * 1139 * @param name The header name. 1140 * @param value The header value. 1141 * <ul> 1142 * <li>Can be any POJO. 1143 * <li>Converted to a string using the specified part serializer. 1144 * </ul> 1145 * @param schema The schema object that defines the format of the output. 1146 * <ul> 1147 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1148 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1149 * </ul> 1150 * @return This object (for method chaining). 1151 */ 1152 @FluentSetter 1153 public RestClientBuilder header(String name, Object value, HttpPartSchema schema) { 1154 return headers(serializedHeader(name, value, null, schema)); 1155 } 1156 1157 /** 1158 * Sets a header with a dynamic value on all requests. 1159 * 1160 * <h5 class='section'>Example:</h5> 1161 * <p class='bcode w800'> 1162 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1163 * 1164 * <jc>// Adds header "Foo: foo|bar" to all requests.</jc> 1165 * RestClient <jv>client</jv> = RestClient 1166 * .<jsm>create</jsm>() 1167 * .header(<js>"Foo"</js>, ()-><jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>); 1168 * .build(); 1169 * </p> 1170 * 1171 * @param name The header name. 1172 * @param value The header value supplier. 1173 * <ul> 1174 * <li>Can be any POJO. 1175 * <li>Converted to a string using the specified part serializer. 1176 * </ul> 1177 * @param schema The schema object that defines the format of the output. 1178 * <ul> 1179 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1180 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1181 * </ul> 1182 * @return This object (for method chaining). 1183 */ 1184 @FluentSetter 1185 public RestClientBuilder header(String name, Supplier<?> value, HttpPartSchema schema) { 1186 return headers(serializedHeader(name, value, null, schema)); 1187 } 1188 1189 /** 1190 * Sets a header on all requests. 1191 * 1192 * <h5 class='section'>Example:</h5> 1193 * <p class='bcode w800'> 1194 * RestClient <jv>client</jv> = RestClient 1195 * .<jsm>create</jsm>() 1196 * .header(<js>"Foo"</js>, <js>"bar"</js>); 1197 * .build(); 1198 * </p> 1199 * 1200 * <ul class='seealso'> 1201 * <li class='jf'>{@link RestClient#RESTCLIENT_headers} 1202 * </ul> 1203 * 1204 * @param name The header name. 1205 * @param value The header value. 1206 * <ul> 1207 * <li>Can be any POJO. 1208 * <li>Converted to a string using the specified part serializer. 1209 * </ul> 1210 * @return This object (for method chaining). 1211 */ 1212 @FluentSetter 1213 public RestClientBuilder header(String name, Object value) { 1214 return headers(serializedHeader(name, value, null, null)); 1215 } 1216 1217 /** 1218 * Sets a header with a dynamic value on all requests. 1219 * 1220 * <h5 class='section'>Example:</h5> 1221 * <p class='bcode w800'> 1222 * RestClient <jv>client</jv> = RestClient 1223 * .<jsm>create</jsm>() 1224 * .header(<js>"Foo"</js>, ()-><js>"bar"</js>); 1225 * .build(); 1226 * </p> 1227 * 1228 * <ul class='seealso'> 1229 * <li class='jf'>{@link RestClient#RESTCLIENT_headers} 1230 * </ul> 1231 * 1232 * @param name The header name. 1233 * @param value The header value supplier. 1234 * <ul> 1235 * <li>Can be any POJO. 1236 * <li>Converted to a string using the specified part serializer. 1237 * </ul> 1238 * @return This object (for method chaining). 1239 */ 1240 @FluentSetter 1241 public RestClientBuilder header(String name, Supplier<?> value) { 1242 return headers(serializedHeader(name, value, null, null)); 1243 } 1244 1245 /** 1246 * Sets a header on all requests. 1247 * 1248 * <h5 class='section'>Example:</h5> 1249 * <p class='bcode w800'> 1250 * RestClient <jv>client</jv> = RestClient 1251 * .<jsm>create</jsm>() 1252 * .header(BasicHeader.<jsm>of</jsm>(<js>"Foo"</js>, <js>"bar"</js>)) 1253 * .build(); 1254 * </p> 1255 * 1256 * @param header The header to set. 1257 * @return This object (for method chaining). 1258 */ 1259 @FluentSetter 1260 public RestClientBuilder header(Header header) { 1261 return headers(header); 1262 } 1263 1264 /** 1265 * Sets multiple headers on all requests. 1266 * 1267 * <h5 class='section'>Example:</h5> 1268 * <p class='bcode w800'> 1269 * RestClient <jv>client</jv> = RestClient 1270 * .<jsm>create</jsm>() 1271 * .headers(BasicHeader.<jsm>of</jsm>(<js>"Foo"</js>, <js>"bar"</js>)) 1272 * .build(); 1273 * </p> 1274 * 1275 * @param headers 1276 * The header to set. 1277 * <br>Can be any of the following types: 1278 * <ul> 1279 * <li>{@link Header} (including any subclasses such as {@link Accept}) 1280 * <li>{@link Headerable} 1281 * <li>{@link java.util.Map.Entry} 1282 * <li>{@link HeaderSupplier} 1283 * <li>{@link Map} 1284 * <ul> 1285 * <li>Values can be any POJO. 1286 * <li>Values converted to a string using the configured part serializer. 1287 * </ul> 1288 * <li>A collection or array of anything on this list. 1289 * </ul> 1290 * @return This object (for method chaining). 1291 */ 1292 @FluentSetter 1293 public RestClientBuilder headers(Object...headers) { 1294 for (Object h : headers) { 1295 if (BasicHeader.canCast(h) || h instanceof HeaderSupplier) { 1296 appendTo(RESTCLIENT_headers, h); 1297 } else if (h instanceof Map) { 1298 for (Map.Entry<Object,Object> e : toMap(h).entrySet()) 1299 appendTo(RESTCLIENT_headers, serializedHeader(e.getKey(), e.getValue(), null, null)); 1300 } else if (h instanceof Collection) { 1301 for (Object o : (Collection<?>)h) 1302 headers(o); 1303 } else if (h != null && h.getClass().isArray()) { 1304 for (int i = 0; i < Array.getLength(h); i++) 1305 headers(Array.get(h, i)); 1306 } else if (h != null) { 1307 throw new RuntimeException("Invalid type passed to headers(): " + className(h)); 1308 } 1309 } 1310 return this; 1311 } 1312 1313 /** 1314 * Sets multiple headers on all requests using freeform key/value pairs. 1315 * 1316 * <h5 class='section'>Example:</h5> 1317 * <p class='bcode w800'> 1318 * RestClient <jv>client</jv> = RestClient 1319 * .<jsm>create</jsm>() 1320 * .headerPairs(<js>"Header1"</js>,<js>"val1"</js>,<js>"Header2"</js>,<js>"val2"</js>) 1321 * .build(); 1322 * </p> 1323 * 1324 * @param pairs The header key/value pairs. 1325 * <ul> 1326 * <li>Values can be any POJO. 1327 * <li>Values converted to a string using the configured part serializer. 1328 * </ul> 1329 * @return This object (for method chaining). 1330 */ 1331 @FluentSetter 1332 public RestClientBuilder headerPairs(Object...pairs) { 1333 if (pairs.length % 2 != 0) 1334 throw new RuntimeException("Odd number of parameters passed into headerPairs()"); 1335 for (int i = 0; i < pairs.length; i+=2) 1336 headers(serializedHeader(pairs[i], pairs[i+1], null, null)); 1337 return this; 1338 } 1339 1340 /** 1341 * Sets the value for the <c>Accept</c> request header on all requests. 1342 * 1343 * <p> 1344 * This overrides the media type specified on the parser, but is overridden by calling 1345 * <code>header(<js>"Accept"</js>, value);</code> 1346 * 1347 * @param value The new header value. 1348 * @return This object (for method chaining). 1349 */ 1350 @FluentSetter 1351 public RestClientBuilder accept(Object value) { 1352 return header("Accept", value); 1353 } 1354 1355 /** 1356 * Sets the value for the <c>Accept-Charset</c> request header on all requests. 1357 * 1358 * <p> 1359 * This is a shortcut for calling <code>header(<js>"Accept-Charset"</js>, value);</code> 1360 * 1361 * @param value The new header value. 1362 * @return This object (for method chaining). 1363 */ 1364 @FluentSetter 1365 public RestClientBuilder acceptCharset(Object value) { 1366 return header("Accept-Charset", value); 1367 } 1368 1369 /** 1370 * Sets the value for the <c>Accept-Encoding</c> request header on all requests. 1371 * 1372 * <p> 1373 * This is a shortcut for calling <code>header(<js>"Accept-Encoding"</js>, value);</code> 1374 * 1375 * @param value The new header value. 1376 * @return This object (for method chaining). 1377 */ 1378 @FluentSetter 1379 public RestClientBuilder acceptEncoding(Object value) { 1380 return header("Accept-Encoding", value); 1381 } 1382 1383 /** 1384 * Sets the value for the <c>Accept-Language</c> request header on all requests. 1385 * 1386 * <p> 1387 * This is a shortcut for calling <code>header(<js>"Accept-Language"</js>, value);</code> 1388 * 1389 * @param value The new header value. 1390 * @return This object (for method chaining). 1391 */ 1392 @FluentSetter 1393 public RestClientBuilder acceptLanguage(Object value) { 1394 return header("Accept-Language", value); 1395 } 1396 1397 /** 1398 * Sets the value for the <c>Authorization</c> request header on all requests. 1399 * 1400 * <p> 1401 * This is a shortcut for calling <code>header(<js>"Authorization"</js>, value);</code> 1402 * 1403 * @param value The new header value. 1404 * @return This object (for method chaining). 1405 */ 1406 @FluentSetter 1407 public RestClientBuilder authorization(Object value) { 1408 return header("Authorization", value); 1409 } 1410 1411 /** 1412 * Sets the value for the <c>Cache-Control</c> request header on all requests. 1413 * 1414 * <p> 1415 * This is a shortcut for calling <code>header(<js>"Cache-Control"</js>, value);</code> 1416 * 1417 * @param value The new header value. 1418 * @return This object (for method chaining). 1419 */ 1420 @FluentSetter 1421 public RestClientBuilder cacheControl(Object value) { 1422 return header("Cache-Control", value); 1423 } 1424 1425 /** 1426 * Sets the client version by setting the value for the <js>"X-Client-Version"</js> header. 1427 * 1428 * @param value The version string (e.g. <js>"1.2.3"</js>) 1429 * @return This object (for method chaining). 1430 */ 1431 @FluentSetter 1432 public RestClientBuilder clientVersion(Object value) { 1433 return header("X-Client-Version", value); 1434 } 1435 1436 /** 1437 * Sets the value for the <c>Connection</c> request header on all requests. 1438 * 1439 * <p> 1440 * This is a shortcut for calling <code>header(<js>"Connection"</js>, value);</code> 1441 * 1442 * @param value The new header value. 1443 * @return This object (for method chaining). 1444 */ 1445 @FluentSetter 1446 public RestClientBuilder connection(Object value) { 1447 return header("Connection", value); 1448 } 1449 1450 /** 1451 * Sets the value for the <c>Content-Length</c> request header on all requests. 1452 * 1453 * <p> 1454 * This is a shortcut for calling <code>header(<js>"Content-Length"</js>, value);</code> 1455 * 1456 * @param value The new header value. 1457 * @return This object (for method chaining). 1458 */ 1459 @FluentSetter 1460 public RestClientBuilder contentLength(Object value) { 1461 return header("Content-Length", value); 1462 } 1463 1464 /** 1465 * Sets the value for the <c>Content-Type</c> request header on all requests. 1466 * 1467 * <p> 1468 * This overrides the media type specified on the serializer, but is overridden by calling 1469 * <code>header(<js>"Content-Type"</js>, value);</code> 1470 * 1471 * @param value The new header value. 1472 * @return This object (for method chaining). 1473 */ 1474 @FluentSetter 1475 public RestClientBuilder contentType(Object value) { 1476 return header("Content-Type", value); 1477 } 1478 1479 /** 1480 * Sets the value for the <c>Content-Encoding</c> request header on all requests. 1481 * 1482 * <p> 1483 * This is a shortcut for calling <code>header(<js>"Content-Encoding"</js>, value);</code> 1484 * 1485 * @param value The new header value. 1486 * @return This object (for method chaining). 1487 */ 1488 @FluentSetter 1489 public RestClientBuilder contentEncoding(Object value) { 1490 return header("Content-Encoding", value); 1491 } 1492 1493 /** 1494 * Sets the value for the <c>Date</c> request header on all requests. 1495 * 1496 * <p> 1497 * This is a shortcut for calling <code>header(<js>"Date"</js>, value);</code> 1498 * 1499 * @param value The new header value. 1500 * @return This object (for method chaining). 1501 */ 1502 @FluentSetter 1503 public RestClientBuilder date(Object value) { 1504 return header("Date", value); 1505 } 1506 1507 /** 1508 * Sets the value for the <c>Expect</c> request header on all requests. 1509 * 1510 * <p> 1511 * This is a shortcut for calling <code>header(<js>"Expect"</js>, value);</code> 1512 * 1513 * @param value The new header value. 1514 * @return This object (for method chaining). 1515 */ 1516 @FluentSetter 1517 public RestClientBuilder expect(Object value) { 1518 return header("Expect", value); 1519 } 1520 1521 /** 1522 * Sets the value for the <c>Forwarded</c> request header on all requests. 1523 * 1524 * <p> 1525 * This is a shortcut for calling <code>header(<js>"Forwarded"</js>, value);</code> 1526 * 1527 * @param value The new header value. 1528 * @return This object (for method chaining). 1529 */ 1530 @FluentSetter 1531 public RestClientBuilder forwarded(Object value) { 1532 return header("Forwarded", value); 1533 } 1534 1535 /** 1536 * Sets the value for the <c>From</c> request header on all requests. 1537 * 1538 * <p> 1539 * This is a shortcut for calling <code>header(<js>"From"</js>, value);</code> 1540 * 1541 * @param value The new header value. 1542 * @return This object (for method chaining). 1543 */ 1544 @FluentSetter 1545 public RestClientBuilder from(Object value) { 1546 return header("From", value); 1547 } 1548 1549 /** 1550 * Sets the value for the <c>Host</c> request header on all requests. 1551 * 1552 * <p> 1553 * This is a shortcut for calling <code>header(<js>"Host"</js>, value);</code> 1554 * 1555 * @param value The new header value. 1556 * @return This object (for method chaining). 1557 */ 1558 @FluentSetter 1559 public RestClientBuilder host(Object value) { 1560 return header("Host", value); 1561 } 1562 1563 /** 1564 * Sets the value for the <c>If-Match</c> request header on all requests. 1565 * 1566 * <p> 1567 * This is a shortcut for calling <code>header(<js>"If-Match"</js>, value);</code> 1568 * 1569 * @param value The new header value. 1570 * @return This object (for method chaining). 1571 */ 1572 @FluentSetter 1573 public RestClientBuilder ifMatch(Object value) { 1574 return header("If-Match", value); 1575 } 1576 1577 /** 1578 * Sets the value for the <c>If-Modified-Since</c> request header on all requests. 1579 * 1580 * <p> 1581 * This is a shortcut for calling <code>header(<js>"If-Modified-Since"</js>, value);</code> 1582 * 1583 * @param value The new header value. 1584 * @return This object (for method chaining). 1585 */ 1586 @FluentSetter 1587 public RestClientBuilder ifModifiedSince(Object value) { 1588 return header("If-Modified-Since", value); 1589 } 1590 1591 /** 1592 * Sets the value for the <c>If-None-Match</c> request header on all requests. 1593 * 1594 * <p> 1595 * This is a shortcut for calling <code>header(<js>"If-None-Match"</js>, value);</code> 1596 * 1597 * @param value The new header value. 1598 * @return This object (for method chaining). 1599 */ 1600 @FluentSetter 1601 public RestClientBuilder ifNoneMatch(Object value) { 1602 return header("If-None-Match", value); 1603 } 1604 1605 /** 1606 * Sets the value for the <c>If-Range</c> request header on all requests. 1607 * 1608 * <p> 1609 * This is a shortcut for calling <code>header(<js>"If-Range"</js>, value);</code> 1610 * 1611 * @param value The new header value. 1612 * @return This object (for method chaining). 1613 */ 1614 @FluentSetter 1615 public RestClientBuilder ifRange(Object value) { 1616 return header("If-Range", value); 1617 } 1618 1619 /** 1620 * Sets the value for the <c>If-Unmodified-Since</c> request header on all requests. 1621 * 1622 * <p> 1623 * This is a shortcut for calling <code>header(<js>"If-Unmodified-Since"</js>, value);</code> 1624 * 1625 * @param value The new header value. 1626 * @return This object (for method chaining). 1627 */ 1628 @FluentSetter 1629 public RestClientBuilder ifUnmodifiedSince(Object value) { 1630 return header("If-Unmodified-Since", value); 1631 } 1632 1633 /** 1634 * Sets the value for the <c>Max-Forwards</c> request header on all requests. 1635 * 1636 * <p> 1637 * This is a shortcut for calling <code>header(<js>"Max-Forwards"</js>, value);</code> 1638 * 1639 * @param value The new header value. 1640 * @return This object (for method chaining). 1641 */ 1642 @FluentSetter 1643 public RestClientBuilder maxForwards(Object value) { 1644 return header("Max-Forwards", value); 1645 } 1646 1647 /** 1648 * When called, <c>No-Trace: true</c> is added to requests. 1649 * 1650 * <p> 1651 * This gives the opportunity for the servlet to not log errors on invalid requests. 1652 * This is useful for testing purposes when you don't want your log file to show lots of errors that are simply the 1653 * results of testing. 1654 * 1655 * @return This object (for method chaining). 1656 */ 1657 @FluentSetter 1658 public RestClientBuilder noTrace() { 1659 return header("No-Trace", true); 1660 } 1661 1662 /** 1663 * Sets the value for the <c>Origin</c> request header on all requests. 1664 * 1665 * <p> 1666 * This is a shortcut for calling <code>header(<js>"Origin"</js>, value);</code> 1667 * 1668 * @param value The new header value. 1669 * @return This object (for method chaining). 1670 */ 1671 @FluentSetter 1672 public RestClientBuilder origin(Object value) { 1673 return header("Origin", value); 1674 } 1675 1676 /** 1677 * Sets the value for the <c>Pragma</c> request header on all requests. 1678 * 1679 * <p> 1680 * This is a shortcut for calling <code>header(<js>"Pragma"</js>, value);</code> 1681 * 1682 * @param value The new header value. 1683 * @return This object (for method chaining). 1684 */ 1685 @FluentSetter 1686 public RestClientBuilder pragma(Object value) { 1687 return header("Pragma", value); 1688 } 1689 1690 /** 1691 * Sets the value for the <c>Proxy-Authorization</c> request header on all requests. 1692 * 1693 * <p> 1694 * This is a shortcut for calling <code>header(<js>"Proxy-Authorization"</js>, value);</code> 1695 * 1696 * @param value The new header value. 1697 * @return This object (for method chaining). 1698 */ 1699 @FluentSetter 1700 public RestClientBuilder proxyAuthorization(Object value) { 1701 return header("Proxy-Authorization", value); 1702 } 1703 1704 /** 1705 * Sets the value for the <c>Range</c> request header on all requests. 1706 * 1707 * <p> 1708 * This is a shortcut for calling <code>header(<js>"Range"</js>, value);</code> 1709 * 1710 * @param value The new header value. 1711 * @return This object (for method chaining). 1712 */ 1713 @FluentSetter 1714 public RestClientBuilder range(Object value) { 1715 return header("Range", value); 1716 } 1717 1718 /** 1719 * Sets the value for the <c>Referer</c> request header on all requests. 1720 * 1721 * <p> 1722 * This is a shortcut for calling <code>header(<js>"Referer"</js>, value);</code> 1723 * 1724 * @param value The new header value. 1725 * @return This object (for method chaining). 1726 */ 1727 @FluentSetter 1728 public RestClientBuilder referer(Object value) { 1729 return header("Referer", value); 1730 } 1731 1732 /** 1733 * Sets the value for the <c>TE</c> request header on all requests. 1734 * 1735 * <p> 1736 * This is a shortcut for calling <code>header(<js>"TE"</js>, value);</code> 1737 * 1738 * @param value The new header value. 1739 * @return This object (for method chaining). 1740 */ 1741 @FluentSetter 1742 public RestClientBuilder te(Object value) { 1743 return header("TE", value); 1744 } 1745 1746 /** 1747 * Sets the value for the <c>User-Agent</c> request header on all requests. 1748 * 1749 * <p> 1750 * This is a shortcut for calling <code>header(<js>"User-Agent"</js>, value);</code> 1751 * 1752 * @param value The new header value. 1753 * @return This object (for method chaining). 1754 */ 1755 @FluentSetter 1756 public RestClientBuilder userAgent(Object value) { 1757 return header("User-Agent", value); 1758 } 1759 1760 /** 1761 * Sets the value for the <c>Upgrade</c> request header on all requests. 1762 * 1763 * <p> 1764 * This is a shortcut for calling <code>header(<js>"Upgrade"</js>, value);</code> 1765 * 1766 * @param value The new header value. 1767 * @return This object (for method chaining). 1768 */ 1769 @FluentSetter 1770 public RestClientBuilder upgrade(Object value) { 1771 return header("Upgrade", value); 1772 } 1773 1774 /** 1775 * Sets the value for the <c>Via</c> request header on all requests. 1776 * 1777 * <p> 1778 * This is a shortcut for calling <code>header(<js>"Via"</js>, value);</code> 1779 * 1780 * @param value The new header value. 1781 * @return This object (for method chaining). 1782 */ 1783 @FluentSetter 1784 public RestClientBuilder via(Object value) { 1785 return header("Via", value); 1786 } 1787 1788 /** 1789 * Sets the value for the <c>Warning</c> request header on all requests. 1790 * 1791 * <p> 1792 * This is a shortcut for calling <code>header(<js>"Warning"</js>, value);</code> 1793 * 1794 * @param value The new header value. 1795 * @return This object (for method chaining). 1796 */ 1797 @FluentSetter 1798 public RestClientBuilder warning(Object value) { 1799 return header("Warning", value); 1800 } 1801 1802 //----------------------------------------------------------------------------------------------------------------- 1803 // Query 1804 //----------------------------------------------------------------------------------------------------------------- 1805 1806 /** 1807 * Adds a query parameter to the URI. 1808 * 1809 * <h5 class='section'>Example:</h5> 1810 * <p class='bcode w800'> 1811 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1812 * 1813 * <jc>// Adds query parameter "foo=foo|bar" to all requests.</jc> 1814 * RestClient <jv>client</jv> = RestClient 1815 * .<jsm>create</jsm>() 1816 * .query(<js>"foo"</js>, <jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>, <jv>myPartSerializer</jv>); 1817 * .build(); 1818 * </p> 1819 * 1820 * @param name The parameter name. 1821 * @param value The parameter value. 1822 * <ul> 1823 * <li>Can be any POJO. 1824 * <li>Converted to a string using the specified part serializer. 1825 * </ul> 1826 * @param schema The schema object that defines the format of the output. 1827 * <ul> 1828 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1829 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1830 * </ul> 1831 * @param serializer The serializer to use for serializing the value to a string. 1832 * <ul> 1833 * <li>If <jk>null</jk>, then the {@link HttpPartSerializer} defined on the client is used ({@link OpenApiSerializer} by default). 1834 * </ul> 1835 * @return This object (for method chaining). 1836 */ 1837 @FluentSetter 1838 public RestClientBuilder query(String name, Object value, HttpPartSchema schema, HttpPartSerializer serializer) { 1839 return queries(serializedNameValuePair(name, value, QUERY, serializer, schema)); 1840 } 1841 1842 /** 1843 * Adds a query parameter with a dynamic value to the URI. 1844 * 1845 * <h5 class='section'>Example:</h5> 1846 * <p class='bcode w800'> 1847 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1848 * 1849 * <jc>// Adds query parameter "foo=foo|bar" to all requests.</jc> 1850 * RestClient <jv>client</jv> = RestClient 1851 * .<jsm>create</jsm>() 1852 * .query(<js>"foo"</js>, ()-><jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>, <jv>myPartSerializer</jv>); 1853 * .build(); 1854 * </p> 1855 * 1856 * @param name The parameter name. 1857 * @param value The parameter value supplier. 1858 * <ul> 1859 * <li>Can be any POJO. 1860 * <li>Converted to a string using the specified part serializer. 1861 * </ul> 1862 * @param schema The schema object that defines the format of the output. 1863 * <ul> 1864 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1865 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1866 * </ul> 1867 * @param serializer The serializer to use for serializing the value to a string. 1868 * <ul> 1869 * <li>If <jk>null</jk>, then the {@link HttpPartSerializer} defined on the client is used ({@link OpenApiSerializer} by default). 1870 * </ul> 1871 * @return This object (for method chaining). 1872 */ 1873 @FluentSetter 1874 public RestClientBuilder query(String name, Supplier<?> value, HttpPartSchema schema, HttpPartSerializer serializer) { 1875 return queries(serializedNameValuePair(name, value, QUERY, serializer, schema)); 1876 } 1877 1878 /** 1879 * Adds a query parameter to the URI. 1880 * 1881 * <h5 class='section'>Example:</h5> 1882 * <p class='bcode w800'> 1883 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1884 * 1885 * <jc>// Adds query parameter "foo=foo|bar" to all requests.</jc> 1886 * RestClient <jv>client</jv> = RestClient 1887 * .<jsm>create</jsm>() 1888 * .query(<js>"foo"</js>, <jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>); 1889 * .build(); 1890 * </p> 1891 * 1892 * @param name The parameter name. 1893 * @param value The parameter value. 1894 * <ul> 1895 * <li>Can be any POJO. 1896 * <li>Converted to a string using the specified part serializer. 1897 * </ul> 1898 * @param schema The schema object that defines the format of the output. 1899 * <ul> 1900 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1901 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1902 * </ul> 1903 * @return This object (for method chaining). 1904 */ 1905 @FluentSetter 1906 public RestClientBuilder query(String name, Object value, HttpPartSchema schema) { 1907 return queries(serializedNameValuePair(name, value, QUERY, null, schema)); 1908 } 1909 1910 /** 1911 * Adds a query parameter with a dynamic value to the URI. 1912 * 1913 * <h5 class='section'>Example:</h5> 1914 * <p class='bcode w800'> 1915 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 1916 * 1917 * <jc>// Adds query parameter "foo=foo|bar" to all requests.</jc> 1918 * RestClient <jv>client</jv> = RestClient 1919 * .<jsm>create</jsm>() 1920 * .query(<js>"foo"</js>, ()-><jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>); 1921 * .build(); 1922 * </p> 1923 * 1924 * @param name The parameter name. 1925 * @param value The parameter value supplier. 1926 * <ul> 1927 * <li>Can be any POJO. 1928 * <li>Converted to a string using the specified part serializer. 1929 * </ul> 1930 * @param schema The schema object that defines the format of the output. 1931 * <ul> 1932 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 1933 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 1934 * </ul> 1935 * @return This object (for method chaining). 1936 */ 1937 @FluentSetter 1938 public RestClientBuilder query(String name, Supplier<?> value, HttpPartSchema schema) { 1939 return queries(serializedNameValuePair(name, value, QUERY, null, schema)); 1940 } 1941 1942 /** 1943 * Adds a query parameter to the URI. 1944 * 1945 * <h5 class='section'>Example:</h5> 1946 * <p class='bcode w800'> 1947 * RestClient <jv>client</jv> = RestClient 1948 * .<jsm>create</jsm>() 1949 * .query(<js>"foo"</js>, <js>"bar"</js>) 1950 * .build(); 1951 * </p> 1952 * 1953 * @param name The parameter name. 1954 * @param value The parameter value. 1955 * <ul> 1956 * <li>Can be any POJO. 1957 * <li>Converted to a string using the specified part serializer. 1958 * </ul> 1959 * @return This object (for method chaining). 1960 */ 1961 @FluentSetter 1962 public RestClientBuilder query(String name, Object value) { 1963 return queries(serializedNameValuePair(name, value, QUERY, null, null)); 1964 } 1965 1966 /** 1967 * Adds a query parameter to the URI. 1968 * 1969 * <h5 class='section'>Example:</h5> 1970 * <p class='bcode w800'> 1971 * RestClient <jv>client</jv> = RestClient 1972 * .<jsm>create</jsm>() 1973 * .query(BasicNameValuePair.<jsm>of</jsm>(<js>"foo"</js>, <js>"bar"</js>)) 1974 * .build(); 1975 * </p> 1976 * 1977 * @param pair The query parameter. 1978 * @return This object (for method chaining). 1979 */ 1980 @FluentSetter 1981 public RestClientBuilder query(NameValuePair pair) { 1982 return queries(pair); 1983 } 1984 1985 /** 1986 * Adds a query parameter with a dynamic value to the URI. 1987 * 1988 * <h5 class='section'>Example:</h5> 1989 * <p class='bcode w800'> 1990 * RestClient <jv>client</jv> = RestClient 1991 * .<jsm>create</jsm>() 1992 * .query(<js>"foo"</js>, ()-><js>"bar"</js>) 1993 * .build(); 1994 * </p> 1995 * 1996 * @param name The parameter name. 1997 * @param value The parameter value supplier. 1998 * <ul> 1999 * <li>Can be any POJO. 2000 * <li>Converted to a string using the specified part serializer. 2001 * </ul> 2002 * @return This object (for method chaining). 2003 */ 2004 @FluentSetter 2005 public RestClientBuilder query(String name, Supplier<?> value) { 2006 return queries(serializedNameValuePair(name, value, QUERY, null, null)); 2007 } 2008 2009 /** 2010 * Adds a query parameter to the URI. 2011 * 2012 * <h5 class='section'>Example:</h5> 2013 * <p class='bcode w800'> 2014 * RestClient <jv>client</jv> = RestClient 2015 * .<jsm>create</jsm>() 2016 * .queries(BasicNameValuePair.<jsm>of</jsm>(<js>"foo"</js>, <js>"bar"</js>)) 2017 * .build(); 2018 * </p> 2019 * 2020 * @param params 2021 * The query parameters. 2022 * <br>Can be any of the following types: 2023 * <ul> 2024 * <li>{@link NameValuePair} 2025 * <li>{@link NameValuePairable} 2026 * <li>{@link java.util.Map.Entry} 2027 * <li>{@link NameValuePairSupplier} 2028 * <li>{@link Map} 2029 * <ul> 2030 * <li>Values can be any POJO. 2031 * <li>Values converted to a string using the configured part serializer. 2032 * </ul> 2033 * <li>A collection or array of anything on this list. 2034 * </ul> 2035 * @return This object (for method chaining). 2036 */ 2037 @FluentSetter 2038 public RestClientBuilder queries(Object...params) { 2039 for (Object p : params) { 2040 if (BasicNameValuePair.canCast(p) || p instanceof NameValuePairSupplier) { 2041 appendTo(RESTCLIENT_query, p); 2042 } else if (p instanceof Map) { 2043 for (Map.Entry<Object,Object> e : toMap(p).entrySet()) 2044 appendTo(RESTCLIENT_query, serializedNameValuePair(e.getKey(), e.getValue(), QUERY, null, null)); 2045 } else if (p instanceof Collection) { 2046 for (Object o : (Collection<?>)p) 2047 queries(o); 2048 } else if (p != null && p.getClass().isArray()) { 2049 for (int i = 0; i < Array.getLength(p); i++) 2050 queries(Array.get(p, i)); 2051 } else if (p != null) { 2052 throw new RuntimeException("Invalid type passed to query(): " + className(p)); 2053 } 2054 } 2055 return this; 2056 } 2057 2058 /** 2059 * Adds query parameters to the URI query using free-form key/value pairs. 2060 * 2061 * <h5 class='section'>Example:</h5> 2062 * <p class='bcode w800'> 2063 * RestClient <jv>client</jv> = RestClient 2064 * .<jsm>create</jsm>() 2065 * .queryPairs(<js>"key1"</js>,<js>"val1"</js>,<js>"key2"</js>,<js>"val2"</js>) 2066 * .build(); 2067 * </p> 2068 * 2069 * @param pairs The query key/value pairs. 2070 * <ul> 2071 * <li>Values can be any POJO. 2072 * <li>Values converted to a string using the configured part serializer. 2073 * </ul> 2074 * @return This object (for method chaining). 2075 */ 2076 @FluentSetter 2077 public RestClientBuilder queryPairs(Object...pairs) { 2078 if (pairs.length % 2 != 0) 2079 throw new RuntimeException("Odd number of parameters passed into queryPairs(Object...)"); 2080 for (int i = 0; i < pairs.length; i+=2) 2081 queries(serializedNameValuePair(pairs[i], pairs[i+1], QUERY, null, null)); 2082 return this; 2083 } 2084 2085 //----------------------------------------------------------------------------------------------------------------- 2086 // Form data 2087 //----------------------------------------------------------------------------------------------------------------- 2088 2089 /** 2090 * Adds a form-data parameter to all request bodies. 2091 * 2092 * <h5 class='section'>Example:</h5> 2093 * <p class='bcode w800'> 2094 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 2095 * 2096 * <jc>// Adds form data parameter "foo=foo|bar" to all requests.</jc> 2097 * RestClient <jv>client</jv> = RestClient 2098 * .<jsm>create</jsm>() 2099 * .formData(<js>"foo"</js>, <jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>, <jv>myPartSerializer</jv>); 2100 * .build(); 2101 * </p> 2102 * 2103 * @param name The parameter name. 2104 * @param value The parameter value. 2105 * <ul> 2106 * <li>Can be any POJO. 2107 * <li>Converted to a string using the specified part serializer. 2108 * </ul> 2109 * @param schema The schema object that defines the format of the output. 2110 * <ul> 2111 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 2112 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 2113 * </ul> 2114 * @param serializer The serializer to use for serializing the value to a string. 2115 * <ul> 2116 * <li>If <jk>null</jk>, then the {@link HttpPartSerializer} defined on the client is used ({@link OpenApiSerializer} by default). 2117 * </ul> 2118 * @return This object (for method chaining). 2119 */ 2120 @FluentSetter 2121 public RestClientBuilder formData(String name, Object value, HttpPartSchema schema, HttpPartSerializer serializer) { 2122 return formDatas(serializedNameValuePair(name, value, FORMDATA, serializer, schema)); 2123 } 2124 2125 /** 2126 * Adds a form-data parameter with a dynamic value to all request bodies. 2127 * 2128 * <h5 class='section'>Example:</h5> 2129 * <p class='bcode w800'> 2130 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 2131 * 2132 * <jc>// Adds form data parameter "foo=foo|bar" to all requests.</jc> 2133 * RestClient <jv>client</jv> = RestClient 2134 * .<jsm>create</jsm>() 2135 * .formData(<js>"foo"</js>, ()-><jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>, <jv>myPartSerializer</jv>); 2136 * .build(); 2137 * </p> 2138 * 2139 * @param name The parameter name. 2140 * @param value The parameter value supplier. 2141 * <ul> 2142 * <li>Can be any POJO. 2143 * <li>Converted to a string using the specified part serializer. 2144 * </ul> 2145 * @param schema The schema object that defines the format of the output. 2146 * <ul> 2147 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 2148 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 2149 * </ul> 2150 * @param serializer The serializer to use for serializing the value to a string. 2151 * <ul> 2152 * <li>If <jk>null</jk>, then the {@link HttpPartSerializer} defined on the client is used ({@link OpenApiSerializer} by default). 2153 * </ul> 2154 * @return This object (for method chaining). 2155 */ 2156 @FluentSetter 2157 public RestClientBuilder formData(String name, Supplier<?> value, HttpPartSchema schema, HttpPartSerializer serializer) { 2158 return formDatas(serializedNameValuePair(name, value, FORMDATA, serializer, schema)); 2159 } 2160 2161 /** 2162 * Adds a form-data parameter to all request bodies. 2163 * 2164 * <h5 class='section'>Example:</h5> 2165 * <p class='bcode w800'> 2166 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 2167 * 2168 * <jc>// Adds form data parameter "foo=foo|bar" to all requests.</jc> 2169 * RestClient <jv>client</jv> = RestClient 2170 * .<jsm>create</jsm>() 2171 * .formData(<js>"foo"</js>, <jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>); 2172 * .build(); 2173 * </p> 2174 * 2175 * @param name The parameter name. 2176 * @param value The parameter value. 2177 * <ul> 2178 * <li>Can be any POJO. 2179 * <li>Converted to a string using the specified part serializer. 2180 * </ul> 2181 * @param schema The schema object that defines the format of the output. 2182 * <ul> 2183 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 2184 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 2185 * </ul> 2186 * @return This object (for method chaining). 2187 */ 2188 @FluentSetter 2189 public RestClientBuilder formData(String name, Object value, HttpPartSchema schema) { 2190 return formDatas(serializedNameValuePair(name, value, FORMDATA, null, schema)); 2191 } 2192 2193 /** 2194 * Adds a form-data parameter with a dynamic value to all request bodies. 2195 * 2196 * <h5 class='section'>Example:</h5> 2197 * <p class='bcode w800'> 2198 * String[] <jv>value</jv> = {<js>"foo"</js>,<js>"bar"</js>}; 2199 * 2200 * <jc>// Adds form data parameter "foo=foo|bar" to all requests.</jc> 2201 * RestClient <jv>client</jv> = RestClient 2202 * .<jsm>create</jsm>() 2203 * .formData(<js>"foo"</js>, ()-><jv>value</jv>, HttpPartSchema.<jsf>T_ARRAY_PIPES</jsf>); 2204 * .build(); 2205 * </p> 2206 * 2207 * @param name The parameter name. 2208 * @param value The parameter value supplier. 2209 * <ul> 2210 * <li>Can be any POJO. 2211 * <li>Converted to a string using the specified part serializer. 2212 * </ul> 2213 * @param schema The schema object that defines the format of the output. 2214 * <ul> 2215 * <li>If <jk>null</jk>, defaults to {@link HttpPartSchema#DEFAULT}. 2216 * <li>Only used if serializer is schema-aware (e.g. {@link OpenApiSerializer}). 2217 * </ul> 2218 * @return This object (for method chaining). 2219 */ 2220 @FluentSetter 2221 public RestClientBuilder formData(String name, Supplier<?> value, HttpPartSchema schema) { 2222 return formDatas(serializedNameValuePair(name, value, FORMDATA, null, schema)); 2223 } 2224 2225 /** 2226 * Adds a form-data parameter to all request bodies. 2227 * 2228 * <h5 class='section'>Example:</h5> 2229 * <p class='bcode w800'> 2230 * RestClient <jv>client</jv> = RestClient 2231 * .<jsm>create</jsm>() 2232 * .formData(<js>"foo"</js>, <js>"bar"</js>) 2233 * .build(); 2234 * </p> 2235 * 2236 * @param name The parameter name. 2237 * @param value The parameter value. 2238 * <ul> 2239 * <li>Can be any POJO. 2240 * <li>Converted to a string using the specified part serializer. 2241 * </ul> 2242 * @return This object (for method chaining). 2243 */ 2244 @FluentSetter 2245 public RestClientBuilder formData(String name, Object value) { 2246 return formDatas(serializedNameValuePair(name, value, FORMDATA, null, null)); 2247 } 2248 2249 /** 2250 * Adds a form-data parameter to all request bodies. 2251 * 2252 * <h5 class='section'>Example:</h5> 2253 * <p class='bcode w800'> 2254 * RestClient <jv>client</jv> = RestClient 2255 * .<jsm>create</jsm>() 2256 * .formData(BasicNameValuePair.<jsm>of</jsm>(<js>"foo"</js>, <js>"bar"</js>)) 2257 * .build(); 2258 * </p> 2259 * 2260 * @param pair The form data parameter. 2261 * @return This object (for method chaining). 2262 */ 2263 @FluentSetter 2264 public RestClientBuilder formData(NameValuePair pair) { 2265 return formDatas(pair); 2266 } 2267 2268 /** 2269 * Adds a form-data parameter with a dynamic value to all request bodies. 2270 * 2271 * <h5 class='section'>Example:</h5> 2272 * <p class='bcode w800'> 2273 * RestClient <jv>client</jv> = RestClient 2274 * .<jsm>create</jsm>() 2275 * .formData(<js>"foo"</js>, ()-><js>"bar"</js>) 2276 * .build(); 2277 * </p> 2278 * 2279 * @param name The parameter name. 2280 * @param value The parameter value supplier. 2281 * <ul> 2282 * <li>Can be any POJO. 2283 * <li>Converted to a string using the specified part serializer. 2284 * </ul> 2285 * @return This object (for method chaining). 2286 */ 2287 @FluentSetter 2288 public RestClientBuilder formData(String name, Supplier<?> value) { 2289 return formDatas(serializedNameValuePair(name, value, FORMDATA, null, null)); 2290 } 2291 2292 /** 2293 * Adds a form-data parameter to all request bodies. 2294 * 2295 * <h5 class='section'>Example:</h5> 2296 * <p class='bcode w800'> 2297 * RestClient <jv>client</jv> = RestClient 2298 * .<jsm>create</jsm>() 2299 * .formData(BasicNameValuePair.<jsm>of</jsm>(<js>"foo"</js>, <js>"bar"</js>)) 2300 * .build(); 2301 * </p> 2302 * 2303 * @param params 2304 * The form-data parameters. 2305 * <br>Can be any of the following types: 2306 * <ul> 2307 * <li>{@link NameValuePair} 2308 * <li>{@link NameValuePairable} 2309 * <li>{@link java.util.Map.Entry} 2310 * <li>{@link NameValuePairSupplier} 2311 * <li>{@link Map} 2312 * <ul> 2313 * <li>Values can be any POJO. 2314 * <li>Values converted to a string using the configured part serializer. 2315 * </ul> 2316 * <li>A collection or array of anything on this list. 2317 * </ul> 2318 * @return This object (for method chaining). 2319 */ 2320 @FluentSetter 2321 public RestClientBuilder formDatas(Object...params) { 2322 for (Object p : params) { 2323 if (BasicNameValuePair.canCast(p) || p instanceof NameValuePairSupplier) { 2324 appendTo(RESTCLIENT_formData, p); 2325 } else if (p instanceof Map) { 2326 for (Map.Entry<Object,Object> e : toMap(p).entrySet()) 2327 appendTo(RESTCLIENT_formData, serializedNameValuePair(e.getKey(), e.getValue(), FORMDATA, null, null)); 2328 } else if (p instanceof Collection) { 2329 for (Object o : (Collection<?>)p) 2330 formDatas(o); 2331 } else if (p != null && p.getClass().isArray()) { 2332 for (int i = 0; i < Array.getLength(p); i++) 2333 formDatas(Array.get(p, i)); 2334 } else if (p != null) { 2335 throw new RuntimeException("Invalid type passed to formData(): " + className(p)); 2336 } 2337 } 2338 return this; 2339 } 2340 2341 /** 2342 * Adds form-data parameters to all request bodies using free-form key/value pairs. 2343 * 2344 * <h5 class='section'>Example:</h5> 2345 * <p class='bcode w800'> 2346 * RestClient <jv>client</jv> = RestClient 2347 * .<jsm>create</jsm>() 2348 * .formDataPairs(<js>"key1"</js>,<js>"val1"</js>,<js>"key2"</js>,<js>"val2"</js>) 2349 * .build(); 2350 * </p> 2351 * 2352 * @param pairs The form-data key/value pairs. 2353 * <ul> 2354 * <li>Values can be any POJO. 2355 * <li>Values converted to a string using the configured part serializer. 2356 * </ul> 2357 * @return This object (for method chaining). 2358 */ 2359 @FluentSetter 2360 public RestClientBuilder formDataPairs(Object...pairs) { 2361 if (pairs.length % 2 != 0) 2362 throw new RuntimeException("Odd number of parameters passed into formDataPairs()"); 2363 for (int i = 0; i < pairs.length; i+=2) 2364 formDatas(serializedNameValuePair(pairs[i], pairs[i+1], FORMDATA, null, null)); 2365 return this; 2366 } 2367 2368 //----------------------------------------------------------------------------------------------------------------- 2369 // Properties 2370 //----------------------------------------------------------------------------------------------------------------- 2371 2372 /** 2373 * <i><l>RestClient</l> configuration property: </i> REST call handler. 2374 * 2375 * <p> 2376 * Allows you to provide a custom handler for making HTTP calls. 2377 * 2378 * <h5 class='section'>Example:</h5> 2379 * <p class='bcode w800'> 2380 * <jc>// Create a client that handles processing of requests using a custom handler.</jc> 2381 * <jk>public class</jk> MyRestCallHandler <jk>implements</jk> RestCallHandler { 2382 * 2383 * <ja>@Override</ja> 2384 * <jk>public</jk> HttpResponse run(HttpHost <jv>target</jv>, HttpRequest <jv>request</jv>, HttpContext <jv>context</jv>) <jk>throws</jk> IOException { 2385 * <jc>// Custom handle requests.</jc> 2386 * } 2387 * } 2388 * 2389 * RestClient <jv>client</jv> = RestClient 2390 * .<jsm>create</jsm>() 2391 * .callHandler(MyRestCallHandler.<jk>class</jk>) 2392 * .build(); 2393 * </p> 2394 * 2395 * <ul class='notes'> 2396 * <li>The {@link RestClient#run(HttpHost, HttpRequest, HttpContext)} method can also be overridden to produce the same results. 2397 * </ul> 2398 * 2399 * <ul class='seealso'> 2400 * <li class='jic'>{@link RestCallHandler} 2401 * <li class='jf'>{@link RestClient#RESTCLIENT_callHandler} 2402 * </ul> 2403 * 2404 * @param value 2405 * The new value for this setting. 2406 * <br>The default value is <jk>null</jk>. 2407 * @return This object (for method chaining). 2408 */ 2409 @FluentSetter 2410 public RestClientBuilder callHandler(Class<? extends RestCallHandler> value) { 2411 return set(RESTCLIENT_callHandler, value); 2412 } 2413 2414 /** 2415 * <i><l>RestClient</l> configuration property: </i> REST call handler. 2416 * 2417 * <p> 2418 * Allows you to provide a custom handler for making HTTP calls. 2419 * 2420 * <h5 class='section'>Example:</h5> 2421 * <p class='bcode w800'> 2422 * <jc>// Create a client that handles processing of requests using a custom handler.</jc> 2423 * RestClient <jv>client</jv> = RestClient 2424 * .<jsm>create</jsm>() 2425 * .callHandler( 2426 * <jk>new</jk> RestCallHandler() { 2427 * <ja>@Override</ja> 2428 * <jk>public</jk> HttpResponse run(HttpHost <jv>target</jv>, HttpRequest <jv>request</jv>, HttpContext <jv>context</jv>) <jk>throws</jk> IOException { 2429 * <jc>// Custom handle requests.</jc> 2430 * } 2431 * } 2432 * ) 2433 * .build(); 2434 * </p> 2435 * 2436 * <ul class='notes'> 2437 * <li>The {@link RestClient#run(HttpHost, HttpRequest, HttpContext)} method can also be overridden to produce the same results. 2438 * </ul> 2439 * 2440 * <ul class='seealso'> 2441 * <li class='jic'>{@link RestCallHandler} 2442 * <li class='jf'>{@link RestClient#RESTCLIENT_callHandler} 2443 * </ul> 2444 * 2445 * @param value 2446 * The new value for this setting. 2447 * <br>The default value is <jk>null</jk>. 2448 * @return This object (for method chaining). 2449 */ 2450 @FluentSetter 2451 public RestClientBuilder callHandler(RestCallHandler value) { 2452 return set(RESTCLIENT_callHandler, value); 2453 } 2454 2455 /** 2456 * <i><l>RestClient</l> configuration property: </i> Console print stream 2457 * 2458 * <p> 2459 * Allows you to redirect the console output to a different print stream. 2460 * 2461 * <ul class='seealso'> 2462 * <li class='jf'>{@link RestClient#RESTCLIENT_console} 2463 * </ul> 2464 * 2465 * @param value 2466 * The new value for this setting. 2467 * @return This object (for method chaining). 2468 */ 2469 @FluentSetter 2470 public RestClientBuilder console(Class<? extends PrintStream> value) { 2471 return set(RESTCLIENT_console, value); 2472 } 2473 2474 /** 2475 * <i><l>RestClient</l> configuration property: </i> Console print stream 2476 * 2477 * <p> 2478 * Allows you to redirect the console output to a different print stream. 2479 * 2480 * <ul class='seealso'> 2481 * <li class='jf'>{@link RestClient#RESTCLIENT_console} 2482 * </ul> 2483 * 2484 * @param value 2485 * The new value for this setting. 2486 * @return This object (for method chaining). 2487 */ 2488 @FluentSetter 2489 public RestClientBuilder console(PrintStream value) { 2490 return set(RESTCLIENT_console, value); 2491 } 2492 2493 /** 2494 * <i><l>RestClient</l> configuration property: </i> Errors codes predicate. 2495 * 2496 * <p> 2497 * Defines a predicate to test for error codes. 2498 * 2499 * <h5 class='section'>Example:</h5> 2500 * <p class='bcode w800'> 2501 * <jc>// Create a client that considers any 300+ responses to be errors.</jc> 2502 * RestClient <jv>client</jv> = RestClient 2503 * .<jsm>create</jsm>() 2504 * .errorCodes(<jv>x</jv> -> <jv>x</jv>>=300) 2505 * .build(); 2506 * </p> 2507 * 2508 * <ul class='seealso'> 2509 * <li class='jf'>{@link RestClient#RESTCLIENT_errorCodes} 2510 * </ul> 2511 * 2512 * @param value 2513 * The new value for this setting. 2514 * <br>The default value is <code>x -> x >= 400</code>. 2515 * @return This object (for method chaining). 2516 */ 2517 @FluentSetter 2518 public RestClientBuilder errorCodes(Predicate<Integer> value) { 2519 return set(RESTCLIENT_errorCodes, value); 2520 } 2521 2522 /** 2523 * <i><l>RestClient</l> configuration property: </i> Executor service. 2524 * 2525 * <p> 2526 * Defines the executor service to use when calling future methods on the {@link RestRequest} class. 2527 * 2528 * <p> 2529 * This executor service is used to create {@link Future} objects on the following methods: 2530 * <ul> 2531 * <li class='jm'>{@link RestRequest#runFuture()} 2532 * <li class='jm'>{@link RestRequest#completeFuture()} 2533 * <li class='jm'>{@link RestResponseBody#asFuture(Class)} (and similar methods) 2534 * </ul> 2535 * 2536 * <p> 2537 * The default executor service is a single-threaded {@link ThreadPoolExecutor} with a 30 second timeout 2538 * and a queue size of 10. 2539 * 2540 * <h5 class='section'>Example:</h5> 2541 * <p class='bcode w800'> 2542 * <jc>// Create a client with a customized executor service.</jc> 2543 * RestClient <jv>client</jv> = RestClient 2544 * .<jsm>create</jsm>() 2545 * .executorService(<jk>new</jk> ThreadPoolExecutor(1, 1, 30, TimeUnit.<jsf>SECONDS</jsf>, <jk>new</jk> ArrayBlockingQueue<Runnable>(10)), <jk>true</jk>) 2546 * .build(); 2547 * 2548 * <jc>// Use it to asynchronously run a request.</jc> 2549 * Future<RestResponse> <jv>responseFuture</jv> = <jv>client</jv>.get(<jsf>URI</jsf>).runFuture(); 2550 * 2551 * <jc>// Do some other stuff.</jc> 2552 * 2553 * <jc>// Now read the response.</jc> 2554 * String <jv>body</jv> = <jv>responseFuture</jv>.get().getBody().asString(); 2555 * 2556 * <jc>// Use it to asynchronously retrieve a response.</jc> 2557 * Future<MyBean> <jv>myBeanFuture</jv> = <jv>client</jv> 2558 * .get(<jsf>URI</jsf>) 2559 * .run() 2560 * .getBody().asFuture(MyBean.<jk>class</jk>); 2561 * 2562 * <jc>// Do some other stuff.</jc> 2563 * 2564 * <jc>// Now read the response.</jc> 2565 * MyBean <jv>bean</jv> = <jv>myBeanFuture</jv>.get(); 2566 * </p> 2567 * 2568 * <ul class='seealso'> 2569 * <li class='jf'>{@link RestClient#RESTCLIENT_executorService} 2570 * <li class='jf'>{@link RestClient#RESTCLIENT_executorServiceShutdownOnClose} 2571 * </ul> 2572 * 2573 * @param executorService The executor service. 2574 * @param shutdownOnClose Call {@link ExecutorService#shutdown()} when {@link RestClient#close()} is called. 2575 * @return This object (for method chaining). 2576 */ 2577 @FluentSetter 2578 public RestClientBuilder executorService(ExecutorService executorService, boolean shutdownOnClose) { 2579 set(RESTCLIENT_executorService, executorService); 2580 set(RESTCLIENT_executorServiceShutdownOnClose, shutdownOnClose); 2581 return this; 2582 } 2583 2584 /** 2585 * <i><l>RestClient</l> configuration property: </i> Keep HttpClient open. 2586 * 2587 * <p> 2588 * Don't close this client when the {@link RestClient#close()} method is called. 2589 * 2590 * <h5 class='section'>Example:</h5> 2591 * <p class='bcode w800'> 2592 * <jc>// Create a client with a customized client and don't close the client service.</jc> 2593 * RestClient <jv>client</jv> = RestClient 2594 * .<jsm>create</jsm>() 2595 * .httpClient(<jv>myHttpClient</jv>) 2596 * .keepHttpClientOpen() 2597 * .build(); 2598 * 2599 * <jv>client</jv>.closeQuietly(); <jc>// Customized HttpClient won't be closed.</jc> 2600 * </p> 2601 * 2602 * <ul class='seealso'> 2603 * <li class='jf'>{@link RestClient#RESTCLIENT_keepHttpClientOpen} 2604 * </ul> 2605 * 2606 * @return This object (for method chaining). 2607 */ 2608 @FluentSetter 2609 public RestClientBuilder keepHttpClientOpen() { 2610 return set(RESTCLIENT_keepHttpClientOpen, true); 2611 } 2612 2613 /** 2614 * <i><l>RestClient</l> configuration property: </i> Ignore errors. 2615 * 2616 * <p> 2617 * When enabled, HTTP error response codes (e.g. <l>>=400</l>) will not cause a {@link RestCallException} to 2618 * be thrown. 2619 * <p> 2620 * Note that this is equivalent to <c>builder.errorCodes(x -> <jk>false</jk>);</c> 2621 * 2622 * <h5 class='section'>Example:</h5> 2623 * <p class='bcode w800'> 2624 * <jc>// Create a client that doesn't throws a RestCallException when a 500 error occurs.</jc> 2625 * RestClient 2626 * .<jsm>create</jsm>() 2627 * .ignoreErrors() 2628 * .build() 2629 * .get(<js>"/error"</js>) <jc>// Throws a 500 error</jc> 2630 * .run() 2631 * .assertStatus().is(500); 2632 * </p> 2633 * 2634 * <ul class='seealso'> 2635 * <li class='jf'>{@link RestClient#RESTCLIENT_ignoreErrors} 2636 * </ul> 2637 * 2638 * @return This object (for method chaining). 2639 */ 2640 @FluentSetter 2641 public RestClientBuilder ignoreErrors() { 2642 return ignoreErrors(true); 2643 } 2644 2645 /** 2646 * <i><l>RestClient</l> configuration property: </i> Ignore errors. 2647 * 2648 * <p> 2649 * When enabled, HTTP error response codes (e.g. <l>>=400</l>) will not cause a {@link RestCallException} to 2650 * be thrown. 2651 * <p> 2652 * Note that this is equivalent to <c>builder.errorCodes(x -> <jk>false</jk>);</c> 2653 * 2654 * <h5 class='section'>Example:</h5> 2655 * <p class='bcode w800'> 2656 * <jc>// Create a client that doesn't throws a RestCallException when a 500 error occurs.</jc> 2657 * RestClient 2658 * .<jsm>create</jsm>() 2659 * .ignoreErrors(<jk>true</jk>) 2660 * .build() 2661 * .get(<js>"/error"</js>) <jc>// Throws a 500 error</jc> 2662 * .run() 2663 * .assertStatus().is(500); 2664 * </p> 2665 * 2666 * <ul class='seealso'> 2667 * <li class='jf'>{@link RestClient#RESTCLIENT_ignoreErrors} 2668 * </ul> 2669 * 2670 * @param value The new value for this property. 2671 * @return This object (for method chaining). 2672 */ 2673 @FluentSetter 2674 public RestClientBuilder ignoreErrors(boolean value) { 2675 return set(RESTCLIENT_ignoreErrors, value); 2676 } 2677 2678 /** 2679 * <i><l>RestClient</l> configuration property: </i> Call interceptors. 2680 * 2681 * <p> 2682 * Adds an interceptor that can be called to hook into specified events in the lifecycle of a single request. 2683 * 2684 * <h5 class='section'>Example:</h5> 2685 * <p class='bcode w800'> 2686 * <jc>// Customized interceptor (note you can also extend from BasicRestCallInterceptor as well.</jc> 2687 * <jk>public class</jk> MyRestCallInterceptor <jk>implements</jk> RestCallInterceptor { 2688 * 2689 * <ja>@Override</ja> 2690 * <jk>public void</jk> onInit(RestRequest <jv>req</jv>) <jk>throws</jk> Exception { 2691 * <jc>// Intercept immediately after RestRequest object is created and all headers/query/form-data has been 2692 * // set on the request from the client.</jc> 2693 * } 2694 * 2695 * <ja>@Override</ja> 2696 * <jk>public void</jk> onConnect(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) <jk>throws</jk> Exception { 2697 * <jc>// Intercept immediately after an HTTP response has been received.</jc> 2698 * } 2699 * 2700 * <ja>@Override</ja> 2701 * <jk>public void</jk> onClose(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) <jk>throws</jk> Exception { 2702 * <jc>// Intercept when the response body is consumed.</jc> 2703 * } 2704 * } 2705 * 2706 * <jc>// Create a client with a customized interceptor.</jc> 2707 * RestClient <jv>client</jv> = RestClient 2708 * .<jsm>create</jsm>() 2709 * .interceptors(MyRestCallInterceptor.<jk>class</jk>) 2710 * .build(); 2711 * </p> 2712 * 2713 * <ul class='notes'> 2714 * <li>The {@link RestClient#onInit(RestRequest)}, {@link RestClient#onConnect(RestRequest,RestResponse)}, and 2715 * {@link RestClient#onClose(RestRequest,RestResponse)} methods can also be overridden to produce the same results. 2716 * </ul> 2717 * 2718 * <ul class='seealso'> 2719 * <li class='jf'>{@link RestClient#RESTCLIENT_interceptors} 2720 * </ul> 2721 * 2722 * @param values 2723 * The values to add to this setting. 2724 * <br>Can be implementations of any of the following: 2725 * <ul> 2726 * <li class='jic'>{@link RestCallInterceptor} 2727 * <li class='jic'>{@link HttpRequestInterceptor} 2728 * <li class='jic'>{@link HttpResponseInterceptor} 2729 * </ul> 2730 * @return This object (for method chaining). 2731 * @throws Exception If one or more interceptors could not be created. 2732 */ 2733 @FluentSetter 2734 public RestClientBuilder interceptors(Class<?>...values) throws Exception { 2735 for (Class<?> c : values) { 2736 ClassInfo ci = ClassInfo.of(c); 2737 if (ci != null) { 2738 if (ci.isChildOfAny(RestCallInterceptor.class, HttpRequestInterceptor.class, HttpResponseInterceptor.class)) 2739 interceptors(ci.newInstance()); 2740 else 2741 throw new ConfigException("Invalid class of type ''{0}'' passed to interceptors().", ci.getName()); 2742 } 2743 } 2744 return this; 2745 } 2746 2747 /** 2748 * <i><l>RestClient</l> configuration property: </i> Call interceptors. 2749 * 2750 * <p> 2751 * Adds an interceptor that gets called immediately after a connection is made. 2752 * 2753 * <h5 class='section'>Example:</h5> 2754 * <p class='bcode w800'> 2755 * <jc>// Create a client with a customized interceptor.</jc> 2756 * RestClient <jv>client</jv> = RestClient 2757 * .<jsm>create</jsm>() 2758 * .interceptors( 2759 * <jk>new</jk> RestCallInterceptor() { 2760 * 2761 * <ja>@Override</ja> 2762 * <jk>public void</jk> onInit(RestRequest <jv>req</jv>) <jk>throws</jk> Exception { 2763 * <jc>// Intercept immediately after RestRequest object is created and all headers/query/form-data has been 2764 * // set on the request from the client.</jc> 2765 * } 2766 * 2767 * <ja>@Override</ja> 2768 * <jk>public void</jk> onConnect(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) <jk>throws</jk> Exception { 2769 * <jc>// Intercept immediately after an HTTP response has been received.</jc> 2770 * } 2771 * 2772 * <ja>@Override</ja> 2773 * <jk>public void</jk> onClose(RestRequest <jv>req</jv>, RestResponse <jv>res</jv>) <jk>throws</jk> Exception { 2774 * <jc>// Intercept when the response body is consumed.</jc> 2775 * } 2776 * } 2777 * ) 2778 * .build(); 2779 * </p> 2780 * 2781 * <ul class='notes'> 2782 * <li>The {@link RestClient#onInit(RestRequest)}, {@link RestClient#onConnect(RestRequest,RestResponse)}, and 2783 * {@link RestClient#onClose(RestRequest,RestResponse)} methods can also be overridden to produce the same results. 2784 * </ul> 2785 * 2786 * <ul class='seealso'> 2787 * <li class='jf'>{@link RestClient#RESTCLIENT_interceptors} 2788 * </ul> 2789 * 2790 * @param value 2791 * The values to add to this setting. 2792 * <br>Can be implementations of any of the following: 2793 * <ul> 2794 * <li class='jic'>{@link RestCallInterceptor} 2795 * <li class='jic'>{@link HttpRequestInterceptor} 2796 * <li class='jic'>{@link HttpResponseInterceptor} 2797 * </ul> 2798 * @return This object (for method chaining). 2799 */ 2800 @FluentSetter 2801 public RestClientBuilder interceptors(Object...value) { 2802 List<RestCallInterceptor> l = new ArrayList<>(); 2803 for (Object o : value) { 2804 ClassInfo ci = ClassInfo.of(o); 2805 if (ci != null) { 2806 if (! ci.isChildOfAny(HttpRequestInterceptor.class, HttpResponseInterceptor.class, RestCallInterceptor.class)) 2807 throw new ConfigException("Invalid object of type ''{0}'' passed to interceptors().", ci.getName()); 2808 if (o instanceof HttpRequestInterceptor) 2809 addInterceptorLast((HttpRequestInterceptor)o); 2810 if (o instanceof HttpResponseInterceptor) 2811 addInterceptorLast((HttpResponseInterceptor)o); 2812 if (o instanceof RestCallInterceptor) 2813 l.add((RestCallInterceptor)o); 2814 } 2815 } 2816 return prependTo(RESTCLIENT_interceptors, l); 2817 } 2818 2819 /** 2820 * <i><l>RestClient</l> configuration property: </i> Enable leak detection. 2821 * 2822 * <p> 2823 * Enable client and request/response leak detection. 2824 * 2825 * <p> 2826 * Causes messages to be logged to the console if clients or request/response objects are not properly closed 2827 * when the <c>finalize</c> methods are invoked. 2828 * 2829 * <p> 2830 * Automatically enabled with {@link Context#CONTEXT_debug}. 2831 * 2832 * <h5 class='section'>Example:</h5> 2833 * <p class='bcode w800'> 2834 * <jc>// Create a client that logs a message if </jc> 2835 * RestClient <jv>client</jv> = RestClient 2836 * .<jsm>create</jsm>() 2837 * .leakDetection() 2838 * .logToConsole() <jc>// Also log the error message to System.err</jc> 2839 * .build(); 2840 * 2841 * <jv>client</jv>.closeQuietly(); <jc>// Customized HttpClient won't be closed.</jc> 2842 * </p> 2843 * 2844 * <ul class='seealso'> 2845 * <li class='jf'>{@link RestClient#RESTCLIENT_leakDetection} 2846 * </ul> 2847 * 2848 * @return This object (for method chaining). 2849 */ 2850 @FluentSetter 2851 public RestClientBuilder leakDetection() { 2852 return set(RESTCLIENT_leakDetection, true); 2853 } 2854 2855 /** 2856 * <i><l>RestClient</l> configuration property: </i> Marshall 2857 * 2858 * <p> 2859 * Shortcut for specifying the {@link RestClient#RESTCLIENT_serializers} and {@link RestClient#RESTCLIENT_parsers} 2860 * using the serializer and parser defined in a marshall. 2861 * 2862 * <ul class='notes'> 2863 * <li>When using this method that takes in a pre-instantiated serializers and parsers, the serializer property setters (e.g. {@link #sortCollections()}), 2864 * parser property setters (e.g. {@link #strict()}), bean context property setters (e.g. {@link #swaps(Object...)}), 2865 * or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class have no effect. 2866 * </ul> 2867 * 2868 * <h5 class='section'>Example:</h5> 2869 * <p class='bcode w800'> 2870 * <jc>// Create a client that uses Simplified-JSON transport using an existing marshall.</jc> 2871 * RestClient <jv>client</jv> = RestClient 2872 * .<jsm>create</jsm>() 2873 * .marshall(SimpleJson.<jsf>DEFAULT_READABLE</jsf>) 2874 * .build(); 2875 * </p> 2876 * 2877 * @param value The values to add to this setting. 2878 * @return This object (for method chaining). 2879 */ 2880 @FluentSetter 2881 public RestClientBuilder marshall(Marshall value) { 2882 if (value != null) 2883 serializer(value.getSerializer()).parser(value.getParser()); 2884 return this; 2885 } 2886 2887 /** 2888 * <i><l>RestClient</l> configuration property: </i> Marshalls 2889 * 2890 * <p> 2891 * Shortcut for specifying the {@link RestClient#RESTCLIENT_serializers} and {@link RestClient#RESTCLIENT_parsers} 2892 * using the serializer and parser defined in a marshall. 2893 * 2894 * <ul class='notes'> 2895 * <li>When using this method that takes in a pre-instantiated serializers and parsers, the serializer property setters (e.g. {@link #sortCollections()}), 2896 * parser property setters (e.g. {@link #strict()}), bean context property setters (e.g. {@link #swaps(Object...)}), 2897 * or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class have no effect. 2898 * </ul> 2899 * 2900 * <h5 class='section'>Example:</h5> 2901 * <p class='bcode w800'> 2902 * <jc>// Create a client that uses JSON and XML transport using existing marshalls.</jc> 2903 * RestClient <jv>client</jv> = RestClient 2904 * .<jsm>create</jsm>() 2905 * .marshall(Json.<jsf>DEFAULT_READABLE</jsf>, Xml.<jsf>DEFAULT_READABLE</jsf>) 2906 * .build(); 2907 * </p> 2908 * 2909 * @param value The values to add to this setting. 2910 * @return This object (for method chaining). 2911 */ 2912 @FluentSetter 2913 public RestClientBuilder marshalls(Marshall...value) { 2914 for (Marshall m : value) 2915 if (m != null) 2916 serializer(m.getSerializer()).parser(m.getParser()); 2917 return this; 2918 } 2919 2920 /** 2921 * <i><l>RestClient</l> configuration property: </i> Parser. 2922 * 2923 * <p> 2924 * Associates the specified {@link Parser Parser} with the HTTP client. 2925 * 2926 * <p> 2927 * The parser is used to parse the HTTP response body into a POJO. 2928 * 2929 * <ul class='notes'> 2930 * <li>When using this method that takes in a class, the parser can be configured using any of the parser property setters (e.g. {@link #strict()}), 2931 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 2932 * </ul> 2933 * 2934 * <h5 class='section'>Example:</h5> 2935 * <p class='bcode w800'> 2936 * <jc>// Create a client that uses JSON transport for response bodies.</jc> 2937 * RestClient <jv>client</jv> = RestClient 2938 * .<jsm>create</jsm>() 2939 * .parser(JsonParser.<jk>class</jk>) 2940 * .strict() <jc>// Enable strict mode on JsonParser.</jc> 2941 * .build(); 2942 * </p> 2943 * 2944 * <ul class='seealso'> 2945 * <li class='jf'>{@link RestClient#RESTCLIENT_parsers} 2946 * </ul> 2947 * 2948 * @param value 2949 * The new value for this setting. 2950 * <br>The default value is {@link JsonParser#DEFAULT}. 2951 * @return This object (for method chaining). 2952 */ 2953 @SuppressWarnings("unchecked") 2954 @FluentSetter 2955 public RestClientBuilder parser(Class<? extends Parser> value) { 2956 return parsers(value); 2957 } 2958 2959 /** 2960 * <i><l>RestClient</l> configuration property: </i> Parser. 2961 * 2962 * <p> 2963 * Associates the specified {@link Parser Parser} with the HTTP client. 2964 * 2965 * <p> 2966 * The parser is used to parse the HTTP response body into a POJO. 2967 * 2968 * <ul class='notes'> 2969 * <li>When using this method that takes in a pre-instantiated parser, the parser property setters (e.g. {@link #strict()}), 2970 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined 2971 * on this builder class have no effect. 2972 * </ul> 2973 * 2974 * <h5 class='section'>Example:</h5> 2975 * <p class='bcode w800'> 2976 * <jc>// Create a client that uses a predefined JSON parser for response bodies.</jc> 2977 * RestClient <jv>client</jv> = RestClient 2978 * .<jsm>create</jsm>() 2979 * .parser(JsonParser.<jsf>DEFAULT_STRICT</jsf>) 2980 * .build(); 2981 * </p> 2982 * 2983 * <ul class='seealso'> 2984 * <li class='jf'>{@link RestClient#RESTCLIENT_parsers} 2985 * </ul> 2986 * 2987 * @param value 2988 * The new value for this setting. 2989 * <br>The default value is {@link JsonParser#DEFAULT}. 2990 * @return This object (for method chaining). 2991 */ 2992 @FluentSetter 2993 public RestClientBuilder parser(Parser value) { 2994 return parsers(value); 2995 } 2996 2997 /** 2998 * <i><l>RestClient</l> configuration property: </i> Parsers. 2999 * 3000 * <p> 3001 * Associates the specified {@link Parser Parsers} with the HTTP client. 3002 * 3003 * <p> 3004 * The parsers are used to parse the HTTP response body into a POJO. 3005 * 3006 * <p> 3007 * The parser that best matches the <c>Accept</c> header will be used to parse the response body. 3008 * <br>If no <c>Accept</c> header is specified, the first parser in the list will be used. 3009 * 3010 * <ul class='notes'> 3011 * <li>When using this method that takes in classes, the parsers can be configured using any of the parser property setters (e.g. {@link #strict()}), 3012 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 3013 * </ul> 3014 * 3015 * <h5 class='section'>Example:</h5> 3016 * <p class='bcode w800'> 3017 * <jc>// Create a client that uses JSON and XML transport for response bodies.</jc> 3018 * RestClient <jv>client</jv> = RestClient 3019 * .<jsm>create</jsm>() 3020 * .parser(JsonParser.<jk>class</jk>, XmlParser.<jk>class</jk>) 3021 * .strict() <jc>// Enable strict mode on parsers.</jc> 3022 * .build(); 3023 * </p> 3024 * 3025 * <ul class='seealso'> 3026 * <li class='jf'>{@link RestClient#RESTCLIENT_parsers} 3027 * </ul> 3028 * 3029 * @param value 3030 * The new value for this setting. 3031 * <br>The default value is {@link JsonParser#DEFAULT}. 3032 * @return This object (for method chaining). 3033 */ 3034 @SuppressWarnings("unchecked") 3035 @FluentSetter 3036 public RestClientBuilder parsers(Class<? extends Parser>...value) { 3037 return prependTo(RESTCLIENT_parsers, value); 3038 } 3039 3040 /** 3041 * <i><l>RestClient</l> configuration property: </i> Parsers. 3042 * 3043 * <p> 3044 * Associates the specified {@link Parser Parsers} with the HTTP client. 3045 * 3046 * <p> 3047 * The parsers are used to parse the HTTP response body into a POJO. 3048 * 3049 * <p> 3050 * The parser that best matches the <c>Accept</c> header will be used to parse the response body. 3051 * <br>If no <c>Accept</c> header is specified, the first parser in the list will be used. 3052 * 3053 * <ul class='notes'> 3054 * <li>When using this method that takes in pre-instantiated parsers, the parser property setters (e.g. {@link #strict()}), 3055 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined 3056 * on this builder class have no effect. 3057 * </ul> 3058 * 3059 * <h5 class='section'>Example:</h5> 3060 * <p class='bcode w800'> 3061 * <jc>// Create a client that uses JSON and XML transport for response bodies.</jc> 3062 * RestClient <jv>client</jv> = RestClient 3063 * .<jsm>create</jsm>() 3064 * .parser(JsonParser.<jsf>DEFAULT_STRICT</jsf>, XmlParser.<jsf>DEFAULT</jsf>) 3065 * .build(); 3066 * </p> 3067 * 3068 * <ul class='seealso'> 3069 * <li class='jf'>{@link RestClient#RESTCLIENT_parsers} 3070 * </ul> 3071 * 3072 * @param value 3073 * The new value for this setting. 3074 * <br>The default value is {@link JsonParser#DEFAULT}. 3075 * @return This object (for method chaining). 3076 */ 3077 @FluentSetter 3078 public RestClientBuilder parsers(Parser...value) { 3079 return prependTo(RESTCLIENT_parsers, value); 3080 } 3081 3082 /** 3083 * <i><l>RestClient</l> configuration property: </i> Part parser. 3084 * 3085 * <p> 3086 * The parser to use for parsing POJOs from form data, query parameters, headers, and path variables. 3087 * 3088 * <p> 3089 * The default part parser is {@link OpenApiParser} which allows for schema-driven marshalling. 3090 * 3091 * <h5 class='section'>Example:</h5> 3092 * <p class='bcode w800'> 3093 * <jc>// Create a client that uses UON format by default for incoming HTTP parts.</jc> 3094 * RestClient <jv>client</jv> = RestClient 3095 * .<jsm>create</jsm>() 3096 * .partParser(UonParser.<jk>class</jk>) 3097 * .build(); 3098 * </p> 3099 * 3100 * <ul class='seealso'> 3101 * <li class='jf'>{@link RestClient#RESTCLIENT_partParser} 3102 * </ul> 3103 * 3104 * @param value 3105 * The new value for this setting. 3106 * <br>The default value is {@link OpenApiParser}. 3107 * @return This object (for method chaining). 3108 */ 3109 @FluentSetter 3110 public RestClientBuilder partParser(Class<? extends HttpPartParser> value) { 3111 return set(RESTCLIENT_partParser, value); 3112 } 3113 3114 /** 3115 * <i><l>RestClient</l> configuration property: </i> Part parser. 3116 * 3117 * <p> 3118 * The parser to use for parsing POJOs from form data, query parameters, headers, and path variables. 3119 * 3120 * <p> 3121 * The default part parser is {@link OpenApiParser} which allows for schema-driven marshalling. 3122 * 3123 * <h5 class='section'>Example:</h5> 3124 * <p class='bcode w800'> 3125 * <jc>// Create a client that uses UON format by default for incoming HTTP parts.</jc> 3126 * RestClient <jv>client</jv> = RestClient 3127 * .<jsm>create</jsm>() 3128 * .partParser(UonParser.<jsf>DEFAULT</jsf>) 3129 * .build(); 3130 * </p> 3131 * 3132 * <ul class='seealso'> 3133 * <li class='jf'>{@link RestClient#RESTCLIENT_partParser} 3134 * </ul> 3135 * 3136 * @param value 3137 * The new value for this setting. 3138 * <br>The default value is {@link OpenApiParser}. 3139 * @return This object (for method chaining). 3140 */ 3141 @FluentSetter 3142 public RestClientBuilder partParser(HttpPartParser value) { 3143 return set(RESTCLIENT_partParser, value); 3144 } 3145 3146 /** 3147 * <i><l>RestClient</l> configuration property: </i> Part serializer. 3148 * 3149 * <p> 3150 * The serializer to use for serializing POJOs in form data, query parameters, headers, and path variables. 3151 * 3152 * <p> 3153 * The default part serializer is {@link OpenApiSerializer} which allows for schema-driven marshalling. 3154 * 3155 * <h5 class='section'>Example:</h5> 3156 * <p class='bcode w800'> 3157 * <jc>// Create a client that uses UON format by default for outgoing HTTP parts.</jc> 3158 * RestClient <jv>client</jv> = RestClient 3159 * .<jsm>create</jsm>() 3160 * .partSerializer(UonSerializer.<jk>class</jk>) 3161 * .build(); 3162 * </p> 3163 * 3164 * <ul class='seealso'> 3165 * <li class='jf'>{@link RestClient#RESTCLIENT_partSerializer} 3166 * </ul> 3167 * 3168 * @param value 3169 * The new value for this setting. 3170 * <br>The default value is {@link OpenApiSerializer}. 3171 * @return This object (for method chaining). 3172 */ 3173 @FluentSetter 3174 public RestClientBuilder partSerializer(Class<? extends HttpPartSerializer> value) { 3175 return set(RESTCLIENT_partSerializer, value); 3176 } 3177 3178 /** 3179 * <i><l>RestClient</l> configuration property: </i> Part serializer. 3180 * 3181 * <p> 3182 * The serializer to use for serializing POJOs in form data, query parameters, headers, and path variables. 3183 * 3184 * <p> 3185 * The default part serializer is {@link OpenApiSerializer} which allows for schema-driven marshalling. 3186 * 3187 * <h5 class='section'>Example:</h5> 3188 * <p class='bcode w800'> 3189 * <jc>// Create a client that uses UON format by default for outgoing HTTP parts.</jc> 3190 * RestClient <jv>client</jv> = RestClient 3191 * .<jsm>create</jsm>() 3192 * .partSerializer(UonSerializer.<jsf>DEFAULT</jsf>) 3193 * .build(); 3194 * </p> 3195 * 3196 * <ul class='seealso'> 3197 * <li class='jf'>{@link RestClient#RESTCLIENT_partSerializer} 3198 * </ul> 3199 * 3200 * @param value 3201 * The new value for this setting. 3202 * <br>The default value is {@link OpenApiSerializer}. 3203 * @return This object (for method chaining). 3204 */ 3205 @FluentSetter 3206 public RestClientBuilder partSerializer(HttpPartSerializer value) { 3207 return set(RESTCLIENT_partSerializer, value); 3208 } 3209 3210 /** 3211 * <i><l>RestClient</l> configuration property: </i> Root URI. 3212 * 3213 * <p> 3214 * When set, relative URI strings passed in through the various rest call methods (e.g. {@link RestClient#get(Object)} 3215 * will be prefixed with the specified root. 3216 * <br>This root URI is ignored on those methods if you pass in a {@link URL}, {@link URI}, or an absolute URI string. 3217 * 3218 * <h5 class='section'>Example:</h5> 3219 * <p class='bcode w800'> 3220 * <jc>// Create a client that uses UON format by default for HTTP parts.</jc> 3221 * RestClient <jv>client</jv> = RestClient 3222 * .<jsm>create</jsm>() 3223 * .rootUri(<js>"http://localhost:10000/foo"</js>) 3224 * .build(); 3225 * 3226 * Bar <jv>bar</jv> = <jv>client</jv> 3227 * .get(<js>"/bar"</js>) <jc>// Relative to http://localhost:10000/foo</jc> 3228 * .run() 3229 * .getBody().as(Bar.<jk>class</jk>); 3230 * </p> 3231 * 3232 * <ul class='seealso'> 3233 * <li class='jf'>{@link RestClient#RESTCLIENT_rootUri} 3234 * </ul> 3235 * 3236 * @param value 3237 * The root URI to prefix to relative URI strings. 3238 * <br>Trailing slashes are trimmed. 3239 * <br>Usually a <c>String</c> but you can also pass in <c>URI</c> and <c>URL</c> objects as well. 3240 * @return This object (for method chaining). 3241 */ 3242 @FluentSetter 3243 public RestClientBuilder rootUri(Object value) { 3244 return set(RESTCLIENT_rootUri, value); 3245 } 3246 3247 /** 3248 * <i><l>RestClient</l> configuration property: </i> Serializer. 3249 * 3250 * <p> 3251 * Associates the specified {@link Serializer Serializer} with the HTTP client. 3252 * 3253 * <p> 3254 * The serializer is used to serialize POJOs into the HTTP request body. 3255 * 3256 * <ul class='notes'> 3257 * <li>When using this method that takes in a class, the serializer can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 3258 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 3259 * </ul> 3260 * 3261 * <h5 class='section'>Example:</h5> 3262 * <p class='bcode w800'> 3263 * <jc>// Create a client that uses JSON transport for request bodies.</jc> 3264 * RestClient <jv>client</jv> = RestClient 3265 * .<jsm>create</jsm>() 3266 * .serializer(JsonSerializer.<jk>class</jk>) 3267 * .sortCollections() <jc>// Sort any collections being serialized.</jc> 3268 * .build(); 3269 * </p> 3270 * 3271 * <ul class='seealso'> 3272 * <li class='jf'>{@link RestClient#RESTCLIENT_serializers} 3273 * </ul> 3274 * 3275 * @param value 3276 * The new value for this setting. 3277 * <br>The default is {@link JsonSerializer}. 3278 * @return This object (for method chaining). 3279 */ 3280 @SuppressWarnings("unchecked") 3281 @FluentSetter 3282 public RestClientBuilder serializer(Class<? extends Serializer> value) { 3283 return serializers(value); 3284 } 3285 3286 /** 3287 * <i><l>RestClient</l> configuration property: </i> Serializer. 3288 * 3289 * <p> 3290 * Associates the specified {@link Serializer Serializer} with the HTTP client. 3291 * 3292 * <p> 3293 * The serializer is used to serialize POJOs into the HTTP request body. 3294 * 3295 * <ul class='notes'> 3296 * <li>When using this method that takes in a pre-instantiated serializer, the serializer property setters (e.g. {@link #sortCollections()}), 3297 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined 3298 * on this builder class have no effect. 3299 * </ul> 3300 * 3301 * <h5 class='section'>Example:</h5> 3302 * <p class='bcode w800'> 3303 * <jc>// Create a client that uses a predefined JSON serializer request bodies.</jc> 3304 * RestClient <jv>client</jv> = RestClient 3305 * .<jsm>create</jsm>() 3306 * .serializer(JsonSerializer.<jsf>DEFAULT_READABLE</jsf>) 3307 * .build(); 3308 * </p> 3309 * 3310 * <ul class='seealso'> 3311 * <li class='jf'>{@link RestClient#RESTCLIENT_serializers} 3312 * </ul> 3313 * 3314 * @param value 3315 * The new value for this setting. 3316 * <br>The default is {@link JsonSerializer}. 3317 * @return This object (for method chaining). 3318 */ 3319 @FluentSetter 3320 public RestClientBuilder serializer(Serializer value) { 3321 return serializers(value); 3322 } 3323 3324 /** 3325 * <i><l>RestClient</l> configuration property: </i> Serializers. 3326 * 3327 * <p> 3328 * Associates the specified {@link Serializer Serializers} with the HTTP client. 3329 * 3330 * <p> 3331 * The serializer is used to serialize POJOs into the HTTP request body. 3332 * 3333 * <p> 3334 * The serializer that best matches the <c>Content-Type</c> header will be used to serialize the request body. 3335 * <br>If no <c>Content-Type</c> header is specified, the first serializer in the list will be used. 3336 * 3337 * <ul class='notes'> 3338 * <li>When using this method that takes in classes, the serializers can be configured using any of the serializer property setters (e.g. {@link #sortCollections()}), 3339 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined on this builder class. 3340 * </ul> 3341 * 3342 * <h5 class='section'>Example:</h5> 3343 * <p class='bcode w800'> 3344 * <jc>// Create a client that uses JSON and XML transport for request bodies.</jc> 3345 * RestClient <jv>client</jv> = RestClient 3346 * .<jsm>create</jsm>() 3347 * .serializers(JsonSerializer.<jk>class</jk>, XmlSerializer.<jk>class</jk>) 3348 * .sortCollections() <jc>// Sort any collections being serialized.</jc> 3349 * .build(); 3350 * </p> 3351 * 3352 * <ul class='seealso'> 3353 * <li class='jf'>{@link RestClient#RESTCLIENT_serializers} 3354 * </ul> 3355 * 3356 * @param value 3357 * The new value for this setting. 3358 * <br>The default is {@link JsonSerializer}. 3359 * @return This object (for method chaining). 3360 */ 3361 @SuppressWarnings("unchecked") 3362 @FluentSetter 3363 public RestClientBuilder serializers(Class<? extends Serializer>...value) { 3364 return prependTo(RESTCLIENT_serializers, value); 3365 } 3366 3367 /** 3368 * <i><l>RestClient</l> configuration property: </i> Serializers. 3369 * 3370 * <p> 3371 * Associates the specified {@link Serializer Serializers} with the HTTP client. 3372 * 3373 * <p> 3374 * The serializer is used to serialize POJOs into the HTTP request body. 3375 * 3376 * <p> 3377 * The serializer that best matches the <c>Content-Type</c> header will be used to serialize the request body. 3378 * <br>If no <c>Content-Type</c> header is specified, the first serializer in the list will be used. 3379 * 3380 * <ul class='notes'> 3381 * <li>When using this method that takes in a pre-instantiated serializers, the serializer property setters (e.g. {@link #sortCollections()}), 3382 * bean context property setters (e.g. {@link #swaps(Object...)}), or generic property setters (e.g. {@link #set(String, Object)}) defined 3383 * on this builder class have no effect. 3384 * </ul> 3385 * 3386 * <h5 class='section'>Example:</h5> 3387 * <p class='bcode w800'> 3388 * <jc>// Create a client that uses predefined JSON and XML serializers for request bodies.</jc> 3389 * RestClient <jv>client</jv> = RestClient 3390 * .<jsm>create</jsm>() 3391 * .serializers(JsonSerializer.<jsf>DEFAULT_READABLE</jsf>, XmlSerializer.<jsf>DEFAULT_READABLE</jsf>) 3392 * .build(); 3393 * </p> 3394 * 3395 * <ul class='seealso'> 3396 * <li class='jf'>{@link RestClient#RESTCLIENT_serializers} 3397 * </ul> 3398 * 3399 * @param value 3400 * The new value for this setting. 3401 * <br>The default is {@link JsonSerializer}. 3402 * @return This object (for method chaining). 3403 */ 3404 @FluentSetter 3405 public RestClientBuilder serializers(Serializer...value) { 3406 return prependTo(RESTCLIENT_serializers, value); 3407 } 3408 3409 //----------------------------------------------------------------------------------------------------------------- 3410 // BeanTraverse Properties 3411 //----------------------------------------------------------------------------------------------------------------- 3412 3413 /** 3414 * <i><l>BeanTraverse</l> configuration property: </i> Automatically detect POJO recursions. 3415 * 3416 * <p> 3417 * When enabled, specifies that recursions should be checked for during traversal. 3418 * 3419 * <p> 3420 * Recursions can occur when traversing models that aren't true trees but rather contain loops. 3421 * <br>In general, unchecked recursions cause stack-overflow-errors. 3422 * <br>These show up as {@link BeanRecursionException BeanRecursionException} with the message <js>"Depth too deep. Stack overflow occurred."</js>. 3423 * 3424 * <ul class='notes'> 3425 * <li> 3426 * Checking for recursion can cause a small performance penalty. 3427 * </ul> 3428 * 3429 * <h5 class='section'>Example:</h5> 3430 * <p class='bcode w800'> 3431 * <jc>// Create a JSON client that automatically checks for recursions.</jc> 3432 * RestClient <jv>client</jv> = RestClient 3433 * .<jsm>create</jsm>() 3434 * .json() 3435 * .detectRecursions() 3436 * .build(); 3437 * 3438 * <jc>// Create a POJO model with a recursive loop.</jc> 3439 * <jk>public class</jk> A { 3440 * <jk>public</jk> Object <jf>f</jf>; 3441 * } 3442 * A <jv>a</jv> = <jk>new</jk> A(); 3443 * <jv>a</jv>.<jf>f</jf> = <jv>a</jv>; 3444 * 3445 * <jk>try</jk> { 3446 * <jc>// Throws a RestCallException with an inner SerializeException and not a StackOverflowError</jc> 3447 * <jv>client</jv> 3448 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>a</jv>) 3449 * .run(); 3450 * } <jk>catch</jk> (RestCallException <jv>e</jv>} { 3451 * <jc>// Handle exception.</jc> 3452 * } 3453 * </p> 3454 * 3455 * <ul class='seealso'> 3456 * <li class='jf'>{@link BeanTraverseContext#BEANTRAVERSE_detectRecursions} 3457 * </ul> 3458 * 3459 * @return This object (for method chaining). 3460 */ 3461 @FluentSetter 3462 public RestClientBuilder detectRecursions() { 3463 return set(BEANTRAVERSE_detectRecursions, true); 3464 } 3465 3466 /** 3467 * <i><l>BeanTraverse</l> configuration property: </i> Ignore recursion errors. 3468 * 3469 * <p> 3470 * When enabled, when we encounter the same object when traversing a tree, we set the value to <jk>null</jk>. 3471 * 3472 * <p> 3473 * For example, if a model contains the links A->B->C->A, then the JSON generated will look like 3474 * the following when <jsf>BEANTRAVERSE_ignoreRecursions</jsf> is <jk>true</jk>... 3475 * 3476 * <p class='bcode w800'> 3477 * {A:{B:{C:<jk>null</jk>}}} 3478 * </p> 3479 * 3480 * <ul class='notes'> 3481 * <li> 3482 * Checking for recursion can cause a small performance penalty. 3483 * </ul> 3484 * 3485 * <h5 class='section'>Example:</h5> 3486 * <p class='bcode w800'> 3487 * <jc>// Create a JSON client that ignores recursions.</jc> 3488 * RestClient <jv>client</jv> = RestClient 3489 * .<jsm>create</jsm>() 3490 * .json() 3491 * .ignoreRecursions() 3492 * .build(); 3493 * 3494 * <jc>// Create a POJO model with a recursive loop.</jc> 3495 * <jk>public class</jk> A { 3496 * <jk>public</jk> Object <jf>f</jf>; 3497 * } 3498 * A <jv>a</jv> = <jk>new</jk> A(); 3499 * <jv>a</jv>.<jf>f</jf> = <jv>a</jv>; 3500 * 3501 * <jc>// Produces request body "{f:null}"</jc> 3502 * <jv>client</jv> 3503 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>a</jv>) 3504 * .run(); 3505 * </p> 3506 * 3507 * <ul class='seealso'> 3508 * <li class='jf'>{@link BeanTraverseContext#BEANTRAVERSE_ignoreRecursions} 3509 * </ul> 3510 * 3511 * @return This object (for method chaining). 3512 */ 3513 @FluentSetter 3514 public RestClientBuilder ignoreRecursions() { 3515 return set(BEANTRAVERSE_ignoreRecursions, true); 3516 } 3517 3518 /** 3519 * <i><l>BeanTraverse</l> configuration property: </i> Initial depth. 3520 * 3521 * <p> 3522 * The initial indentation level at the root. 3523 * 3524 * <p> 3525 * Useful when constructing document fragments that need to be indented at a certain level when whitespace is enabled. 3526 * 3527 * <h5 class='section'>Example:</h5> 3528 * <p class='bcode w800'> 3529 * <jc>// Create a REST client with JSON serializer with whitespace enabled and an initial depth of 2.</jc> 3530 * RestClient <jv>client</jv> = RestClient 3531 * .<jsm>create</jsm>() 3532 * .json() 3533 * .ws() 3534 * .initialDepth(2) 3535 * .build(); 3536 * 3537 * <jc>// Our bean to serialize.</jc> 3538 * <jk>public class</jk> MyBean { 3539 * <jk>public</jk> String <jf>foo</jf> = <jk>null</jk>; 3540 * } 3541 * 3542 * <jc>// Produces request body "\t\t{\n\t\t\t'foo':'bar'\n\t\t}\n"</jc> 3543 * <jv>client</jv> 3544 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 3545 * .run(); 3546 * </p> 3547 * 3548 * <ul class='seealso'> 3549 * <li class='jf'>{@link BeanTraverseContext#BEANTRAVERSE_initialDepth} 3550 * </ul> 3551 * 3552 * @param value 3553 * The new value for this property. 3554 * <br>The default is <c>0</c>. 3555 * @return This object (for method chaining). 3556 */ 3557 @FluentSetter 3558 public RestClientBuilder initialDepth(int value) { 3559 return set(BEANTRAVERSE_initialDepth, value); 3560 } 3561 3562 /** 3563 * <i><l>BeanTraverse</l> configuration property: </i> Max serialization depth. 3564 * 3565 * <p> 3566 * When enabled, abort traversal if specified depth is reached in the POJO tree. 3567 * 3568 * <p> 3569 * If this depth is exceeded, an exception is thrown. 3570 * 3571 * <p> 3572 * This prevents stack overflows from occurring when trying to traverse models with recursive references. 3573 * 3574 * <h5 class='section'>Example:</h5> 3575 * <p class='bcode w800'> 3576 * <jc>// Create a REST client with JSON serializer that throws an exception if the depth reaches greater than 20.</jc> 3577 * RestClient <jv>client</jv> = RestClient 3578 * .<jsm>create</jsm>() 3579 * .json() 3580 * .maxDepth(20) 3581 * .build(); 3582 * </p> 3583 * 3584 * <ul class='seealso'> 3585 * <li class='jf'>{@link BeanTraverseContext#BEANTRAVERSE_maxDepth} 3586 * </ul> 3587 * 3588 * @param value 3589 * The new value for this property. 3590 * <br>The default is <c>100</c>. 3591 * @return This object (for method chaining). 3592 */ 3593 @FluentSetter 3594 public RestClientBuilder maxDepth(int value) { 3595 return set(BEANTRAVERSE_maxDepth, value); 3596 } 3597 3598 //----------------------------------------------------------------------------------------------------------------- 3599 // Serializer Properties 3600 //----------------------------------------------------------------------------------------------------------------- 3601 3602 /** 3603 * <i><l>Serializer</l> configuration property: </i> Add <js>"_type"</js> properties when needed. 3604 * 3605 * <p> 3606 * When enabled, <js>"_type"</js> properties will be added to beans if their type cannot be inferred 3607 * through reflection. 3608 * 3609 * <p> 3610 * This is used to recreate the correct objects during parsing if the object types cannot be inferred. 3611 * <br>For example, when serializing a <c>Map<String,Object></c> field where the bean class cannot be determined from 3612 * the type of the values. 3613 * 3614 * <p> 3615 * Note the differences between the following settings: 3616 * <ul class='javatree'> 3617 * <li class='jf'>{@link #addRootType()} - Affects whether <js>'_type'</js> is added to root node. 3618 * <li class='jf'>{@link #addBeanTypes()} - Affects whether <js>'_type'</js> is added to any nodes. 3619 * </ul> 3620 * 3621 * <h5 class='section'>Example:</h5> 3622 * <p class='bcode w800'> 3623 * <jc>// Create a JSON client that adds _type to nodes in the request body.</jc> 3624 * RestClient <jv>client</jv> = RestClient 3625 * .<jsm>create</jsm>() 3626 * .json() 3627 * .addBeanTypes() 3628 * .build(); 3629 * 3630 * <jc>// Our map of beans to serialize.</jc> 3631 * <ja>@Bean</ja>(typeName=<js>"mybean"</js>) 3632 * <jk>public class</jk> MyBean { 3633 * <jk>public</jk> String <jf>foo</jf> = <js>"bar"</js>; 3634 * } 3635 * 3636 * AMap <jv>map</jv> = AMap.of(<js>"foo"</js>, <jk>new</jk> MyBean()); 3637 * 3638 * <jc>// Request body will contain: {"foo":{"_type":"mybean","foo":"bar"}}</jc> 3639 * <jv>client</jv> 3640 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>map</jv>) 3641 * .run(); 3642 * </p> 3643 * 3644 * <ul class='seealso'> 3645 * <li class='jf'>{@link Serializer#SERIALIZER_addBeanTypes} 3646 * </ul> 3647 * 3648 * @return This object (for method chaining). 3649 */ 3650 @FluentSetter 3651 public RestClientBuilder addBeanTypes() { 3652 return set(SERIALIZER_addBeanTypes, true); 3653 } 3654 3655 /** 3656 * <i><l>Serializer</l> configuration property: </i> Add type attribute to root nodes. 3657 * 3658 * <p> 3659 * When enabled, <js>"_type"</js> properties will be added to top-level beans. 3660 * 3661 * <p> 3662 * When disabled, it is assumed that the parser knows the exact Java POJO type being parsed, and therefore top-level 3663 * type information that might normally be included to determine the data type will not be serialized. 3664 * 3665 * <p> 3666 * For example, when serializing a top-level POJO with a {@link Bean#typeName() @Bean(typeName)} value, a 3667 * <js>'_type'</js> attribute will only be added when this setting is enabled. 3668 * 3669 * <p> 3670 * Note the differences between the following settings: 3671 * <ul class='javatree'> 3672 * <li class='jf'>{@link #addRootType()} - Affects whether <js>'_type'</js> is added to root node. 3673 * <li class='jf'>{@link #addBeanTypes()} - Affects whether <js>'_type'</js> is added to any nodes. 3674 * </ul> 3675 * 3676 * <h5 class='section'>Example:</h5> 3677 * <p class='bcode w800'> 3678 * <jc>// Create a JSON client that adds _type to root node.</jc> 3679 * RestClient <jv>client</jv> = RestClient 3680 * .<jsm>create</jsm>() 3681 * .json() 3682 * .addRootType() 3683 * .build(); 3684 * 3685 * <jc>// Our bean to serialize.</jc> 3686 * <ja>@Bean</ja>(typeName=<js>"mybean"</js>) 3687 * <jk>public class</jk> MyBean { 3688 * <jk>public</jk> String <jf>foo</jf> = <js>"bar"</js>; 3689 * } 3690 * 3691 * <jc>// Request body will contain: {"_type":"mybean","foo":"bar"}</jc> 3692 * <jv>client</jv> 3693 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 3694 * .run(); 3695 * </p> 3696 * 3697 * <ul class='seealso'> 3698 * <li class='jf'>{@link Serializer#SERIALIZER_addRootType} 3699 * </ul> 3700 * 3701 * @return This object (for method chaining). 3702 */ 3703 @FluentSetter 3704 public RestClientBuilder addRootType() { 3705 return set(SERIALIZER_addRootType, true); 3706 } 3707 3708 /** 3709 * <i><l>Serializer</l> configuration property: </i> Don't trim null bean property values. 3710 * 3711 * <p> 3712 * When enabled, null bean values will be serialized to the output. 3713 * 3714 * <ul class='notes'> 3715 * <li>Not enabling this setting will cause <c>Map</c>s with <jk>null</jk> values to be lost during parsing. 3716 * </ul> 3717 * 3718 * <h5 class='section'>Example:</h5> 3719 * <p class='bcode w800'> 3720 * <jc>// Create a REST client with JSON serializer that serializes null properties.</jc> 3721 * RestClient <jv>client</jv> = RestClient 3722 * .<jsm>create</jsm>() 3723 * .json() 3724 * .keepNullProperties() 3725 * .build(); 3726 * 3727 * <jc>// Our bean to serialize.</jc> 3728 * <jk>public class</jk> MyBean { 3729 * <jk>public</jk> String <jf>foo</jf> = <jk>null</jk>; 3730 * } 3731 * 3732 * <jc>// Request body will contain: {foo:null}</jc> 3733 * <jv>client</jv> 3734 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 3735 * .run(); 3736 * </p> 3737 * 3738 * <ul class='seealso'> 3739 * <li class='jf'>{@link Serializer#SERIALIZER_keepNullProperties} 3740 * </ul> 3741 * 3742 * @return This object (for method chaining). 3743 */ 3744 @FluentSetter 3745 public RestClientBuilder keepNullProperties() { 3746 return set(SERIALIZER_keepNullProperties, true); 3747 } 3748 3749 /** 3750 * <i><l>Serializer</l> configuration property: </i> Sort arrays and collections alphabetically. 3751 * 3752 * <p> 3753 * When enabled, copies and sorts the contents of arrays and collections before serializing them. 3754 * 3755 * <p> 3756 * Note that this introduces a performance penalty since it requires copying the existing collection. 3757 * 3758 * <h5 class='section'>Example:</h5> 3759 * <p class='bcode w800'> 3760 * <jc>// Create a REST client with JSON serializer that sorts arrays and collections before serialization.</jc> 3761 * RestClient <jv>client</jv> = RestClient 3762 * .<jsm>create</jsm>() 3763 * .json() 3764 * .sortCollections() 3765 * .build(); 3766 * 3767 * <jc>// An unsorted array</jc> 3768 * String[] <jv>array</jv> = {<js>"foo"</js>,<js>"bar"</js>,<js>"baz"</js>} 3769 * 3770 * <jc>// Request body will contain: ["bar","baz","foo"]</jc> 3771 * <jv>client</jv> 3772 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>array</jv>) 3773 * .run(); 3774 * </p> 3775 * 3776 * <ul class='seealso'> 3777 * <li class='jf'>{@link Serializer#SERIALIZER_sortCollections} 3778 * </ul> 3779 * 3780 * @return This object (for method chaining). 3781 */ 3782 @FluentSetter 3783 public RestClientBuilder sortCollections() { 3784 return set(SERIALIZER_sortCollections, true); 3785 } 3786 3787 /** 3788 * <i><l>Serializer</l> configuration property: </i> Sort maps alphabetically. 3789 * 3790 * <p> 3791 * When enabled, copies and sorts the contents of maps by their keys before serializing them. 3792 * 3793 * <p> 3794 * Note that this introduces a performance penalty. 3795 * 3796 * <h5 class='section'>Example:</h5> 3797 * <p class='bcode w800'> 3798 * <jc>// Create a REST client with JSON serializer that sorts maps before serialization.</jc> 3799 * RestClient <jv>client</jv> = RestClient 3800 * .<jsm>create</jsm>() 3801 * .json() 3802 * .sortMaps() 3803 * .build(); 3804 * 3805 * <jc>// An unsorted map.</jc> 3806 * AMap <jv>map</jv> = AMap.<jsm>of</jsm>(<js>"foo"</js>,1,<js>"bar"</js>,2,<js>"baz"</js>,3); 3807 * 3808 * <jc>// Request body will contain: {"bar":2,"baz":3,"foo":1}</jc> 3809 * <jv>client</jv> 3810 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>map</jv>) 3811 * .run(); 3812 * </p> 3813 * 3814 * <ul class='seealso'> 3815 * <li class='jf'>{@link Serializer#SERIALIZER_sortMaps} 3816 * </ul> 3817 * 3818 * @return This object (for method chaining). 3819 */ 3820 @FluentSetter 3821 public RestClientBuilder sortMaps() { 3822 return set(SERIALIZER_sortMaps, true); 3823 } 3824 3825 /** 3826 * <i><l>Serializer</l> configuration property: </i> Trim empty lists and arrays. 3827 * 3828 * <p> 3829 * When enabled, empty lists and arrays will not be serialized. 3830 * 3831 * <p> 3832 * Note that enabling this setting has the following effects on parsing: 3833 * <ul class='spaced-list'> 3834 * <li> 3835 * Map entries with empty list values will be lost. 3836 * <li> 3837 * Bean properties with empty list values will not be set. 3838 * </ul> 3839 * 3840 * <h5 class='section'>Example:</h5> 3841 * <p class='bcode w800'> 3842 * <jc>// Create a serializer that skips empty arrays and collections.</jc> 3843 * WriterSerializer <jv>s</jv> = JsonSerializer 3844 * .<jsm>create</jsm>() 3845 * .trimEmptyCollections() 3846 * .build(); 3847 * 3848 * <jc>// A bean with a field with an empty array.</jc> 3849 * <jk>public class</jk> MyBean { 3850 * <jk>public</jk> String[] <jf>foo</jf> = {}; 3851 * } 3852 * 3853 * <jc>// Request body will contain: {}</jc> 3854 * <jv>client</jv> 3855 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 3856 * .run(); 3857 * </p> 3858 * 3859 * <ul class='seealso'> 3860 * <li class='jf'>{@link Serializer#SERIALIZER_trimEmptyCollections} 3861 * </ul> 3862 * 3863 * @return This object (for method chaining). 3864 */ 3865 @FluentSetter 3866 public RestClientBuilder trimEmptyCollections() { 3867 return set(SERIALIZER_trimEmptyCollections, true); 3868 } 3869 3870 /** 3871 * <i><l>Serializer</l> configuration property: </i> Trim empty maps. 3872 * 3873 * <p> 3874 * When enabled, empty map values will not be serialized to the output. 3875 * 3876 * <p> 3877 * Note that enabling this setting has the following effects on parsing: 3878 * <ul class='spaced-list'> 3879 * <li> 3880 * Bean properties with empty map values will not be set. 3881 * </ul> 3882 * 3883 * <h5 class='section'>Example:</h5> 3884 * <p class='bcode w800'> 3885 * <jc>// Create a REST client with JSON serializer that skips empty maps.</jc> 3886 * RestClient <jv>client</jv> = RestClient 3887 * .<jsm>create</jsm>() 3888 * .json() 3889 * .trimEmptyMaps() 3890 * .build(); 3891 * 3892 * <jc>// A bean with a field with an empty map.</jc> 3893 * <jk>public class</jk> MyBean { 3894 * <jk>public</jk> AMap <jf>foo</jf> = AMap.<jsm>of</jsm>(); 3895 * } 3896 * 3897 * <jc>// Request body will contain: {}</jc> 3898 * <jv>client</jv> 3899 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 3900 * .run(); 3901 * </p> 3902 * 3903 * <ul class='seealso'> 3904 * <li class='jf'>{@link Serializer#SERIALIZER_trimEmptyMaps} 3905 * </ul> 3906 * 3907 * @return This object (for method chaining). 3908 */ 3909 @FluentSetter 3910 public RestClientBuilder trimEmptyMaps() { 3911 return set(SERIALIZER_trimEmptyMaps, true); 3912 } 3913 3914 /** 3915 * <i><l>Serializer</l> configuration property: </i> Trim strings. 3916 * 3917 * <p> 3918 * When enabled, string values will be trimmed of whitespace using {@link String#trim()} before being serialized. 3919 * 3920 * <h5 class='section'>Example:</h5> 3921 * <p class='bcode w800'> 3922 * <jc>// Create a REST client with JSON serializer that trims strings before serialization.</jc> 3923 * RestClient <jv>client</jv> = RestClient 3924 * .<jsm>create</jsm>() 3925 * .json() 3926 * .trimStrings() 3927 * .build(); 3928 * 3929 * <jc>// A map with space-padded keys/values</jc> 3930 * AMap <jv>map</jv> = AMap.<jsm>of</jsm>(<js>" foo "</js>, <js>" bar "</js>); 3931 * 3932 * <jc>// Request body will contain: {"foo":"bar"}</jc> 3933 * <jv>client</jv> 3934 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>map</jv>) 3935 * .run(); 3936 * </p> 3937 * 3938 * <ul class='seealso'> 3939 * <li class='jf'>{@link Serializer#SERIALIZER_trimStrings} 3940 * </ul> 3941 * 3942 * @return This object (for method chaining). 3943 */ 3944 @FluentSetter 3945 public RestClientBuilder trimStringsOnWrite() { 3946 return set(SERIALIZER_trimStrings, true); 3947 } 3948 3949 /** 3950 * <i><l>Serializer</l> configuration property: </i> URI context bean. 3951 * 3952 * <p> 3953 * Bean used for resolution of URIs to absolute or root-relative form. 3954 * 3955 * <h5 class='section'>Example:</h5> 3956 * <p class='bcode w800'> 3957 * <jc>// Our URI contextual information.</jc> 3958 * String <jv>authority</jv> = <js>"http://localhost:10000"</js>; 3959 * String <jv>contextRoot</jv> = <js>"/myContext"</js>; 3960 * String <jv>servletPath</jv> = <js>"/myServlet"</js>; 3961 * String <jv>pathInfo</jv> = <js>"/foo"</js>; 3962 * 3963 * <jc>// Create a UriContext object.</jc> 3964 * UriContext <jv>uriContext</jv> = <jk>new</jk> UriContext(<jv>authority</jv>, <jv>contextRoot</jv>, <jv>servletPath</jv>, <jv>pathInfo</jv>); 3965 * 3966 * <jc>// Create a REST client with JSON serializer and associate our context.</jc> 3967 * RestClient <jv>client</jv> = RestClient 3968 * .<jsm>create</jsm>() 3969 * .json() 3970 * .uriContext(<jv>uriContext</jv>) 3971 * .uriRelativity(<jsf>RESOURCE</jsf>) <jc>// Assume relative paths are relative to servlet.</jc> 3972 * .uriResolution(<jsf>ABSOLUTE</jsf>) <jc>// Serialize URIs as absolute paths.</jc> 3973 * .build(); 3974 * 3975 * <jc>// A relative URI</jc> 3976 * URI <jv>uri</jv> = <jk>new</jk> URI(<js>"bar"</js>); 3977 * 3978 * <jc>// Request body will contain: "http://localhost:10000/myContext/myServlet/foo/bar"</jc> 3979 * <jv>client</jv> 3980 * .doPost(<js>"http://localhost:10000/foo"</js>, <jv>uri</jv>) 3981 * .run(); 3982 * </p> 3983 * 3984 * <ul class='seealso'> 3985 * <li class='jf'>{@link Serializer#SERIALIZER_uriContext} 3986 * <li class='link'>{@doc MarshallingUris} 3987 * </ul> 3988 * 3989 * @param value The new value for this property. 3990 * @return This object (for method chaining). 3991 */ 3992 @FluentSetter 3993 public RestClientBuilder uriContext(UriContext value) { 3994 return set(SERIALIZER_uriContext, value); 3995 } 3996 3997 /** 3998 * <i><l>Serializer</l> configuration property: </i> URI relativity. 3999 * 4000 * <p> 4001 * Defines what relative URIs are relative to when serializing any of the following: 4002 * <ul> 4003 * <li>{@link java.net.URI} 4004 * <li>{@link java.net.URL} 4005 * <li>Properties and classes annotated with {@link org.apache.juneau.annotation.URI @URI} 4006 * </ul> 4007 * 4008 * <p> 4009 * Possible values are: 4010 * <ul class='javatree'> 4011 * <li class='jf'>{@link org.apache.juneau.UriRelativity#RESOURCE} 4012 * - Relative URIs should be considered relative to the servlet URI. 4013 * <li class='jf'>{@link org.apache.juneau.UriRelativity#PATH_INFO} 4014 * - Relative URIs should be considered relative to the request URI. 4015 * </ul> 4016 * 4017 * <p> 4018 * See {@link #uriContext(UriContext)} for examples. 4019 * 4020 * <ul class='seealso'> 4021 * <li class='jf'>{@link Serializer#SERIALIZER_uriRelativity} 4022 * <li class='link'>{@doc MarshallingUris} 4023 * </ul> 4024 * 4025 * @param value 4026 * The new value for this property. 4027 * <br>The default is {@link UriRelativity#RESOURCE} 4028 * @return This object (for method chaining). 4029 */ 4030 @FluentSetter 4031 public RestClientBuilder uriRelativity(UriRelativity value) { 4032 return set(SERIALIZER_uriRelativity, value); 4033 } 4034 4035 /** 4036 * <i><l>Serializer</l> configuration property: </i> URI resolution. 4037 * 4038 * <p> 4039 * Defines the resolution level for URIs when serializing any of the following: 4040 * <ul> 4041 * <li>{@link java.net.URI} 4042 * <li>{@link java.net.URL} 4043 * <li>Properties and classes annotated with {@link org.apache.juneau.annotation.URI @URI} 4044 * </ul> 4045 * 4046 * <p> 4047 * Possible values are: 4048 * <ul> 4049 * <li class='jf'>{@link UriResolution#ABSOLUTE} 4050 * - Resolve to an absolute URI (e.g. <js>"http://host:port/context-root/servlet-path/path-info"</js>). 4051 * <li class='jf'>{@link UriResolution#ROOT_RELATIVE} 4052 * - Resolve to a root-relative URI (e.g. <js>"/context-root/servlet-path/path-info"</js>). 4053 * <li class='jf'>{@link UriResolution#NONE} 4054 * - Don't do any URI resolution. 4055 * </ul> 4056 * 4057 * <p> 4058 * See {@link #uriContext(UriContext)} for examples. 4059 * 4060 * <ul class='seealso'> 4061 * <li class='jf'>{@link Serializer#SERIALIZER_uriResolution} 4062 * <li class='link'>{@doc MarshallingUris} 4063 * </ul> 4064 * 4065 * @param value 4066 * The new value for this property. 4067 * <br>The default is {@link UriResolution#NONE} 4068 * @return This object (for method chaining). 4069 */ 4070 @FluentSetter 4071 public RestClientBuilder uriResolution(UriResolution value) { 4072 return set(SERIALIZER_uriResolution, value); 4073 } 4074 4075 //----------------------------------------------------------------------------------------------------------------- 4076 // WriterSerializer Properties 4077 //----------------------------------------------------------------------------------------------------------------- 4078 4079 /** 4080 * <i><l>WriterSerializer</l> configuration property: </i> Maximum indentation. 4081 * 4082 * <p> 4083 * Specifies the maximum indentation level in the serialized document. 4084 * 4085 * <ul class='notes'> 4086 * <li>This setting does not apply to the RDF serializers. 4087 * </ul> 4088 * 4089 * <h5 class='section'>Example:</h5> 4090 * <p class='bcode w800'> 4091 * <jc>// Create a REST client with JSON serializer that indents a maximum of 20 tabs.</jc> 4092 * RestClient <jv>client</jv> = RestClient 4093 * .<jsm>create</jsm>() 4094 * .json() 4095 * .ws() <jc>// Enable whitespace</jc> 4096 * .maxIndent(20) 4097 * .build(); 4098 * </p> 4099 * 4100 * <ul class='seealso'> 4101 * <li class='jf'>{@link WriterSerializer#WSERIALIZER_maxIndent} 4102 * </ul> 4103 * 4104 * @param value 4105 * The new value for this property. 4106 * <br>The default is <c>100</c>. 4107 * @return This object (for method chaining). 4108 */ 4109 @FluentSetter 4110 public RestClientBuilder maxIndent(int value) { 4111 return set(WSERIALIZER_maxIndent, value); 4112 } 4113 4114 /** 4115 * <i><l>WriterSerializer</l> configuration property: </i> Quote character. 4116 * 4117 * <p> 4118 * Specifies the character to use for quoting attributes and values. 4119 * 4120 * <ul class='notes'> 4121 * <li>This setting does not apply to the RDF serializers. 4122 * </ul> 4123 * 4124 * <h5 class='section'>Example:</h5> 4125 * <p class='bcode w800'> 4126 * <jc>// Create a REST client with JSON serializer that uses single quotes.</jc> 4127 * RestClient <jv>client</jv> = RestClient 4128 * .<jsm>create</jsm>() 4129 * .json() 4130 * .quoteChar(<js>'\''</js>) 4131 * .build(); 4132 * 4133 * <jc>// A bean with a single property</jc> 4134 * <jk>public class</jk> MyBean { 4135 * <jk>public</jk> String <jf>foo</jf> = <js>"bar"</js>; 4136 * } 4137 * 4138 * <jc>// Request body will contain: {'foo':'bar'}</jc> 4139 * <jv>client</jv> 4140 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 4141 * .run(); 4142 * </p> 4143 * 4144 * <ul class='seealso'> 4145 * <li class='jf'>{@link WriterSerializer#WSERIALIZER_quoteChar} 4146 * </ul> 4147 * 4148 * @param value 4149 * The new value for this property. 4150 * <br>The default is <js>'"'</js>. 4151 * @return This object (for method chaining). 4152 */ 4153 @FluentSetter 4154 public RestClientBuilder quoteChar(char value) { 4155 return set(WSERIALIZER_quoteChar, value); 4156 } 4157 4158 /** 4159 * <i><l>WriterSerializer</l> configuration property: </i> Quote character. 4160 * 4161 * <p> 4162 * Specifies to use single quotes for quoting attributes and values. 4163 * 4164 * <ul class='notes'> 4165 * <li>This setting does not apply to the RDF serializers. 4166 * </ul> 4167 * 4168 * <h5 class='section'>Example:</h5> 4169 * <p class='bcode w800'> 4170 * <jc>// Create a REST client with JSON serializer that uses single quotes.</jc> 4171 * RestClient <jv>client</jv> = RestClient 4172 * .<jsm>create</jsm>() 4173 * .json() 4174 * .sq() 4175 * .build(); 4176 * 4177 * <jc>// A bean with a single property</jc> 4178 * <jk>public class</jk> MyBean { 4179 * <jk>public</jk> String <jf>foo</jf> = <js>"bar"</js>; 4180 * } 4181 * 4182 * <jc>// Request body will contain: {'foo':'bar'}</jc> 4183 * <jv>client</jv> 4184 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 4185 * .run(); 4186 * </p> 4187 * 4188 * <ul class='seealso'> 4189 * <li class='jf'>{@link WriterSerializer#WSERIALIZER_quoteChar} 4190 * </ul> 4191 * 4192 * @return This object (for method chaining). 4193 */ 4194 @FluentSetter 4195 public RestClientBuilder sq() { 4196 return set(WSERIALIZER_quoteChar, '\''); 4197 } 4198 4199 /** 4200 * <i><l>WriterSerializer</l> configuration property: </i> Use whitespace. 4201 * 4202 * <p> 4203 * When enabled, whitespace is added to the output to improve readability. 4204 * 4205 * <h5 class='section'>Example:</h5> 4206 * <p class='bcode w800'> 4207 * <jc>// Create a REST client with JSON serializer with whitespace enabled.</jc> 4208 * RestClient <jv>client</jv> = RestClient 4209 * .<jsm>create</jsm>() 4210 * .json() 4211 * .useWhitespace() 4212 * .build(); 4213 * 4214 * <jc>// A bean with a single property</jc> 4215 * <jk>public class</jk> MyBean { 4216 * <jk>public</jk> String <jf>foo</jf> = <js>"bar"</js>; 4217 * } 4218 * 4219 * <jc>// Request body will contain: {\n\t"foo": "bar"\n\}\n</jc> 4220 * <jv>client</jv> 4221 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 4222 * .run(); 4223 * </p> 4224 * 4225 * <ul class='seealso'> 4226 * <li class='jf'>{@link WriterSerializer#WSERIALIZER_useWhitespace} 4227 * </ul> 4228 * @return This object (for method chaining). 4229 */ 4230 @FluentSetter 4231 public RestClientBuilder useWhitespace() { 4232 return set(WSERIALIZER_useWhitespace, true); 4233 } 4234 4235 /** 4236 * <i><l>WriterSerializer</l> configuration property: </i> Use whitespace. 4237 * 4238 * <p> 4239 * When enabled, whitespace is added to the output to improve readability. 4240 * 4241 * <h5 class='section'>Example:</h5> 4242 * <p class='bcode w800'> 4243 * <jc>// Create a REST client with JSON serializer with whitespace enabled.</jc> 4244 * RestClient <jv>client</jv> = RestClient 4245 * .<jsm>create</jsm>() 4246 * .json() 4247 * .ws() 4248 * .build(); 4249 * 4250 * <jc>// A bean with a single property</jc> 4251 * <jk>public class</jk> MyBean { 4252 * <jk>public</jk> String <jf>foo</jf> = <js>"bar"</js>; 4253 * } 4254 * 4255 * <jc>// Request body will contain: {\n\t"foo": "bar"\n\}\n</jc> 4256 * <jv>client</jv> 4257 * .doPost(<js>"http://localhost:10000/foo"</js>, <jk>new</jk> MyBean()) 4258 * .run(); 4259 * </p> 4260 * 4261 * <ul class='seealso'> 4262 * <li class='jf'>{@link WriterSerializer#WSERIALIZER_useWhitespace} 4263 * </ul> 4264 * 4265 * @return This object (for method chaining). 4266 */ 4267 @FluentSetter 4268 public RestClientBuilder ws() { 4269 return set(WSERIALIZER_useWhitespace, true); 4270 } 4271 4272 //----------------------------------------------------------------------------------------------------------------- 4273 // OutputStreamSerializer Properties 4274 //----------------------------------------------------------------------------------------------------------------- 4275 4276 //----------------------------------------------------------------------------------------------------------------- 4277 // Parser Properties 4278 //----------------------------------------------------------------------------------------------------------------- 4279 4280 /** 4281 * <i><l>Parser</l> configuration property: </i> Debug output lines. 4282 * 4283 * <p> 4284 * When parse errors occur, this specifies the number of lines of input before and after the 4285 * error location to be printed as part of the exception message. 4286 * 4287 * <h5 class='section'>Example:</h5> 4288 * <p class='bcode w800'> 4289 * <jc>// Create a parser whose exceptions print out 100 lines before and after the parse error location.</jc> 4290 * RestClient <jv>client</jv> = RestClient 4291 * .<jsm>create</jsm>() 4292 * .json() 4293 * .debug() <jc>// Enable debug mode to capture Reader contents as strings.</jc> 4294 * .debugOuputLines(100) 4295 * .build(); 4296 * 4297 * <jc>// Try to parse some bad JSON.</jc> 4298 * <jk>try</jk> { 4299 * <jv>client</jv> 4300 * .get(<js>"/pathToBadJson"</js>) 4301 * .run() 4302 * .getBody().as(Object.<jk>class</jk>); <jc>// Try to parse it.</jc> 4303 * } <jk>catch</jk> (RestCallException <jv>e</jv>) { 4304 * System.<jsf>err</jsf>.println(<jv>e</jv>.getMessage()); <jc>// Will display 200 lines of the output.</jc> 4305 * } 4306 * </p> 4307 * 4308 * <ul class='seealso'> 4309 * <li class='jf'>{@link Parser#PARSER_debugOutputLines} 4310 * </ul> 4311 * 4312 * @param value 4313 * The new value for this property. 4314 * <br>The default value is <c>5</c>. 4315 * @return This object (for method chaining). 4316 */ 4317 @FluentSetter 4318 public RestClientBuilder debugOutputLines(int value) { 4319 set(PARSER_debugOutputLines, value); 4320 return this; 4321 } 4322 4323 /** 4324 * <i><l>Parser</l> configuration property: </i> Strict mode. 4325 * 4326 * <p> 4327 * When enabled, strict mode for the parser is enabled. 4328 * 4329 * <p> 4330 * Strict mode can mean different things for different parsers. 4331 * 4332 * <table class='styled'> 4333 * <tr><th>Parser class</th><th>Strict behavior</th></tr> 4334 * <tr> 4335 * <td>All reader-based parsers</td> 4336 * <td> 4337 * When enabled, throws {@link ParseException ParseExceptions} on malformed charset input. 4338 * Otherwise, malformed input is ignored. 4339 * </td> 4340 * </tr> 4341 * <tr> 4342 * <td>{@link JsonParser}</td> 4343 * <td> 4344 * When enabled, throws exceptions on the following invalid JSON syntax: 4345 * <ul> 4346 * <li>Unquoted attributes. 4347 * <li>Missing attribute values. 4348 * <li>Concatenated strings. 4349 * <li>Javascript comments. 4350 * <li>Numbers and booleans when Strings are expected. 4351 * <li>Numbers valid in Java but not JSON (e.g. octal notation, etc...) 4352 * </ul> 4353 * </td> 4354 * </tr> 4355 * </table> 4356 * 4357 * <h5 class='section'>Example:</h5> 4358 * <p class='bcode w800'> 4359 * <jc>// Create a REST client with JSON parser using strict mode.</jc> 4360 * RestClient <jv>client</jv> = RestClient 4361 * .<jsm>create</jsm>() 4362 * .json() 4363 * .strict() 4364 * .build(); 4365 * 4366 * <jc>// Try to parse some bad JSON.</jc> 4367 * <jk>try</jk> { 4368 * <jv>client</jv> 4369 * .get(<js>"/pathToBadJson"</js>) 4370 * .run() 4371 * .getBody().as(Object.<jk>class</jk>); <jc>// Try to parse it.</jc> 4372 * } <jk>catch</jk> (RestCallException <jv>e</jv>) { 4373 * <jc>// Handle exception.</jc> 4374 * } 4375 * </p> 4376 * 4377 * <ul class='seealso'> 4378 * <li class='jf'>{@link Parser#PARSER_strict} 4379 * </ul> 4380 * 4381 * @return This object (for method chaining). 4382 */ 4383 @FluentSetter 4384 public RestClientBuilder strict() { 4385 return set(PARSER_strict, true); 4386 } 4387 4388 /** 4389 * <i><l>Parser</l> configuration property: </i> Trim parsed strings. 4390 * 4391 * <p> 4392 * When enabled, string values will be trimmed of whitespace using {@link String#trim()} before being added to 4393 * the POJO. 4394 * 4395 * <h5 class='section'>Example:</h5> 4396 * <p class='bcode w800'> 4397 * <jc>// Create a REST client with JSON parser with trim-strings enabled.</jc> 4398 * RestClient <jv>client</jv> = RestClient 4399 * .<jsm>create</jsm>() 4400 * .json() 4401 * .trimStringsOnRead() 4402 * .build(); 4403 * 4404 * <jc>// Try to parse JSON containing {" foo ":" bar "}.</jc> 4405 * Map<String,String> <jv>map</jv> = <jv>client</jv> 4406 * .get(<js>"/pathToJson"</js>) 4407 * .run() 4408 * .getBody().as(HashMap.<jk>class</jk>, String.<jk>class</jk>, String.<jk>class</jk>); 4409 * 4410 * <jc>// Make sure strings are trimmed.</jc> 4411 * <jsm>assertEquals</jsm>(<js>"bar"</js>, <jv>map</jv>.get(<js>"foo"</js>)); 4412 * </p> 4413 * 4414 * <ul class='seealso'> 4415 * <li class='jf'>{@link Parser#PARSER_trimStrings} 4416 * </ul> 4417 * 4418 * @return This object (for method chaining). 4419 */ 4420 @FluentSetter 4421 public RestClientBuilder trimStringsOnRead() { 4422 return set(PARSER_trimStrings, true); 4423 } 4424 4425 //----------------------------------------------------------------------------------------------------------------- 4426 // ReaderParser Properties 4427 //----------------------------------------------------------------------------------------------------------------- 4428 4429 //----------------------------------------------------------------------------------------------------------------- 4430 // InputStreamParser Properties 4431 //----------------------------------------------------------------------------------------------------------------- 4432 4433 //----------------------------------------------------------------------------------------------------------------- 4434 // OpenApi Properties 4435 //----------------------------------------------------------------------------------------------------------------- 4436 4437 /** 4438 * <i><l>OpenApiCommon</l> configuration property: </i> Default OpenAPI format for HTTP parts. 4439 * 4440 * <p> 4441 * Specifies the format to use for HTTP parts when not otherwise specified via {@link org.apache.juneau.jsonschema.annotation.Schema#format()} for 4442 * the OpenAPI serializer and parser on this client. 4443 * 4444 * <p> 4445 * Possible values: 4446 * <ul class='javatree'> 4447 * <li class='jc'>{@link org.apache.juneau.httppart.HttpPartFormat} 4448 * <ul> 4449 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#UON UON} - UON notation (e.g. <js>"'foo bar'"</js>). 4450 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#INT32 INT32} - Signed 32 bits. 4451 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#INT64 INT64} - Signed 64 bits. 4452 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#FLOAT FLOAT} - 32-bit floating point number. 4453 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#DOUBLE DOUBLE} - 64-bit floating point number. 4454 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#BYTE BYTE} - BASE-64 encoded characters. 4455 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#BINARY BINARY} - Hexadecimal encoded octets (e.g. <js>"00FF"</js>). 4456 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#BINARY_SPACED BINARY_SPACED} - Spaced-separated hexadecimal encoded octets (e.g. <js>"00 FF"</js>). 4457 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#DATE DATE} - An <a href='http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14'>RFC3339 full-date</a>. 4458 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#DATE_TIME DATE_TIME} - An <a href='http://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14'>RFC3339 date-time</a>. 4459 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#PASSWORD PASSWORD} - Used to hint UIs the input needs to be obscured. 4460 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartFormat#NO_FORMAT NO_FORMAT} - (default) Not specified. 4461 * </ul> 4462 * </ul> 4463 * 4464 * <h5 class='section'>Example:</h5> 4465 * <p class='bcode w800'> 4466 * <jc>// Create a REST client with UON part serialization and parsing.</jc> 4467 * RestClient client = RestClient 4468 * .<jsm>create</jsm>() 4469 * .oapiFormat(<jsf>UON</jsf>) 4470 * .build(); 4471 * 4472 * <jc>// Set a header with a value in UON format.</jc> 4473 * <jv>client</jv> 4474 * .get(<js>"/uri"</js>) 4475 * .header(<js>"Foo"</js>, <js>"bar baz"</js>) <jc>// Will be serialized as: 'bar baz'</jc> 4476 * .run(); 4477 * </p> 4478 * 4479 * <ul class='seealso'> 4480 * <li class='jf'>{@link OpenApiCommon#OAPI_format} 4481 * </ul> 4482 * 4483 * @param value 4484 * The new value for this property. 4485 * <br>The default value is {@link HttpPartFormat#NO_FORMAT}. 4486 * @return This object (for method chaining). 4487 */ 4488 @FluentSetter 4489 public RestClientBuilder oapiFormat(HttpPartFormat value) { 4490 return set(OAPI_format, value); 4491 } 4492 4493 /** 4494 * <i><l>OpenApiCommon</l> configuration property: </i> Default collection format for HTTP parts. 4495 * 4496 * <p> 4497 * Specifies the collection format to use for HTTP parts when not otherwise specified via {@link org.apache.juneau.jsonschema.annotation.Schema#collectionFormat()} for the 4498 * OpenAPI serializer and parser on this client. 4499 * 4500 * <p> 4501 * Possible values: 4502 * <ul class='javatree'> 4503 * <li class='jc'>{@link org.apache.juneau.httppart.HttpPartFormat} 4504 * <ul> 4505 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartCollectionFormat#CSV CSV} - (default) Comma-separated values (e.g. <js>"foo,bar"</js>). 4506 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartCollectionFormat#SSV SSV} - Space-separated values (e.g. <js>"foo bar"</js>). 4507 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartCollectionFormat#TSV TSV} - Tab-separated values (e.g. <js>"foo\tbar"</js>). 4508 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartCollectionFormat#PIPES PIPES} - Pipe-separated values (e.g. <js>"foo|bar"</js>). 4509 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartCollectionFormat#MULTI MULTI} - Corresponds to multiple parameter instances instead of multiple values for a single instance (e.g. <js>"foo=bar&foo=baz"</js>). 4510 * <li class='jf'>{@link org.apache.juneau.httppart.HttpPartCollectionFormat#UONC UONC} - UON collection notation (e.g. <js>"@(foo,bar)"</js>). 4511 * </ul> 4512 * </ul> 4513 * 4514 * <h5 class='section'>Example:</h5> 4515 * <p class='bcode w800'> 4516 * <jc>// Create a REST client with CSV format for http parts.</jc> 4517 * RestClient <jv>client</jv> = RestClient 4518 * .<jsm>create</jsm>() 4519 * .collectionFormat(<jsf>CSV</jsf>) 4520 * .build(); 4521 * 4522 * <jc>// An arbitrary data structure.</jc> 4523 * AList <jv>list</jv> = AList.<jsm>of</jsm>( 4524 * <js>"foo"</js>, 4525 * <js>"bar"</js>, 4526 * AMap.<jsm>of</jsm>( 4527 * <js>"baz"</js>, AList.<jsm>of</jsm>(<js>"qux"</js>,<js>"true"</js>,<js>"123"</js>) 4528 * ) 4529 * ); 4530 * 4531 * <jc>// Set a header with a comma-separated list.</jc> 4532 * <jv>client</jv> 4533 * .get(<js>"/uri"</js>) 4534 * .header(<js>"Foo"</js>, <jv>list</jv>) <jc>// Will be serialized as: foo=bar,baz=qux\,true\,123</jc> 4535 * .run(); 4536 * </p> 4537 * 4538 * <ul class='seealso'> 4539 * <li class='jf'>{@link OpenApiCommon#OAPI_collectionFormat} 4540 * </ul> 4541 * 4542 * @param value 4543 * The new value for this property. 4544 * <br>The default value is {@link HttpPartCollectionFormat#NO_COLLECTION_FORMAT}. 4545 * @return This object (for method chaining). 4546 */ 4547 @FluentSetter 4548 public RestClientBuilder oapiCollectionFormat(HttpPartCollectionFormat value) { 4549 return set(OAPI_collectionFormat, value); 4550 } 4551 4552 //----------------------------------------------------------------------------------------------------------------- 4553 // UON Properties 4554 //----------------------------------------------------------------------------------------------------------------- 4555 4556 /** 4557 * <i><l>UonSerializer</l> configuration property: </i> Parameter format. 4558 * 4559 * <p> 4560 * Specifies the format of parameters when using the {@link UrlEncodingSerializer} to serialize Form Posts. 4561 * 4562 * <p> 4563 * Specifies the format to use for GET parameter keys and values. 4564 * 4565 * <p> 4566 * Possible values: 4567 * <ul class='javatree'> 4568 * <li class='jf'>{@link ParamFormat#UON} (default) - Use UON notation for parameters. 4569 * <li class='jf'>{@link ParamFormat#PLAINTEXT} - Use plain text for parameters. 4570 * </ul> 4571 * 4572 * <h5 class='section'>Example:</h5> 4573 * <p class='bcode w800'> 4574 * <jc>// Create a REST client with URL-Encoded serializer that serializes values in plain-text format.</jc> 4575 * RestClient <jv>client</jv> = RestClient 4576 * .<jsm>create</jsm>() 4577 * .urlEnc() 4578 * .paramFormat(<jsf>PLAINTEXT</jsf>) 4579 * .build(); 4580 * 4581 * <jc>// An arbitrary data structure.</jc> 4582 * AMap <jv>map</jv> = AMap.<jsm>of</jsm>( 4583 * <js>"foo"</js>, <js>"bar"</js>, 4584 * <js>"baz"</js>, <jk>new</jk> String[]{<js>"qux"</js>, <js>"true"</js>, <js>"123"</js>} 4585 * ); 4586 * 4587 * <jc>// Request body will be serialized as: foo=bar,baz=qux,true,123</jc> 4588 * <jv>client</jv> 4589 * .post(<js>"/uri"</js>, <jv>map</jv>) 4590 * .run(); 4591 * </p> 4592 * 4593 * <ul class='seealso'> 4594 * <li class='jf'>{@link UonSerializer#UON_paramFormat} 4595 * </ul> 4596 * 4597 * @param value The new value for this property. 4598 * @return This object (for method chaining). 4599 */ 4600 @FluentSetter 4601 public RestClientBuilder paramFormat(ParamFormat value) { 4602 return set(UON_paramFormat, value); 4603 } 4604 4605 /** 4606 * <i><l>UonSerializer</l> configuration property: </i> Parameter format. 4607 * 4608 * <p> 4609 * Specifies the format of parameters when using the {@link UrlEncodingSerializer} to serialize Form Posts. 4610 * 4611 * <p> 4612 * Specifies plaintext as the format to use for GET parameter keys and values. 4613 * 4614 * <h5 class='section'>Example:</h5> 4615 * <p class='bcode w800'> 4616 * <jc>// Create a REST client with URL-Encoded serializer that serializes values in plain-text format.</jc> 4617 * RestClient <jv>client</jv> = RestClient 4618 * .<jsm>create</jsm>() 4619 * .urlEnc() 4620 * .build(); 4621 * 4622 * <jc>// An arbitrary data structure.</jc> 4623 * AMap <jv>map</jv> = AMap.<jsm>of</jsm>( 4624 * <js>"foo"</js>, <js>"bar"</js>, 4625 * <js>"baz"</js>, <jk>new</jk> String[]{<js>"qux"</js>, <js>"true"</js>, <js>"123"</js>} 4626 * ); 4627 * 4628 * <jc>// Request body will be serialized as: foo=bar,baz=qux,true,123</jc> 4629 * <jv>client</jv> 4630 * .post(<js>"/uri"</js>, <jv>map</jv>) 4631 * .run(); 4632 * </p> 4633 * 4634 * <ul class='seealso'> 4635 * <li class='jf'>{@link UonSerializer#UON_paramFormat} 4636 * </ul> 4637 * 4638 * @return This object (for method chaining). 4639 */ 4640 @FluentSetter 4641 public RestClientBuilder paramFormatPlain() { 4642 return set(UON_paramFormat, ParamFormat.PLAINTEXT); 4643 } 4644 4645 // <FluentSetters> 4646 4647 @Override /* GENERATED - ContextBuilder */ 4648 public RestClientBuilder add(Map<String,Object> properties) { 4649 super.add(properties); 4650 return this; 4651 } 4652 4653 @Override /* GENERATED - ContextBuilder */ 4654 public RestClientBuilder addTo(String name, Object value) { 4655 super.addTo(name, value); 4656 return this; 4657 } 4658 4659 @Override /* GENERATED - ContextBuilder */ 4660 public RestClientBuilder appendTo(String name, Object value) { 4661 super.appendTo(name, value); 4662 return this; 4663 } 4664 4665 @Override /* GENERATED - ContextBuilder */ 4666 public RestClientBuilder apply(PropertyStore copyFrom) { 4667 super.apply(copyFrom); 4668 return this; 4669 } 4670 4671 @Override /* GENERATED - ContextBuilder */ 4672 public RestClientBuilder applyAnnotations(java.lang.Class<?>...fromClasses) { 4673 super.applyAnnotations(fromClasses); 4674 return this; 4675 } 4676 4677 @Override /* GENERATED - ContextBuilder */ 4678 public RestClientBuilder applyAnnotations(Method...fromMethods) { 4679 super.applyAnnotations(fromMethods); 4680 return this; 4681 } 4682 4683 @Override /* GENERATED - ContextBuilder */ 4684 public RestClientBuilder applyAnnotations(AnnotationList al, VarResolverSession r) { 4685 super.applyAnnotations(al, r); 4686 return this; 4687 } 4688 4689 @Override /* GENERATED - ContextBuilder */ 4690 public RestClientBuilder debug() { 4691 super.debug(); 4692 return this; 4693 } 4694 4695 @Override /* GENERATED - ContextBuilder */ 4696 public RestClientBuilder locale(Locale value) { 4697 super.locale(value); 4698 return this; 4699 } 4700 4701 @Override /* GENERATED - ContextBuilder */ 4702 public RestClientBuilder mediaType(MediaType value) { 4703 super.mediaType(value); 4704 return this; 4705 } 4706 4707 @Override /* GENERATED - ContextBuilder */ 4708 public RestClientBuilder prependTo(String name, Object value) { 4709 super.prependTo(name, value); 4710 return this; 4711 } 4712 4713 @Override /* GENERATED - ContextBuilder */ 4714 public RestClientBuilder putAllTo(String name, Object value) { 4715 super.putAllTo(name, value); 4716 return this; 4717 } 4718 4719 @Override /* GENERATED - ContextBuilder */ 4720 public RestClientBuilder putTo(String name, String key, Object value) { 4721 super.putTo(name, key, value); 4722 return this; 4723 } 4724 4725 @Override /* GENERATED - ContextBuilder */ 4726 public RestClientBuilder removeFrom(String name, Object value) { 4727 super.removeFrom(name, value); 4728 return this; 4729 } 4730 4731 @Override /* GENERATED - ContextBuilder */ 4732 public RestClientBuilder set(Map<String,Object> properties) { 4733 super.set(properties); 4734 return this; 4735 } 4736 4737 @Override /* GENERATED - ContextBuilder */ 4738 public RestClientBuilder set(String name, Object value) { 4739 super.set(name, value); 4740 return this; 4741 } 4742 4743 @Override /* GENERATED - ContextBuilder */ 4744 public RestClientBuilder timeZone(TimeZone value) { 4745 super.timeZone(value); 4746 return this; 4747 } 4748 4749 @Override /* GENERATED - BeanContextBuilder */ 4750 public RestClientBuilder annotations(Annotation...values) { 4751 super.annotations(values); 4752 return this; 4753 } 4754 4755 @Override /* GENERATED - BeanContextBuilder */ 4756 public RestClientBuilder beanClassVisibility(Visibility value) { 4757 super.beanClassVisibility(value); 4758 return this; 4759 } 4760 4761 @Override /* GENERATED - BeanContextBuilder */ 4762 public RestClientBuilder beanConstructorVisibility(Visibility value) { 4763 super.beanConstructorVisibility(value); 4764 return this; 4765 } 4766 4767 @Override /* GENERATED - BeanContextBuilder */ 4768 public RestClientBuilder beanFieldVisibility(Visibility value) { 4769 super.beanFieldVisibility(value); 4770 return this; 4771 } 4772 4773 @Override /* GENERATED - BeanContextBuilder */ 4774 public RestClientBuilder beanInterceptor(Class<?> on, Class<? extends org.apache.juneau.transform.BeanInterceptor<?>> value) { 4775 super.beanInterceptor(on, value); 4776 return this; 4777 } 4778 4779 @Override /* GENERATED - BeanContextBuilder */ 4780 public RestClientBuilder beanMethodVisibility(Visibility value) { 4781 super.beanMethodVisibility(value); 4782 return this; 4783 } 4784 4785 @Override /* GENERATED - BeanContextBuilder */ 4786 public RestClientBuilder beansDontRequireSomeProperties() { 4787 super.beansDontRequireSomeProperties(); 4788 return this; 4789 } 4790 4791 @Override /* GENERATED - BeanContextBuilder */ 4792 public RestClientBuilder beansRequireDefaultConstructor() { 4793 super.beansRequireDefaultConstructor(); 4794 return this; 4795 } 4796 4797 @Override /* GENERATED - BeanContextBuilder */ 4798 public RestClientBuilder beansRequireSerializable() { 4799 super.beansRequireSerializable(); 4800 return this; 4801 } 4802 4803 @Override /* GENERATED - BeanContextBuilder */ 4804 public RestClientBuilder beansRequireSettersForGetters() { 4805 super.beansRequireSettersForGetters(); 4806 return this; 4807 } 4808 4809 @Override /* GENERATED - BeanContextBuilder */ 4810 public RestClientBuilder bpi(Map<String,Object> values) { 4811 super.bpi(values); 4812 return this; 4813 } 4814 4815 @Override /* GENERATED - BeanContextBuilder */ 4816 public RestClientBuilder bpi(Class<?> beanClass, String properties) { 4817 super.bpi(beanClass, properties); 4818 return this; 4819 } 4820 4821 @Override /* GENERATED - BeanContextBuilder */ 4822 public RestClientBuilder bpi(String beanClassName, String properties) { 4823 super.bpi(beanClassName, properties); 4824 return this; 4825 } 4826 4827 @Override /* GENERATED - BeanContextBuilder */ 4828 public RestClientBuilder bpro(Map<String,Object> values) { 4829 super.bpro(values); 4830 return this; 4831 } 4832 4833 @Override /* GENERATED - BeanContextBuilder */ 4834 public RestClientBuilder bpro(Class<?> beanClass, String properties) { 4835 super.bpro(beanClass, properties); 4836 return this; 4837 } 4838 4839 @Override /* GENERATED - BeanContextBuilder */ 4840 public RestClientBuilder bpro(String beanClassName, String properties) { 4841 super.bpro(beanClassName, properties); 4842 return this; 4843 } 4844 4845 @Override /* GENERATED - BeanContextBuilder */ 4846 public RestClientBuilder bpwo(Map<String,Object> values) { 4847 super.bpwo(values); 4848 return this; 4849 } 4850 4851 @Override /* GENERATED - BeanContextBuilder */ 4852 public RestClientBuilder bpwo(Class<?> beanClass, String properties) { 4853 super.bpwo(beanClass, properties); 4854 return this; 4855 } 4856 4857 @Override /* GENERATED - BeanContextBuilder */ 4858 public RestClientBuilder bpwo(String beanClassName, String properties) { 4859 super.bpwo(beanClassName, properties); 4860 return this; 4861 } 4862 4863 @Override /* GENERATED - BeanContextBuilder */ 4864 public RestClientBuilder bpx(Map<String,Object> values) { 4865 super.bpx(values); 4866 return this; 4867 } 4868 4869 @Override /* GENERATED - BeanContextBuilder */ 4870 public RestClientBuilder bpx(Class<?> beanClass, String properties) { 4871 super.bpx(beanClass, properties); 4872 return this; 4873 } 4874 4875 @Override /* GENERATED - BeanContextBuilder */ 4876 public RestClientBuilder bpx(String beanClassName, String properties) { 4877 super.bpx(beanClassName, properties); 4878 return this; 4879 } 4880 4881 @Override /* GENERATED - BeanContextBuilder */ 4882 public RestClientBuilder dictionary(Object...values) { 4883 super.dictionary(values); 4884 return this; 4885 } 4886 4887 @Override /* GENERATED - BeanContextBuilder */ 4888 public RestClientBuilder dictionaryOn(Class<?> on, java.lang.Class<?>...values) { 4889 super.dictionaryOn(on, values); 4890 return this; 4891 } 4892 4893 @Override /* GENERATED - BeanContextBuilder */ 4894 public RestClientBuilder dontIgnorePropertiesWithoutSetters() { 4895 super.dontIgnorePropertiesWithoutSetters(); 4896 return this; 4897 } 4898 4899 @Override /* GENERATED - BeanContextBuilder */ 4900 public RestClientBuilder dontIgnoreTransientFields() { 4901 super.dontIgnoreTransientFields(); 4902 return this; 4903 } 4904 4905 @Override /* GENERATED - BeanContextBuilder */ 4906 public RestClientBuilder dontIgnoreUnknownNullBeanProperties() { 4907 super.dontIgnoreUnknownNullBeanProperties(); 4908 return this; 4909 } 4910 4911 @Override /* GENERATED - BeanContextBuilder */ 4912 public RestClientBuilder dontUseInterfaceProxies() { 4913 super.dontUseInterfaceProxies(); 4914 return this; 4915 } 4916 4917 @Override /* GENERATED - BeanContextBuilder */ 4918 public RestClientBuilder fluentSetters() { 4919 super.fluentSetters(); 4920 return this; 4921 } 4922 4923 @Override /* GENERATED - BeanContextBuilder */ 4924 public RestClientBuilder fluentSetters(Class<?> on) { 4925 super.fluentSetters(on); 4926 return this; 4927 } 4928 4929 @Override /* GENERATED - BeanContextBuilder */ 4930 public RestClientBuilder ignoreInvocationExceptionsOnGetters() { 4931 super.ignoreInvocationExceptionsOnGetters(); 4932 return this; 4933 } 4934 4935 @Override /* GENERATED - BeanContextBuilder */ 4936 public RestClientBuilder ignoreInvocationExceptionsOnSetters() { 4937 super.ignoreInvocationExceptionsOnSetters(); 4938 return this; 4939 } 4940 4941 @Override /* GENERATED - BeanContextBuilder */ 4942 public RestClientBuilder ignoreUnknownBeanProperties() { 4943 super.ignoreUnknownBeanProperties(); 4944 return this; 4945 } 4946 4947 @Override /* GENERATED - BeanContextBuilder */ 4948 public RestClientBuilder implClass(Class<?> interfaceClass, Class<?> implClass) { 4949 super.implClass(interfaceClass, implClass); 4950 return this; 4951 } 4952 4953 @Override /* GENERATED - BeanContextBuilder */ 4954 public RestClientBuilder implClasses(Map<Class<?>,Class<?>> values) { 4955 super.implClasses(values); 4956 return this; 4957 } 4958 4959 @Override /* GENERATED - BeanContextBuilder */ 4960 public RestClientBuilder interfaceClass(Class<?> on, Class<?> value) { 4961 super.interfaceClass(on, value); 4962 return this; 4963 } 4964 4965 @Override /* GENERATED - BeanContextBuilder */ 4966 public RestClientBuilder interfaces(java.lang.Class<?>...value) { 4967 super.interfaces(value); 4968 return this; 4969 } 4970 4971 @Override /* GENERATED - BeanContextBuilder */ 4972 public RestClientBuilder notBeanClasses(Object...values) { 4973 super.notBeanClasses(values); 4974 return this; 4975 } 4976 4977 @Override /* GENERATED - BeanContextBuilder */ 4978 public RestClientBuilder notBeanPackages(Object...values) { 4979 super.notBeanPackages(values); 4980 return this; 4981 } 4982 4983 @Override /* GENERATED - BeanContextBuilder */ 4984 public RestClientBuilder propertyNamer(Class<? extends org.apache.juneau.PropertyNamer> value) { 4985 super.propertyNamer(value); 4986 return this; 4987 } 4988 4989 @Override /* GENERATED - BeanContextBuilder */ 4990 public RestClientBuilder propertyNamer(Class<?> on, Class<? extends org.apache.juneau.PropertyNamer> value) { 4991 super.propertyNamer(on, value); 4992 return this; 4993 } 4994 4995 @Override /* GENERATED - BeanContextBuilder */ 4996 public RestClientBuilder sortProperties() { 4997 super.sortProperties(); 4998 return this; 4999 } 5000 5001 @Override /* GENERATED - BeanContextBuilder */ 5002 public RestClientBuilder sortProperties(java.lang.Class<?>...on) { 5003 super.sortProperties(on); 5004 return this; 5005 } 5006 5007 @Override /* GENERATED - BeanContextBuilder */ 5008 public RestClientBuilder stopClass(Class<?> on, Class<?> value) { 5009 super.stopClass(on, value); 5010 return this; 5011 } 5012 5013 @Override /* GENERATED - BeanContextBuilder */ 5014 public RestClientBuilder swaps(Object...values) { 5015 super.swaps(values); 5016 return this; 5017 } 5018 5019 @Override /* GENERATED - BeanContextBuilder */ 5020 public RestClientBuilder typeName(Class<?> on, String value) { 5021 super.typeName(on, value); 5022 return this; 5023 } 5024 5025 @Override /* GENERATED - BeanContextBuilder */ 5026 public RestClientBuilder typePropertyName(String value) { 5027 super.typePropertyName(value); 5028 return this; 5029 } 5030 5031 @Override /* GENERATED - BeanContextBuilder */ 5032 public RestClientBuilder typePropertyName(Class<?> on, String value) { 5033 super.typePropertyName(on, value); 5034 return this; 5035 } 5036 5037 @Override /* GENERATED - BeanContextBuilder */ 5038 public RestClientBuilder useEnumNames() { 5039 super.useEnumNames(); 5040 return this; 5041 } 5042 5043 @Override /* GENERATED - BeanContextBuilder */ 5044 public RestClientBuilder useJavaBeanIntrospector() { 5045 super.useJavaBeanIntrospector(); 5046 return this; 5047 } 5048 5049 // </FluentSetters> 5050 5051 //------------------------------------------------------------------------------------------------ 5052 // Passthrough methods for HttpClientBuilder. 5053 //------------------------------------------------------------------------------------------------ 5054 5055 /** 5056 * Disables automatic redirect handling. 5057 * 5058 * @return This object (for method chaining). 5059 * @see HttpClientBuilder#disableRedirectHandling() 5060 */ 5061 @FluentSetter 5062 public RestClientBuilder disableRedirectHandling() { 5063 httpClientBuilder.disableRedirectHandling(); 5064 return this; 5065 } 5066 5067 /** 5068 * Assigns {@link RedirectStrategy} instance. 5069 * 5070 * <ul class='notes'> 5071 * <li>This value can be overridden by the {@link #disableRedirectHandling()} method. 5072 * </ul> 5073 * 5074 * @param redirectStrategy New property value. 5075 * @return This object (for method chaining). 5076 * @see HttpClientBuilder#setRedirectStrategy(RedirectStrategy) 5077 */ 5078 @FluentSetter 5079 public RestClientBuilder redirectStrategy(RedirectStrategy redirectStrategy) { 5080 httpClientBuilder.setRedirectStrategy(redirectStrategy); 5081 return this; 5082 } 5083 5084 /** 5085 * Assigns default {@link CookieSpec} registry which will be used for request execution if not explicitly set in the client execution context. 5086 * 5087 * @param cookieSpecRegistry New property value. 5088 * @return This object (for method chaining). 5089 * @see HttpClientBuilder#setDefaultCookieSpecRegistry(Lookup) 5090 */ 5091 @FluentSetter 5092 public RestClientBuilder defaultCookieSpecRegistry(Lookup<CookieSpecProvider> cookieSpecRegistry) { 5093 httpClientBuilder.setDefaultCookieSpecRegistry(cookieSpecRegistry); 5094 return this; 5095 } 5096 5097 /** 5098 * Assigns {@link HttpRequestExecutor} instance. 5099 * 5100 * @param requestExec New property value. 5101 * @return This object (for method chaining). 5102 * @see HttpClientBuilder#setRequestExecutor(HttpRequestExecutor) 5103 */ 5104 @FluentSetter 5105 public RestClientBuilder requestExecutor(HttpRequestExecutor requestExec) { 5106 httpClientBuilder.setRequestExecutor(requestExec); 5107 return this; 5108 } 5109 5110 /** 5111 * Assigns {@link javax.net.ssl.HostnameVerifier} instance. 5112 * 5113 * <ul class='notes'> 5114 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} 5115 * and the {@link #sslSocketFactory(LayeredConnectionSocketFactory)} methods. 5116 * </ul> 5117 * 5118 * @param hostnameVerifier New property value. 5119 * @return This object (for method chaining). 5120 * @see HttpClientBuilder#setSSLHostnameVerifier(HostnameVerifier) 5121 */ 5122 @FluentSetter 5123 public RestClientBuilder sslHostnameVerifier(HostnameVerifier hostnameVerifier) { 5124 httpClientBuilder.setSSLHostnameVerifier(hostnameVerifier); 5125 return this; 5126 } 5127 5128 /** 5129 * Assigns file containing public suffix matcher. 5130 * 5131 * <ul class='notes'> 5132 * <li>Instances of this class can be created with {@link PublicSuffixMatcherLoader}. 5133 * </ul> 5134 * 5135 * @param publicSuffixMatcher New property value. 5136 * @return This object (for method chaining). 5137 * @see HttpClientBuilder#setPublicSuffixMatcher(PublicSuffixMatcher) 5138 */ 5139 @FluentSetter 5140 public RestClientBuilder publicSuffixMatcher(PublicSuffixMatcher publicSuffixMatcher) { 5141 httpClientBuilder.setPublicSuffixMatcher(publicSuffixMatcher); 5142 return this; 5143 } 5144 5145 /** 5146 * Assigns {@link SSLContext} instance. 5147 * 5148 * <ul class='notes'> 5149 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} 5150 * and the {@link #sslSocketFactory(LayeredConnectionSocketFactory)} methods. 5151 * </ul> 5152 * 5153 * @param sslContext New property value. 5154 * @return This object (for method chaining). 5155 * @see HttpClientBuilder#setSSLContext(SSLContext) 5156 */ 5157 @FluentSetter 5158 public RestClientBuilder sslContext(SSLContext sslContext) { 5159 httpClientBuilder.setSSLContext(sslContext); 5160 return this; 5161 } 5162 5163 /** 5164 * Assigns {@link LayeredConnectionSocketFactory} instance. 5165 * 5166 * <ul class='notes'> 5167 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} method. 5168 * </ul> 5169 * 5170 * @param sslSocketFactory New property value. 5171 * @return This object (for method chaining). 5172 * @see HttpClientBuilder#setSSLSocketFactory(LayeredConnectionSocketFactory) 5173 */ 5174 @FluentSetter 5175 public RestClientBuilder sslSocketFactory(LayeredConnectionSocketFactory sslSocketFactory) { 5176 httpClientBuilder.setSSLSocketFactory(sslSocketFactory); 5177 return this; 5178 } 5179 5180 /** 5181 * Assigns maximum total connection value. 5182 * 5183 * <ul class='notes'> 5184 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} method. 5185 * </ul> 5186 * 5187 * @param maxConnTotal New property value. 5188 * @return This object (for method chaining). 5189 * @see HttpClientBuilder#setMaxConnTotal(int) 5190 */ 5191 @FluentSetter 5192 public RestClientBuilder maxConnTotal(int maxConnTotal) { 5193 httpClientBuilder.setMaxConnTotal(maxConnTotal); 5194 return this; 5195 } 5196 5197 /** 5198 * Assigns maximum connection per route value. 5199 * 5200 * <ul class='notes'> 5201 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} method. 5202 * </ul> 5203 * 5204 * @param maxConnPerRoute New property value. 5205 * @return This object (for method chaining). 5206 * @see HttpClientBuilder#setMaxConnPerRoute(int) 5207 */ 5208 @FluentSetter 5209 public RestClientBuilder maxConnPerRoute(int maxConnPerRoute) { 5210 httpClientBuilder.setMaxConnPerRoute(maxConnPerRoute); 5211 return this; 5212 } 5213 5214 /** 5215 * Assigns default {@link SocketConfig}. 5216 * 5217 * <ul class='notes'> 5218 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} method. 5219 * </ul> 5220 * 5221 * @param config New property value. 5222 * @return This object (for method chaining). 5223 * @see HttpClientBuilder#setDefaultSocketConfig(SocketConfig) 5224 */ 5225 @FluentSetter 5226 public RestClientBuilder defaultSocketConfig(SocketConfig config) { 5227 httpClientBuilder.setDefaultSocketConfig(config); 5228 return this; 5229 } 5230 5231 /** 5232 * Assigns default {@link ConnectionConfig}. 5233 * 5234 * <ul class='notes'> 5235 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} method. 5236 * </ul> 5237 * 5238 * @param config New property value. 5239 * @return This object (for method chaining). 5240 * @see HttpClientBuilder#setDefaultConnectionConfig(ConnectionConfig) 5241 */ 5242 @FluentSetter 5243 public RestClientBuilder defaultConnectionConfig(ConnectionConfig config) { 5244 httpClientBuilder.setDefaultConnectionConfig(config); 5245 return this; 5246 } 5247 5248 /** 5249 * Sets maximum time to live for persistent connections. 5250 * 5251 * <ul class='notes'> 5252 * <li>This value can be overridden by the {@link #connectionManager(HttpClientConnectionManager)} method. 5253 * </ul> 5254 * 5255 * @param connTimeToLive New property value. 5256 * @param connTimeToLiveTimeUnit New property value. 5257 * @return This object (for method chaining). 5258 * @see HttpClientBuilder#setConnectionTimeToLive(long,TimeUnit) 5259 */ 5260 @FluentSetter 5261 public RestClientBuilder connectionTimeToLive(long connTimeToLive, TimeUnit connTimeToLiveTimeUnit) { 5262 httpClientBuilder.setConnectionTimeToLive(connTimeToLive, connTimeToLiveTimeUnit); 5263 return this; 5264 } 5265 5266 /** 5267 * Assigns {@link HttpClientConnectionManager} instance. 5268 * 5269 * @param connManager New property value. 5270 * @return This object (for method chaining). 5271 * @see HttpClientBuilder#setConnectionManager(HttpClientConnectionManager) 5272 */ 5273 @FluentSetter 5274 public RestClientBuilder connectionManager(HttpClientConnectionManager connManager) { 5275 this.httpClientConnectionManager = connManager; 5276 httpClientBuilder.setConnectionManager(connManager); 5277 return this; 5278 } 5279 5280 /** 5281 * Defines the connection manager is to be shared by multiple client instances. 5282 * 5283 * <ul class='notes'> 5284 * <li>If the connection manager is shared its life-cycle is expected to be managed by the caller and it will not be shut down if the client is closed. 5285 * </ul> 5286 * 5287 * @param shared New property value. 5288 * @return This object (for method chaining). 5289 * @see HttpClientBuilder#setConnectionManagerShared(boolean) 5290 */ 5291 @FluentSetter 5292 public RestClientBuilder connectionManagerShared(boolean shared) { 5293 httpClientBuilder.setConnectionManagerShared(shared); 5294 return this; 5295 } 5296 5297 /** 5298 * Assigns {@link ConnectionReuseStrategy} instance. 5299 * 5300 * @param reuseStrategy New property value. 5301 * @return This object (for method chaining). 5302 * @see HttpClientBuilder#setConnectionReuseStrategy(ConnectionReuseStrategy) 5303 */ 5304 @FluentSetter 5305 public RestClientBuilder connectionReuseStrategy(ConnectionReuseStrategy reuseStrategy) { 5306 httpClientBuilder.setConnectionReuseStrategy(reuseStrategy); 5307 return this; 5308 } 5309 5310 /** 5311 * Assigns {@link ConnectionKeepAliveStrategy} instance. 5312 * 5313 * @param keepAliveStrategy New property value. 5314 * @return This object (for method chaining). 5315 * @see HttpClientBuilder#setKeepAliveStrategy(ConnectionKeepAliveStrategy) 5316 */ 5317 @FluentSetter 5318 public RestClientBuilder keepAliveStrategy(ConnectionKeepAliveStrategy keepAliveStrategy) { 5319 httpClientBuilder.setKeepAliveStrategy(keepAliveStrategy); 5320 return this; 5321 } 5322 5323 /** 5324 * Assigns {@link AuthenticationStrategy} instance for target host authentication. 5325 * 5326 * @param targetAuthStrategy New property value. 5327 * @return This object (for method chaining). 5328 * @see HttpClientBuilder#setTargetAuthenticationStrategy(AuthenticationStrategy) 5329 */ 5330 @FluentSetter 5331 public RestClientBuilder targetAuthenticationStrategy(AuthenticationStrategy targetAuthStrategy) { 5332 httpClientBuilder.setTargetAuthenticationStrategy(targetAuthStrategy); 5333 return this; 5334 } 5335 5336 /** 5337 * Assigns {@link AuthenticationStrategy} instance for proxy authentication. 5338 * 5339 * @param proxyAuthStrategy New property value. 5340 * @return This object (for method chaining). 5341 * @see HttpClientBuilder#setProxyAuthenticationStrategy(AuthenticationStrategy) 5342 */ 5343 @FluentSetter 5344 public RestClientBuilder proxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy) { 5345 httpClientBuilder.setProxyAuthenticationStrategy(proxyAuthStrategy); 5346 return this; 5347 } 5348 5349 /** 5350 * Assigns {@link UserTokenHandler} instance. 5351 * 5352 * <ul class='notes'> 5353 * <li>This value can be overridden by the {@link #disableConnectionState()} method. 5354 * </ul> 5355 * 5356 * @param userTokenHandler New property value. 5357 * @return This object (for method chaining). 5358 * @see HttpClientBuilder#setUserTokenHandler(UserTokenHandler) 5359 */ 5360 @FluentSetter 5361 public RestClientBuilder userTokenHandler(UserTokenHandler userTokenHandler) { 5362 httpClientBuilder.setUserTokenHandler(userTokenHandler); 5363 return this; 5364 } 5365 5366 /** 5367 * Disables connection state tracking. 5368 * 5369 * @return This object (for method chaining). 5370 * @see HttpClientBuilder#disableConnectionState() 5371 */ 5372 @FluentSetter 5373 public RestClientBuilder disableConnectionState() { 5374 httpClientBuilder.disableConnectionState(); 5375 return this; 5376 } 5377 5378 /** 5379 * Assigns {@link SchemePortResolver} instance. 5380 * 5381 * @param schemePortResolver New property value. 5382 * @return This object (for method chaining). 5383 * @see HttpClientBuilder#setSchemePortResolver(SchemePortResolver) 5384 */ 5385 @FluentSetter 5386 public RestClientBuilder schemePortResolver(SchemePortResolver schemePortResolver) { 5387 httpClientBuilder.setSchemePortResolver(schemePortResolver); 5388 return this; 5389 } 5390 5391 /** 5392 * Assigns <c>User-Agent</c> value. 5393 * 5394 * <ul class='notes'> 5395 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5396 * <li>{@link #userAgent(Object)} is an equivalent method. 5397 * </ul> 5398 * 5399 * @param userAgent New property value. 5400 * @return This object (for method chaining). 5401 * @see HttpClientBuilder#setUserAgent(String) 5402 */ 5403 @FluentSetter 5404 public RestClientBuilder userAgent(String userAgent) { 5405 httpClientBuilder.setUserAgent(userAgent); 5406 return this; 5407 } 5408 5409 /** 5410 * Assigns default request header values. 5411 * 5412 * <ul class='notes'> 5413 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5414 * <li>{@link #headers(Object...)} is an equivalent method. 5415 * </ul> 5416 * 5417 * @param defaultHeaders New property value. 5418 * @return This object (for method chaining). 5419 * @see HttpClientBuilder#setDefaultHeaders(Collection) 5420 */ 5421 @FluentSetter 5422 public RestClientBuilder defaultHeaders(Collection<? extends Header> defaultHeaders) { 5423 httpClientBuilder.setDefaultHeaders(defaultHeaders); 5424 return this; 5425 } 5426 5427 /** 5428 * Adds this protocol interceptor to the head of the protocol processing list. 5429 * 5430 * <ul class='notes'> 5431 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5432 * </ul> 5433 * 5434 * @param itcp New property value. 5435 * @return This object (for method chaining). 5436 * @see HttpClientBuilder#addInterceptorFirst(HttpResponseInterceptor) 5437 */ 5438 @FluentSetter 5439 public RestClientBuilder addInterceptorFirst(HttpResponseInterceptor itcp) { 5440 httpClientBuilder.addInterceptorFirst(itcp); 5441 return this; 5442 } 5443 5444 /** 5445 * Adds this protocol interceptor to the tail of the protocol processing list. 5446 * 5447 * <ul class='notes'> 5448 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5449 * </ul> 5450 * 5451 * @param itcp New property value. 5452 * @return This object (for method chaining). 5453 * @see HttpClientBuilder#addInterceptorLast(HttpResponseInterceptor) 5454 */ 5455 @FluentSetter 5456 public RestClientBuilder addInterceptorLast(HttpResponseInterceptor itcp) { 5457 httpClientBuilder.addInterceptorLast(itcp); 5458 return this; 5459 } 5460 5461 /** 5462 * Adds this protocol interceptor to the head of the protocol processing list. 5463 * 5464 * <ul class='notes'> 5465 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5466 * </ul> 5467 * 5468 * @param itcp New property value. 5469 * @return This object (for method chaining). 5470 * @see HttpClientBuilder#addInterceptorFirst(HttpRequestInterceptor) 5471 */ 5472 @FluentSetter 5473 public RestClientBuilder addInterceptorFirst(HttpRequestInterceptor itcp) { 5474 httpClientBuilder.addInterceptorFirst(itcp); 5475 return this; 5476 } 5477 5478 /** 5479 * Adds this protocol interceptor to the tail of the protocol processing list. 5480 * 5481 * <ul class='notes'> 5482 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5483 * </ul> 5484 * 5485 * @param itcp New property value. 5486 * @return This object (for method chaining). 5487 * @see HttpClientBuilder#addInterceptorLast(HttpRequestInterceptor) 5488 */ 5489 @FluentSetter 5490 public RestClientBuilder addInterceptorLast(HttpRequestInterceptor itcp) { 5491 httpClientBuilder.addInterceptorLast(itcp); 5492 return this; 5493 } 5494 5495 /** 5496 * Disables state (cookie) management. 5497 * 5498 * <ul class='notes'> 5499 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5500 * </ul> 5501 * 5502 * @return This object (for method chaining). 5503 * @see HttpClientBuilder#disableCookieManagement() 5504 */ 5505 @FluentSetter 5506 public RestClientBuilder disableCookieManagement() { 5507 httpClientBuilder.disableCookieManagement(); 5508 return this; 5509 } 5510 5511 /** 5512 * Disables automatic content decompression. 5513 * 5514 * <ul class='notes'> 5515 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5516 * </ul> 5517 * 5518 * @return This object (for method chaining). 5519 * @see HttpClientBuilder#disableContentCompression() 5520 */ 5521 @FluentSetter 5522 public RestClientBuilder disableContentCompression() { 5523 httpClientBuilder.disableContentCompression(); 5524 return this; 5525 } 5526 5527 /** 5528 * Disables authentication scheme caching. 5529 * 5530 * <ul class='notes'> 5531 * <li>This value can be overridden by the {@link #httpProcessor(HttpProcessor)} method. 5532 * </ul> 5533 * 5534 * @return This object (for method chaining). 5535 * @see HttpClientBuilder#disableAuthCaching() 5536 */ 5537 @FluentSetter 5538 public RestClientBuilder disableAuthCaching() { 5539 httpClientBuilder.disableAuthCaching(); 5540 return this; 5541 } 5542 5543 /** 5544 * Assigns {@link HttpProcessor} instance. 5545 * 5546 * @param httpprocessor New property value. 5547 * @return This object (for method chaining). 5548 * @see HttpClientBuilder#setHttpProcessor(HttpProcessor) 5549 */ 5550 @FluentSetter 5551 public RestClientBuilder httpProcessor(HttpProcessor httpprocessor) { 5552 httpClientBuilder.setHttpProcessor(httpprocessor); 5553 return this; 5554 } 5555 5556 /** 5557 * Assigns {@link HttpRequestRetryHandler} instance. 5558 * 5559 * <ul class='notes'> 5560 * <li>This value can be overridden by the {@link #disableAutomaticRetries()} method. 5561 * </ul> 5562 * 5563 * @param retryHandler New property value. 5564 * @return This object (for method chaining). 5565 * @see HttpClientBuilder#setRetryHandler(HttpRequestRetryHandler) 5566 */ 5567 @FluentSetter 5568 public RestClientBuilder retryHandler(HttpRequestRetryHandler retryHandler) { 5569 httpClientBuilder.setRetryHandler(retryHandler); 5570 return this; 5571 } 5572 5573 /** 5574 * Disables automatic request recovery and re-execution. 5575 * 5576 * @return This object (for method chaining). 5577 * @see HttpClientBuilder#disableAutomaticRetries() 5578 */ 5579 @FluentSetter 5580 public RestClientBuilder disableAutomaticRetries() { 5581 httpClientBuilder.disableAutomaticRetries(); 5582 return this; 5583 } 5584 5585 /** 5586 * Assigns default proxy value. 5587 * 5588 * <ul class='notes'> 5589 * <li>This value can be overridden by the {@link #routePlanner(HttpRoutePlanner)} method. 5590 * </ul> 5591 * 5592 * @param proxy New property value. 5593 * @return This object (for method chaining). 5594 * @see HttpClientBuilder#setProxy(HttpHost) 5595 */ 5596 @FluentSetter 5597 public RestClientBuilder proxy(HttpHost proxy) { 5598 httpClientBuilder.setProxy(proxy); 5599 return this; 5600 } 5601 5602 /** 5603 * Assigns {@link HttpRoutePlanner} instance. 5604 * 5605 * @param routePlanner New property value. 5606 * @return This object (for method chaining). 5607 * @see HttpClientBuilder#setRoutePlanner(HttpRoutePlanner) 5608 */ 5609 @FluentSetter 5610 public RestClientBuilder routePlanner(HttpRoutePlanner routePlanner) { 5611 httpClientBuilder.setRoutePlanner(routePlanner); 5612 return this; 5613 } 5614 5615 /** 5616 * Assigns {@link ConnectionBackoffStrategy} instance. 5617 * 5618 * @param connectionBackoffStrategy New property value. 5619 * @return This object (for method chaining). 5620 * @see HttpClientBuilder#setConnectionBackoffStrategy(ConnectionBackoffStrategy) 5621 */ 5622 @FluentSetter 5623 public RestClientBuilder connectionBackoffStrategy(ConnectionBackoffStrategy connectionBackoffStrategy) { 5624 httpClientBuilder.setConnectionBackoffStrategy(connectionBackoffStrategy); 5625 return this; 5626 } 5627 5628 /** 5629 * Assigns {@link BackoffManager} instance. 5630 * 5631 * @param backoffManager New property value. 5632 * @return This object (for method chaining). 5633 * @see HttpClientBuilder#setBackoffManager(BackoffManager) 5634 */ 5635 @FluentSetter 5636 public RestClientBuilder backoffManager(BackoffManager backoffManager) { 5637 httpClientBuilder.setBackoffManager(backoffManager); 5638 return this; 5639 } 5640 5641 /** 5642 * Assigns {@link ServiceUnavailableRetryStrategy} instance. 5643 * 5644 * @param serviceUnavailStrategy New property value. 5645 * @return This object (for method chaining). 5646 * @see HttpClientBuilder#setServiceUnavailableRetryStrategy(ServiceUnavailableRetryStrategy) 5647 */ 5648 @FluentSetter 5649 public RestClientBuilder serviceUnavailableRetryStrategy(ServiceUnavailableRetryStrategy serviceUnavailStrategy) { 5650 httpClientBuilder.setServiceUnavailableRetryStrategy(serviceUnavailStrategy); 5651 return this; 5652 } 5653 5654 /** 5655 * Assigns default {@link CookieStore} instance which will be used for request execution if not explicitly set in the client execution context. 5656 * 5657 * @param cookieStore New property value. 5658 * @return This object (for method chaining). 5659 * @see HttpClientBuilder#setDefaultCookieStore(CookieStore) 5660 */ 5661 @FluentSetter 5662 public RestClientBuilder defaultCookieStore(CookieStore cookieStore) { 5663 httpClientBuilder.setDefaultCookieStore(cookieStore); 5664 return this; 5665 } 5666 5667 /** 5668 * Assigns default {@link CredentialsProvider} instance which will be used for request execution if not explicitly set in the client execution context. 5669 * 5670 * @param credentialsProvider New property value. 5671 * @return This object (for method chaining). 5672 * @see HttpClientBuilder#setDefaultCredentialsProvider(CredentialsProvider) 5673 */ 5674 @FluentSetter 5675 public RestClientBuilder defaultCredentialsProvider(CredentialsProvider credentialsProvider) { 5676 httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); 5677 return this; 5678 } 5679 5680 /** 5681 * Assigns default {@link org.apache.http.auth.AuthScheme} registry which will be used for request execution if not explicitly set in the client execution context. 5682 * 5683 * @param authSchemeRegistry New property value. 5684 * @return This object (for method chaining). 5685 * @see HttpClientBuilder#setDefaultAuthSchemeRegistry(Lookup) 5686 */ 5687 @FluentSetter 5688 public RestClientBuilder defaultAuthSchemeRegistry(Lookup<AuthSchemeProvider> authSchemeRegistry) { 5689 httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry); 5690 return this; 5691 } 5692 5693 /** 5694 * Assigns a map of {@link org.apache.http.client.entity.InputStreamFactory InputStreamFactories} to be used for automatic content decompression. 5695 * 5696 * @param contentDecoderMap New property value. 5697 * @return This object (for method chaining). 5698 * @see HttpClientBuilder#setContentDecoderRegistry(Map) 5699 */ 5700 @FluentSetter 5701 public RestClientBuilder contentDecoderRegistry(Map<String,InputStreamFactory> contentDecoderMap) { 5702 httpClientBuilder.setContentDecoderRegistry(contentDecoderMap); 5703 return this; 5704 } 5705 5706 /** 5707 * Assigns default {@link RequestConfig} instance which will be used for request execution if not explicitly set in the client execution context. 5708 * 5709 * @param config New property value. 5710 * @return This object (for method chaining). 5711 * @see HttpClientBuilder#setDefaultRequestConfig(RequestConfig) 5712 */ 5713 @FluentSetter 5714 public RestClientBuilder defaultRequestConfig(RequestConfig config) { 5715 httpClientBuilder.setDefaultRequestConfig(config); 5716 return this; 5717 } 5718 5719 /** 5720 * Use system properties when creating and configuring default implementations. 5721 * 5722 * @return This object (for method chaining). 5723 * @see HttpClientBuilder#useSystemProperties() 5724 */ 5725 @FluentSetter 5726 public RestClientBuilder useSystemProperties() { 5727 httpClientBuilder.useSystemProperties(); 5728 return this; 5729 } 5730 5731 /** 5732 * Makes this instance of {@link HttpClient} proactively evict expired connections from the connection pool using a background thread. 5733 * 5734 * <ul class='notes'> 5735 * <li>One MUST explicitly close HttpClient with {@link CloseableHttpClient#close()} in order to stop and release the background thread. 5736 * <li>This method has no effect if the instance of {@link HttpClient} is configured to use a shared connection manager. 5737 * <li>This method may not be used when the instance of {@link HttpClient} is created inside an EJB container. 5738 * </ul> 5739 * 5740 * @return This object (for method chaining). 5741 * @see HttpClientBuilder#evictExpiredConnections() 5742 */ 5743 @FluentSetter 5744 public RestClientBuilder evictExpiredConnections() { 5745 httpClientBuilder.evictExpiredConnections(); 5746 return this; 5747 } 5748 5749 /** 5750 * Makes this instance of {@link HttpClient} proactively evict idle connections from the connection pool using a background thread. 5751 * 5752 * <ul class='notes'> 5753 * <li>One MUST explicitly close HttpClient with {@link CloseableHttpClient#close()} in order to stop and release the background thread. 5754 * <li>This method has no effect if the instance of {@link HttpClient} is configured to use a shared connection manager. 5755 * <li>This method may not be used when the instance of {@link HttpClient} is created inside an EJB container. 5756 * </ul> 5757 * 5758 * @param maxIdleTime New property value. 5759 * @param maxIdleTimeUnit New property value. 5760 * @return This object (for method chaining). 5761 * @see HttpClientBuilder#evictIdleConnections(long,TimeUnit) 5762 */ 5763 @FluentSetter 5764 public RestClientBuilder evictIdleConnections(long maxIdleTime, TimeUnit maxIdleTimeUnit) { 5765 httpClientBuilder.evictIdleConnections(maxIdleTime, maxIdleTimeUnit); 5766 return this; 5767 } 5768 5769 //------------------------------------------------------------------------------------------------------------------ 5770 // Utility methods. 5771 //------------------------------------------------------------------------------------------------------------------ 5772 5773 @SuppressWarnings("unchecked") 5774 private static Map<Object,Object> toMap(Object o) { 5775 return (Map<Object,Object>)o; 5776 } 5777 5778 private static SerializedNameValuePair serializedNameValuePair(Object key, Object value, HttpPartType type, HttpPartSerializer serializer, HttpPartSchema schema) { 5779 return key == null ? null : SerializedNameValuePair.of(stringify(key),value).type(type).serializer(serializer).schema(schema); 5780 } 5781 5782 private static SerializedHeader serializedHeader(Object key, Object value, HttpPartSerializer serializer, HttpPartSchema schema) { 5783 return key == null ? null : SerializedHeader.of(stringify(key),value).serializer(serializer).schema(schema); 5784 } 5785}