-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAbstractStudentService.java
120 lines (108 loc) · 3.41 KB
/
AbstractStudentService.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
/*******************************************************************************
* Copyright (c) 2019 Composent, Inc. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Composent, Inc. - initial API and implementation
******************************************************************************/
package com.mycorp.examples.student.remoteservice.host;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
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.core.MediaType;
import com.mycorp.examples.student.Address;
import com.mycorp.examples.student.Student;
public class AbstractStudentService {
// Provide a map-based storage of students
private static Map<String, Student> students = Collections.synchronizedMap(new HashMap<String, Student>());
// Create a single student and add to students map
static {
Student s = new Student("Joe Senior");
s.setId(UUID.randomUUID().toString());
s.setGrade("First");
Address a = new Address();
a.setCity("New York");
a.setState("NY");
a.setPostalCode("11111");
a.setStreet("111 Park Ave");
s.setAddress(a);
students.put(s.getId(), s);
}
// Implementation of StudentService based upon the students map
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/students")
public List<Student> getStudents() {
return new ArrayList<Student>(students.values());
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/studentscf")
public CompletableFuture<List<Student>> getStudentsCF() {
CompletableFuture<List<Student>> cf = new CompletableFuture<List<Student>>();
cf.complete(new ArrayList<Student>(students.values()));
return cf;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/students/{studentId}")
public Student getStudent(@PathParam("studentId") String id) {
return students.get(id);
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/students/{studentName}")
public Student createStudent(@PathParam("studentName") String studentName) {
if (studentName == null)
return null;
synchronized (students) {
Student s = new Student(studentName);
s.setId(UUID.randomUUID().toString());
students.put(s.getId(), s);
return s;
}
}
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/students")
public Student updateStudent(Student student) {
Student result = null;
if (student != null) {
String id = student.getId();
if (id != null) {
synchronized (students) {
result = students.get(student.getId());
if (result != null) {
String newName = student.getName();
if (newName != null)
result.setName(newName);
result.setGrade(student.getGrade());
result.setAddress(student.getAddress());
}
}
}
}
return result;
}
@DELETE
@Path("/students/{studentId}")
@Produces(MediaType.APPLICATION_JSON)
public Student deleteStudent(@PathParam("studentId") String studentId) {
return students.remove(studentId);
}
}