Skip to content

Commit

Permalink
Serve data to http server
Browse files Browse the repository at this point in the history
  • Loading branch information
ffrmns committed Nov 29, 2021
1 parent 9c20396 commit 5fa0b58
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/swingstudentform/ServeAPI.java
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();
}
}
}
36 changes: 36 additions & 0 deletions src/swingstudentform/ServeAPIHttpHandler.java
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();
}

}
10 changes: 10 additions & 0 deletions src/swingstudentform/StudentForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class StudentForm {
private JButton trendButton;
private JButton connectButton;
private ActionListener search;
private ActionListener serve;
private final static String databaseURL = "jdbc:oracle:thin:@172.17.0.2:1521/ORCLPDB1";
private final static String usernameDB = "ffa";
private final static String passwordDB = "passwordffa";
Expand Down Expand Up @@ -409,6 +410,14 @@ public void actionPerformed(ActionEvent e) {

}
};
serve = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new ServeAPI(studentArray);
}
};
}

/**
Expand All @@ -434,6 +443,7 @@ private void assignActionToButtonPress () {
storeButton.addActionListener(toDatabase);
retrieveButton.addActionListener(fromDatabase);
searchButton.addActionListener(search);
serveButton.addActionListener(serve);
}

/**
Expand Down

0 comments on commit 5fa0b58

Please sign in to comment.