-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package swingstudentform; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.util.ArrayList; | ||
import java.util.logging.Logger; | ||
|
||
import com.sun.net.httpserver.HttpServer; | ||
|
||
public class ServeAPI { | ||
private ArrayList<Student> student; | ||
ServeAPI (ArrayList<Student> student){ | ||
this.student = student; | ||
try { | ||
Logger logger = Logger.getLogger(ServeAPI.class.getName()); | ||
|
||
HttpServer studentHttpsApiServer = HttpServer.create(new InetSocketAddress("localhost", 8001),0); | ||
studentHttpsApiServer.createContext("/", new ServeAPIHttpHandler(this.student)); | ||
|
||
studentHttpsApiServer.start(); | ||
logger.info("Server started on port 8001"); | ||
} catch (IOException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters