Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
9bbe41093c | |||
5103d2b3cf | |||
4499d901b4 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -21,3 +21,4 @@
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
.idea/
|
||||
|
@ -7,6 +7,22 @@
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="junit-4.13.1" level="project" />
|
||||
<orderEntry type="module-library">
|
||||
<library name="JUnit5.4">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.4.2/junit-jupiter-5.4.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.4.2/junit-jupiter-params-5.4.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
|
||||
</module>
|
Binary file not shown.
@ -1,27 +1,65 @@
|
||||
package at.reisinger.server;
|
||||
|
||||
import at.reisinger.server.thread.ClientThread;
|
||||
import at.reisinger.server.msg.MsgHandler;
|
||||
import at.reisinger.server.objects.Request;
|
||||
import at.reisinger.server.objects.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Grundlegende Server logic
|
||||
* Vereint alle anderen Klassen
|
||||
*/
|
||||
public class Main {
|
||||
static final int port = 80;
|
||||
private Socket socket;
|
||||
private int id;
|
||||
private MsgHandler msgHandler;
|
||||
|
||||
/**
|
||||
* Initial Start
|
||||
* @param args Nicht Verwendet
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Starte Server auf Port 80");
|
||||
new Main(port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Öffnet den Server Socket und akzepiert diesen
|
||||
* @param port Port auf dem der Server läuft
|
||||
*/
|
||||
public Main(int port){
|
||||
ServerSocket serverSocket = null;
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
|
||||
int id = 0;
|
||||
this.msgHandler = new MsgHandler();
|
||||
this.id = 0;
|
||||
while (true){
|
||||
Socket socket = serverSocket.accept();
|
||||
new ClientThread(id, socket).start();
|
||||
this.socket = serverSocket.accept();
|
||||
requestResponding();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Jeder Request durchläuft diese Funktion, reagiert auf requests
|
||||
*/
|
||||
public void requestResponding(){
|
||||
try{
|
||||
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
||||
Request rq = new Request(this.socket, this.id);
|
||||
Response rp = new Response(this.id, rq.getUrl(), rq.getCmd(), rq.getOut(), this.msgHandler, rq.getPayload());
|
||||
this.msgHandler = rp.getMsghandler();
|
||||
this.socket.close();
|
||||
System.out.println("Socket von Client #" + this.id + " wurde geschlossen!");
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
89
src/at/reisinger/server/msg/MsgHandler.java
Normal file
89
src/at/reisinger/server/msg/MsgHandler.java
Normal file
@ -0,0 +1,89 @@
|
||||
package at.reisinger.server.msg;
|
||||
|
||||
import at.reisinger.server.objects.Msg;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Der Speicher der Nachrichten
|
||||
* Mit hilfe einer HashMap werden die MsgId und die Msg selbst gespeichert
|
||||
* @author Georg Reisinger
|
||||
*/
|
||||
public class MsgHandler {
|
||||
private HashMap<Integer , String> msgHashMap;
|
||||
private int lastID;
|
||||
|
||||
/**
|
||||
* Erstellt den MsgHandler mit standart Test Messages
|
||||
*/
|
||||
public MsgHandler(){
|
||||
msgHashMap = new HashMap<Integer, String>();
|
||||
addMsg("Hallo");
|
||||
addMsg("Wie");
|
||||
addMsg("Geht");
|
||||
addMsg("Es");
|
||||
addMsg("Dir?");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermitelt die nächste freie Id
|
||||
* @return Next ID
|
||||
*/
|
||||
private int nextId(){
|
||||
return this.lastID + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Msg hinzufügen
|
||||
* @param msg Message Text
|
||||
*/
|
||||
public int addMsg(String msg){
|
||||
int id = nextId();
|
||||
msgHashMap.put(id, msg);
|
||||
this.lastID = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Msg löschen
|
||||
* @param id Message Id
|
||||
*/
|
||||
public String delMsg(int id){
|
||||
return msgHashMap.remove(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Msg bearbeiten
|
||||
* @param id Message Id
|
||||
* @param msg Message Text
|
||||
*/
|
||||
public String editMsg(int id, String msg){
|
||||
return msgHashMap.replace(id, msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Msg als Objekt holen
|
||||
* @param id Message Id
|
||||
* @return Message als Msg Objekt
|
||||
*/
|
||||
public Msg getMsg(int id){
|
||||
return new Msg(id, msgHashMap.get(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Alle Nachrichten werden in den Format Key, Value besorgt
|
||||
* bsp: key: 1 value: Nachricht
|
||||
* @return Alle nachrichten in einem String
|
||||
*/
|
||||
public String getAllMsg(){
|
||||
String returnStr = "";
|
||||
// Print keys and values
|
||||
for (Integer i : msgHashMap.keySet()) {
|
||||
String item = "key: " + i + " value: " + msgHashMap.get(i) + "\n";
|
||||
returnStr += item;
|
||||
}
|
||||
System.out.println(returnStr);
|
||||
return returnStr;
|
||||
}
|
||||
}
|
37
src/at/reisinger/server/objects/Msg.java
Normal file
37
src/at/reisinger/server/objects/Msg.java
Normal file
@ -0,0 +1,37 @@
|
||||
package at.reisinger.server.objects;
|
||||
|
||||
/**
|
||||
* Message Objekt beinmhaltet die MsgId und die Msg selbst
|
||||
* @author Georg Reisinger
|
||||
*/
|
||||
public class Msg {
|
||||
private int id;
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* Erstellt eine Message
|
||||
*/
|
||||
public Msg(int id, String msg){
|
||||
this.id = id;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get field
|
||||
*
|
||||
* @return id Message Id
|
||||
*/
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* get field
|
||||
*
|
||||
* @return msg Message String
|
||||
*/
|
||||
public String getMsg() {
|
||||
return this.msg;
|
||||
}
|
||||
}
|
110
src/at/reisinger/server/objects/Request.java
Normal file
110
src/at/reisinger/server/objects/Request.java
Normal file
@ -0,0 +1,110 @@
|
||||
package at.reisinger.server.objects;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Verarbeitet einen Request
|
||||
* @author Georg Reisinger
|
||||
*/
|
||||
public class Request {
|
||||
|
||||
private final Socket socket;
|
||||
private final int id;
|
||||
private PrintStream out;
|
||||
private String cmd;
|
||||
private String url;
|
||||
private final StringBuilder rqBuilder;
|
||||
private String payload;
|
||||
|
||||
/**
|
||||
* Get Request
|
||||
* @param socket Socket von dem der Request kommt
|
||||
* @param id Thread ID
|
||||
*/
|
||||
public Request(Socket socket, int id) throws IOException {
|
||||
this.socket = socket;
|
||||
this.rqBuilder = new StringBuilder();
|
||||
this.id = id;
|
||||
this.out = new PrintStream(this.socket.getOutputStream());
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
String line = bufferedReader.readLine();
|
||||
while (!line.isBlank()) {
|
||||
rqBuilder.append(line + "\r\n");
|
||||
line = bufferedReader.readLine();
|
||||
System.out.println(line);
|
||||
}
|
||||
String request = rqBuilder.toString();
|
||||
String[] requestsLines = request.split("\r\n");
|
||||
String[] requestLine = requestsLines[0].split(" ");
|
||||
String method = requestLine[0];
|
||||
String path = requestLine[1];
|
||||
String version = requestLine[2];
|
||||
String host = requestsLines[1].split(" ")[1];
|
||||
|
||||
//code to read the post payload data
|
||||
StringBuilder payload = new StringBuilder();
|
||||
while(bufferedReader.ready()){
|
||||
payload.append((char) bufferedReader.read());
|
||||
}
|
||||
System.out.println("Payload: " + payload.toString());
|
||||
this.payload = payload.toString();
|
||||
|
||||
this.url = path;
|
||||
this.cmd = method;
|
||||
|
||||
List<String> headers = new ArrayList<>();
|
||||
for (int h = 2; h < requestsLines.length; h++) {
|
||||
String header = requestsLines[h];
|
||||
headers.add(header);
|
||||
}
|
||||
|
||||
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
|
||||
socket.toString(), method, path, version, host, headers.toString());
|
||||
System.out.println(accessLog);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get PrintStream --> Output
|
||||
*
|
||||
* @return out PrintStream --> Output
|
||||
*/
|
||||
public PrintStream getOut() {
|
||||
return this.out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Command wie GET, PUT, POST, DEL
|
||||
*
|
||||
* @return cmd als String
|
||||
*/
|
||||
public String getCmd() {
|
||||
return this.cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request url
|
||||
*
|
||||
* @return url als String
|
||||
*/
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Payload des Request
|
||||
*
|
||||
* @return payload als String
|
||||
*/
|
||||
public String getPayload() {
|
||||
return this.payload;
|
||||
}
|
||||
|
||||
}
|
140
src/at/reisinger/server/objects/Response.java
Normal file
140
src/at/reisinger/server/objects/Response.java
Normal file
@ -0,0 +1,140 @@
|
||||
package at.reisinger.server.objects;
|
||||
|
||||
import at.reisinger.server.msg.MsgHandler;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.net.Socket;
|
||||
|
||||
/**
|
||||
* Erstellt und sendet eine Response anhand des Requests
|
||||
* @author Georg Reisinger
|
||||
*/
|
||||
public class Response {
|
||||
|
||||
//private final Socket socket;
|
||||
private final int id;
|
||||
private PrintStream out;
|
||||
private String cmd;
|
||||
private String url;
|
||||
private MsgHandler msgHandler;
|
||||
private StringBuilder rqBuilder;
|
||||
|
||||
/**
|
||||
* Nimmt die Daten des requests und generiert eine Response
|
||||
* @param id Thread Id
|
||||
* @param url Request Url
|
||||
* @param cmd Request CMD
|
||||
* @param out out Print Stream
|
||||
* @param msgHandler Der MsgHandler
|
||||
* @param payload Payload des Requests
|
||||
*/
|
||||
public Response(int id, String url, String cmd, PrintStream out, MsgHandler msgHandler, String payload){
|
||||
this.id = id;
|
||||
this.msgHandler = msgHandler;
|
||||
this.url = url;
|
||||
this.cmd = cmd;
|
||||
this.out = out;
|
||||
this.rqBuilder = new StringBuilder();
|
||||
System.out.println(cmd);
|
||||
if (this.url != null) {
|
||||
if (this.cmd.equals("GET")) {
|
||||
if (this.url.startsWith("/messages")) {
|
||||
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||
System.out.println("Last Bit: " + lastBit);
|
||||
if(lastBit.equals("messages")){
|
||||
listAllMsg();
|
||||
}else{
|
||||
String message = msgHandler.getMsg(Integer.parseInt(lastBit)).getMsg();
|
||||
if(message == null){
|
||||
sendError("404");
|
||||
}else {
|
||||
sendResponse(message, "200");
|
||||
}
|
||||
}
|
||||
} else if (this.url.startsWith("/")) {
|
||||
startseite();
|
||||
}
|
||||
}else if (this.cmd.equals("POST")){
|
||||
if (this.url.startsWith("/messages")) {
|
||||
sendResponse(msgHandler.addMsg(payload) + "", "201");
|
||||
}
|
||||
}else if (this.cmd.equals("PUT")){
|
||||
if (this.url.startsWith("/messages")) {
|
||||
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||
System.out.println("Last Bit: " + lastBit);
|
||||
System.out.println("Payload" + payload);
|
||||
String message = msgHandler.editMsg(Integer.parseInt(lastBit), payload);
|
||||
if(message == null){
|
||||
sendError("404");
|
||||
}else {
|
||||
sendResponse("","200");
|
||||
}
|
||||
}
|
||||
}else if (this.cmd.equals("DELETE")){
|
||||
if (this.url.startsWith("/messages")) {
|
||||
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||
String message = msgHandler.delMsg(Integer.parseInt(lastBit));
|
||||
if(message == null){
|
||||
sendError("404");
|
||||
}else {
|
||||
sendResponse("", "200");
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sendError("405");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sendet einen Error Response
|
||||
* @param errorCode Der Error Code
|
||||
*/
|
||||
private void sendError(String errorCode) {
|
||||
out.print("HTTP/1.0 "+errorCode+"\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/plain\r\n");
|
||||
out.print("Content-Length: 1\r\n");
|
||||
out.print("\r\n");
|
||||
//out.print(responseText);
|
||||
}
|
||||
|
||||
private void startseite() {
|
||||
sendResponse("lists all messages: GET /messages<br>" +
|
||||
"add message: POST /messages (Payload: the message; Response an id like1)<br>" +
|
||||
"show first message: GET /messages/1<br>" +
|
||||
"show third message: GET /messages/3<br>" +
|
||||
"update first message: PUT /messages/1 (Payload: the message)<br>" +
|
||||
"remove first message: DELETE /messages/1<br>", "200");
|
||||
}
|
||||
|
||||
private void listAllMsg() {
|
||||
sendResponse(msgHandler.getAllMsg(), "200");
|
||||
//sendResponse("Test");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sendet eine Response
|
||||
* @param responseText Text der zu senden ist
|
||||
* @param code Http code
|
||||
*/
|
||||
private void sendResponse(String responseText, String code){
|
||||
out.print("HTTP/1.0 "+code+"\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/plain\r\n");
|
||||
out.print("Content-Length: "+responseText.length()+"\r\n");
|
||||
out.print("\r\n");
|
||||
out.print(responseText);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Msg Handler
|
||||
*
|
||||
* @return msgHandler Handler der Nachrichten
|
||||
*/
|
||||
public MsgHandler getMsghandler() {
|
||||
return this.msgHandler;
|
||||
}
|
||||
|
||||
}
|
@ -1,224 +0,0 @@
|
||||
package at.reisinger.server.thread;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientThread extends Thread{
|
||||
private final Socket socket;
|
||||
private final int id;
|
||||
private PrintStream out;
|
||||
private String cmd;
|
||||
private String url;
|
||||
private final StringBuilder rqBuilder;
|
||||
|
||||
/**
|
||||
* Neuer Client wird erstellt
|
||||
* @param id Id des Clients
|
||||
* @param socket Socket des Clients
|
||||
*/
|
||||
public ClientThread(int id, Socket socket) {
|
||||
this.id = id;
|
||||
this.socket = socket;
|
||||
this.rqBuilder = new StringBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Hauptmethode des ClientThreads
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
||||
out = new PrintStream(socket.getOutputStream());
|
||||
InputStream in = socket.getInputStream();
|
||||
getRequest();
|
||||
createResponse();
|
||||
socket.close();
|
||||
System.out.println("Socket von Client #" + this.id + " wurde geschlossen!");
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt alle Nachrichten aus
|
||||
*/
|
||||
private void listAllMsg(){
|
||||
out.print("HTTP/1.0 200 OK\r\n");
|
||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/html\r\n");
|
||||
out.print("Content-Length: 59\r\n");
|
||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
||||
out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
|
||||
out.print("\r\n");
|
||||
out.print("<TITLE>GET /messages</TITLE>");
|
||||
out.print("<P>Listet alle Nachrichten</P>");
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt die startseite aus
|
||||
*/
|
||||
private void startseite(){
|
||||
out.print("HTTP/1.0 200 OK\r\n");
|
||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/html\r\n");
|
||||
out.print("Content-Length: 59\r\n");
|
||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
||||
out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
|
||||
out.print("\r\n");
|
||||
out.print("<TITLE>Startseite</TITLE>");
|
||||
out.print("<p>lists all messages: GET /messages</p>" +
|
||||
"<p>add message: POST /messages (Payload: the message; Response an id like 1)</p>" +
|
||||
"<p>show first message: GET /messages/1</p> " +
|
||||
"<p>show third message: GET /messages/3</p>" +
|
||||
"<p>update first message: PUT /messages/1 (Payload: the message)</p>" +
|
||||
"<p>remove first message: DELETE /messages/1</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Erstellt eine Respone Message anhand des gegebenen Requests
|
||||
*/
|
||||
private void createResponse() {
|
||||
if (this.url != null) {
|
||||
if (this.cmd.equals("GET")) {
|
||||
if (this.url.startsWith("/messages")) {
|
||||
String lastBit = this.url.substring(this.url.lastIndexOf('/') + 1);
|
||||
System.out.println("Last Bit: " + lastBit);
|
||||
if(lastBit.equals("messages")){
|
||||
listAllMsg();
|
||||
}else{
|
||||
getMsg(Integer.parseInt(lastBit));
|
||||
}
|
||||
} else if (this.url.startsWith("/")) {
|
||||
startseite();
|
||||
}
|
||||
}else if (this.cmd.equals("POST")){
|
||||
if (this.url.startsWith("/messages")) {
|
||||
addMsg(id);
|
||||
}
|
||||
}else if (this.cmd.equals("PUT")){
|
||||
if (this.url.startsWith("/messages")) {
|
||||
editMsg(id);
|
||||
}
|
||||
}else if (this.cmd.equals("DELETE")){
|
||||
if (this.url.startsWith("/messages")) {
|
||||
delMsg(id);
|
||||
}
|
||||
}else{
|
||||
sendError();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt eine Nachricht
|
||||
* @param id ID der nachricht die geholt werden soll
|
||||
*/
|
||||
private void getMsg(int id) {
|
||||
out.print("HTTP/1.0 200 OK\r\n");
|
||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/html\r\n");
|
||||
out.print("Content-Length: 59\r\n");
|
||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
||||
out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
|
||||
out.print("\r\n");
|
||||
out.print("<TITLE>Get MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* sendet eine Fehlermeldung zurück
|
||||
*/
|
||||
private void sendError() {
|
||||
out.print("HTTP/1.0 500 ERR\r\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht eine Nachricht
|
||||
* @param id Nachricht die zu löschen ist
|
||||
*/
|
||||
private void delMsg(int id) {
|
||||
out.print("HTTP/1.0 200 OK\r\n");
|
||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/html\r\n");
|
||||
out.print("Content-Length: 59\r\n");
|
||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
||||
out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
|
||||
out.print("\r\n");
|
||||
out.print("<TITLE>Del MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Bearbeitet eine Nachricht
|
||||
* @param id Nachricht die zu bearbeiten ist
|
||||
*/
|
||||
private void editMsg(int id) {
|
||||
out.print("HTTP/1.0 200 OK\r\n");
|
||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/html\r\n");
|
||||
out.print("Content-Length: 59\r\n");
|
||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
||||
out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
|
||||
out.print("\r\n");
|
||||
out.print("<TITLE>Edit MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt eine Nachricht hinzu
|
||||
* @param id Id der neuen Nachricht
|
||||
*/
|
||||
private void addMsg(int id) {
|
||||
out.print("HTTP/1.0 200 OK\r\n");
|
||||
out.print("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
|
||||
out.print("Server: Apache/0.8.4\r\n");
|
||||
out.print("Content-Type: text/html\r\n");
|
||||
out.print("Content-Length: 59\r\n");
|
||||
out.print("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
|
||||
out.print("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
|
||||
out.print("\r\n");
|
||||
out.print("<TITLE>Add MSG</TITLE>");
|
||||
out.print("<p> Aktuelle MSG Number: " + id + "</p>");
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt den HTTP Request
|
||||
*/
|
||||
private void getRequest() throws IOException {
|
||||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
String line;
|
||||
while (!(line = bufferedReader.readLine()).isBlank()) {
|
||||
rqBuilder.append(line + "\r\n");
|
||||
}
|
||||
String request = rqBuilder.toString();
|
||||
String[] requestsLines = request.split("\r\n");
|
||||
String[] requestLine = requestsLines[0].split(" ");
|
||||
String method = requestLine[0];
|
||||
String path = requestLine[1];
|
||||
String version = requestLine[2];
|
||||
String host = requestsLines[1].split(" ")[1];
|
||||
|
||||
this.url = path;
|
||||
this.cmd = method;
|
||||
|
||||
List<String> headers = new ArrayList<>();
|
||||
for (int h = 2; h < requestsLines.length; h++) {
|
||||
String header = requestsLines[h];
|
||||
headers.add(header);
|
||||
}
|
||||
|
||||
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
|
||||
socket.toString(), method, path, version, host, headers.toString());
|
||||
System.out.println(accessLog);
|
||||
|
||||
}
|
||||
}
|
57
src/test/MyTests.java
Normal file
57
src/test/MyTests.java
Normal file
@ -0,0 +1,57 @@
|
||||
package test;
|
||||
|
||||
import at.reisinger.server.msg.MsgHandler;
|
||||
import at.reisinger.server.objects.Msg;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class MyTests{
|
||||
@Test
|
||||
@DisplayName("Msg - GetMsg")
|
||||
public void msgTest(){
|
||||
Msg msg = new Msg(1, "Nachricht");
|
||||
assertTrue(msg.getMsg().equals("Nachricht"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Msg - getId")
|
||||
public void msgIdTest(){
|
||||
Msg msg = new Msg(1, "Nachricht");
|
||||
assertTrue(msg.getId() == 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("MsgHandler - GetMsg")
|
||||
public void msgHandlerGetTest(){
|
||||
MsgHandler msgHandler = new MsgHandler();
|
||||
assertTrue(msgHandler.getMsg(5).getMsg().equals("Dir?"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("MsgHandler - AddMsg")
|
||||
public void msgHandlerAddTest(){
|
||||
MsgHandler msgHandler = new MsgHandler();
|
||||
msgHandler.addMsg("Nachricht"); //id = 6
|
||||
assertTrue(msgHandler.getMsg(6).getMsg().equals("Nachricht"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("MsgHandler - editMsg")
|
||||
public void msgHandlerEditTest(){
|
||||
MsgHandler msgHandler = new MsgHandler();
|
||||
msgHandler.addMsg("Nachricht"); //id = 6
|
||||
msgHandler.editMsg(6, "Neu");
|
||||
assertTrue(msgHandler.getMsg(6).getMsg().equals("Neu"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("MsgHandler - DelMsg")
|
||||
public void msgHandlerDelTest(){
|
||||
MsgHandler msgHandler = new MsgHandler();
|
||||
msgHandler.addMsg("Nachricht"); //id = 6
|
||||
msgHandler.delMsg(6);
|
||||
assertTrue(msgHandler.getMsg(6).getMsg() == null);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user