Created
February 7, 2010 18:32
-
-
Save kmichel/297584 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
import com.sun.net.httpserver.*; | |
public class Reddit implements Serializable { | |
public String title; | |
public URL url; | |
public int rating; | |
public int id; | |
public static String htmlentities(String s) { | |
return s.replace("&", "&").replace("'", "'") | |
.replace("\"", """).replace("<", "<").replace(">", ">"); | |
} | |
public static void printall(Formatter out, ArrayList<Reddit> reddits, Comparator<Reddit> cmp) { | |
Collections.sort(reddits, cmp); | |
for (Reddit reddit: reddits) { | |
out.format("<li>%1$s <form style='display:inline' action='/up/%2$s' method='post'><button>↑</button></form><form style='display:inline' action='/down/%2$s' method='post'><button>↓</button></form> <a href='%3$s'>%4$s</a></li>", | |
reddit.rating, reddit.id, | |
Reddit.htmlentities(reddit.url.toString()) | |
, Reddit.htmlentities(reddit.title)); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
final HttpServer server = HttpServer.create(new InetSocketAddress(8080), 42); | |
server.createContext("/", new HttpHandler() {{ | |
try { | |
new ObjectInputStream(new FileInputStream("reddit.db")) {{ | |
reddits = (ArrayList<Reddit>)readObject(); | |
}}.close(); | |
} catch (final Exception e) { | |
reddits = new ArrayList<Reddit>(); | |
}} | |
ArrayList<Reddit> reddits; | |
public void handle(final HttpExchange exchange) { | |
try { | |
final String path = exchange.getRequestURI().getPath(); | |
if (exchange.getRequestMethod().equals("POST")) { | |
exchange.getResponseHeaders().set("Location", "/"); | |
if (path.matches("/up/\\d+")) { | |
exchange.sendResponseHeaders(303, -1); | |
reddits.get(Integer.parseInt(path.substring(4))).rating += 1; | |
} else if (path.matches("/down/\\d+")) { | |
exchange.sendResponseHeaders(303, -1); | |
reddits.get(Integer.parseInt(path.substring(6))).rating -= 1; | |
} else if (path.equals("/add/")) { | |
exchange.sendResponseHeaders(303, -1); | |
final char[] raw = new char[4242]; | |
new Reddit() {{ | |
for (String arg: new String(raw, 0, | |
new InputStreamReader(exchange.getRequestBody()).read(raw)).split("&")) { | |
String [] kv = arg.split("=", 2); | |
kv[1] = URLDecoder.decode(kv[1], "UTF-8"); | |
if ("url".equals(kv[0])) { | |
if (!kv[1].matches("http(s?)://.*")) kv[1] = "http://" + kv[1]; | |
url = new URL(kv[1]); | |
} else if ("title".equals(kv[0])) { | |
title = kv[1]; | |
} | |
} | |
id = reddits.size(); | |
if (url != null && null != title) reddits.add(this); | |
}}; | |
} else { | |
exchange.sendResponseHeaders(404, -1); | |
} | |
new ObjectOutputStream(new FileOutputStream("reddit.db")) {{ | |
writeObject(reddits); | |
}}.close(); | |
} else if (exchange.getRequestMethod().equals("GET")) { | |
if (path.equals("/")) { | |
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8"); | |
exchange.sendResponseHeaders(200, 0); | |
new OutputStreamWriter(exchange.getResponseBody(), "UTF-8") {{ | |
write("<html><head><title>Reddit</title></head><body><form action='/add/' method='post'>Url:<input name='url'> Title:<input name='title'><input type='submit'></form><h1>Best</h1><ul>"); | |
final ArrayList<Reddit> r2d2 = new ArrayList<Reddit>(reddits); | |
Reddit.printall(new Formatter(this), r2d2, new Comparator<Reddit>() { | |
public int compare(Reddit a, Reddit b) { | |
return b.rating - a.rating; | |
}}); | |
write("</ul><h1>Newest</h1><ul>"); | |
Reddit.printall(new Formatter(this), r2d2, new Comparator<Reddit>() { | |
public int compare(Reddit a, Reddit b) { | |
return b.id - a.id; | |
}}); | |
write("</ul></body></html>"); | |
}}.flush(); | |
} else { | |
exchange.sendResponseHeaders(404, -1); | |
} | |
} else { | |
exchange.sendResponseHeaders(500, -1); | |
} | |
} catch (final Exception e) { | |
e.printStackTrace(); | |
} finally { | |
exchange.close(); | |
}}}); | |
System.out.println("Reddit running on 8080 !"); | |
server.start(); | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment