-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServeAPIHttpHandler.java
36 lines (32 loc) · 1.29 KB
/
ServeAPIHttpHandler.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
package swingstudentform;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
public class ServeAPIHttpHandler implements HttpHandler {
private ArrayList<Student> student;
ServeAPIHttpHandler (ArrayList<Student> student) {
this.student = student;
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
// TODO Auto-generated method stub
; StringBuilder responseBuilder = new StringBuilder().append("{\n\"Student\": [\n");
Iterator<Student> studentIterator = this.student.iterator();
while(studentIterator.hasNext()) {
Student singleStudent = studentIterator.next();
responseBuilder.append("{\n\"Student Name:\" \"" + singleStudent.getStudentName() + "\",\n" +
"\"Student Year:\" \"" + singleStudent.getStudentYear() + "\",\n" +
"\"Student ID:\" \"" + singleStudent.getStudentID() + "\"\n},\n");
}
responseBuilder.append("]\n}");
String response = responseBuilder.toString();
httpExchange.sendResponseHeaders(200, response.length());
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write(response.getBytes());
outputStream.flush();
outputStream.close();
}
}