diff --git a/src/at/reisinger/server/thread/ClientThread.java b/src/at/reisinger/server/thread/ClientThread.java index 290e0aa..2e23416 100644 --- a/src/at/reisinger/server/thread/ClientThread.java +++ b/src/at/reisinger/server/thread/ClientThread.java @@ -43,52 +43,153 @@ public class ClientThread extends Thread{ } + /** + * 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("
Listet alle Nachrichten
"); + } + + /** + * 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("lists all messages: GET /messages
" + + "add message: POST /messages (Payload: the message; Response an id like 1)
" + + "show first message: GET /messages/1
" + + "show third message: GET /messages/3
" + + "update first message: PUT /messages/1 (Payload: the message)
" + + "remove first message: DELETE /messages/1
"); + } + /** * Erstellt eine Respone Message anhand des gegebenen Requests */ private void createResponse() { if (this.url != null) { if (this.cmd.equals("GET")) { - if (this.url.startsWith("/test")) { - 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("Das ist ein Test
"); + 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("/")) { - 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("Hallo Welt!
"); + 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{ - /* - - ERROR senden - - - */ + 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("Aktuelle MSG Number: " + id + "
"); + } + + /** + * 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("Aktuelle MSG Number: " + id + "
"); + } + + /** + * 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("Aktuelle MSG Number: " + id + "
"); + } + + /** + * 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("Aktuelle MSG Number: " + id + "
"); + } + /** * Holt den HTTP Request */