Started register, added jQuery, ...

This commit is contained in:
Norbi Peti 2016-07-20 15:02:49 +02:00
parent 127a37b097
commit ef70448ca6
12 changed files with 10215 additions and 11 deletions

7
.buildpath Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="src" path="src"/>
<buildpathentry kind="con" path="org.eclipse.dltk.mod.launching.INTERPRETER_CONTAINER"/>
<buildpathentry kind="con" path="org.eclipse.vjet.eclipse.core.JSNATIVE_CONTAINER/JS Native Types"/>
<buildpathentry kind="con" path="org.eclipse.vjet.eclipse.core.BROWSER_CONTAINER/Browser SDK"/>
</buildpath>

View file

@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.vjet.eclipse.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
@ -17,6 +22,7 @@
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.vjet.core.nature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

View file

@ -1,5 +1,11 @@
<!DOCTYPE html>
<head>
<title>ChatServer</title>
<script src="js/jquery-3.1.0.js"></script>
<script src="js/message.js"></script>
</head>
<html>
<h1>Index</h1>
<p>Hello</p>
<input id="msginput" value="" />
</html>

10074
pages/js/jquery-3.1.0.js vendored Normal file

File diff suppressed because it is too large Load diff

4
pages/js/message.js Normal file
View file

@ -0,0 +1,4 @@
$(document).on("keypress", function(e) {
if (e.which == '\r'.charCodeAt(0))
document.write(document.getElementById("msginput").value);
});

View file

@ -2,6 +2,7 @@ package io.github.norbipeti.chat.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
@ -33,18 +34,35 @@ public class IOHelper {
exchange);
return null;
}
return ReadFile(file);
}
public static String ReadFile(File file) throws FileNotFoundException, IOException {
FileInputStream inputStream = new FileInputStream(file);
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
return content;
}
public static String GetPOST(HttpExchange exchange) throws IOException {
public static HashMap<String, String> GetPOST(HttpExchange exchange) throws IOException {
System.out.println(exchange.getRequestBody().available());
if (exchange.getRequestBody().available() == 0)
return new HashMap<>();
String[] content = IOUtils.toString(exchange.getRequestBody(), StandardCharsets.ISO_8859_1).split("\\&");
System.out.println(content);
HashMap<String, String> vars = new HashMap<>();
for (String var : content) {
String[] spl = var.split("\\=");
vars.put(spl[0], spl[1]);
}
return null;
return vars;
}
public static boolean SendModifiedPage(int code, Page page, String replace, String with, HttpExchange exchange)
throws IOException {
String content = GetPage(page, exchange);
if (content == null)
return false;
SendResponse(200, content.replace(replace, with), exchange);
return true;
}
}

View file

@ -27,13 +27,16 @@ public class Main {
System.out.println("Loading database...");
try (DataProvider provider = new DataProvider()) {
User user = new User();
user.setName("asd");
provider.addUser(user);
User user2 = new User();
user2.setName("Teszt");
user2.getContacts().add(user);
user.getContacts().add(user2);
provider.addUser(user2);
System.out.println(provider.getUsers());
System.out.println("Contact: " + user2.getContacts().get(0));
System.out.println("1st's contact: " + user.getContacts().get(0));
System.out.println("2nd's contact: " + user2.getContacts().get(0));
}
System.out.println("Starting webserver...");
HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10);

View file

@ -42,8 +42,11 @@ public class User {
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + ", contacts="
+ contacts + "]";
List<String> c = new ArrayList<>();
for (User u : contacts)
c.add(u.name);
return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + ", contacts=" + c
+ "]";
}
public void setEmail(String email) {

View file

@ -0,0 +1,20 @@
package io.github.norbipeti.chat.server.page;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
public class MessageAjaxPage extends Page {
@Override
public String GetName() {
return "message";
}
@Override
public void handlePage(HttpExchange exchange) throws IOException {
// TODO Auto-generated method stub
}
}

View file

@ -22,10 +22,14 @@ public abstract class Page implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
if (!getDo404() || exchange.getRequestURI().getPath().equals("/" + GetName()))
handlePage(exchange);
else {
IOHelper.SendPage(404, NotFoundPage.Instance, exchange);
try {
if (!getDo404() || exchange.getRequestURI().getPath().equals("/" + GetName()))
handlePage(exchange);
else {
IOHelper.SendPage(404, NotFoundPage.Instance, exchange);
}
} catch (IOException e) {
e.printStackTrace();
}
}

View file

@ -1,6 +1,7 @@
package io.github.norbipeti.chat.server.page;
import java.io.IOException;
import java.util.HashMap;
import com.sun.net.httpserver.HttpExchange;
import io.github.norbipeti.chat.server.IOHelper;
@ -8,11 +9,35 @@ import io.github.norbipeti.chat.server.IOHelper;
public class RegisterPage extends Page {
@Override
public void handlePage(HttpExchange exchange) throws IOException {
/*for(String line : IOHelper.GetPOST(exchange))
System.out.println(line);*/
HashMap<String, String> post = IOHelper.GetPOST(exchange);
System.out.println("POST: " + post);
if (post.size() > 0) {
String errormsg = CheckValues(post, "name", "email", "pass", "pass2");
System.out.println(errormsg);
if (errormsg.length() == 0) {
// Process register
String successmsg = "";
IOHelper.SendModifiedPage(200, this, "<successmsg />", successmsg, exchange);
return;
} else
IOHelper.SendModifiedPage(200, this, "<errormsg />", errormsg, exchange);
return;
}
IOHelper.SendPage(200, this, exchange);
}
private String CheckValues(HashMap<String, String> post, String... values) {
String errormsg = "";
for (String value : values)
if (!CheckValue(post.get(value)))
errormsg += "<p>" + value + " can't be empty</p>";
return errormsg;
}
private boolean CheckValue(String val) {
return val != null && val.length() > 0;
}
@Override
public String GetName() {
return "register";

View file

@ -0,0 +1,34 @@
package io.github.norbipeti.chat.server.page;
import java.io.File;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
import io.github.norbipeti.chat.server.IOHelper;
public class ScriptsPage extends Page {
@Override
public boolean getDo404() {
return false;
}
@Override
public String GetName() {
return "js/";
}
@Override
public void handlePage(HttpExchange exchange) throws IOException {
if (exchange.getRequestURI().getPath().startsWith("/js/")) {
File jsfile = new File("pages", exchange.getRequestURI().getPath());
if (!jsfile.exists())
IOHelper.SendResponse(404, "<h1>JavaScript file not found</h1>", exchange);
else
IOHelper.SendResponse(200, IOHelper.ReadFile(jsfile), exchange);
}
else
System.out.println(exchange.getRequestURI().getPath());
}
}