import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
class Handler implements URLHandler {
// The one bit of state on the server: a number that will be manipulated by
// various requests.
int num = 1;
StringBuffer buf = new StringBuffer();
String theR = new String();
public String handleRequest(URI url) {
if (url.getPath().equals("/")) {
return theR;
} else {
if (url.getPath().contains("/add-message")) {
String[] parameters = url.getQuery().split("s=");
buf.append(num++ + ". ");
buf.append(parameters[1]);
buf.append("\n");
theR = buf.toString();
return theR;
}
return "404 Not Found";
}
}
}
class StringServer {
public static void main(String[] args) throws IOException {
if(args.length == 0){
System.out.println("Missing port number! Try any number between 1024 to 49151");
return;
}
Integer port = Integer.parseInt(args[0]);
Server.start(port, new Handler());
}
}