Added methods and a not found page
This commit is contained in:
parent
bcbf53c803
commit
e48c318e42
7 changed files with 114 additions and 18 deletions
|
@ -1,14 +1,33 @@
|
|||
package io.github.norbipeti.chat.server;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import io.github.norbipeti.chat.server.page.Page;
|
||||
|
||||
public class IOHelper {
|
||||
public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
|
||||
exchange.sendResponseHeaders(code, content.length());
|
||||
IOUtils.write(content, exchange.getResponseBody());
|
||||
IOUtils.write(content, exchange.getResponseBody(), StandardCharsets.UTF_8);
|
||||
exchange.getResponseBody().close();
|
||||
}
|
||||
|
||||
public static boolean SendPage(int code, Page page, HttpExchange exchange) throws IOException {
|
||||
File file = new File(page.GetHTMLPath());
|
||||
if (!file.exists()) {
|
||||
SendResponse(501,
|
||||
"<h1>501 Not Implemented</h1><p>The page \"" + page.GetName() + "\" cannot be found on disk.</h1>",
|
||||
exchange);
|
||||
return false;
|
||||
}
|
||||
FileInputStream inputStream = new FileInputStream(file);
|
||||
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
|
||||
SendResponse(code, content, exchange);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,18 +2,11 @@ package io.github.norbipeti.chat.server;
|
|||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.sql.*;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import io.github.norbipeti.chat.server.db.DataProvider;
|
||||
import io.github.norbipeti.chat.server.page.IndexPage;
|
||||
import io.github.norbipeti.chat.server.page.RegisterPage;
|
||||
import io.github.norbipeti.chat.server.page.*;
|
||||
|
||||
public class Main {
|
||||
// public static final HashMap<String, Page> Pages = new HashMap<String,
|
||||
|
@ -25,12 +18,13 @@ public class Main {
|
|||
// https://docs.oracle.com/javase/8/docs/api/
|
||||
System.out.println("Loading database...");
|
||||
try (DataProvider provider = new DataProvider()) {
|
||||
|
||||
|
||||
}
|
||||
System.out.println("Starting webserver...");
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10);
|
||||
server.createContext("/").setHandler(new IndexPage());
|
||||
server.createContext("/register").setHandler(new RegisterPage());
|
||||
addPage(server, new IndexPage());
|
||||
addPage(server, new RegisterPage());
|
||||
addPage(server, new LoginPage());
|
||||
server.start();
|
||||
System.out.println("Ready... Press Enter to stop.");
|
||||
System.in.read();
|
||||
|
@ -41,4 +35,8 @@ public class Main {
|
|||
}
|
||||
System.out.println("Stopped");
|
||||
}
|
||||
|
||||
private static void addPage(HttpServer server, Page page) {
|
||||
server.createContext("/" + page.GetName(), page);
|
||||
}
|
||||
}
|
|
@ -9,8 +9,13 @@ import io.github.norbipeti.chat.server.IOHelper;
|
|||
public class IndexPage extends Page {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
IOHelper.SendResponse(200, "<h1>Index</h1>", exchange);
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
IOHelper.SendPage(200, this, exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetName() {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
package io.github.norbipeti.chat.server.page;
|
||||
|
||||
public class LoginPage {
|
||||
import java.io.IOException;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import io.github.norbipeti.chat.server.IOHelper;
|
||||
|
||||
public class LoginPage extends Page {
|
||||
|
||||
@Override
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
IOHelper.SendPage(200, this, exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetName() {
|
||||
return "login";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
28
src/io/github/norbipeti/chat/server/page/NotFoundPage.java
Normal file
28
src/io/github/norbipeti/chat/server/page/NotFoundPage.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package io.github.norbipeti.chat.server.page;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
|
||||
import io.github.norbipeti.chat.server.IOHelper;
|
||||
|
||||
public class NotFoundPage extends Page {
|
||||
|
||||
@Override
|
||||
public String GetName() {
|
||||
return "notfound";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
IOHelper.SendPage(404, this, exchange);
|
||||
}
|
||||
|
||||
public NotFoundPage() {
|
||||
if (Instance != null)
|
||||
throw new UnsupportedOperationException("There can only be one instance of a page.");
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public static NotFoundPage Instance;
|
||||
}
|
|
@ -1,7 +1,10 @@
|
|||
package io.github.norbipeti.chat.server.page;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.sun.net.httpserver.*;
|
||||
|
||||
import io.github.norbipeti.chat.server.IOHelper;
|
||||
import io.github.norbipeti.chat.server.Main;
|
||||
|
||||
/**
|
||||
|
@ -9,4 +12,27 @@ import io.github.norbipeti.chat.server.Main;
|
|||
*
|
||||
*/
|
||||
public abstract class Page implements HttpHandler {
|
||||
public abstract String GetName();
|
||||
|
||||
public final String GetHTMLPath() {
|
||||
if (GetName().length() == 0)
|
||||
return "pages/index.html";
|
||||
return new StringBuilder("pages/").append(GetName().length() == 0).append(".html").toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
if (exchange.getRequestURI().getPath().equals("/" + GetName()))
|
||||
handlePage(exchange);
|
||||
else {
|
||||
if (!IOHelper.SendPage(404, NotFoundPage.Instance, exchange))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void handlePage(HttpExchange exchange) throws IOException;
|
||||
|
||||
public boolean getDo404() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,13 @@ import io.github.norbipeti.chat.server.IOHelper;
|
|||
|
||||
public class RegisterPage extends Page {
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
//exchange.getRequestURI().getPath()
|
||||
IOHelper.SendResponse(200, "<h1>Register</h1>", exchange);
|
||||
public void handlePage(HttpExchange exchange) throws IOException {
|
||||
IOHelper.SendPage(200, this, exchange);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String GetName() {
|
||||
return "register";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue