• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperwallet / java-sdk / #654

01 May 2025 10:10PM CUT coverage: 97.037%. Remained the same
#654

push

grmeyer-hw-dev
Update changelog

5567 of 5737 relevant lines covered (97.04%)

67.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.03
/src/main/java/com/hyperwallet/clientsdk/util/Message.java
1
package com.hyperwallet.clientsdk.util;
2

3
import cc.protea.util.http.Response;
4
import java.io.UnsupportedEncodingException;
5
import java.net.URLEncoder;
6
import java.util.ArrayList;
7
import java.util.HashMap;
8
import java.util.List;
9
import java.util.Map;
10

11
/*
12

13
 Modified with modifications copyright Richard Stanford
14

15
 -- Original portions by Joe Ernst --
16

17
 Copyright 2012 Joe J. Ernst
18

19
 Licensed under the Apache License, Version 2.0 (the "License");
20
 you may not use this file except in compliance with the License.
21
 You may obtain a copy of the License at
22

23
 http://www.apache.org/licenses/LICENSE-2.0
24

25
 Unless required by applicable law or agreed to in writing, software
26
 distributed under the License is distributed on an "AS IS" BASIS,
27
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
 See the License for the specific language governing permissions and
29
 limitations under the License.
30

31
 */
32

33
/**
34
 * This class represents an HTTP Message, which could either be a Request or a Response. Message is an abstract class that contains fields and methods that are common to both types of Messages.
35
 *
36
 * @param <T>
37
 *            A Type that extends Message (either {@link Request} or {@link Response})
38
 */
39
public abstract class Message<T extends Message<T>> {
40
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
83✔
41
    String body;
42

43
    /**
44
     * The default constructor is a no-op constructor.
45
     */
46
    public Message() {
83✔
47
        // No-arg, No-op constructor
48
    }
83✔
49

50
    /**
51
     * Returns the Message body (also known as the Entity body).
52
     *
53
     * @return The body of the HTTP Message. It will typically be HTML, JSON, or XML.
54
     */
55
    public String getBody() {
56
        return this.body;
×
57
    }
58

59
    public Map<String, List<String>> getHeaders() {
60
        return this.headers;
×
61
    }
62

63
    public List<String> getHeaders(final String label) {
64
        if (label == null) {
×
65
            return null;
×
66
        }
67
        for (String key : headers.keySet()) {
×
68
            if (label.equalsIgnoreCase(key)) {
×
69
                return headers.get(key);
×
70
            }
71
        }
×
72
        return null;
×
73
    }
74

75
    public String getHeader(final String label) {
76
        List<String> list = getHeaders(label);
×
77
        if (list == null || list.isEmpty()) {
×
78
            return null;
×
79
        }
80
        return list.get(0);
×
81
    }
82

83
    /**
84
     * Sets the body of the Message.
85
     *
86
     * @param body
87
     *            This is typically the JSON, XML, or Form Parameters being sent to the server.
88
     * @return this Message, to support chained method calls
89
     */
90
    @SuppressWarnings("unchecked")
91
    public T setBody(final String body) {
92
        this.body = body;
48✔
93
        return (T) this;
48✔
94
    }
95

96
    /**
97
     * Adds a single header value to the Message.
98
     *
99
     * @param name
100
     *            The header name.
101
     * @param value
102
     *            The header value
103
     * @return this Message, to support chained method calls
104
     */
105
    @SuppressWarnings("unchecked")
106
    public T addHeader(final String name, final String value) {
107
        List<String> values = new ArrayList<String>();
607✔
108
        values.add(value);
607✔
109

110
        this.headers.put(name, values);
607✔
111
        return (T) this;
607✔
112
    }
113

114
    /**
115
     * Removes the specified header.
116
     *
117
     * @param name
118
     *            The name of the header to remove.
119
     * @return this Message, to support chained method calls
120
     */
121
    @SuppressWarnings("unchecked")
122
    public T removeHeader(final String name) {
123
        this.headers.remove(name);
×
124
        return (T) this;
×
125
    }
126

127
    /**
128
     * Sets all of the headers in one call.
129
     *
130
     * @param headers
131
     *            A Map of headers, where the header name is a String, and the value is a List of one or more values.
132
     * @return this Message, to support chained method calls
133
     */
134
    @SuppressWarnings("unchecked")
135
    public T setHeaders(final Map<String, List<String>> headers) {
136
        this.headers = headers;
×
137
        return (T) this;
×
138
    }
139

140
    String encode(final String in) {
141
        try {
142
            return URLEncoder.encode(in, "UTF-8");
×
143
        } catch (UnsupportedEncodingException e) {
×
144
            return in;
×
145
        }
146
    }
147

148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc