Add, Get added
This commit is contained in:
parent
0d7a7152c4
commit
4499d901b4
@ -1,10 +1,12 @@
|
|||||||
package at.reisinger.server;
|
package at.reisinger.server;
|
||||||
|
|
||||||
|
import at.reisinger.server.msg.MsgHandler;
|
||||||
import at.reisinger.server.thread.ClientThread;
|
import at.reisinger.server.thread.ClientThread;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static final int port = 80;
|
static final int port = 80;
|
||||||
@ -18,7 +20,13 @@ public class Main {
|
|||||||
int id = 0;
|
int id = 0;
|
||||||
while (true){
|
while (true){
|
||||||
Socket socket = serverSocket.accept();
|
Socket socket = serverSocket.accept();
|
||||||
new ClientThread(id, socket).start();
|
Thread client = new ClientThread(id, socket);
|
||||||
|
client.start();
|
||||||
|
try {
|
||||||
|
client.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
84
src/at/reisinger/server/msg/MsgHandler.java
Normal file
84
src/at/reisinger/server/msg/MsgHandler.java
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package at.reisinger.server.msg;
|
||||||
|
|
||||||
|
import at.reisinger.server.objects.Msg;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
|
public 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 void delMsg(int id){
|
||||||
|
msgHashMap.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Msg bearbeiten
|
||||||
|
* @param id Message Id
|
||||||
|
* @param msg Message Text
|
||||||
|
*/
|
||||||
|
public void editMsg(int id, String msg){
|
||||||
|
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);
|
||||||
|
returnStr += item;
|
||||||
|
}
|
||||||
|
System.out.println(returnStr);
|
||||||
|
return returnStr;
|
||||||
|
}
|
||||||
|
}
|
54
src/at/reisinger/server/objects/Msg.java
Normal file
54
src/at/reisinger/server/objects/Msg.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package at.reisinger.server.objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message Objekt
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param id Message Id
|
||||||
|
*/
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return msg Message String
|
||||||
|
*/
|
||||||
|
public String getMsg() {
|
||||||
|
return this.msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param msg Message String
|
||||||
|
*/
|
||||||
|
public void setMsg(String msg) {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
}
|
139
src/at/reisinger/server/objects/Request.java
Normal file
139
src/at/reisinger/server/objects/Request.java
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
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];
|
||||||
|
|
||||||
|
//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 field
|
||||||
|
*
|
||||||
|
* @return out
|
||||||
|
*/
|
||||||
|
public PrintStream getOut() {
|
||||||
|
return this.out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param out
|
||||||
|
*/
|
||||||
|
public void setOut(PrintStream out) {
|
||||||
|
this.out = out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return cmd
|
||||||
|
*/
|
||||||
|
public String getCmd() {
|
||||||
|
return this.cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param cmd
|
||||||
|
*/
|
||||||
|
public void setCmd(String cmd) {
|
||||||
|
this.cmd = cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return url
|
||||||
|
*/
|
||||||
|
public String getUrl() {
|
||||||
|
return this.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
*/
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return payload
|
||||||
|
*/
|
||||||
|
public String getPayload() {
|
||||||
|
return this.payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param payload
|
||||||
|
*/
|
||||||
|
public void setPayload(String payload) {
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
}
|
169
src/at/reisinger/server/objects/Response.java
Normal file
169
src/at/reisinger/server/objects/Response.java
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
package at.reisinger.server.objects;
|
||||||
|
|
||||||
|
import at.reisinger.server.msg.MsgHandler;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class Response {
|
||||||
|
|
||||||
|
//private final Socket socket;
|
||||||
|
private final int id;
|
||||||
|
private PrintStream out;
|
||||||
|
private String cmd;
|
||||||
|
private String url;
|
||||||
|
private final StringBuilder rqBuilder;
|
||||||
|
private MsgHandler msgHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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{
|
||||||
|
sendResponse(msgHandler.getMsg(Integer.parseInt(lastBit)).getMsg());
|
||||||
|
}
|
||||||
|
} else if (this.url.startsWith("/")) {
|
||||||
|
startseite();
|
||||||
|
}
|
||||||
|
}else if (this.cmd.equals("POST")){
|
||||||
|
if (this.url.startsWith("/messages")) {
|
||||||
|
sendResponse(msgHandler.addMsg(payload) + "");
|
||||||
|
}
|
||||||
|
}else if (this.cmd.equals("PUT")){
|
||||||
|
if (this.url.startsWith("/messages")) {
|
||||||
|
msgHandler.editMsg(id, "Edit");
|
||||||
|
}
|
||||||
|
}else if (this.cmd.equals("DELETE")){
|
||||||
|
if (this.url.startsWith("/messages")) {
|
||||||
|
msgHandler.delMsg(id);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
sendResponse(sendError());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private String sendError() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
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>");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void listAllMsg() {
|
||||||
|
sendResponse(msgHandler.getAllMsg());
|
||||||
|
//sendResponse("Test");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendResponse(String responseText){
|
||||||
|
out.print("HTTP/1.0 200 OK\r\n");
|
||||||
|
out.print("Date: Fri, 31 Dec 2020 23:59:59 GMT\r\n");
|
||||||
|
out.print("Server: Apache/0.8.4\r\n");
|
||||||
|
out.print("Content-Type: text/plain\r\n");
|
||||||
|
out.print("Content-Length: 59\r\n");
|
||||||
|
out.print("Expires: Sat, 01 Jan 2025 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(responseText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return out
|
||||||
|
*/
|
||||||
|
public PrintStream getOut() {
|
||||||
|
return this.out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param out
|
||||||
|
*/
|
||||||
|
public void setOut(PrintStream out) {
|
||||||
|
this.out = out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return cmd
|
||||||
|
*/
|
||||||
|
public String getCmd() {
|
||||||
|
return this.cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param cmd
|
||||||
|
*/
|
||||||
|
public void setCmd(String cmd) {
|
||||||
|
this.cmd = cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return url
|
||||||
|
*/
|
||||||
|
public String getUrl() {
|
||||||
|
return this.url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
*/
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get field
|
||||||
|
*
|
||||||
|
* @return msgHandler
|
||||||
|
*/
|
||||||
|
public MsgHandler getMsghandler() {
|
||||||
|
return this.msgHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field
|
||||||
|
*
|
||||||
|
* @param msgHandler
|
||||||
|
*/
|
||||||
|
public void setMsghandler(MsgHandler msgHandler) {
|
||||||
|
this.msgHandler = msgHandler;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,9 @@
|
|||||||
package at.reisinger.server.thread;
|
package at.reisinger.server.thread;
|
||||||
|
|
||||||
|
import at.reisinger.server.msg.MsgHandler;
|
||||||
|
import at.reisinger.server.objects.Request;
|
||||||
|
import at.reisinger.server.objects.Response;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -8,10 +12,7 @@ import java.util.List;
|
|||||||
public class ClientThread extends Thread{
|
public class ClientThread extends Thread{
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
private final int id;
|
private final int id;
|
||||||
private PrintStream out;
|
private MsgHandler msgHandler;
|
||||||
private String cmd;
|
|
||||||
private String url;
|
|
||||||
private final StringBuilder rqBuilder;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Neuer Client wird erstellt
|
* Neuer Client wird erstellt
|
||||||
@ -21,7 +22,6 @@ public class ClientThread extends Thread{
|
|||||||
public ClientThread(int id, Socket socket) {
|
public ClientThread(int id, Socket socket) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.rqBuilder = new StringBuilder();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,11 +31,10 @@ public class ClientThread extends Thread{
|
|||||||
public void run() {
|
public void run() {
|
||||||
try{
|
try{
|
||||||
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
System.out.println("Socket von Client #" + this.id + " wurde gestartet!");
|
||||||
out = new PrintStream(socket.getOutputStream());
|
Request rq = new Request(this.socket, this.id);
|
||||||
InputStream in = socket.getInputStream();
|
Response rp = new Response(this.id, rq.getUrl(), rq.getCmd(), rq.getOut(), this.msgHandler, rq.getPayload());
|
||||||
getRequest();
|
this.msgHandler = rp.getMsghandler();
|
||||||
createResponse();
|
this.socket.close();
|
||||||
socket.close();
|
|
||||||
System.out.println("Socket von Client #" + this.id + " wurde geschlossen!");
|
System.out.println("Socket von Client #" + this.id + " wurde geschlossen!");
|
||||||
}catch (IOException e){
|
}catch (IOException e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -43,182 +42,5 @@ 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("<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);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user