Added files

This commit is contained in:
Norbi Peti 2016-07-18 13:53:12 +02:00
parent d6b631db1c
commit 11ee73657e
6 changed files with 159 additions and 0 deletions

36
pom.xml Normal file
View file

@ -0,0 +1,36 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ChatServer</groupId>
<artifactId>ChatServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.12.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.12.1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

View file

@ -0,0 +1,14 @@
package io.github.norbipeti.chat.server;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import com.sun.net.httpserver.HttpExchange;
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());
exchange.getResponseBody().close();
}
}

View file

@ -0,0 +1,14 @@
package io.github.norbipeti.chat.server;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
public class Index extends Page {
@Override
public void handle(HttpExchange exchange) throws IOException {
}
}

View file

@ -0,0 +1,71 @@
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;
public class Main {
// public static final HashMap<String, Page> Pages = new HashMap<String,
// Page>();
public static void main(String[] args) { // http://stackoverflow.com/questions/9266632/access-restriction-is-not-accessible-due-to-restriction-on-required-library/10642163#10642163
try { // rt.jar Javadoc:
// https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/
// https://docs.oracle.com/javase/8/docs/api/
System.out.println("Loading database...");
Connection conn = DriverManager.getConnection("jdbc:derby:memory:chatserver;create=true");
Statement statement = conn.createStatement();
if (statement.execute("CREATE TABLE users ( username varchar(255), password varchar(255), id int )"))
System.out.println("Created users table");
else
System.out.println("Failed to create users table!");
ResultSet results = statement.executeQuery("SELECT * FROM users");
while (results.next())
results.getString(0);
System.out.println("Starting webserver...");
HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10);
server.createContext("/").setHandler(new HttpHandler() {
@Override
public void handle(HttpExchange exchange) {
try {
System.out.println("Received request from " + exchange.getRemoteAddress());
System.out.println("Body: " + IOUtils.toString(exchange.getRequestBody(), "ASCII"));
System.out.println("Headers: ");
for (Entry<String, List<String>> entry : exchange.getRequestHeaders().entrySet())
System.out.println(entry.getKey() + " - " + entry.getValue());
System.out.println(exchange.getRequestURI().getPath());
/*
* String resp =
* Pages.get(exchange.getRequestURI().getPath()).Run(
* exchange); exchange.sendResponseHeaders(200,
* resp.length()); IOUtils.write(resp,
* exchange.getResponseBody());
* exchange.getResponseBody().close();
*/
exchange.sendResponseHeaders(404, 0);
exchange.getResponseBody().close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
server.createContext("/register").setHandler(new RegisterPage());
server.start();
System.out.println("Ready... Press Enter to stop.");
System.in.read();
System.out.println("Stopping...");
server.stop(1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Stopped");
}
}

View file

@ -0,0 +1,10 @@
package io.github.norbipeti.chat.server;
import com.sun.net.httpserver.*;
/**
* Add to {@link Main}.Pages
*
*/
public abstract class Page implements HttpHandler {
}

View file

@ -0,0 +1,14 @@
package io.github.norbipeti.chat.server;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
public class RegisterPage extends Page {
@Override
public void handle(HttpExchange exchange) throws IOException {
//exchange.getRequestURI().getPath()
IOHelper.SendResponse(200, "<h1>Register</h1>", exchange);
}
}