-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathResponseUtil.java
More file actions
505 lines (410 loc) · 13.2 KB
/
ResponseUtil.java
File metadata and controls
505 lines (410 loc) · 13.2 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
* Copyright (C) 2019-2023 Authlete, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package com.authlete.jaxrs.server.util;
import java.util.Map;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.server.mvc.Viewable;
/**
* Utility class for responses.
*
* @author Hideki Ikeda
*/
public class ResponseUtil
{
/**
* {@code "text/html;charset=UTF-8"}
*/
private static final MediaType MEDIA_TYPE_HTML =
MediaType.TEXT_HTML_TYPE.withCharset("UTF-8");
/**
* {@code "text/plain;charset=UTF-8"}
*/
private static final MediaType MEDIA_TYPE_PLAIN =
MediaType.TEXT_PLAIN_TYPE.withCharset("UTF-8");
/**
* {@code "application/json"}
*/
private static final MediaType MEDIA_TYPE_JSON =
MediaType.APPLICATION_JSON_TYPE;
/**
* {@code "application/jwt"}
*/
private static final MediaType MEDIA_TYPE_JWT =
new MediaType("application", "jwt");
/**
* Build a "text/plain" response of "200 OK".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* A "text/plain" response of "200 OK".
*/
public static Response ok(String entity)
{
return ok(entity, /* headers */ null);
}
public static Response ok(String entity, Map<String, Object> headers)
{
return builderForTextPlain(Status.OK, entity, headers).build();
}
/**
* Build an "application/json" response of "200 OK".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* An "application/json" response of "200 OK".
*/
public static Response okJson(String entity)
{
return okJson(entity, /* headers */ null);
}
public static Response okJson(String entity, Map<String, Object> headers)
{
return builderForJson(Status.OK, entity, headers).build();
}
public static Response okJwt(String entity, Map<String, Object> headers)
{
return builderForJwt(Status.OK, entity, headers).build();
}
/**
* Build a "text/html" response of "200 OK".
*
* @param entity
* A {@link Viewable} entity to contain in the response.
*
* @return
* A "text/html" response of "200 OK".
*/
public static Response ok(Viewable entity)
{
return ok(entity, /* headers */ null);
}
public static Response ok(Viewable entity, Map<String, Object> headers)
{
return builderForTextHtml(Status.OK, entity, headers).build();
}
/**
* Build an "application/json" response of "202 ACCEPTED".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* An "application/json" response of "202 ACCEPTED".
*/
public static Response acceptedJson(String entity)
{
return acceptedJson(entity, /* headers */ null);
}
public static Response acceptedJson(String entity, Map<String, Object> headers)
{
return builderForJson(Status.ACCEPTED, entity, headers).build();
}
public static Response acceptedJwt(String entity, Map<String, Object> headers)
{
return builderForJwt(Status.ACCEPTED, entity, headers).build();
}
/**
* Build a response of "204 No Content".
*
* @return
* A response of "204 No Content".
*/
public static Response noContent()
{
return Response.noContent().build();
}
/**
* Build a "text/plain" response of "400 Bad Request".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* A "text/plain" response of "400 Bad Request".
*/
public static Response badRequest(String entity)
{
return badRequest(entity, /* headers */ null);
}
public static Response badRequest(String entity, Map<String, Object> headers)
{
return builderForTextPlain(Status.BAD_REQUEST, entity, headers).build();
}
/**
* Build an "application/json" response of "400 Bad Request".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* An "application/json" response of "400 Bad Request".
*/
public static Response badRequestJson(String entity)
{
return badRequestJson(entity, /* headers */ null);
}
public static Response badRequestJson(String entity, Map<String, Object> headers)
{
return builderForJson(Status.BAD_REQUEST, entity, headers).build();
}
/**
* Build a "text/html" response of "400 Bad Request".
*
* @param entity
* A {@link Viewable} entity to contain in the response.
*
* @return
* A "text/html" response of "400 Bad Request".
*/
public static Response badRequest(Viewable entity)
{
return badRequest(entity, /* headers */ null);
}
public static Response badRequest(Viewable entity, Map<String, Object> headers)
{
return builderForTextHtml(Status.BAD_REQUEST, entity, headers).build();
}
/**
* Build a "text/plain" response of "401 Unauthorized".
*
* @param entity
* A string entity to contain in the response.
*
* @param challenge
* The value of the "WWW-Authenticate" header of the response.
*
* @return
* A "text/plain" response of "401 Unauthorized".
*/
public static Response unauthorized(String entity, String challenge)
{
return unauthorized(entity, challenge, /* headers */ null);
}
public static Response unauthorized(
String entity, String challenge, Map<String, Object> headers)
{
return builderForTextPlain(Status.UNAUTHORIZED, entity, headers)
.header(HttpHeaders.WWW_AUTHENTICATE, challenge)
.build();
}
/**
* Build a "text/html" response of "401 Unauthorized".
*
* @param entity
* A {@link Viewable} entity to contain in the response.
*
* @param challenge
* The value of the "WWW-Authenticate" header of the response.
*
* @return
* A "text/html" response of "401 Unauthorized".
*/
public static Response unauthorized(Viewable entity, String challenge)
{
return unauthorized(entity, challenge, /* headers */ null);
}
public static Response unauthorized(
Viewable entity, String challenge, Map<String, Object> headers)
{
return builderForTextHtml(Status.UNAUTHORIZED, entity, headers)
.header(HttpHeaders.WWW_AUTHENTICATE, challenge)
.build();
}
/**
* Build a "text/plain" response of "403 Forbidden".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* An "text/plain" response of "403 Forbidden".
*/
public static Response forbidden(String entity)
{
return forbidden(entity, /* headers */ null);
}
public static Response forbidden(String entity, Map<String, Object> headers)
{
return builderForTextPlain(Status.FORBIDDEN, entity, headers).build();
}
/**
* Build an "application/json" response of "403 Forbidden".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* An "application/json" response of "403 Forbidden".
*/
public static Response forbiddenJson(String entity)
{
return forbiddenJson(entity, /* headers */ null);
}
public static Response forbiddenJson(String entity, Map<String, Object> headers)
{
return builderForJson(Status.FORBIDDEN, entity, headers).build();
}
/**
* Build a "text/plain" response of "404 Not Found".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* A "text/plain" response of "404 Not Found".
*/
public static Response notFound(String entity)
{
return notFound(entity, /* headers */ null);
}
public static Response notFound(String entity, Map<String, Object> headers)
{
return builderForTextPlain(Status.NOT_FOUND, entity, headers).build();
}
/**
* Build an "application/json" response of "404 Not Found".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* An "application/json" response of "404 Not Found".
*/
public static Response notFoundJson(String entity)
{
return notFoundJson(entity, /* headers */ null);
}
public static Response notFoundJson(String entity, Map<String, Object> headers)
{
return builderForJson(Status.NOT_FOUND, entity, headers).build();
}
/**
* Build a "text/html" response of "404 Not Found".
*
* @param entity
* A {@link Viewable} entity to contain in the response.
*
* @return
* A "text/html" response of "404 Not Found".
*/
public static Response notFound(Viewable entity)
{
return notFound(entity, /* headers */ null);
}
public static Response notFound(Viewable entity, Map<String, Object> headers)
{
return builderForTextHtml(Status.NOT_FOUND, entity, headers).build();
}
/**
* Build a "text/plain" response of "500 Internal Server Error".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* A "text/plain" response of "500 Internal Server Error".
*/
public static Response internalServerError(String entity)
{
return internalServerError(entity, /* headers */ null);
}
public static Response internalServerError(String entity, Map<String, Object> headers)
{
return builderForTextPlain(Status.INTERNAL_SERVER_ERROR, entity, headers).build();
}
/**
* Build a "text/plain" response of "500 Internal Server Error".
*
* @param entity
* A string entity to contain in the response.
*
* @return
* A "text/plain" response of "500 Internal Server Error".
*/
public static Response internalServerErrorJson(String entity)
{
return internalServerErrorJson(entity, /* headers */ null);
}
public static Response internalServerErrorJson(String entity, Map<String, Object> headers)
{
return builderForJson(Status.INTERNAL_SERVER_ERROR, entity, headers).build();
}
/**
* Build a "text/html" response of "500 Internal Server Error".
*
* @param entity
* A {@link Viewable} entity to contain in the response.
*
* @return
* A "text/html" response of "500 Internal Server Error".
*/
public static Response internalServerError(Viewable entity)
{
return internalServerError(entity, /* headers */ null);
}
public static Response internalServerError(Viewable entity, Map<String, Object> headers)
{
return builderForTextHtml(Status.INTERNAL_SERVER_ERROR, entity, headers).build();
}
private static ResponseBuilder builderForTextPlain(
Status status, String entity, Map<String, Object> headers)
{
return builder(status, entity, MEDIA_TYPE_PLAIN, headers);
}
private static ResponseBuilder builderForTextHtml(
Status status, Viewable entity, Map<String, Object> headers)
{
return builder(status, entity, MEDIA_TYPE_HTML, headers);
}
private static ResponseBuilder builderForJson(
Status status, String entity, Map<String, Object> headers)
{
return builder(status, entity, MEDIA_TYPE_JSON, headers);
}
private static ResponseBuilder builderForJwt(
Status status, String entity, Map<String, Object> headers)
{
return builder(status, entity, MEDIA_TYPE_JWT, headers);
}
private static ResponseBuilder builder(
Status status, Object entity, MediaType type, Map<String, Object> headers)
{
ResponseBuilder builder = Response
.status(status)
.entity(entity)
.type(type);
// If additional headers are given.
if (headers != null)
{
// For each additional header.
for (Map.Entry<String, Object> header : headers.entrySet())
{
// Add the header.
builder.header(header.getKey(), header.getValue());
}
}
return builder;
}
}