MCPcopy Index your code
hub / github.com/OpenFeign/feign / BodyTemplate

Class BodyTemplate

core/src/main/java/feign/template/BodyTemplate.java:26–72  ·  view source on GitHub ↗

Template for @feign.Body annotated Templates. Unresolved expressions are preserved as literals and literals are not URI encoded.

Source from the content-addressed store, hash-verified

24 * literals and literals are not URI encoded.
25 */
26public final class BodyTemplate extends Template {
27
28 private static final String JSON_TOKEN_START = "{";
29 private static final String JSON_TOKEN_END = "}";
30 private static final String JSON_TOKEN_START_ENCODED = "%7B";
31 private static final String JSON_TOKEN_END_ENCODED = "%7D";
32 private boolean json = false;
33
34 /**
35 * Create a new Body Template.
36 *
37 * @param template to parse.
38 * @return a Body Template instance.
39 */
40 public static BodyTemplate create(String template) {
41 return new BodyTemplate(template, Util.UTF_8);
42 }
43
44 /**
45 * Create a new Body Template.
46 *
47 * @param template to parse.
48 * @param charset to use when encoding the template.
49 * @return a Body Template instance.
50 */
51 public static BodyTemplate create(String template, Charset charset) {
52 return new BodyTemplate(template, charset);
53 }
54
55 private BodyTemplate(String value, Charset charset) {
56 super(value, ExpansionOptions.ALLOW_UNRESOLVED, EncodingOptions.NOT_REQUIRED, false, charset);
57 if (value.startsWith(JSON_TOKEN_START_ENCODED) && value.endsWith(JSON_TOKEN_END_ENCODED)) {
58 this.json = true;
59 }
60 }
61
62 @Override
63 public String expand(Map<String, ?> variables) {
64 String expanded = super.expand(variables);
65 if (this.json) {
66 /* restore all start and end tokens */
67 expanded = expanded.replaceAll(JSON_TOKEN_START_ENCODED, JSON_TOKEN_START);
68 expanded = expanded.replaceAll(JSON_TOKEN_END_ENCODED, JSON_TOKEN_END);
69 }
70 return expanded;
71 }
72}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected