1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package com.p4square.ccbapi.serializer;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Utility for building URL encoded form payloads.
*/
public class FormBuilder {
/**
* This is the datetime format specified by the CCB API Doc.
*/
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final StringBuilder builder;
/**
* Construct a new FormBuilder.
*/
public FormBuilder() {
this(new StringBuilder());
}
/**
* Construct a new FormBuilder which will append fields to the given StringBuilder.
*
* @param builder The StringBuilder to append to.
*/
public FormBuilder(StringBuilder builder) {
this.builder = builder;
}
/**
* This methods provides access to the StringBuilder.
*
* @return The StringBuilder passed in at construct time.
*/
public StringBuilder getStringBuilder() {
return builder;
}
/**
* Build the entire URL encoded form.
*
* @return All form fields as a URL encoded string.
*/
public String build() {
return builder.toString();
}
/**
* Append a field to the form.
*
* @param key The form key, which must be URLEncoded before calling this method.
* @param value The value associated with the key. The value will be URLEncoded by this method.
*/
public void appendField(final String key, final String value) {
if (builder.length() > 0) {
builder.append("&");
}
try {
builder.append(key).append("=").append(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 encoding should always be available.");
}
}
/**
* Append an integer field to the form.
*
* @param key The form key, which must be URLEncoded before calling this method.
* @param value The value associated with the key.
*/
public void appendField(final String key, final int value) {
if (builder.length() > 0) {
builder.append("&");
}
builder.append(key).append("=").append(value);
}
/**
* Append a boolean field to the form.
*
* @param key The form key, which must be URLEncoded before calling this method.
* @param value The value associated with the key.
*/
public void appendField(final String key, final boolean value) {
if (builder.length() > 0) {
builder.append("&");
}
builder.append(key).append("=").append(value ? "true" : "false");
}
/**
* Append a LocalDate field to the form.
*
* @param key The form key, which must be URLEncoded before calling this method.
* @param value The value associated with the key.
*/
public void appendField(final String key, final LocalDate value) {
appendField(key, value.toString());
}
/**
* Append a LocalDateTime field to the form.
*
* @param key The form key, which must be URLEncoded before calling this method.
* @param value The value associated with the key.
*/
public void appendField(final String key, final LocalDateTime value) {
appendField(key, DATE_TIME_FORMAT.format(value));
}
}
|