1 package com.acumenvelocity.ath.common;
2
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.ws.rs.core.MediaType;
8 import javax.ws.rs.core.Response.Status;
9
10 import com.acumenvelocity.ath.model.ResponseCodeMessage;
11
12 import io.swagger.oas.inflector.models.ResponseContext;
13 import net.sf.okapi.common.Util;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class Response {
32
33 static public final MediaType APPLICATION_JSON_MEDIA_TYPE = new MediaType("application", "json");
34 static public final String APPLICATION_JSON = APPLICATION_JSON_MEDIA_TYPE.toString();
35
36 static public final MediaType APPLICATION_JSON_UTF8_MEDIA_TYPE = new MediaType("application",
37 "json", "utf-8");
38 static public final String APPLICATION_JSON_UTF8 = APPLICATION_JSON_UTF8_MEDIA_TYPE.toString();
39
40
41
42
43
44
45
46
47
48 private static ResponseContext makeResponse(int code, String message) {
49
50 if (code >= 200 && code <= 299) {
51 Log.info(Response.class, message);
52 } else {
53 Log.warn(Response.class, message);
54 }
55
56
57 ResponseCodeMessage rc = new ResponseCodeMessage();
58 rc.setCode(code);
59 rc.setMessage(message);
60 String st = JacksonUtil.toJson(rc, false);
61
62
63 return new ResponseContext()
64 .status(code)
65 .entity(EncodingUtil.toUtf8(st))
66 .contentType(APPLICATION_JSON_UTF8_MEDIA_TYPE);
67 }
68
69
70
71
72
73
74
75
76 public static ResponseContext error(int code, String message) {
77 return makeResponse(code, message);
78 }
79
80
81
82
83
84
85
86
87
88 public static ResponseContext error(int code, String format, Object... arguments) {
89 String message = Log.format(format, arguments);
90 return error(code, message);
91 }
92
93
94
95
96
97
98
99
100
101 public static ResponseContext error(int code, Throwable cause, String message) {
102 String st = Log.format("{} -- {}", message, unwindCause(cause));
103 Log.error(Response.class, st);
104 return makeResponse(code, st);
105 }
106
107 public static String unwindCause(Throwable t) {
108 if (t == null) {
109 return "";
110 }
111
112 StringBuilder sb = new StringBuilder();
113 Throwable cur = t;
114
115 while (cur != null) {
116 String msg = cur.getMessage();
117
118 if (msg != null && !msg.isBlank()) {
119 if (sb.length() > 0) {
120 sb.append(" -- ");
121 }
122
123 sb.append(msg.trim());
124 }
125
126 cur = cur.getCause();
127
128 if (cur == t) {
129 break;
130 }
131 }
132
133 return sb.toString();
134 }
135
136
137
138
139
140
141
142
143
144
145 public static ResponseContext error(int code, Throwable cause, String format,
146 Object... arguments) {
147 return error(code, cause, Log.format(format, arguments));
148 }
149
150
151
152
153
154
155
156
157
158 public static ResponseContext success(int code, String format, Object... arguments) {
159 String message = Log.format(format, arguments);
160 return success(code, message);
161 }
162
163
164
165
166
167
168
169
170
171 public static ResponseContext success(int code, Object obj) {
172 return success(code, obj, true);
173 }
174
175
176
177
178
179
180
181
182
183
184
185 public static ResponseContext success(int code, Object obj, boolean forceObjectIdAsObjects) {
186
187 if (obj instanceof String) {
188 return makeResponse(code, (String) obj);
189 }
190
191
192
193
194 String st = forceObjectIdAsObjects ? JacksonUtil.toJson(obj, false)
195 : JacksonUtil.toJson(obj, false);
196
197 return new ResponseContext()
198 .status(code)
199 .entity(EncodingUtil.toUtf8(st))
200 .contentType(APPLICATION_JSON_UTF8_MEDIA_TYPE);
201 }
202
203
204
205
206
207
208
209 public static ResponseContext success(int code) {
210 return makeResponse(code, "Success");
211 }
212
213 public static ResponseContext makeResponse(Status status, Object obj, String headerKey,
214 String headerValue) {
215
216
217 if (obj instanceof String) {
218 return makeResponse(status.getStatusCode(), (String) obj);
219
220 } else if (obj instanceof File) {
221 return new ResponseContext()
222 .status(status)
223 .header(headerKey, headerValue)
224 .contentType(MediaType.APPLICATION_OCTET_STREAM)
225 .entity(obj);
226 }
227
228
229 String st = JacksonUtil.toJson(obj, false);
230
231 return Util.isEmpty(headerKey) || Util.isEmpty(headerValue) ? new ResponseContext()
232 .status(status)
233 .entity(EncodingUtil.toUtf8(st))
234 .contentType(Response.APPLICATION_JSON_UTF8_MEDIA_TYPE)
235 : new ResponseContext()
236 .status(status)
237 .header(headerKey, headerValue)
238 .entity(EncodingUtil.toUtf8(st))
239 .contentType(Response.APPLICATION_JSON_UTF8_MEDIA_TYPE);
240 }
241
242
243
244
245
246
247 public static Builder builder() {
248 return new Builder();
249 }
250
251
252
253
254 public static class Builder {
255 private Status status;
256 private Object entity;
257 private final Map<String, String> headers = new HashMap<>();
258
259
260
261
262
263
264
265 public Builder status(Status status) {
266 this.status = status;
267 return this;
268 }
269
270
271
272
273
274
275
276
277 public Builder header(String name, String value) {
278 if (!Util.isEmpty(name) && !Util.isEmpty(value)) {
279 headers.put(name, value);
280 }
281 return this;
282 }
283
284
285
286
287
288
289
290 public Builder entity(Object entity) {
291 this.entity = entity;
292 return this;
293 }
294
295
296
297
298
299
300 public ResponseContext build() {
301 if (status == null) {
302 throw new IllegalStateException("Status must be set");
303 }
304
305 if (entity == null) {
306 return makeResponse(status.getStatusCode(), "Success");
307 }
308
309 ResponseContext context = new ResponseContext();
310 context.status(status);
311
312 for (Map.Entry<String, String> header : headers.entrySet()) {
313 context.header(header.getKey(), header.getValue());
314 }
315
316 if (entity instanceof String) {
317 return makeResponse(status.getStatusCode(), (String) entity);
318
319 } else if (entity instanceof File) {
320 context.contentType(MediaType.APPLICATION_OCTET_STREAM);
321 context.entity(entity);
322
323 } else {
324 String st = JacksonUtil.toJson(entity, false);
325 context.entity(EncodingUtil.toUtf8(st));
326 context.contentType(APPLICATION_JSON_UTF8_MEDIA_TYPE);
327 }
328
329 return context;
330 }
331 }
332
333 public static String getMessage(ResponseContext res) {
334 ResponseCodeMessage rcm = JacksonUtil.fromJson(res.getEntity().toString(),
335 ResponseCodeMessage.class);
336
337 return rcm == null ? null : rcm.getMessage();
338 }
339 }