4
4
import java .util .HashMap ;
5
5
import java .util .Iterator ;
6
6
import java .util .Map ;
7
- import java .util .regex .Pattern ;
8
- import java .util .regex .Matcher ;
9
7
10
8
/**
11
9
* Response covers all functionality to wrap a Backend API Response like accessing data
14
12
* @version %I%, %G%
15
13
* @since 2.0
16
14
*/
17
- public class Response extends ResponseTemplate {
15
+ public class Response {
16
+ /** backend system plain response data */
17
+ private String raw ;
18
+ /** backend system parsed response data (hash format) */
19
+ private Map <String , Object > hash ;
20
+
18
21
/**
19
22
* The API Command used within this request
20
23
*/
@@ -37,6 +40,15 @@ public class Response extends ResponseTemplate {
37
40
*/
38
41
private ArrayList <Record > records ;
39
42
43
+ /**
44
+ * Constructor
45
+ *
46
+ * @param raw API plain response
47
+ */
48
+ public Response (String raw ) {
49
+ this (raw , Map .ofEntries (), Map .ofEntries ());
50
+ }
51
+
40
52
/**
41
53
* Constructor
42
54
*
@@ -56,22 +68,14 @@ public Response(String raw, Map<String, String> cmd) {
56
68
*/
57
69
@ SuppressWarnings ("unchecked" ) // not really a good way ...
58
70
public Response (String raw , Map <String , String > cmd , Map <String , String > ph ) {
59
- super (raw );
60
-
61
- String regex = "\\ {[^}]+\\ }" ;
62
- Pattern pattern = Pattern .compile (regex );
63
- Matcher matcher = pattern .matcher (this .raw );
64
- if (matcher .find ()) {
65
- for (Map .Entry <String , String > entry : ph .entrySet ()) {
66
- this .raw = this .raw .replace ("{" + entry .getKey () + "}" , entry .getValue ());
67
- }
68
- super .init (this .raw .replaceAll (regex , "" ));
71
+ // secure password for output
72
+ if (cmd .containsKey ("PASSWORD" )) {
73
+ cmd .replace ("PASSWORD" , "***" );
69
74
}
70
75
76
+ this .raw = ResponseTranslator .translate (raw , cmd , ph );
77
+ this .hash = ResponseParser .parse (this .raw );
71
78
this .command = new HashMap <String , String >(cmd );
72
- if (this .command .containsKey ("PASSWORD" )) { // make password no longer accessible
73
- this .command .replace ("PASSWORD" , "***" );
74
- }
75
79
this .columnkeys = new ArrayList <String >();
76
80
this .columns = new ArrayList <Column >();
77
81
this .recordIndex = 0 ;
@@ -106,6 +110,112 @@ public Response(String raw, Map<String, String> cmd, Map<String, String> ph) {
106
110
}
107
111
}
108
112
113
+ /**
114
+ * Get API response code
115
+ *
116
+ * @return API response code
117
+ */
118
+ public int getCode () {
119
+ return Integer .parseInt ((String ) this .hash .get ("CODE" ));
120
+ }
121
+
122
+ /**
123
+ * Get API response description
124
+ *
125
+ * @return API response description
126
+ */
127
+ public String getDescription () {
128
+ return (String ) this .hash .get ("DESCRIPTION" );
129
+ }
130
+
131
+ /**
132
+ * Get Plain API response
133
+ *
134
+ * @return Plain API response
135
+ */
136
+ public String getPlain () {
137
+ return this .raw ;
138
+ }
139
+
140
+ /**
141
+ * Get Queuetime of API response
142
+ *
143
+ * @return Queuetime of API response
144
+ */
145
+ public double getQueuetime () {
146
+ String rt = (String ) this .hash .get ("QUEUETIME" );
147
+ if (rt != null ) {
148
+ return Double .parseDouble ((String ) this .hash .get ("QUEUETIME" ));
149
+ }
150
+ return 0.00 ;
151
+ }
152
+
153
+ /**
154
+ * Get API response as Hash
155
+ *
156
+ * @return API response hash
157
+ */
158
+ public Map <String , Object > getHash () {
159
+ return this .hash ;
160
+ }
161
+
162
+ /**
163
+ * Get Runtime of API response
164
+ *
165
+ * @return Runtime of API response
166
+ */
167
+ public double getRuntime () {
168
+ String rt = (String ) this .hash .get ("RUNTIME" );
169
+ if (rt != null ) {
170
+ return Double .parseDouble ((String ) this .hash .get ("RUNTIME" ));
171
+ }
172
+ return 0.00 ;
173
+ }
174
+
175
+ /**
176
+ * Check if current API response represents an error case API response code is an 5xx code
177
+ *
178
+ * @return boolean result
179
+ */
180
+ public boolean isError () {
181
+ String code = (String ) this .hash .get ("CODE" );
182
+ return code .charAt (0 ) == '5' ;
183
+ }
184
+
185
+ /**
186
+ * Check if current API response represents a success case API response code is an 2xx code
187
+ *
188
+ * @return boolean result
189
+ */
190
+ public boolean isSuccess () {
191
+ String code = (String ) this .hash .get ("CODE" );
192
+ return code .charAt (0 ) == '2' ;
193
+ }
194
+
195
+ /**
196
+ * Check if current API response represents a temporary error case API response code is an 4xx
197
+ * code
198
+ *
199
+ * @return boolean result
200
+ */
201
+ public boolean isTmpError () {
202
+ String code = (String ) this .hash .get ("CODE" );
203
+ return code .charAt (0 ) == '4' ;
204
+ }
205
+
206
+ /**
207
+ * Check if current operation is returned as pending
208
+ *
209
+ * @return boolean result
210
+ */
211
+ public boolean isPending () {
212
+ String pending = (String ) this .hash .get ("PENDING" );
213
+ if (pending != null ) {
214
+ return pending .equals ("1" );
215
+ }
216
+ return false ;
217
+ }
218
+
109
219
/**
110
220
* Add a column to the column list
111
221
*
0 commit comments