/
UserResource.java
594 lines (575 loc) · 22.1 KB
/
UserResource.java
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
* Copyright (c) 2017 Contributors to the Eclipse Foundation
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.eclipse.microprofile.openapi.apps.airlines.resources;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.openapi.annotations.ExternalDocumentation;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterIn;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterStyle;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.enums.SecuritySchemeType;
import org.eclipse.microprofile.openapi.annotations.headers.Header;
import org.eclipse.microprofile.openapi.annotations.links.Link;
import org.eclipse.microprofile.openapi.annotations.links.LinkParameter;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Encoding;
import org.eclipse.microprofile.openapi.annotations.media.ExampleObject;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameters;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirement;
import org.eclipse.microprofile.openapi.annotations.security.SecurityRequirementsSet;
import org.eclipse.microprofile.openapi.annotations.security.SecurityScheme;
import org.eclipse.microprofile.openapi.annotations.servers.Server;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;
import org.eclipse.microprofile.openapi.annotations.tags.Tags;
import org.eclipse.microprofile.openapi.apps.airlines.data.UserData;
import org.eclipse.microprofile.openapi.apps.airlines.exception.ApiException;
import org.eclipse.microprofile.openapi.apps.airlines.exception.NotFoundException;
import org.eclipse.microprofile.openapi.apps.airlines.model.User;
@Path("/user")
@Produces({"application/json", "application/xml"})
@SecurityScheme(
description = "user security scheme",
type = SecuritySchemeType.HTTP,
securitySchemeName = "httpSchemeForTest",
scheme = "testScheme")
@SecurityRequirement(
name = "httpSchemeForTest"
)
public class UserResource {
private static UserData userData = new UserData();
public UserData getUserData() {
return UserResource.userData;
}
public void setUserData(UserData userData) {
UserResource.userData = userData;
}
@POST
@Tags(refs={"user","create"})
@APIResponses(value={
@APIResponse(
responseCode = "200",
description = "New user record successfully created."
),
@APIResponse(
responseCode = "400",
description = "Unable to create this user record."
)
})
@Parameters(value={
@Parameter(
name = "id",
in = ParameterIn.QUERY,
description = "User id for the new user record to be created",
required = true,
allowReserved = true,
style = ParameterStyle.FORM,
schema = @Schema(type = SchemaType.INTEGER, format = "int32")
),
@Parameter(
name = "userName",
in = ParameterIn.QUERY,
description = "Username for the new user record to be created",
required = true,
schema = @Schema(
type = SchemaType.STRING,
externalDocs = @ExternalDocumentation(
description = "How to create good user names.",
url = "http://exampleurl.com/usernames"
)
)
),
@Parameter(
name = "password",
in = ParameterIn.QUERY,
description = "User password for the new user record to be created",
required = true,
hidden = true,
schema = @Schema(
type = SchemaType.STRING,
externalDocs = @ExternalDocumentation(
description = "How to create good passwords.",
url = "http://exampleurl.com/passwords"
)
)
),
@Parameter(
name = "firstName",
in = ParameterIn.QUERY,
description = "User's first name for the new user record to be created",
required = true,
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "lastName",
in = ParameterIn.QUERY,
description = "User's last name for the new user record to be created",
style = ParameterStyle.FORM,
required = true,
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "sex",
in = ParameterIn.QUERY,
description = "User's sex for the new user record to be created",
required = true,
style = ParameterStyle.FORM,
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "age",
in = ParameterIn.QUERY,
description = "User's age for the new user record to be created",
required = true,
schema = @Schema(type = SchemaType.INTEGER, format = "int64")
),
@Parameter(
name = "phone",
in = ParameterIn.QUERY,
description = "User phone number for the new user record to be created",
required = true,
schema = @Schema(type = SchemaType.STRING)
),
@Parameter(
name = "status",
in = ParameterIn.QUERY,
description = "User status for the new user record to be created",
required = true,
schema = @Schema(type = SchemaType.INTEGER)
)
})
@Operation(
summary = "Create user",
description = "This can only be done by the logged in user.",
operationId = "createUser"
)
public Response createUser(
@RequestBody(
description = "Record of a new user to be created in the system.",
required = true,
content = @Content(
mediaType = "application/json",
schema = @Schema(
name = "testUser",
type = SchemaType.OBJECT,
maxProperties = 1024,
minProperties = 1,
required = true,
implementation = User.class),
examples = @ExampleObject(
name = "user",
summary = "External user example",
externalValue = "http://foo.bar/examples/user-example.json"
),
encoding = @Encoding(
name = "email",
contentType = "text/plain",
style = "form",
allowReserved = true,
explode = true,
headers = @Header(
name = "testHeader",
description = "Minimum rate",
schema = @Schema(type = SchemaType.INTEGER),
required = true,
allowEmptyValue = true,
deprecated = true
)
)
)
) User user) {
userData.addUser(user);
return Response.ok().entity("").build();
}
@POST
@Path("/createWithArray")
@Tag(ref="user")
@Tag(ref="create")
@APIResponse(
responseCode = "200",
description = "Successfully created list of users."
)
@APIResponse(
responseCode = "400",
description = "Unable to create list of users."
)
@Operation(
summary = "Creates list of users with given input array", //Array of User objects
operationId = "createUsersFromArray"
/* tags = {"user"}, //this operation intentionally doesn't have tags attribute, since above Tag ref should apply */
)
public Response createUsersWithArrayInput(
@RequestBody(
description = "Array of user object",
required = true,
content = @Content(
mediaType = "application/json",
schema = @Schema(
type = SchemaType.ARRAY,
implementation = User.class,
nullable = true,
writeOnly = true,
minItems = 2,
maxItems = 20,
uniqueItems = true
),
encoding = @Encoding(
name = "firstName",
contentType = "text/plain",
style = "form",
allowReserved = true,
explode = true
)
)
) User[] users) {
for (User user : users) {
userData.addUser(user);
}
return Response.ok().entity("").build();
}
@POST
@Path("/createWithList")
@Tag(ref="user")
@Tags(refs="create")
@APIResponse(
responseCode = "200",
description = "Successfully created list of users."
)
@APIResponse(
responseCode = "400",
description = "Unable to create list of users."
)
@Operation(
summary = "Creates list of users with given input list", //List of User objects
operationId = "createUsersFromList"
)
public Response createUsersWithListInput(
@RequestBody(
description = "List of user object",
required = true
)
java.util.List<User> users) {
for (User user : users) {
userData.addUser(user);
}
return Response.ok().entity("").build();
}
@Path("/{username}")
@PUT
@RequestBody(
name = "user",
description = "Record of a new user to be created in the system.",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = User.class),
examples = @ExampleObject(
name = "user",
summary = "Example user properties to update",
value = "Properties to update in JSON format here"
)
)
)
@Operation(
summary = "Update user",
description = "This can only be done by the logged in user.",
operationId = "updateUser")
@APIResponses(value={
@APIResponse(
responseCode = "200",
description = "User updated successfully",
content = @Content(
schema = @Schema(ref="User"),
encoding = @Encoding(
name = "password",
contentType = "text/plain",
style = "form",
allowReserved = true,
explode = true,
headers = @Header(ref="Max-Rate")
)
)
),
@APIResponse(
responseCode = "400",
description = "Invalid user supplied"
),
@APIResponse(
responseCode = "404",
description = "User not found"
)
})
@Tag(ref="user")
public Response updateUser(
@Parameter(
name = "username",
description = "User that needs to be updated",
schema = @Schema(type = SchemaType.STRING),
required = true
)
@PathParam("username") String username,
User user) {
userData.addUser(user);
return Response.ok().entity("").build();
}
@PATCH
@Path("/{username}")
@APIResponse(
responseCode = "200",
description = "Password was changed successfully"
)
@Operation(
summary = "Change user password",
description = "This changes the password for the logged in user.",
operationId = "changePassword"
)
@SecurityRequirementsSet(
value = {
@SecurityRequirement(
name = "userApiKey"
),
@SecurityRequirement(
name = "userBearerHttp"
)
}
)
public void changePassword(
@Parameter(
name = "username",
description = "name that need to be deleted",
schema = @Schema(type = SchemaType.STRING),
required = true)
@PathParam("username") String username,
@Parameter(
name = "currentPassword",
description = "the user's current password",
schema = @Schema(type = SchemaType.STRING),
required = true)
@QueryParam("currentPassword") String currentPassword,
@Parameter(
name = "newPassword",
description = "the user's desired new password",
schema = @Schema(type = SchemaType.STRING),
required = true)
@QueryParam("newPassword") String newPassword) {
// don't do anything - this is just a TCK test
}
@DELETE
@Path("/{username}")
@Tag(ref="user")
@APIResponse(
responseCode = "200",
description = "User deleted successfully"
)
@APIResponse(
responseCode = "400",
description = "Invalid username supplied"
)
@APIResponse(
responseCode = "404",
description = "User not found"
)
@Operation(
summary = "Delete user",
description = "This can only be done by the logged in user.",
operationId = "deleteUser")
public Response deleteUser(
@Parameter(
name = "username",
description = "The name that needs to be deleted",
schema = @Schema(type = SchemaType.STRING),
required = true
)
@PathParam("username") String userName) {
if (userData.removeUser(userName)) {
return Response.ok().entity("").build();
}
else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
@GET
@Path("/{username}")
@Tag(ref="user")
@APIResponses(value={
@APIResponse(
responseCode = "200",
description = "Successfully retrieved user by user name.",
content = @Content(
schema = @Schema(implementation = User.class)
)),
@APIResponse(
responseCode = "400",
description = "Invalid username supplied",
content = @Content(
schema = @Schema(implementation = User.class)
)),
@APIResponse(
responseCode = "404",
description = "User not found",
content = @Content(
schema = @Schema(implementation = User.class)
)
)
})
@Operation(
summary = "Get user by user name",
operationId = "getUserByUserName")
public Response getUserByName(
@Parameter(
ref= "username"
)
@PathParam("username") String userName) throws ApiException {
User user = userData.findUserByName(userName);
if (null != user) {
return Response.ok().entity(user).build();
}
else {
throw new NotFoundException(404, "User not found");
}
}
@GET
@Path("/{id}")
@Tag(ref="user")
@APIResponse(
responseCode = "200",
description = "Successfully retrieved user by id.",
content = @Content(
schema = @Schema(implementation = User.class)),
links = {
@Link(
name = "User name",
description = "The username corresponding to provided user id",
operationId = "getUserByName",
parameters = @LinkParameter(
name = "userId",
expression = "$request.path.id")),
@Link(
name = "Review",
description = "The reviews provided by user",
operationRef = "/db/reviews/{userName}",
parameters = @LinkParameter(
name = "path.userName",
expression = "$response.body#userName"),
requestBody = "$request.path.id",
server = @Server(url = "http://example.com"))
}
)
@APIResponse(
responseCode = "400",
description = "Invalid id supplied",
content = @Content(
schema = @Schema(implementation = User.class)
))
@APIResponse(
responseCode = "404",
description = "User not found",
content = @Content(
schema = @Schema(implementation = User.class)
)
)
@Operation(
summary = "Get user by id",
operationId = "getUserById")
public Response getUserById(
@Parameter(
name = "id",
description = "The name that needs to be fetched. Use 1 for testing.",
schema = @Schema(type = SchemaType.INTEGER),
required = true
)
@PathParam("id") int id) throws ApiException {
User user = userData.findUserById(id);
if (null != user) {
return Response.ok().entity(user).build();
}
else {
throw new NotFoundException(404, "User not found");
}
}
@GET
@Path("/login")
@Tag()
@ExternalDocumentation(
description = "Policy on user security.",
url = "http://exampleurl.com/policy"
)
@APIResponse(
responseCode = "200",
description = "Successful user login.",
content = @Content(
schema = @Schema(implementation = User.class)
)
)
@APIResponse(
responseCode = "400",
description = "Invalid username/password supplied"
)
@Operation(
summary = "Logs user into the system",
operationId = "logInUser"
)
@SecurityScheme(
ref = "#/components/securitySchemes/httpTestScheme"
)
@SecurityRequirement(
name = "httpTestScheme"
)
public Response loginUser(
@Parameter(
name = "username",
description = "The user name for login",
schema = @Schema(type = SchemaType.STRING),
required = true
)
@QueryParam("username") String username,
@Parameter(
name = "password",
description = "The password for login in clear text",
schema = @Schema(type = SchemaType.STRING),
required = true)
@QueryParam("password") String password) {
return Response.ok()
.entity("logged in user session:" + System.currentTimeMillis())
.build();
}
@GET
@Path("/logout")
@APIResponse(
responseCode = "200",
description = "Successful user logout."
)
@ExternalDocumentation(
description = "Policy on user security.",
url = "http://exampleurl.com/policy"
)
@Operation(
summary = "Logs out current logged in user session",
operationId = "logOutUser"
/* tags = {"user"}, // intentionally removed to have a method with no tags */)
public Response logoutUser() {
return Response.ok().entity("").build();
}
}