Methods Summary |
---|
public void | addRequestHandler(HTTPRequestHandler h)Registers the given HTTPRequestHandler
handler.add(h);
|
public void | autoCreateRootPage(int index)Creates the ReplyHTML data for the root page
if (handler.get(index) instanceof RootRequestHandler) {
RootRequestHandler r = (RootRequestHandler)handler.get(index);
String html = "<HTML><HEAD><TITLE>"+r.getTitle()+"</TITLE></HEAD>\r\n";
html = html + "<BODY><H1>"+r.getDescription()+"</H1>\r\n";
for (int i = 0; i < handler.size(); i++) {
html = html + "<a href=\"" + ((HTTPRequestHandler)handler.get(i)).getHandledPath();
html = html + "\">" + ((HTTPRequestHandler)handler.get(i)).getDescription() + "</a><br>";
}
html = html + "</BODY></HTML>\r\n";
r.setReplyHTML(html);
}
|
public static void | main(java.lang.String[] args)Demo how to use the PluggableHTTPServer.
int thePort;
// create some logging stuff
BasicConfigurator.configure();
Category cat1 = Category.getInstance("cat1");
cat1.addAppender(new org.apache.log4j.ConsoleAppender(new PatternLayout("%m%n")));
Category cat2 = Category.getInstance("cat2");
cat2.setPriority(Priority.INFO);
cat2.addAppender(new org.apache.log4j.ConsoleAppender(new PatternLayout("%c - %m%n")));
// set TCP port number
try {
thePort = Integer.parseInt(args[1]);
}
catch (Exception e) {
thePort = PluggableHTTPServer.DEFAULT_PORT;
}
PluggableHTTPServer server = null;
while (server == null) {
try {
server = new PluggableHTTPServer(thePort);
server.addRequestHandler(new RootRequestHandler());
server.addRequestHandler(new Log4jRequestHandler());
server.addRequestHandler(new UserDialogRequestHandler());
server.autoCreateRootPage(0);
Thread t = new Thread(server);
t.start();
} catch (IOException e) {
server = null;
thePort++;
}
}
|
public void | removeRequestHandler(HTTPRequestHandler h)Unregisters the given HTTPRequestHandler
handler.remove(h);
|
public static void | replyMethodNotAllowed(java.io.Writer out)Sends the HTTP message 405 - Method Not Allowed
see RFC2616 for details
try {
out.write("HTTP/1.1 405 Method Not Allowed\r\n");
out.write("Allow: GET, PUT\r\n");
out.write("<HTML><HEAD><TITLE>Method Not Allowed</TITLE></HEAD>\r\n");
out.write("<BODY><H1>Method Not Allowed</H1>\r\n");
out.write("</BODY></HTML>\r\n");
out.flush();
} // end try
catch (IOException e) {
}
|
public static void | replyNotFound(java.io.Writer out)Sends the HTTP message 404 - File Not Found
see RFC2616 for details
try {
out.write("HTTP/1.0 404 Not Found\r\n");
out.write("<HTML><HEAD><TITLE>Not Found</TITLE></HEAD>\r\n");
out.write("<BODY><H1>Not Found</H1>\r\n");
out.write("</BODY></HTML>\r\n");
out.flush();
} // end try
catch (IOException e) {
}
|
public void | run()Main loop of the PluggableHTTPServer
while (true) {
try {
Socket s = server.accept();
Thread t = new ServerThread(s);
t.start();
}
catch (IOException e) {
}
}
|