summaryrefslogtreecommitdiff
path: root/src/main/java/com/p4square/ccbapi/serializer/AbstractFormSerializer.java
blob: 969b11d6a4fdc3e8151ff5eed5fe6335be35c8d5 (plain)
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
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;

/**
 * AbstractFormSerializer provides default implementations for some methods defined in Serializer
 * and methods to support encoding form data for CCB.
 */
public abstract class AbstractFormSerializer<T> implements Serializer<T> {

    /**
     * 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");

    @Override
    public String encode(final T obj) {
        final StringBuilder sb = new StringBuilder();
        encode(obj, sb);
        return sb.toString();
    }

    @Override
    public void encode(final T obj, final StringBuilder sb) {
        FormBuilder builder = new FormBuilder(sb);
        encode(obj, builder);
    }

    /**
     * This method performs the actual serialize of T into a URL encoded form.
     *
     * @param obj The object to serialize
     * @param builder The FormBuilder to use.
     */
    protected abstract void encode(final T obj, final FormBuilder builder);

    /**
     * Append a field to the form.
     *
     * @param builder The StringBuilder to use.
     * @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.
     */
    protected void appendField(final StringBuilder builder, 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 builder The StringBuilder to use.
     * @param key The form key, which must be URLEncoded before calling this method.
     * @param value The value associated with the key.
     */
    protected void appendField(final StringBuilder builder, 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 builder The StringBuilder to use.
     * @param key The form key, which must be URLEncoded before calling this method.
     * @param value The value associated with the key.
     */
    protected void appendField(final StringBuilder builder, 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 builder The StringBuilder to use.
     * @param key The form key, which must be URLEncoded before calling this method.
     * @param value The value associated with the key.
     */
    protected void appendField(final StringBuilder builder, final String key, final LocalDate value) {
        appendField(builder, key, value.toString());

    }

    /**
     * Append a LocalDateTime field to the form.
     *
     * @param builder The StringBuilder to use.
     * @param key The form key, which must be URLEncoded before calling this method.
     * @param value The value associated with the key.
     */
    protected void appendField(final StringBuilder builder, final String key, final LocalDateTime value) {
        appendField(builder, key, DATE_TIME_FORMAT.format(value));
    }
}